diff options
Diffstat (limited to 'test/behaviour/addons.zig')
| -rw-r--r-- | test/behaviour/addons.zig | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig index 523aefb9..c7a59951 100644 --- a/test/behaviour/addons.zig +++ b/test/behaviour/addons.zig @@ -33,13 +33,12 @@ fn unload(s: *Session, name: []const u8) void { test "addons: Addon load takes a library in, /addons lists it with the kinds compiled in, its words join /commands and Help, and Addon unload lets it go" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); - try std.testing.expect(has(try s.read("/addons"), "image 1 2.0 0 - - static\n")); try load(&s, fake.ok); defer unload(&s, "fake"); // `<kind> <version> <abi> <panes open> <path> <blake3> <state>`. const listed = try s.read("/addons"); - const at = std.mem.indexOf(u8, listed, "\nfake 1 2.0 0 /").?; - var fields = std.mem.tokenizeScalar(u8, listed[at + 1 .. std.mem.indexOfScalarPos(u8, listed, at + 1, '\n').?], ' '); + try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 2.0 0 /")); + var fields = std.mem.tokenizeScalar(u8, listed[0..std.mem.indexOfScalar(u8, listed, '\n').?], ' '); for (0..5) |_| _ = fields.next(); try std.testing.expectEqual(@as(usize, 64), fields.next().?.len); try std.testing.expectEqualStrings("loaded", fields.next().?); @@ -52,16 +51,13 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com // A pane of it open, it stays; closed, it goes. try s.write("/ctl", "Fake\n"); const pane = try paneOf(&s, "fake"); - try std.testing.expect(has(try s.read("/addons"), "\nfake 1 2.0 1 /")); + try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 2.0 1 /")); try std.testing.expect(try s.refused("/ctl", "Addon unload fake\n") != 0); try std.testing.expect(has(s.said, "1 fake pane is open")); try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n"); try s.write("/ctl", "Addon unload fake\n"); try std.testing.expect(!has(try s.read("/addons"), "fake")); try std.testing.expect(!has(try s.read("/commands"), "Fake")); - // A compiled-in kind is no library to let go. - try std.testing.expect(try s.refused("/ctl", "Addon unload image\n") != 0); - try std.testing.expect(has(s.said, "image is compiled in")); } test "addons: a library of another ABI, one with no pardes_addon, and one whose word is the core's are refused, each listed with why" { @@ -69,7 +65,7 @@ test "addons: a library of another ABI, one with no pardes_addon, and one whose defer s.deinit(); for ([_][]const u8{ fake.abi, fake.no_export, fake.core_word }) |path| try load(&s, path); const listed = try s.read("/addons"); - try std.testing.expect(has(listed, "\nfakeabi 1 3.0 0 /")); + try std.testing.expect(std.mem.startsWith(u8, listed, "fakeabi 1 3.0 0 /")); try std.testing.expect(has(listed, " refused: wants abi 3, this pardes speaks 2\n")); try std.testing.expect(has(listed, " refused: it has no pardes_addon\n")); try std.testing.expect(has(listed, "\nfakeword 1 2.0 0 /")); @@ -119,6 +115,65 @@ test "addons: a pane of an addon's kind shows its cells and its picture, takes i try std.testing.expect(try s.focused() != pane); } +test "addons: the image addon opens the pictures a Look names, fitted, with the core's art words in its tag; refuses what it cannot read; and keeps a picture's bytes through a dump" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try load(&s, Session.image_addon); + const listed = try s.read("/addons"); + try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 2.0 0 /") and has(listed, " loaded\n")); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + // A PNG 4x2, a JPEG 16x8 baseline and progressive, a PPM 2x1: each a + // pane showing its pixels (`src`), over the cells it fits. + const b64 = std.base64.standard.Decoder; + const pictures = [_]struct { []const u8, []const u8, u32, u32 }{ + .{ "red.png", "iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAIAAADwyuo0AAAAE0lEQVR4nGP4z8AARGDiPwMyBwBnsgf5Q2OaZwAAAABJRU5ErkJggg==", 4, 2 }, + .{ "red.jpg", "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBA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AQAAAAAAAAAAAAAAAAAAAAf/xAAVAQEBAAAAAAAAAAAAAAAAAAAGCP/aAAwDAQACEAMQAAABkIC1V//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAQUCP//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Bf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Bf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEABj8CP//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8hP//aAAwDAQACAAMAAAAQA//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Qf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Qf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8QP//Z", 16, 8 }, + .{ "pixel.ppm", "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7", 2, 1 }, + }; + for (pictures) |pic| { + const bytes = if (std.mem.endsWith(u8, pic[0], ".ppm")) pic[1] else blk: { + const out = try s.arena.allocator().alloc(u8, try b64.calcSizeForSlice(pic[1])); + try b64.decode(out, pic[1]); + break :blk out; + }; + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = pic[0], .data = bytes }); + try s.write("/look", s.at("{s}/{s}\n", .{ dir, pic[0] })); + try std.testing.expect(has(try s.read("/screen"), s.at("\"serial\":{d},\"rect\":", .{try s.focused()}))); + try std.testing.expect(has(try s.read("/screen"), s.at("\"src\":[0,0,{d},{d}]", .{ pic[2], pic[3] }))); + } + const pane = try s.focused(); + // Its tag: the kind's `img`, the core's art words, its path. With no + // pixels to show (no frontend), glyph art whatever Petscii says. + const tag = s.at("/pane/{d}/tag", .{pane}); + try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), s.at("img petscii:on palette:commodore ascii:on {s}/pixel.ppm ", .{dir}))); + const ctl = s.at("/pane/{d}/ctl", .{pane}); + for ([_][]const u8{ "Petscii on\n", "Palette terminal\n", "Palette terminal\n", "Ascii off\n" }) |line| try s.write(ctl, line); + try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), "img petscii:on palette:terminal ascii:off ")); + try std.testing.expect(has(try s.read("/screen"), "\"petscii\":true")); + // A file it cannot read: the Look fails saying so, and opens nothing. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "bad.png", .data = "\x89PNG not really" }); + const panes = (try s.read("/index")).len; + try std.testing.expect(try s.refused("/look", s.at("{s}/bad.png\n", .{dir})) != 0); + try std.testing.expect(has(s.said, "bad.png: not an image pardes can read")); + try std.testing.expectEqual(panes, (try s.read("/index")).len); + // Its record is the file's bytes: restored without the addon it waits + // as a placeholder holding them, and shows them again when it comes. + try s.write("/ctl", "Dump\n"); + const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' ')); + var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (lines.next()) |line| if (std.mem.indexOf(u8, line, " image ") != null) try s.write(s.at("/pane/{s}/ctl", .{std.mem.sliceTo(line, ' ')}), "Del\n"); + try s.write("/ctl", "Addon unload image\n"); + try s.write("/ctl", s.at("Restore {s}\n", .{label})); + try std.testing.expect(has(try s.screen(), "addon image 1 is not loaded: Addon load <path>")); + try load(&s, Session.image_addon); + defer unload(&s, "image"); + try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,4,2]")); +} + test "addons: a dump keeps an addon's pane; restored without its addon it waits as a placeholder, and opens in place when the addon comes" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); |
