summaryrefslogtreecommitdiff
path: root/src/addon_test.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 /src/addon_test.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 'src/addon_test.zig')
-rw-r--r--src/addon_test.zig174
1 files changed, 174 insertions, 0 deletions
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 });
+ };
+}