summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-03 16:17:51 -0300
committerGabriel Schneider <[email protected]>2026-11-03 16:21:16 -0300
commitd197289c331365081295cb44f27ee2893294cffa (patch)
treef4d64abdcdfcb714ece786fd050264dcc22a2bf5
parent86e9f8e241df16d96df542e08684af638c39fb89 (diff)
downloadpardes-d197289c331365081295cb44f27ee2893294cffa.tar.gz
pardes-d197289c331365081295cb44f27ee2893294cffa.zip
test: the behaviour-test step and test/session.zig, T1 of docs/next-tests.md: a Session drives the core as a 9P client does (walk by path, open, read or write, clunk, held lines run at the clunk as src/9p_io.zig runs them), the core opaque and the step's module importing session alone, so a behavioural test cannot reach past the 9P tree; keys (kbdin), click (on text found on /screen, mousein), screen, focused, refused and the refusal's words; test/behaviour/{other_files,panes,edit}.zig, a file a section of the reference: kbdin and mousein, a collapsed pane's ctl, name's leading blank, Putall, Edit blocks, moved out of src/ninep/ctl.zig (an Edit block left open now checked as a client sees it, an err in the log at its clunk), each checked to fail against a break (collapsed left off the ctl read, Putall saving nothing); the plan doc says the layout, the decisions and the progress
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--build.zig27
-rw-r--r--docs/next-tests.md40
-rw-r--r--src/ninep/ctl.zig85
-rw-r--r--test/behaviour.zig9
-rw-r--r--test/behaviour/edit.zig21
-rw-r--r--test/behaviour/other_files.zig23
-rw-r--r--test/behaviour/panes.zig40
-rw-r--r--test/session.zig179
8 files changed, 335 insertions, 89 deletions
diff --git a/build.zig b/build.zig
index 4485140d..8a9451eb 100644
--- a/build.zig
+++ b/build.zig
@@ -1864,6 +1864,33 @@ pub fn build(b: *std.Build) void {
pane_test_step.dependOn(&run_pane_tests.step);
if (testMatch(b, unit_profile, test_filters, pane_test_step)) |check| check.addArtifactArg(pane_tests);
+ // The behavioural tests (docs/next-tests.md): their module has the
+ // session alone, so none can reach past what a 9P client sees.
+ const session_module = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ .root_source_file = b.path("test/session.zig"),
+ .link_libc = true,
+ });
+ session_module.addImport("pardes", core_module);
+ const behaviour_module = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ .root_source_file = b.path("test/behaviour.zig"),
+ .link_libc = true,
+ });
+ behaviour_module.addImport("session", session_module);
+ const behaviour_tests = b.addTest(.{ .root_module = behaviour_module, .filters = test_filters });
+ if (test_nonce) |nonce| behaviour_tests.root_module.addImport("test_rebuild_nonce", nonce);
+ test_build.dependOn(&behaviour_tests.step);
+ const run_behaviour_tests = b.addRunArtifact(behaviour_tests);
+ run_behaviour_tests.has_side_effects = true;
+ unit_step.dependOn(&run_behaviour_tests.step);
+ if (unit_match) |check| check.addArtifactArg(behaviour_tests);
+ const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/)");
+ behaviour_test_step.dependOn(&run_behaviour_tests.step);
+ if (testMatch(b, unit_profile, test_filters, behaviour_test_step)) |check| check.addArtifactArg(behaviour_tests);
+
// Kitty graphics conformance at the core: what a terminal pane's
// program sends, what it reads back, what ghostty stores and what a
// frame draws (test/kitty.zig). The end-to-end arm, through a real
diff --git a/docs/next-tests.md b/docs/next-tests.md
index f05a6e73..7dbbff60 100644
--- a/docs/next-tests.md
+++ b/docs/next-tests.md
@@ -15,9 +15,29 @@ agent's survey. The `e2e` line (kbdin, mousein) is its first step.
- **T1, in-process 9P, is the main tier.** The core is driven only through
`serveFs` (src/ninep/testing.zig's `call`, `rd`, `wr`): as behavioural
as a client on a socket, and milliseconds a case.
-- **Process-level tests (T2) get a Zig runner, not Python.** The Python
- suites (test/fs.py and the rest) stay as examples of scripting a session
- and are not grown.
+- **Process-level tests (T2) get a Zig runner, not Python:** test/snapshot.zig,
+ the `.snap` runner, which already forks the app in a pty and has a 9P
+ client (`snap9p`), grows 9P verbs (`9pwrite`, `9pexpect`, `signal`). The
+ Python suites (test/fs.py and the rest) stay as examples of scripting a
+ session and are not grown.
+- **Layout (from a ponytail review, 2026-11-03):**
+ - **test/session.zig is the T1 `Session`:** a 9P client's walk, open,
+ read or write, clunk, by path, with the core opaque.
+ - **test/behaviour/<label>.zig holds the tests,** a file a section of
+ the book named by its label (`other-files` is other_files.zig), each
+ test's name its label first.
+ - **The `behaviour-test` step:** its module imports `session` alone, so
+ a behavioural test that reaches for `pardes` does not compile. That
+ boundary is the lint.
+ - **Expectations are inline;** goldens stay where they are.
+- **A surface is added when a test being migrated needs one,** as a word in
+ a file that exists where it can be (a pane ctl's read already ends
+ `collapsed fit:… tint:…`; rio's wctl read is its window's rectangle).
+- **A migration is one commit:** the behavioural test in, the M test it
+ replaces out, and the break it was checked against named. Phases 2 and 4
+ are one.
+- **A replica's agreement is checked opt-in** (a Session option, to come),
+ for kbdin and mousein input, which a replica takes as events.
- **Later, not now:**
- The reference as the spec: one test per documented behaviour, named by
its section (docs/typ/reference.typ for the 9P surface, guide.typ and
@@ -32,7 +52,6 @@ agent's survey. The `e2e` line (kbdin, mousein) is its first step.
| T1 | the core through `serveFs`, in process | most behaviour; each case also through a replica that must agree |
| T2 | a real `pardes` over its socket, a Zig runner | daemon, attach, mounts, shells, flags |
| T3 | terminal bytes, the `.snap` scripts | tty rendering and keys |
-
| T4 | the window: `pardes-gui` hidden (`PARDES_TEST`, offscreen), its input through the same 9P files, frames captured once its status file says idle | what only pixels show |
| perf | T1 or T4 gestures, timed | latency gates against baselines |
@@ -80,6 +99,19 @@ What the M tests reach into, and the public surface for it:
| `.pdf.?` | 25 | **missing**: page, viewport |
| terminal cursor, overlay | 60 | partial |
+## Progress
+
+Left to migrate, a file a row (`grep -c '^test "' <file>` before and
+after; M as the inventory classed them):
+
+| file | M at the start | moved | checked against |
+|---|---|---|---|
+| src/ninep/ctl.zig | 82 | 5 | `collapsed ` left off the ctl read; Putall saving nothing |
+
+Migrated so far, in test/behaviour/: other_files (kbdin, mousein), panes
+(collapsed, name's leading blank, Putall), edit (an Edit block to a pane's
+ctl, the root's and exec, one left open at its clunk an err in the log).
+
## Phases
0. Inventory. Done, above.
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 37f27dac..69a0e791 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -1681,22 +1681,6 @@ test "Edit through a pane's ctl renames every foo in one undo step, and a failed
try testing.expectEqualStrings("foo x foo y foo\n", rd(p, body, 0, 64).bytes);
}
-test "a write of an Edit block carries it whole, to a pane's ctl, the root's ctl and exec" {
- const p = try withFile(testing.allocator, "foo x\nfoo y\n");
- defer p.deinit();
- const serial = serialOf(p);
- const body = Node.of(serial, .body);
- try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Edit ,x/foo/{\ni/[/\na/]/\n}\n").reply.status);
- try testing.expectEqualStrings("[foo] x\n[foo] y\n", rd(p, body, 0, 64).bytes);
- p.active = p.paneBySerial(serial).?;
- try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Edit ,x/\\[/{\nc/</\n}\n").reply.status);
- try testing.expectEqualStrings("<foo] x\n<foo] y\n", rd(p, body, 0, 64).bytes);
- try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Edit $a\nend\n.\n").reply.status);
- try testing.expectEqualStrings("<foo] x\n<foo] y\nend\n", rd(p, body, 0, 64).bytes);
- // An unclosed group is refused, and nothing after it runs as a line.
- try testing.expectEqual(E.IO, wr(p, Node.of(serial, .ctl), "Edit ,x/foo/{\nd\n").errno());
- try testing.expectEqualStrings("<foo] x\n<foo] y\nend\n", rd(p, body, 0, 64).bytes);
-}
test "dirty goes back to 0 when the text is the saved text again" {
const p = try withFile(testing.allocator, "one\n");
@@ -2012,24 +1996,6 @@ test "Get reads the pane's file again, refusing unsaved edits once, and Get file
try testing.expect(!pane_files.dirtyOf(pane));
}
-test "Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
- const p = try withFile(testing.allocator, "x\n");
- defer p.deinit();
- const first = serialOf(p);
- _ = wr(p, Node.of(first, .name), "/tmp/pardes-putall-a.txt\n");
- _ = wr(p, Node.of(first, .body), "edited\n");
- const second = p.freeSlot().?;
- _ = try p.newDocPane(second);
- p.panes[second].?.file = .{ .path = try p.gpa.dupe(u8, "/tmp/pardes-putall-b.txt"), .content = try p.gpa.dupe(u8, "clean\n"), .history = try panes.File.History.create(p.gpa) };
- while (p.nextEffect()) |_| {}
- try testing.expect(p.executeBuiltinLine(0, "Putall"));
- var saves: usize = 0;
- while (p.nextEffect()) |e| if (e == .save_file) {
- saves += 1;
- try testing.expectEqual(@as(u16, 0), e.save_file.pane);
- };
- try testing.expectEqual(@as(usize, 1), saves);
-}
test "Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Save cleans both, Del of one asks nothing" {
var tmp = testing.tmpDir(.{});
@@ -3690,16 +3656,6 @@ test "a line starting with # runs as nothing at an exec or a ctl, silently" {
try testing.expectEqualStrings(log_before.bytes, log_after.bytes);
}
-test "the ctl word name refuses a leading blank, as the name file does" {
- const p = try withFile(testing.allocator, "x\n");
- defer p.deinit();
- const serial = serialOf(p);
- const r = wr(p, Node.of(serial, .ctl), "name /tmp/lead.txt\n");
- try testing.expectEqual(E.INVAL, r.errno());
- try testing.expectEqualStrings("bad character in file name: a blank at its start", r.reply.ename);
- try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "name /tmp/ok.txt\n").reply.status);
- try testing.expectEqualStrings("/tmp/ok.txt", pane_files.nameOf(p, p.panes[0].?));
-}
test "a builtin that takes no argument, written with one to an exec, is refused wrong #args, as at a ctl" {
const p = try withFile(testing.allocator, "x\n");
@@ -5042,15 +4998,6 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its +
try testing.expect(std.mem.endsWith(u8, p.panes[shown].?.file.?.path, "+Lsp"));
}
-test "a collapsed pane's ctl says so" {
- const p = try withFile(testing.allocator, "x\n");
- defer p.deinit();
- _ = try th.newPane(p);
- const id = p.paneBySerial(serialOf(p)).?;
- layout.toggleCollapse(p, id);
- try testing.expect(p.panes[id].?.collapsed);
- try testing.expect(std.mem.endsWith(u8, rd(p, Node.of(serialOf(p), .ctl), 0, 4096).bytes, " collapsed "));
-}
test "a look of ./x that is not there is a miss, as x and /abs/x are: answered, the err logged" {
const p = try withFile(testing.allocator, "x\n");
@@ -5180,35 +5127,3 @@ test "the init file saved in a pane applies its settings again, and runs none of
try testing.expectEqualStrings("acme", p.theme().name);
try testing.expectEqual(cols, p.ncol);
}
-
-test "kbdin types at the pane with the keyboard and mousein clicks, as a user would, each done before its write is answered" {
- const p = try withFile(testing.allocator, "x\n");
- defer p.deinit();
- const s = serialOf(p);
- const kbdin = @backingInt(tree.TopFile.kbdin);
- const mousein = @backingInt(tree.TopFile.mousein);
- const body = Node.of(s, .body);
- try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "ihello \x1b").errno());
- try testing.expectEqualStrings("hello x\n", rd(p, body, 0, 64).bytes);
- // Plan 9's Khome rune, then text before it.
- try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "A!\x1b\u{F00D}i>\x1b").errno());
- try testing.expectEqualStrings(">hello x!\n", rd(p, body, 0, 64).bytes);
- // A left click on the second l, then text there; where it is, as a
- // client reads it off /screen, from a frame drawn.
- var frame: std.heap.ArenaAllocator = .init(p.gpa);
- defer frame.deinit();
- _ = try p.render(frame.allocator());
- const id = p.paneBySerial(s).?;
- const pane = p.panes[id].?;
- const r = p.rects[id];
- var line: [64]u8 = undefined;
- const at = .{ r.x + config.GUTTER + panes.File.gutterWidth(pane) + 4, p.bodyTop(pane, r) };
- try testing.expectEqual(@as(u16, 0), wr(p, mousein, try std.fmt.bufPrint(&line, "m {d} {d} 1\nm {d} {d} 0\n", at ++ at)).errno());
- try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "i#\x1b").errno());
- try testing.expectEqualStrings(">hel#lo x!\n", rd(p, body, 0, 64).bytes);
- // Refused, in words, as rio and devmouse refuse.
- try testing.expectEqual(E.INVAL, wr(p, mousein, "m 1 2\n").errno());
- try testing.expectEqual(E.INVAL, wr(p, mousein, "x 1 2 0\n").errno());
- try testing.expectEqual(E.INVAL, wr(p, kbdin, "\xff").errno());
- try testing.expectEqual(E.PERM, rd(p, kbdin, 0, 64).errno());
-}
diff --git a/test/behaviour.zig b/test/behaviour.zig
new file mode 100644
index 00000000..31da19a1
--- /dev/null
+++ b/test/behaviour.zig
@@ -0,0 +1,9 @@
+//! The behavioural tests (docs/next-tests.md): a file a section of the
+//! book, named by its label (`other-files` is other_files.zig), each test
+//! 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/edit.zig");
+ _ = @import("behaviour/other_files.zig");
+ _ = @import("behaviour/panes.zig");
+}
diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig
new file mode 100644
index 00000000..e95471c6
--- /dev/null
+++ b/test/behaviour/edit.zig
@@ -0,0 +1,21 @@
+//! docs/typ/reference.typ, Edit <edit>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root's ctl and exec" {
+ var s = try Session.initFile(std.testing.allocator, "foo x\nfoo y\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\ni/[/\na/]/\n}\n");
+ try s.expect(body, "[foo] x\n[foo] y\n");
+ try s.write("/ctl", "Edit ,x/\\[/{\nc/</\n}\n");
+ try s.expect(body, "<foo] x\n<foo] y\n");
+ try s.write(s.at("/pane/{d}/exec", .{n}), "Edit $a\nend\n.\n");
+ try s.expect(body, "<foo] x\n<foo] y\nend\n");
+ // A group still open when its open closes fails there, an err in the
+ // log, and nothing after it runs as a line.
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\nd\n");
+ try s.expect(body, "<foo] x\n<foo] y\nend\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null);
+}
diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig
new file mode 100644
index 00000000..b97affc4
--- /dev/null
+++ b/test/behaviour/other_files.zig
@@ -0,0 +1,23 @@
+//! docs/typ/reference.typ, Other files <other-files>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "other-files: kbdin types at the pane with the keyboard and mousein clicks, each done before its write is answered" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const body = try std.fmt.allocPrint(s.arena.allocator(), "/pane/{d}/body", .{try s.focused()});
+ try s.keys("ihello \x1b");
+ try s.expect(body, "hello x\n");
+ // Plan 9's Khome rune, then text before it.
+ try s.keys("A!\x1b\u{F00D}i>\x1b");
+ try s.expect(body, ">hello x!\n");
+ // A left click on the second l, then text there.
+ try s.click(">hello", 4, 1);
+ try s.keys("i#\x1b");
+ try s.expect(body, ">hel#lo x!\n");
+ // Refused, in words, as rio and devmouse refuse.
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "m 1 2\n"));
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "x 1 2 0\n"));
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/kbdin", "\xff"));
+ try std.testing.expectError(error.Refused, s.read("/kbdin"));
+}
diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig
new file mode 100644
index 00000000..45b7152c
--- /dev/null
+++ b/test/behaviour/panes.zig
@@ -0,0 +1,40 @@
+//! docs/typ/reference.typ, Panes <panes>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "panes: a collapsed pane's ctl says so" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const first = try s.focused();
+ _ = try s.newPane();
+ try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n");
+ try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed "));
+}
+
+test "panes: the ctl word name refuses a leading blank, as the name file does" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
+ try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name /tmp/lead.txt\n"));
+ try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said);
+ try s.write(ctl, "name /tmp/ok.txt\n");
+ try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n");
+}
+
+test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const first = try s.focused();
+ try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n");
+ try s.write(s.at("/pane/{d}/body", .{first}), "edited\n");
+ // A file opened from disk, unedited.
+ try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir}));
+ try std.testing.expectEqual(@as(usize, 0), s.saved);
+ try s.write("/ctl", "Putall\n");
+ try std.testing.expectEqual(@as(usize, 1), s.saved);
+}
diff --git a/test/session.zig b/test/session.zig
new file mode 100644
index 00000000..28557322
--- /dev/null
+++ b/test/session.zig
@@ -0,0 +1,179 @@
+//! T1 of docs/next-tests.md: a session driven as a 9P client drives one,
+//! in process. Each call walks a path from the root, opens, reads or writes,
+//! and clunks, through `serveFs` alone. The core is opaque here, so a
+//! behavioural test (test/behaviour/, whose module imports only this) has
+//! no field to reach: what it can see is what a client can.
+const std = @import("std");
+const pardes = @import("pardes");
+
+const Pardes = pardes.Pardes;
+const Req = pardes.ctlfs.Req;
+const Reply = pardes.ctlfs.Reply;
+
+pub const Session = struct {
+ core: *anyopaque,
+ /// What the calls answered, kept until deinit.
+ arena: std.heap.ArenaAllocator,
+ /// Saves the core asked its host for (a host would perform them).
+ saved: usize = 0,
+ /// The words of the last refusal `refused` answered.
+ said: []const u8 = "",
+ tag: u64 = 0,
+
+ /// A session showing one file pane holding `text`.
+ pub fn initFile(gpa: std.mem.Allocator, text: []const u8) !Session {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ errdefer p.deinit();
+ _ = try p.setTestFile(text);
+ var s: Session = .{ .core = p, .arena = .init(gpa) };
+ s.drain();
+ return s;
+ }
+
+ /// A session showing its terminal pane, as `pardes` starts.
+ pub fn initTerm(gpa: std.mem.Allocator) !Session {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ var s: Session = .{ .core = p, .arena = .init(gpa) };
+ s.drain();
+ return s;
+ }
+
+ pub fn deinit(s: *Session) void {
+ pardesOf(s).deinit();
+ s.arena.deinit();
+ }
+
+ /// The whole file at `path` (`/index`, `/pane/3/body`).
+ pub fn read(s: *Session, path: []const u8) ![]const u8 {
+ const node, const h = try s.open(path, 0);
+ defer _ = s.serve(.{ .tag = 0, .op = .release, .node = node, .handle = h, .opened = true });
+ var out: std.ArrayList(u8) = .empty;
+ while (true) {
+ const r = s.serve(.{ .tag = 0, .op = .read, .node = node, .handle = h, .off = out.items.len, .size = 1 << 16 });
+ if (r.status != .ok) return refusal(path, r);
+ const bytes = pardesOf(s).fsPayload(r);
+ if (bytes.len == 0) break;
+ try out.appendSlice(s.arena.allocator(), bytes);
+ }
+ return out.items;
+ }
+
+ /// `data` written to `path` in one write, the open clunked after.
+ pub fn write(s: *Session, path: []const u8, data: []const u8) !void {
+ const errno = try s.refused(path, data);
+ if (errno != 0) return error.Refused;
+ }
+
+ /// Like `write`, answering the refusal's errno, 0 when it was taken.
+ pub fn refused(s: *Session, path: []const u8, data: []const u8) !u16 {
+ const node, const h = try s.open(path, 1);
+ const w = s.serve(.{ .tag = 0, .op = .write, .node = node, .handle = h, .data = data });
+ // The clunk can refuse too (an Edit block left open), as close(2) does.
+ const c = s.serve(.{ .tag = 0, .op = .release, .node = node, .handle = h, .opened = true });
+ const r = if (w.status != .ok) w else c;
+ if (r.status == .ok) return 0;
+ s.said = try s.arena.allocator().dupe(u8, r.ename);
+ return r.errno;
+ }
+
+ /// A path built from `args` (`s.at("/pane/{d}/body", .{n})`), kept
+ /// until deinit.
+ pub fn at(s: *Session, comptime fmt: []const u8, args: anytype) []const u8 {
+ return std.fmt.allocPrint(s.arena.allocator(), fmt, args) catch @panic("out of memory");
+ }
+
+ /// A new pane, made by opening /pane/new; its serial.
+ pub fn newPane(s: *Session) !u32 {
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/pane/new"), "\n"), 10);
+ }
+
+ pub fn expect(s: *Session, path: []const u8, want: []const u8) !void {
+ try std.testing.expectEqualStrings(want, try s.read(path));
+ }
+
+ /// Keys typed at the pane with the keyboard (`/kbdin`'s runes).
+ pub fn keys(s: *Session, runes: []const u8) !void {
+ try s.write("/kbdin", runes);
+ }
+
+ /// A click (Plan 9's buttons: 1 left, 2 middle, 4 right) on the first
+ /// place `text` is drawn, `into` cells into it, found as a user finds it.
+ pub fn click(s: *Session, text: []const u8, into: usize, buttons: u8) !void {
+ const col, const row = try s.find(text);
+ var line: [64]u8 = undefined;
+ const x = col + into;
+ try s.write("/mousein", try std.fmt.bufPrint(&line, "m {d} {d} {d}\nm {d} {d} 0\n", .{ x, row, buttons, x, row }));
+ }
+
+ /// The screen a frame draws, a line a row.
+ pub fn screen(s: *Session) ![]const u8 {
+ const a = s.arena.allocator();
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{});
+ const cols: usize = @intCast(json.object.get("cols").?.integer);
+ const cells = json.object.get("cells").?.array.items;
+ var out: std.ArrayList(u8) = .empty;
+ for (cells, 0..) |cell, i| {
+ if (i > 0 and i % cols == 0) try out.append(a, '\n');
+ const g = cell.array.items[0].string;
+ try out.appendSlice(a, if (g.len == 0) " " else g);
+ }
+ return out.items;
+ }
+
+ /// The serial of the pane with the keyboard.
+ pub fn focused(s: *Session) !u32 {
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/focus"), "\n"), 10);
+ }
+
+ // shortcut: a grapheme is taken as one cell wide, so text past a wide
+ // character is found a column off; count widths when a test needs it.
+ fn find(s: *Session, text: []const u8) !struct { usize, usize } {
+ var rows = std.mem.splitScalar(u8, try s.screen(), '\n');
+ var row: usize = 0;
+ while (rows.next()) |line| : (row += 1) {
+ if (std.mem.indexOf(u8, line, text)) |i| return .{ try std.unicode.utf8CountCodepoints(line[0..i]), row };
+ }
+ std.debug.print("not on the screen: {s}\n", .{text});
+ return error.NotOnScreen;
+ }
+
+ fn open(s: *Session, path: []const u8, omode: u8) !struct { u64, u32 } {
+ var node: u64 = pardes.ctlfs.root;
+ var parts = std.mem.tokenizeScalar(u8, path, '/');
+ while (parts.next()) |name| {
+ const r = s.serve(.{ .tag = 0, .op = .lookup, .node = node, .data = name });
+ if (r.status != .ok) return refusal(path, r);
+ node = r.attr.node;
+ }
+ const r = s.serve(.{ .tag = 0, .op = .open, .node = node, .omode = omode });
+ if (r.status != .ok) return refusal(path, r);
+ return .{ node, r.handle };
+ }
+
+ fn serve(s: *Session, req: Req) Reply {
+ var q = req;
+ s.tag += 1;
+ q.tag = s.tag;
+ const r = pardesOf(s).serveFs(q);
+ // As the server does (src/9p_io.zig): a clunk's held lines run then.
+ if (q.op == .release) pardes.ctlfs.runClosedLines(pardesOf(s));
+ s.drain();
+ return r;
+ }
+
+ fn drain(s: *Session) void {
+ while (pardesOf(s).nextEffect()) |e| switch (e) {
+ .save_file, .save_text => s.saved += 1,
+ else => {},
+ };
+ }
+
+ fn pardesOf(s: *Session) *Pardes {
+ return @ptrCast(@alignCast(s.core));
+ }
+};
+
+fn refusal(path: []const u8, r: Reply) error{Refused} {
+ std.debug.print("{s}: refused, errno {d}: {s}\n", .{ path, r.errno, r.ename });
+ return error.Refused;
+}