summaryrefslogtreecommitdiff
path: root/test/behaviour/addons.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 00:02:06 -0300
committerGabriel Schneider <[email protected]>2026-11-06 01:21:34 -0300
commita6f294ef5b50aeb5b69e486838378654e318ed04 (patch)
tree3a223f1027389dd30d8347462cee4f9b6eb7ecf6 /test/behaviour/addons.zig
parente9b4f0e1c37c5c1f369d152d0a70b672433c94b5 (diff)
downloadpardes-a6f294ef5b50aeb5b69e486838378654e318ed04.tar.gz
pardes-a6f294ef5b50aeb5b69e486838378654e318ed04.zip
kinds #4, the fake addon and the addon test: addons/fake/fake.c is the template, every function of src/kinds/pardes_addon.h in a few lines (a counter its keys, clicks, words, ticks and jobs move, a 2x2 picture, a body, an address, a ctl read, a dump and a load), built by the build with -Wall -Wextra -Werror four more ways for the loader to refuse (another ABI, no pardes_addon, a word that is the core's) or reload (no record of its own); `pardes --addon-test <lib> <script>` loads a library into a session of its own (80x24 unless `size`, `seed N` for the panes' seeds, jobs at once, a clock moved only by `tick` and `settle`, which ticks until the kind asks for no frame) and prints the pane's tag, cells and placements after each line of keys, clicks and words; the addon-test step (and behaviour-test) runs it over the fake against addons/fake/fake.out; T1 (test/behaviour/addons.zig): Addon load, /addons with its path, BLAKE3 and panes open, the words in /commands and Help, a word refused away from its kind, unload refused while a pane is open, a compiled-in kind not unloadable; a library of another ABI, one with no export, one whose word is the core's and a file that is no library each refused and listed with why; a pane of the kind taking keys, a click, a word, a job, its body and ctl read, Esc leaving it; a dump restored with the addon, without it (the placeholder, a word refused there), and the addon loaded again reopening it in place with its count; a reload refused for a half-built library with the old one running on, one taking the state across by dump and load, one with no record coming back from its words' last values; a Look by an addon's extension; a test session lets go of its addons at its end (the registry is the process's); gates on the head: core-test 961/961 and unit-test 1494/1495 tui, 1538/1539 gui, behaviour-test 250/250, pane-test 171/172, snap all 108 ok, process-test 0 of 37 failed on both, addon-test ok, hxdiff 19 and hxparity 6 mismatches (the base's), hxgolf 0, replicas, gui-golden, monkey-gui, daemon.py main and window-clients, soak local pass
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/behaviour/addons.zig')
-rw-r--r--test/behaviour/addons.zig210
1 files changed, 210 insertions, 0 deletions
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
new file mode 100644
index 00000000..c91bda1a
--- /dev/null
+++ b/test/behaviour/addons.zig
@@ -0,0 +1,210 @@
+//! docs/typ/reference.typ, Addons <addons>: a library that brings a kind
+//! of pane, driven here through addons/fake/fake.c (every function of
+//! src/kinds/pardes_addon.h) and the copies of it built wrong.
+const std = @import("std");
+const Session = @import("session").Session;
+const fake = Session.fake_addons;
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+/// The serial of the pane /index lists of kind `kind`.
+fn paneOf(s: *Session, kind: []const u8) !u32 {
+ var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
+ while (lines.next()) |line| {
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ const serial = words.next() orelse continue;
+ if (std.mem.eql(u8, words.next() orelse continue, kind)) return std.fmt.parseInt(u32, serial, 10);
+ }
+ return error.NoSuchPane;
+}
+
+/// The library at `path` loaded, as from a terminal's directory.
+fn load(s: *Session, path: []const u8) !void {
+ const real = try std.Io.Dir.cwd().realPathFileAlloc(std.testing.io, path, s.arena.allocator());
+ try s.write("/ctl", s.at("Addon load {s}\n", .{real}));
+}
+
+fn unload(s: *Session, name: []const u8) void {
+ _ = s.refused("/ctl", s.at("Addon unload {s}\n", .{name})) catch {};
+}
+
+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 1.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 1.0 0 /").?;
+ var fields = std.mem.tokenizeScalar(u8, listed[at + 1 .. std.mem.indexOfScalarPos(u8, listed, at + 1, '\n').?], ' ');
+ for (0..5) |_| _ = fields.next();
+ try std.testing.expectEqual(@as(usize, 64), fields.next().?.len);
+ try std.testing.expectEqualStrings("loaded", fields.next().?);
+ const commands = try s.read("/commands");
+ try std.testing.expect(has(commands, "\nFake both -- a fake pane, counting (addon fake)\n"));
+ try std.testing.expect(has(commands, "\nFakeBump arg pane -- add to the count (addon fake)\n"));
+ // Its word run where no pane of its kind is: refused, saying where.
+ try std.testing.expect(try s.refused("/ctl", "FakeBump\n") != 0);
+ try std.testing.expect(has(s.said, "FakeBump is a fake pane's word"));
+ // 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 1.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" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ 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 2.0 0 /"));
+ try std.testing.expect(has(listed, " refused: wants abi 2, this pardes speaks 1\n"));
+ try std.testing.expect(has(listed, " refused: it has no pardes_addon\n"));
+ try std.testing.expect(has(listed, "\nfakeword 1 1.0 0 /"));
+ try std.testing.expect(has(listed, " refused: word Save is the core's\n"));
+ // A file that is no library at all: listed with what the loader said.
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notalib.so", .data = "not a library\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ try s.write("/ctl", s.at("Addon load {s}/notalib.so\n", .{dir}));
+ try std.testing.expect(has(try s.read("/addons"), s.at("notalib.so 0 0.0 0 {s}/notalib.so - ", .{dir})));
+ try std.testing.expect(!has(try s.read("/addons"), "notalib.so - loaded"));
+ // None of them brought a word.
+ try std.testing.expect(!has(try s.read("/commands"), "(addon fake"));
+}
+
+test "addons: a pane of an addon's kind shows its cells and its picture, takes its keys, clicks, words and ticks, and answers its body and ctl" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try load(&s, fake.ok);
+ defer unload(&s, "fake");
+ try s.write("/ctl", "Fake\n");
+ const pane = try paneOf(&s, "fake");
+ try std.testing.expectEqual(pane, try s.focused());
+ try std.testing.expect(has(try s.screen(), "count 0"));
+ // Its picture is a placement over its cells, in /screen's images.
+ try std.testing.expect(has(try s.read("/screen"), "\"images\":[{"));
+ // Every key but Esc is its own.
+ try s.keys("ab");
+ try std.testing.expect(has(try s.screen(), "count 2"));
+ try std.testing.expect(has(try s.screen(), "kb"));
+ try s.click("count 2", 3, 1);
+ try std.testing.expect(has(try s.screen(), "count 3"));
+ try std.testing.expect(has(try s.screen(), "m3"));
+ // Its words go to it from its ctl; one it refuses fails the write.
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 10\n");
+ try std.testing.expect(has(try s.screen(), "count 13"));
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{pane})), "count:13 "));
+ // A job's answer lands at the end of the step that asked for it (a
+ // test's job runs at once; a real one lands on a later step).
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
+ try std.testing.expect(has(try s.screen(), "count 19"));
+ try s.expect(s.at("/pane/{d}/body", .{pane}), "fake\n\n");
+ // Esc leaves it, as from a raw terminal.
+ try s.keys("\x1b");
+ try std.testing.expect(try s.focused() != pane);
+}
+
+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();
+ try load(&s, fake.ok);
+ try s.write("/ctl", "Fake\n");
+ const pane = try paneOf(&s, "fake");
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 41\n");
+ try s.write("/ctl", "Dump\n");
+ const label = std.mem.sliceTo(try s.read("/dumps"), ' ');
+ const kept = try s.arena.allocator().dupe(u8, label);
+ // Restored with it: the pane as it was, through the kind's own record.
+ try s.write("/ctl", s.at("Restore {s}\n", .{kept}));
+ try std.testing.expect(has(try s.screen(), "count 41"));
+ // Restored without it: a placeholder in its place, saying what it waits for.
+ try s.write(s.at("/pane/{d}/ctl", .{try paneOf(&s, "fake")}), "Del\n");
+ try s.write("/ctl", "Addon unload fake\n");
+ try s.write("/ctl", s.at("Restore {s}\n", .{kept}));
+ const waiting = try paneOf(&s, "fake");
+ try std.testing.expect(has(try s.screen(), "addon fake 1 is not loaded: Addon load <path>"));
+ try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{waiting}), "FakeBump\n") != 0);
+ // The addon back, the pane opens where it stands, its count kept.
+ try load(&s, fake.ok);
+ defer unload(&s, "fake");
+ try std.testing.expectEqual(waiting, try paneOf(&s, "fake"));
+ try std.testing.expect(has(try s.screen(), "count 41"));
+}
+
+test "addons: Addon reload moves every pane of the kind to the library as it is now, its state kept; a library that fails its checks leaves the old one running" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ 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)];
+ const lib = s.at("{s}/fake.so", .{dir});
+ const bytes = struct {
+ fn of(ss: *Session, path: []const u8) ![]u8 {
+ return std.Io.Dir.cwd().readFileAlloc(std.testing.io, path, ss.arena.allocator(), .limited(64 << 20));
+ }
+ }.of;
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.ok) });
+ try s.write("/ctl", s.at("Addon load {s}\n", .{lib}));
+ defer unload(&s, "fake");
+ try s.write("/ctl", "Fake\n");
+ const pane = try paneOf(&s, "fake");
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 7\n");
+ try s.keys("ab");
+ try std.testing.expect(has(try s.screen(), "count 9"));
+ const before = try s.arena.allocator().dupe(u8, try s.read("/addons"));
+ // Half built: another ABI's. Refused, and the old library still runs.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.abi) });
+ try s.write("/ctl", "Addon reload fake\n");
+ try std.testing.expectEqualStrings(before, try s.read("/addons"));
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 1\n");
+ try std.testing.expect(has(try s.screen(), "count 10"));
+ // As it is now: its record taken across, by the old library's dump
+ // and the new one's load.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.ok) });
+ try s.write("/ctl", "Addon reload fake\n");
+ try std.testing.expectEqual(pane, try paneOf(&s, "fake"));
+ try std.testing.expect(has(try s.screen(), "count 10"));
+ // A library with no record of its own: the pane opens again from its
+ // words' last values (FakeBump 1), what keys did gone.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.no_dump) });
+ try s.write("/ctl", "Addon reload fake\n");
+ try std.testing.expect(has(try s.screen(), "count 1"));
+ // /addons says the file it runs now: another BLAKE3.
+ try std.testing.expect(!std.mem.eql(u8, before, try s.read("/addons")));
+}
+
+test "addons: a Look at a file whose extension an addon claims opens a pane of its kind, and its words are in Help" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try load(&s, fake.ok);
+ defer unload(&s, "fake");
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "five.fake", .data = "12345" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ const pane = try s.lookFile(s.at("{s}/five.fake", .{dir}));
+ try std.testing.expectEqual(pane, try paneOf(&s, "fake"));
+ // Its bytes were the kind's to open: five of them.
+ try std.testing.expect(has(try s.screen(), "count 5"));
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n");
+ try s.write("/ctl", "Help\n");
+ const help = try s.read(s.at("/pane/{d}/body", .{try s.focused()}));
+ try std.testing.expect(has(help, "SPC z f Fake a fake pane, counting (addon fake)\n"));
+}