diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 00:02:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 01:21:34 -0300 |
| commit | a6f294ef5b50aeb5b69e486838378654e318ed04 (patch) | |
| tree | 3a223f1027389dd30d8347462cee4f9b6eb7ecf6 | |
| parent | e9b4f0e1c37c5c1f369d152d0a70b672433c94b5 (diff) | |
| download | pardes-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
| -rw-r--r-- | addons/fake/fake.c | 199 | ||||
| -rw-r--r-- | addons/fake/fake.out | 78 | ||||
| -rw-r--r-- | addons/fake/fake.script | 9 | ||||
| -rw-r--r-- | build.zig | 31 | ||||
| -rw-r--r-- | src/addon_test.zig | 174 | ||||
| -rw-r--r-- | src/main.zig | 3 | ||||
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/addons.zig | 210 | ||||
| -rw-r--r-- | test/session.zig | 12 |
9 files changed, 717 insertions, 0 deletions
diff --git a/addons/fake/fake.c b/addons/fake/fake.c new file mode 100644 index 00000000..ad0f83ee --- /dev/null +++ b/addons/fake/fake.c @@ -0,0 +1,199 @@ +/* fake.c: the template addon, every function of pardes_addon.h in a few + * lines, and what the loader's tests drive (test/behaviour/addons.zig, + * `pardes --addon-test`). + * + * cc -shared -fPIC -I src/kinds -o fake.so addons/fake/fake.c + * + * A pane of it counts: keys and clicks and ticks and words each add to a + * count it paints, dumps and answers `where` with; `:N` sets it. */ +#include "pardes_addon.h" + +/* The loader's tests build it wrong on purpose: another ABI, no export, + * a word that is the core's; and once with no record of its own. */ +#ifndef FAKE_NAME +#define FAKE_NAME "fake" +#endif +#ifndef FAKE_WORD +#define FAKE_WORD "Fake" +#endif +#ifdef FAKE_NO_EXPORT +#define pardes_addon fake_not_exported +#endif + +static const pa_api *api; + +typedef struct { + unsigned count, ticks; + char last[16]; + uint8_t pixels[2 * 2 * 4]; +} fake; + +static int init(const pa_api *a) { + api = a; + return 0; +} + +static void *open(const char *path, const uint8_t *bytes, uint64_t len) { + fake *f = api->alloc(sizeof *f); + if (!f) return 0; + uint64_t seed = api->seed(); + for (int i = 0; i < 16; i++) f->pixels[i] = (uint8_t)(i * 16 + (seed >> (i % 8)) % 2); + f->count = bytes ? (unsigned)len : 0; + (void)path; + return f; +} + +static void close(void *st) { (void)st; } + +static uint64_t tick(void *st, uint64_t now) { + fake *f = st; + f->ticks++; + return f->ticks < 3 ? now + 100000000u : 0; /* ten a second, three times */ +} + +static void num(char *out, unsigned n) { + char t[12]; + int k = 0; + do t[k++] = (char)('0' + n % 10); while (n /= 10); + while (k) *out++ = t[--k]; + *out = 0; +} + +static void paint(void *st, const pa_window *w, pa_paint *out) { + fake *f = st; + char line[48] = "count "; + num(line + 6, f ? f->count : 0); + pa_put(w, 0, 0, line, PA_INDEX(2), PA_DEFAULT); + if (f && f->last[0]) pa_put(w, 1, 0, f->last, PA_DEFAULT, PA_DEFAULT); + const char *p = "fake"; + for (out->prefix_len = 0; p[out->prefix_len]; out->prefix_len++) out->prefix[out->prefix_len] = p[out->prefix_len]; + if (f && out->cap > 0 && w->rows > 2) { + pa_placement *pl = &out->placements[out->n++]; + *pl = (pa_placement){.rgba = f->pixels, .w = 2, .h = 2, .stride = 8, .src = {0, 0, 2, 2}, .dst = {0, 2, 2, 1}, .key = f->count}; + } +} + +static int key(void *st, const pa_key *k) { + fake *f = st; + if (k->cp == 'q') return 0; /* not taken: the core's */ + f->count++; + f->last[0] = 'k'; + f->last[1] = k->cp < 128 ? (char)k->cp : '?'; + f->last[2] = 0; + return 1; +} + +static int mouse(void *st, const pa_mouse *m) { + fake *f = st; + if (m->action != PA_PRESS) return 1; + f->count++; + f->last[0] = 'm'; + num(f->last + 1, m->col); + return 1; +} + +static uint64_t body_len(void *st) { (void)st; return 6; } + +static uint32_t body(void *st, uint64_t off, char *buf, uint32_t n) { + (void)st; + const char *text = "fake\n\n"; + uint32_t k = 0; + while (off + k < 6 && k < n) { buf[k] = text[off + k]; k++; } + return k; +} + +static int address(void *st, const char *text, uint32_t n) { + fake *f = st; + unsigned v = 0; + uint32_t i = text[0] == ':' ? 1 : 0; + if (i >= n) return 0; + for (; i < n; i++) { + if (text[i] < '0' || text[i] > '9') return 0; + v = v * 10 + (unsigned)(text[i] - '0'); + } + f->count = v; + return 1; +} + +static uint32_t where(void *st, char *buf, uint32_t n) { + fake *f = st; + char t[12]; + num(t, f->count); + uint32_t k = 0; + while (t[k] && k < n) { buf[k] = t[k]; k++; } + return k; +} + +static int ctl(void *st, const char *line, uint32_t n, char *reply, uint32_t cap) { + fake *f = st; + /* FakeBump [n]: adds n (1); FakeJob: counts the bytes a job answers. */ + if (n >= 8 && line[4] == 'B') { + unsigned by = 0; + for (uint32_t i = 9; i < n && line[i] >= '0' && line[i] <= '9'; i++) by = by * 10 + (unsigned)(line[i] - '0'); + f->count += by ? by : 1; + return 0; + } + if (n >= 7 && line[4] == 'J') return api->job("abc", 3) ? 0 : 1; + const char *no = "no such fake word"; + uint32_t k = 0; + while (no[k] && k + 1 < cap) { reply[k] = no[k]; k++; } + reply[k] = 0; + return 1; +} + +static uint32_t ctl_read(void *st, char *buf, uint32_t n) { + fake *f = st; + char t[24] = "count:"; + num(t + 6, f->count); + uint32_t k = 0; + while (t[k] && k < n) { buf[k] = t[k]; k++; } + return k; +} + +static uint32_t job_run(const uint8_t *in, uint32_t n, pa_out *out) { + uint8_t *o = out->alloc(out, n * 2); + if (!o) return 0; + for (uint32_t i = 0; i < n; i++) o[i] = o[n + i] = in[i]; + return n * 2; +} + +static void job_done(void *st, uint32_t token, const uint8_t *out, uint32_t n) { + fake *f = st; + (void)token; (void)out; + f->count += n; +} + +#ifndef FAKE_NO_DUMP +static uint32_t dump(void *st, uint8_t *buf, uint32_t n) { + fake *f = st; + if (n >= 4) for (int i = 0; i < 4; i++) buf[i] = (uint8_t)(f->count >> (8 * i)); + return 4; +} + +static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) { + fake *f = st; + if (n != 4 || version > 1) return 1; + f->count = (unsigned)buf[0] | (unsigned)buf[1] << 8 | (unsigned)buf[2] << 16 | (unsigned)buf[3] << 24; + return 0; +} +#endif + +static const pa_word words[] = { + {.size = sizeof(pa_word), .name = FAKE_WORD, .leader = "zf", .opens = 1, .doc = "a fake pane, counting"}, + {.size = sizeof(pa_word), .name = "FakeBump", .arg = PA_ARG_OPTIONAL, .doc = "add to the count"}, + {.size = sizeof(pa_word), .name = "FakeJob", .doc = "count a job's answer"}, +}; + +const pa_ops pardes_addon = { + .head = PA_ADDON(FAKE_NAME, 1, ".fake", words, 3), +#ifdef FAKE_ABI + .head.abi = FAKE_ABI, +#endif + .init = init, .open = open, .close = close, .tick = tick, .paint = paint, + .key = key, .mouse = mouse, .body_len = body_len, .body = body, + .address = address, .where = where, .ctl = ctl, .ctl_read = ctl_read, + .job_run = job_run, .job_done = job_done, +#ifndef FAKE_NO_DUMP + .dump = dump, .load = load, +#endif +}; diff --git a/addons/fake/fake.out b/addons/fake/fake.out new file mode 100644 index 00000000..40b958f8 --- /dev/null +++ b/addons/fake/fake.out @@ -0,0 +1,78 @@ +> seed 7 +> size 60 20 +tag fake +|count 0 +| +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 4b3cd99b86ea3486 +> key ab +tag fake +|count 2 +|kb +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key fdcaab9076ad067e +> click 3 1 +tag fake +|count 3 +|m3 +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 71e39225f0cec2a5 +> FakeBump 10 +tag fake +|count 13 +|m3 +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 54740e23065232cf +> FakeJob +tag fake +|count 13 +|m3 +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 54740e23065232cf +> tick 100 +tag fake +|count 19 +|m3 +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 +> settle +tag fake +|count 19 +|m3 +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 diff --git a/addons/fake/fake.script b/addons/fake/fake.script new file mode 100644 index 00000000..35407cd7 --- /dev/null +++ b/addons/fake/fake.script @@ -0,0 +1,9 @@ +# pardes --addon-test fake.so addons/fake/fake.script | diff addons/fake/fake.out - +seed 7 +size 60 20 +key ab +click 3 1 +FakeBump 10 +FakeJob +tick 100 +settle @@ -1528,6 +1528,24 @@ pub fn build(b: *std.Build) void { .link_libc = true, }), }).getEmittedBin()); + var fake_addon_lib: ?*std.Build.Step.Compile = null; + // The fake addon (addons/fake/fake.c) and three copies of it built + // wrong, which the loader must refuse (test/behaviour/addons.zig). + for ([_]struct { []const u8, []const u8 }{ + .{ "fake_addon", "-DFAKE_PLAIN" }, + .{ "fake_addon_abi", "-DFAKE_ABI=0x20000" }, + .{ "fake_addon_no_export", "-DFAKE_NO_EXPORT" }, + .{ "fake_addon_core_word", "-DFAKE_WORD=\"Save\"" }, + .{ "fake_addon_no_dump", "-DFAKE_NO_DUMP" }, + }) |variant| { + const lib = b.addLibrary(.{ .name = variant[0], .linkage = .dynamic, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true }) }); + lib.root_module.addIncludePath(b.path("src/kinds")); + const name_flag = if (std.mem.eql(u8, variant[0], "fake_addon_abi")) "-DFAKE_NAME=\"fakeabi\"" else if (std.mem.eql(u8, variant[0], "fake_addon_core_word")) "-DFAKE_NAME=\"fakeword\"" else "-DFAKE_PLAIN"; + // The header is the whole contract: it compiles clean under every warning. + lib.root_module.addCSourceFile(.{ .file = b.path("addons/fake/fake.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-Wno-override-init", variant[1], name_flag } }); + session_options.addOptionPath(variant[0], lib.getEmittedBin()); + if (std.mem.eql(u8, variant[0], "fake_addon")) fake_addon_lib = lib; + } session_module.addOptions("session_options", session_options); const behaviour_module = b.createModule(.{ .target = target, @@ -1542,6 +1560,19 @@ pub fn build(b: *std.Build) void { const behaviour_tests = b.addTest(.{ .root_module = behaviour_module, .filters = test_filters }); const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/)"); _ = units.add(b, behaviour_tests, behaviour_test_step); + // An addon's own test, the template's: `--addon-test` over the fake + // and its script, compared with what it printed when written. + if (fake_addon_lib) |fake_lib| { + const run_addon_test = b.addRunArtifact(exe); + run_addon_test.addArg("--addon-test"); + run_addon_test.addArtifactArg(fake_lib); + run_addon_test.addFileArg(b.path("addons/fake/fake.script")); + const compare = b.addSystemCommand(&.{ "diff", "-u" }); + compare.addFileArg(b.path("addons/fake/fake.out")); + compare.addFileArg(run_addon_test.captureStdOut(.{})); + behaviour_test_step.dependOn(&compare.step); + b.step("addon-test", "pardes --addon-test over the fake addon, against addons/fake/fake.out").dependOn(&compare.step); + } // The 9P monkey at T1 (test/monkey9p.zig): random requests against // an in-process session, the documented rules checked after each. // Its fixed-seed smoke run is part of behaviour-test. diff --git a/src/addon_test.zig b/src/addon_test.zig new file mode 100644 index 00000000..496b7504 --- /dev/null +++ b/src/addon_test.zig @@ -0,0 +1,174 @@ +//! `pardes --addon-test <addon.so> <script>`: an addon's author's test. +//! The addon is loaded into a session of its own (80x24, no terminal, no +//! disk, its jobs run at once, its clock virtual and moved by `tick` and +//! `settle` alone), a pane of it opened, and the +//! script's lines run in turn, each printed with the pane's cells and +//! placements after it; a script compares the output with what it wants +//! as a snapshot script does (`diff`). +//! +//! open [path] a pane of the kind (the first step opens one bare) +//! key hjkl<up> keys, a character or <up> <down> <left> <right> +//! <enter> <tab> <bs> <esc> <space> each +//! tick 100 the clock 100 ms on, the kind's ticks run +//! settle ticks until the kind asks for none (at most 1000) +//! size 60 20 the session's cells, the pane's share of them +//! seed 7 what every pane's seed is made from (before open) +//! click 3 1 a left click at the body's column 3, row 1 +//! Word arg one of the kind's words, at its pane +//! # anything a comment, not printed +const std = @import("std"); +const pardes = @import("pardes.zig"); +const Kind = @import("Kind.zig"); +const Addon = @import("Addon.zig"); +const Pardes = pardes.Pardes; + +pub fn main(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]const u8) u8 { + var out_buf: [4096]u8 = undefined; + var out_file = std.Io.File.stdout().writer(io, &out_buf); + const out = &out_file.interface; + defer out.flush() catch {}; + run(io, gpa, lib, script_path, out) catch |err| { + out.print("addon-test: {t}\n", .{err}) catch {}; + return 1; + }; + return 0; +} + +fn run(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]const u8, out: *std.Io.Writer) !void { + Kind.inline_jobs = true; + var why: Kind.Buf(160) = .{}; + if (!Addon.load(lib, &why)) { + try out.print("addon-test: {s}\n", .{why.slice()}); + return error.KindRefused; + } + const ops = Kind.byName(why.slice()).?; + const script = try std.Io.Dir.cwd().readFileAlloc(io, script_path orelse return error.NoScript, gpa, .limited(1 << 20)); + defer gpa.free(script); + + // The regions and the C libraries' allocators a frontend sets up. + const allocs = pardes.memory.init(gpa); + defer pardes.memory.deinit(); + pardes.image.start(io, allocs.image); + defer pardes.image.stop(); + pardes.syntax.start(allocs.tree_sitter); + defer pardes.syntax.stop(); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24, .state = .memory, .image_allocator = allocs.image, .pdf_allocator = allocs.pdf, .tree_sitter_allocator = allocs.tree_sitter, .frame_allocator = allocs.frame }); + defer p.deinit(); + var now: u64 = std.time.ns_per_s; + p.advance(now); + var id: ?usize = null; + var lines = std.mem.splitScalar(u8, script, '\n'); + while (lines.next()) |raw| { + const line = std.mem.trim(u8, raw, " \t\r"); + if (line.len == 0 or line[0] == '#') continue; + try out.print("> {s}\n", .{line}); + const verb_end = std.mem.indexOfScalar(u8, line, ' ') orelse line.len; + const verb = line[0..verb_end]; + const arg = std.mem.trim(u8, line[verb_end..], " "); + if (std.mem.eql(u8, verb, "seed")) { + Kind.seed_base = std.fmt.parseInt(u64, arg, 10) catch 0; + } else if (std.mem.eql(u8, verb, "open")) { + id = try open(p, ops, if (arg.len > 0) arg else null); + } else { + if (id == null) id = try open(p, ops, null); + const at = id.?; + if (std.mem.eql(u8, verb, "key")) { + try keys(p, arg); + } else if (std.mem.eql(u8, verb, "tick")) { + now += (std.fmt.parseInt(u64, arg, 10) catch 0) * std.time.ns_per_ms; + p.advance(now); + } else if (std.mem.eql(u8, verb, "settle")) { + var n: usize = 0; + while (pardes.Kind.wake(p) != null and n < 1000) : (n += 1) { + now = @max(now + 1, p.nextWake() orelse break); + p.advance(now); + } + if (n == 1000) try out.writeAll("settle: still asking for frames after 1000 ticks\n"); + } else if (std.mem.eql(u8, verb, "size")) { + var it = std.mem.tokenizeScalar(u8, arg, ' '); + const cols = std.fmt.parseInt(u16, it.next() orelse "80", 10) catch 80; + const rows = std.fmt.parseInt(u16, it.next() orelse "24", 10) catch 24; + p.update(.{ .resize = .{ .cols = cols, .rows = rows } }); + } else if (std.mem.eql(u8, verb, "click")) { + var it = std.mem.tokenizeScalar(u8, arg, ' '); + const col = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0; + const row = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0; + const body = bodyRect(p, at) orelse continue; + for ([_]pardes.Mouse.Kind{ .press, .release }) |k| + p.update(.{ .mouse = .{ .button = .left, .kind = k, .col = body.x + col, .row = body.y + row } }); + } else { + p.active = at; + if (!p.executeBuiltinLine(at, line)) try out.print("not a word here: {s}\n", .{verb}); + } + } + drain(p); + if (id) |at| try show(p, gpa, at, out); + } +} + +fn open(p: *Pardes, ops: *const Kind.Ops, path: ?[]const u8) !usize { + const free = p.freeSlot() orelse return error.NoPaneSlots; + _ = if (path) |at| try Kind.openFile(p, free, ops, at) else try Kind.open(p, free, ops, null, null); + pardes.exec.placeNew(p, p.active, null, free, .doc); + p.active = free; + drain(p); + return free; +} + +fn drain(p: *Pardes) void { + while (p.nextEffect()) |_| {} +} + +fn keys(p: *Pardes, text: []const u8) !void { + const Key = pardes.Key; + const names = [_]struct { []const u8, u21 }{ + .{ "<up>", Key.up }, .{ "<down>", Key.down }, .{ "<left>", Key.left }, .{ "<right>", Key.right }, + .{ "<enter>", Key.enter }, .{ "<tab>", Key.tab }, .{ "<bs>", Key.backspace }, .{ "<esc>", Key.escape }, + .{ "<space>", ' ' }, + }; + var i: usize = 0; + while (i < text.len) { + const cp: u21 = for (names) |n| { + if (std.mem.startsWith(u8, text[i..], n[0])) { + i += n[0].len; + break n[1]; + } + } else blk: { + const len = std.unicode.utf8ByteSequenceLength(text[i]) catch 1; + const c = std.unicode.utf8Decode(text[i..@min(text.len, i + len)]) catch text[i]; + i += len; + break :blk c; + }; + p.update(.{ .key = .{ .cp = cp } }); + } +} + +fn bodyRect(p: *Pardes, id: usize) ?pardes.Rect { + var arena: std.heap.ArenaAllocator = .init(p.gpa); + defer arena.deinit(); + const s = p.render(arena.allocator()) catch return null; + const r = s.region(.body, id) orelse return null; + return r.rect; +} + +/// The pane's body, a line a row (an unpainted cell a space), then each +/// placement over it. +fn show(p: *Pardes, gpa: std.mem.Allocator, id: usize, out: *std.Io.Writer) !void { + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const pane = p.panes[id] orelse return out.writeAll("(the pane is gone)\n"); + try out.print("tag {s}\n", .{try pardes.tagline.tagPrefix(p, pane)}); + const r = (s.region(.body, id) orelse return).rect; + for (r.y..r.y + r.h) |y| { + var row: std.ArrayList(u8) = .empty; + for (r.x..r.x + r.w) |x| { + const c = s.at(@intCast(x), @intCast(y)); + try row.appendSlice(arena.allocator(), if (c.default) " " else c.grapheme()); + } + try out.print("|{s}\n", .{std.mem.trimEnd(u8, row.items, " ")}); + } + for (s.placements.items) |pl| if (pl.pane == id) { + try out.print("placement {d}x{d} src {d},{d} {d}x{d} over {d},{d} {d}x{d} key {x}\n", .{ pl.raster.w, pl.raster.h, pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.dst.x - r.x, pl.dst.y - r.y, pl.dst.w, pl.dst.h, pl.key }); + }; +} diff --git a/src/main.zig b/src/main.zig index aaa27031..6f8a9923 100644 --- a/src/main.zig +++ b/src/main.zig @@ -378,6 +378,9 @@ fn nativeMain(std_init: std.process.Init) !void { var mounts_len: usize = 0; const arena = init.arena.allocator(); const args = try init.minimal.args.toSlice(arena); + // An addon's author's test (addon_test.zig): nothing else. + if (args.len >= 3 and std.mem.eql(u8, args[1], "--addon-test")) + std.process.exit(@import("addon_test.zig").main(init.io, init.gpa, args[2], if (args.len > 3) args[3] else null)); // The per-user daemon a session started (daemon.zig), or an upgrade of // one with its registry: nothing else. if (comptime daemon.supported) if (args.len >= 2 and args.len <= 3 and std.mem.eql(u8, args[1], "--daemon")) diff --git a/test/behaviour.zig b/test/behaviour.zig index 2e632911..685128df 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -3,6 +3,7 @@ //! named by the label first. This module imports test/session.zig alone, //! so a test sees the session as a 9P client does. test { + _ = @import("behaviour/addons.zig"); _ = @import("behaviour/addresses.zig"); _ = @import("behaviour/builtins.zig"); _ = @import("behaviour/callstack.zig"); 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")); +} diff --git a/test/session.zig b/test/session.zig index c3e6c579..87bf784e 100644 --- a/test/session.zig +++ b/test/session.zig @@ -13,6 +13,16 @@ const Reply = pardes.ctlfs.Reply; extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; pub const Session = struct { + /// The fake addon, and its copies built wrong (build.zig). + pub const fake_addons = struct { + const o = @import("session_options"); + pub const ok = o.fake_addon; + pub const abi = o.fake_addon_abi; + pub const no_export = o.fake_addon_no_export; + pub const core_word = o.fake_addon_core_word; + pub const no_dump = o.fake_addon_no_dump; + }; + core: *anyopaque, /// What the calls answered, kept until deinit. arena: std.heap.ArenaAllocator, @@ -78,6 +88,7 @@ pub const Session = struct { pub fn deinit(s: *Session) void { pardesOf(s).deinit(); + pardes.Addon.unloadAll(); s.arena.deinit(); } @@ -218,6 +229,7 @@ pub const Session = struct { .save_file, .save_text => s.saved += 1, // Into the session's state (a database in memory, or its file). .write_dump => pardes.host_io.writeDump(pardesOf(s), pardesOf(s).dump_out.?), + .addon => |a| pardes.Addon.perform(pardesOf(s), a.op, a.pane, a.arg.slice()), .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), else => {}, }; |
