diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 20:52:50 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 21:22:58 -0300 |
| commit | a75956bb3443aa8aac778d2ff12c42f5ef99e0e9 (patch) | |
| tree | 17547cbf6606c8bd05be313cd8f835cb85d13e57 /runtime/addon_test.zig | |
| parent | 5b33c88476054fffc1044fff0526f7f0b28563cf (diff) | |
| download | pardes-a75956bb3443aa8aac778d2ff12c42f5ef99e0e9.tar.gz pardes-a75956bb3443aa8aac778d2ff12c42f5ef99e0e9.zip | |
runtime: main, daemon, upgrade, crash, addon_test, 9p_io, 9p and macos.zig into runtime/ (the reorganisation's tree, step 2a), skill.txt with main.zig, its one reader: module `runtime` (runtime/root.zig) and the core, each naming the other, a product's root (main.zig, macos.zig) a module of its own over both; the files name across by module (`@import("runtime").daemon`, `@import("core").look`), pardes.zig exporting what they name (ctl, edit_cmd, tracy, selection_pipe, and the frontends until they move); core-test and shell-test run each module's test program, so the same tests run (the ledger: every named test of every suite the base's, tui and gui); nothing else changes: frames byte-identical, the same counts (tui core-test 822/963, the one more an anonymous collector)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/addon_test.zig')
| -rw-r--r-- | runtime/addon_test.zig | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/runtime/addon_test.zig b/runtime/addon_test.zig new file mode 100644 index 00000000..a08237fc --- /dev/null +++ b/runtime/addon_test.zig @@ -0,0 +1,253 @@ +//! `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, starting at 1 s and moved +//! by `tick` and `settle` alone), a pane of it opened, and the script's +//! lines run in turn, each printed with the start of the pane's tag (the +//! kind's prefix, the core's art words, the path), cells and placements +//! after it; a script compares the output with what it wants as a snapshot +//! script does (`diff`). A call into the kind over its budget (16 ms in a +//! Debug pardes) is said, and fails the script. +//! +//! 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): +//! a kind that asks for ticks while idle fails it +//! size 60 20 the session's terminal, the pane's share of it +//! (`window CxR` says what the kind paints) +//! 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 +//! focus, blur the view's window takes the keyboard, or loses it +//! graphics on|off the view's frontend shows pictures, or not +//! (glyph art, the start's) +//! view 2 on|off into view 2 (1 is the start's; one opened as the +//! first is), its frontend with pictures or not +//! dump the pane's record (its length) loaded into a +//! fresh pane, which the script goes on with +//! body, ctl the pane's body, or its ctl file's read +//! Word arg one of the kind's words, at its pane +//! # anything a comment, not printed +const std = @import("std"); +const pardes = @import("core"); +const Kind = @import("core").Kind; +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 { + 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.syntax.start(allocs.tree_sitter); + defer pardes.syntax.stop(); + // A terminal's view on every build: what a script prints is the same + // from a window's binary (a picture's raster is a view's, Kind.viewOf). + const p = try Pardes.init(gpa, .{ .tty_only = true, .windowed = false, .cols = 80, .rows = 24, .state = .memory, .image_allocator = allocs.image, .tree_sitter_allocator = allocs.tree_sitter, .frame_allocator = allocs.frame }); + defer p.deinit(); + // Loaded as `Addon load` loads it, the host's half here. + pardes.Addon.perform(p, .load, 0, lib); + const last = &p.kinds.kinds[p.kinds.n - 1]; + if (last.refused) { + try out.print("addon-test: {s}\n", .{last.why.slice()}); + return error.KindRefused; + } + const k = p.kinds.n - 1; + var now: u64 = std.time.ns_per_s; + p.advance(now); + var id: ?usize = null; + var said = false; + 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..], " "); + const is = struct { + fn is(v: []const u8, w: []const u8) bool { + return std.mem.eql(u8, v, w); + } + }.is; + if (is(verb, "seed")) { + Kind.seed_base = std.fmt.parseInt(u64, arg, 10) catch 0; + continue; + } + const opened = id == null or is(verb, "open"); + if (opened) id = try open(p, k, if (is(verb, "open") and arg.len > 0) arg else null); + const at = id.?; + if (is(verb, "key")) { + try keys(p, arg); + } else if (is(verb, "tick")) { + now += (std.fmt.parseInt(u64, arg, 10) catch 0) * std.time.ns_per_ms; + p.advance(now); + } else if (is(verb, "settle")) { + var n: usize = 0; + while (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 (is(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 (is(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 = window(p, at) orelse continue; + for ([_]pardes.Mouse.Kind{ .press, .release }) |mk| + p.update(.{ .mouse = .{ .button = .left, .kind = mk, .col = body.x + col, .row = body.y + row } }); + } else if (is(verb, "focus") or is(verb, "blur")) { + p.update(.{ .focus = is(verb, "focus") }); + } else if (is(verb, "graphics") or is(verb, "view")) { + // Into view N (another client's), then its frontend: with + // pictures (`on`), or glyph art. + if (is(verb, "view")) { + const n = std.fmt.parseInt(u8, arg[0..@min(1, arg.len)], 10) catch 1; + while (n > 1 and !pardes.views.isOpen(p, n - 1)) _ = p.openView() orelse break; + if (pardes.views.isOpen(p, n -| 1)) p.toView(n -| 1); + } + p.update(.{ .frontend = .{ .windowed = false, .native_images = std.mem.endsWith(u8, arg, "on") } }); + } else if (is(verb, "dump")) { + id = try reloaded(p, gpa, at, out); + } else if (is(verb, "body") or is(verb, "ctl")) { + drain(p); + var fields: [4096]u8 = undefined; + const text = if (is(verb, "body")) try Kind.body(p.panes[at].?, p.scratch.allocator()) else Kind.ctlRead(p.panes[at].?, &fields); + var rows = std.mem.splitScalar(u8, std.mem.trimEnd(u8, text, "\n"), '\n'); + while (rows.next()) |row| try out.print("={s}\n", .{row}); + continue; + } else if (!is(verb, "open")) { + p.view().active = at; + if (!p.executeBuiltinLine(at, line)) try out.print("not a word here: {s}\n", .{verb}); + } + drain(p); + // The session's own steps owed (a tick asked outside a step, a view + // whose facts moved, a focus), as a host's pump takes them (17.2). + var owed: usize = 0; + while (owed < 4 and window(p, id.?) != null and (Kind.wake(p) orelse std.math.maxInt(u64)) <= @max(1, p.clock.stepped_ns)) : (owed += 1) { + p.update(.tick); + drain(p); + } + try show(p, gpa, id.?, out, opened or is(verb, "size")); + if (Kind.breach.len > 0 and !said) try out.print("addon-test: {s}\n", .{Kind.breach.slice()}); + said = Kind.breach.len > 0; + } + if (said) return error.OverBudget; +} + +fn open(p: *Pardes, k: u8, path: ?[]const u8) !usize { + const free = p.freeSlot() orelse return error.NoPaneSlots; + _ = if (path) |at| try Kind.openFile(p, free, k, at) else try Kind.open(p, free, k, null, null); + pardes.layout.placeNew(p, p.view().active, null, free, .doc); + p.view().active = free; + drain(p); + return free; +} + +/// The pane's record loaded into a fresh pane in its place, as a Restore +/// would: what the script goes on with. +fn reloaded(p: *Pardes, gpa: std.mem.Allocator, at: usize, out: *std.Io.Writer) !usize { + const slot = p.panes[at].?.addon.?; + const record = try pardes.dump.slotRecord(slot); + defer if (record) |r| slot.gpa.free(r); + try out.print("dump {d} bytes\n", .{if (record) |r| r.len else 0}); + var bytes: std.Io.Writer.Allocating = .init(gpa); + defer bytes.deinit(); + try pardes.dump.slotWrite(slot, gpa, &bytes.writer); + const fresh = try slot.gpa.create(Kind.Slot); + try pardes.dump.slotRead(fresh, slot.gpa, bytes.written()); + try p.removePane(at, null); + const free = p.freeSlot() orelse return error.NoPaneSlots; + const pane = try p.newDocPane(free); + pane.addon = fresh; + pardes.dump.reviveSlot(p, free, fresh); + pardes.layout.placeNew(p, p.view().active, null, free, .doc); + p.view().active = free; + drain(p); + return free; +} + +/// Its effects done as a host does them: its jobs here, at once. +fn drain(p: *Pardes) void { + while (p.nextEffect()) |e| if (e == .job) p.perform(e); +} + +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 } }); + drain(p); + } +} + +/// The cells the kind paints: its body less the notices over it. +fn window(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; + var r = (s.region(.body, id) orelse return null).rect; + const shown = p.panes[id].?.notices.span(); + r.y += shown; + r.h -|= shown; + return r; +} + +/// The pane's body, a line a row (an unpainted cell a space), then each +/// placement over it; `window CxR` first when asked. +fn show(p: *Pardes, gpa: std.mem.Allocator, id: usize, out: *std.Io.Writer, size: bool) !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"); + if (size) if (window(p, id)) |w| try out.print("window {d}x{d}\n", .{ w.w, w.h }); + try out.print("tag {s}\n", .{try Kind.prefix(p, arena.allocator(), 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}", .{ 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 }); + if (pl.at) |at| try out.print(" at {d},{d} {d}x{d}", .{ at.x, at.y, at.w, at.h }); + if (pl.dirty) |d| try out.print(" rows {d}+{d} of {x}", .{ d.y, d.h, d.from }); + try out.writeAll("\n"); + }; +} |
