From d197289c331365081295cb44f27ee2893294cffa Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 3 Nov 2026 16:17:51 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/session.zig | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 test/session.zig (limited to 'test/session.zig') 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; +} -- cgit v1.3