From f7442cb1f23ce10ccedaa37b1851d751f689943d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 19:19:26 -0300 Subject: test: the process tests run on each backend (the user: an effect's test is its backend's): zig build process-test -Dplatform=tty drives pardes, -Dplatform=gui drives pardes-gui hidden (PARDES_TEST, SDL_VIDEO_DRIVER=offscreen, no display of the user's touched) on a pty of its own and a detached session's 160x50 grid, its frames and status in a capture directory of its own, Detached.frame() waiting for it to say it is idle; the runner told its backend (--backend, from the build), each line marked [tty] or [gui] with its time, a case that skips said so; the terminal frontend's and a detached session's own cases (pardes -, a killed tty editor, a signalled detached session, a message expiring with nothing attached) process.only(.tty), a detached session's size checks and pty pixel cells the tty's alone; the canary tty-only for now (on the window its Newcol is refused after the terminal steps, docs/next-tests.md); tty 36 of 36, gui 31 and 5 skipped Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- build.zig | 1 + build/snap.zig | 3 + docs/next-tests.md | 13 ++++ test/process.zig | 136 ++++++++++++++++++++++++++++++++++++++++- test/process/flags.zig | 6 ++ test/process/look_and_exec.zig | 4 ++ test/process/root_ctl.zig | 5 +- test/process/terminals.zig | 10 ++- test/process/tree.zig | 15 +++-- test/process_test.zig | 34 ++++++++--- 10 files changed, 207 insertions(+), 20 deletions(-) diff --git a/build.zig b/build.zig index b3b9eeea..05bc59b7 100644 --- a/build.zig +++ b/build.zig @@ -1390,6 +1390,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .ghostty_vt = ghostty_vt_for_snap, .cloud9 = cloud9, + .gui = platform == .gui, }); const image_harness = b.addExecutable(.{ diff --git a/build/snap.zig b/build/snap.zig index 407463bc..75e578bf 100644 --- a/build/snap.zig +++ b/build/snap.zig @@ -6,6 +6,8 @@ pub fn addTty(b: *std.Build, opts: struct { optimize: std.builtin.OptimizeMode, ghostty_vt: ?*std.Build.Module, cloud9: *std.Build.Module, + /// The build is the window's (-Dplatform=gui): the process tests drive it. + gui: bool = false, }) void { const snap = b.addExecutable(.{ .name = "pardes-snap", @@ -62,6 +64,7 @@ pub fn addTty(b: *std.Build, opts: struct { } const run_process_test = b.addRunArtifact(process_test); run_process_test.addArtifactArg(opts.exe); + run_process_test.addArg(if (opts.gui) "--backend=gui" else "--backend=tty"); run_process_test.addPassthruArgs(); run_process_test.has_side_effects = true; b.step("process-test", "run the process tests: real sessions driven over their sockets (-- [filter])").dependOn(&run_process_test.step); diff --git a/docs/next-tests.md b/docs/next-tests.md index 7f80fa7a..059faf09 100644 --- a/docs/next-tests.md +++ b/docs/next-tests.md @@ -50,6 +50,19 @@ agent's survey. The `e2e` line (kbdin, mousein) is its first step. agent_session.py tool. - **Still Python in the build:** gui_monkey.py (monkey-gui, for T4). + Each backend's own (2026-11-04, the user: an effect's test is the + backend's): + - **`zig build process-test -Dplatform=tty` drives `pardes`, `-Dplatform=gui` + drives `pardes-gui`,** hidden (PARDES_TEST, SDL_VIDEO_DRIVER=offscreen, + no display of the user's) on a pty of its own and a 160x50 grid. + `Detached.frame()` waits for the window to say it is idle and reads its + capture. + - **A case about one frontend's own behaviour says `process.only(.tty)`.** + On the window, 31 cases pass and 5 skip. + - **Open:** on the window the canary's Newcol is refused after its + terminal steps (a fresh window takes it), and one run hung there. The + canary is tty-only until that is found. + Before the port, the plan was: the Python suites in the build (fs.py, monkey9p.py, gui_monkey.py, agent_session_test.py) move onto it or onto Session, then leave the build. The developer tools (ui_review, record_demos, diff --git a/test/process.zig b/test/process.zig index fd423985..f272fda5 100644 --- a/test/process.zig +++ b/test/process.zig @@ -500,8 +500,28 @@ pub fn of(gpa: std.mem.Allocator, h: *const eh.Harness) Files { /// and config directory, as test/fs.py's session started one: no session /// of the user's is reached. No daemon (PARDES_DAEMON=off, as the pty /// harness starts one): the process is the session, and nothing outlives it. +/// Which build the process tests drive (`zig build process-test +/// -Dplatform=tty|gui`, the runner's --backend): a session's effects +/// (saves, shells, watches, dumps) are its host's, so each case runs on the +/// host it was built with. +pub const Backend = enum { tty, gui }; +pub var backend: Backend = .tty; + +/// A case about one frontend's own behaviour (`--tty`, a killed terminal +/// editor) skips on the other. +pub fn only(b: Backend) !void { + if (backend != b) return error.SkipZigTest; +} + pub const Detached = struct { pid: libc.pid_t, + /// The window's (gui): the pty it runs on, a thread draining it, and + /// the directory its frames and status go to (PARDES_TEST_CAPTURE_DIR, + /// outside the root, whose panes would see a file changing every frame). + master: c_int = -1, + drain: ?std.Thread = null, + cap_buf: [64]u8 = undefined, + cap_len: usize = 0, root_buf: [4096]u8 = undefined, root_len: usize = 0, socket_buf: [4096]u8 = undefined, @@ -551,7 +571,26 @@ pub const Detached = struct { const sock_path = try std.fmt.bufPrint(&d.socket_buf, "{s}/9p/pardes/{s}", .{ dir, opts.socket orelse name }); d.socket_len = sock_path.len; var arg_buf: [256]u8 = undefined; - const detach = try std.fmt.bufPrintSentinel(&arg_buf, "--detach={s}", .{name}, 0); + // A window takes no --detach: it is its own session, named by --9p. + const detach = if (backend == .gui) + try std.fmt.bufPrintSentinel(&arg_buf, "--9p={s}", .{opts.socket orelse name}, 0) + else + try std.fmt.bufPrintSentinel(&arg_buf, "--detach={s}", .{name}, 0); + var cap_z: [64]u8 = undefined; + if (backend == .gui) { + const template = "/tmp/pardes-process-cap-XXXXXX"; + @memcpy(cap_z[0..template.len], template); + cap_z[template.len] = 0; + _ = mkdtemp(cap_z[0..template.len :0]) orelse return error.NoTempDir; + @memcpy(d.cap_buf[0..template.len], cap_z[0..template.len]); + d.cap_len = template.len; + } + errdefer if (d.cap_len > 0) removeDir(d.captures()); + var slave: c_int = -1; + if (backend == .gui) { + const ws: std.posix.winsize = .{ .row = 24, .col = 80, .xpixel = 0, .ypixel = 0 }; + if (openpty(&d.master, &slave, null, null, &ws) != 0) return error.NoPty; + } var env_buf: [3][4096]u8 = undefined; const root_z = try std.fmt.bufPrintSentinel(&env_buf[0], "{s}", .{dir}, 0); const config = try std.fmt.bufPrintSentinel(&env_buf[1], "{s}/config", .{dir}, 0); @@ -580,6 +619,23 @@ pub const Detached = struct { .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" }, .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" }, .{ "PARDES_DAEMON", "off" }, }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + if (backend == .gui) { + // Hidden and offscreen, its frames and status in its own + // directory: no display of the user's is ever touched. + for ([_][*:0]const u8{ "WAYLAND_DISPLAY", "DISPLAY" }) |v| _ = unsetenv(v); + for ([_][2][*:0]const u8{ + .{ "PARDES_TEST", "1" }, .{ "PARDES_TEST_CAPTURE_DIR", cap_z[0..d.cap_len :0] }, + // A detached session's grid, so a case means the same on both. + .{ "PARDES_TEST_COLS", "160" }, .{ "PARDES_TEST_ROWS", "50" }, + .{ "SDL_VIDEO_DRIVER", "offscreen" }, .{ "TERM", "xterm-256color" }, + }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + // On a terminal of its own, as one started from a shell is. + _ = libc.setsid(); + _ = ioctl(slave, 0x540E, @as(c_int, 0)); // shortcut: Linux's TIOCSCTTY + _ = libc.dup2(slave, 0); + _ = libc.dup2(slave, 1); + _ = libc.close(d.master); + } for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); if (libc.chdir(root_z) != 0) _exit(126); const fd = libc.open(err_log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); @@ -589,7 +645,13 @@ pub const Detached = struct { } d.pid = pid; errdefer d.stop(); - const deadline = eh.nowMs() + 10_000; + if (backend == .gui) { + _ = libc.close(slave); + // What it writes to its terminal is read and dropped, or a full + // pty would stop it. + d.drain = try std.Thread.spawn(.{}, drainPty, .{d.master}); + } + const deadline = eh.nowMs() + 20_000; var sock_z: [4096]u8 = undefined; const socket_z = try std.fmt.bufPrintSentinel(&sock_z, "{s}", .{sock_path}, 0); while (libc.access(socket_z, 0) != 0) { @@ -669,9 +731,49 @@ pub const Detached = struct { pub fn deinit(d: *Detached) void { d.files.deinit(); d.stop(); + if (d.master >= 0) _ = libc.close(d.master); + if (d.drain) |t| t.join(); + if (d.cap_len > 0) removeDir(d.captures()); if (d.owns_root) d.removeRoot(); } + /// Where the window writes its frames and status (gui alone). + pub fn captures(d: *const Detached) []const u8 { + return d.cap_buf[0..d.cap_len]; + } + + /// The window's frame once it is idle: a loop step two past the one + /// current when this was asked, nothing owing a frame (gui.zig's + /// writeTestStatus). Every request before this one has been served (a + /// 9P write returns once the core has it), so the frame is the scene's + /// however loaded the machine is. A binary PPM (P6), kept by the caller. + pub fn frame(d: *Detached, gpa: std.mem.Allocator) ![]u8 { + if (d.cap_len == 0) return error.NotAWindow; + var path: [128]u8 = undefined; + const status_path = try std.fmt.bufPrint(&path, "{s}/status", .{d.captures()}); + const deadline = eh.nowMs() + 60_000; + var first_step: ?u64 = null; + while (eh.nowMs() < deadline) : (sleepMs(20)) { + const text = readWhole(gpa, status_path) catch continue; + defer gpa.free(text); + var words = std.mem.tokenizeAny(u8, text, " \n"); + const step = std.fmt.parseInt(u64, words.next() orelse continue, 10) catch continue; + const frames = std.fmt.parseInt(u64, words.next() orelse continue, 10) catch continue; + const idle = std.mem.eql(u8, words.next() orelse continue, "1"); + const first = first_step orelse { + first_step = step; + continue; + }; + if (step >= first + 2 and idle and frames > 0) { + var ppm: [128]u8 = undefined; + const got = try readWhole(gpa, try std.fmt.bufPrint(&ppm, "{s}/latest.ppm", .{d.captures()})); + if (std.mem.startsWith(u8, got, "P6")) return got; + gpa.free(got); + } + } + return error.NeverIdle; + } + fn removeRoot(d: *Detached) void { var cmd: [4200]u8 = undefined; if (std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{d.root()}, 0)) |c| { @@ -1012,3 +1114,33 @@ pub fn quicIncluded(gpa: std.mem.Allocator, exe: [:0]const u8) !bool { defer gpa.free(r.said); return !has(r.said, "QUIC is not included"); } + +fn drainPty(fd: c_int) void { + var buf: [4096]u8 = undefined; + while (libc.read(fd, &buf, buf.len) > 0) {} +} + +fn removeDir(path: []const u8) void { + var cmd: [4200]u8 = undefined; + if (std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{path}, 0)) |c| { + _ = system(c); + } else |_| {} +} + +fn readWhole(gpa: std.mem.Allocator, path: []const u8) ![]u8 { + var buf: [4096]u8 = undefined; + const z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0); + const fd = libc.open(z, .{}, @as(libc.mode_t, 0)); + if (fd < 0) return error.FileNotFound; + defer _ = libc.close(fd); + var out: std.ArrayList(u8) = .empty; + errdefer out.deinit(gpa); + var chunk: [64 * 1024]u8 = undefined; + while (true) { + const n = libc.read(fd, &chunk, chunk.len); + if (n < 0) return error.ReadFailed; + if (n == 0) break; + try out.appendSlice(gpa, chunk[0..@intCast(n)]); + } + return out.toOwnedSlice(gpa); +} diff --git a/test/process/flags.zig b/test/process/flags.zig index fffc2867..41aacc03 100644 --- a/test/process/flags.zig +++ b/test/process/flags.zig @@ -652,6 +652,8 @@ fn commandBody(a: std.mem.Allocator, c: *Conn, node: []const u8) ![]const u8 { } fn outside(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // The terminal frontend's and a detached session's own: a window is neither. + try process.only(.tty); var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); @@ -696,6 +698,8 @@ fn socketFile(path: []const u8) !void { } fn killedTty(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // The terminal frontend's and a detached session's own: a window is neither. + try process.only(.tty); var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); @@ -758,6 +762,8 @@ fn daemonGone(d: *const process.Detached) void { } fn signalCleansUp(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // The terminal frontend's and a detached session's own: a window is neither. + try process.only(.tty); // A detached session told to stop by a signal unlinks its sockets. A // hangup is a terminal's end, not a session's: the daemon keeps it. for ([_]libc.SIG{ .TERM, .INT, .HUP }) |first| { diff --git a/test/process/look_and_exec.zig b/test/process/look_and_exec.zig index 2a0266f8..8ff01054 100644 --- a/test/process/look_and_exec.zig +++ b/test/process/look_and_exec.zig @@ -63,6 +63,10 @@ const Flow = struct { }; fn canary(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // shortcut: on the window, after its terminal steps, Newcol is refused + // (or a run hangs), where a fresh window takes it; tty alone until that + // is found (docs/next-tests.md). + try process.only(.tty); const started = process.nowMs(); var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); diff --git a/test/process/root_ctl.zig b/test/process/root_ctl.zig index 008cc882..8ad92f7d 100644 --- a/test/process/root_ctl.zig +++ b/test/process/root_ctl.zig @@ -27,6 +27,8 @@ fn exitAnswers(gpa: std.mem.Allocator, exe: [:0]const u8) !void { /// Nothing attached, no key, no click, no other request comes to move it along. fn messagesExpire(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // With nothing attached: a window always is. + try process.only(.tty); var d = try process.Detached.start(gpa, exe, "msg", .{}); defer d.deinit(); try d.files.write("/pane/1/ctl", "Msg lingering-words\n"); @@ -176,7 +178,8 @@ fn restore(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var screen: process.Files = .init(gpa, address); defer screen.deinit(); const width = try std.unicode.utf8CountCodepoints(std.mem.sliceTo(try screen.screen(), '\n')); - try std.testing.expectEqual(@as(usize, if (tty) 80 else 160), width); // a detached one is 160x50 + // A detached one is 160x50; a window keeps its own size. + if (process.backend == .tty) try std.testing.expectEqual(@as(usize, if (tty) 80 else 160), width); const opened = try restored.look(a, try std.fmt.allocPrint(a, "/n/own/os{s}", .{d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name}))}), ids[1]); try std.testing.expectEqualStrings("initial\n", try restored.pane(a, opened, "body")); } diff --git a/test/process/terminals.zig b/test/process/terminals.zig index e810ceb1..0c95e960 100644 --- a/test/process/terminals.zig +++ b/test/process/terminals.zig @@ -118,15 +118,19 @@ fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try expectEqualStrings("error not run\n", try run(a, c, term, "fi\n")); // a syntax error runs nothing try expectEqualStrings("error not run\n", try run(a, c, term, "echo 'open\n")); // nor does an open quote try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n")); // ...and the shell is clear after - // With no frontend attached the pty's size is in pixels too, at a - // nominal 8x16 cell: a program sizes its kitty graphics by it. + // The pty's size is in pixels too: with no frontend attached at a + // nominal 8x16 cell, in a window at its font's cell. A program sizes + // its kitty graphics by it. const said = try run(a, c, term, "python3 -c 'import fcntl,termios,struct;print(*struct.unpack(\"4H\",fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8))))'\n"); var lines = std.mem.splitScalar(u8, said, '\n'); _ = lines.next(); var size = std.mem.tokenizeScalar(u8, lines.next() orelse "", ' '); var n: [4]u32 = undefined; for (&n) |*v| v.* = try std.fmt.parseInt(u32, size.next() orelse return error.NoSize, 10); - try expect(n[2] == n[1] * 8 and n[3] == n[0] * 16 and n[2] > 0); + if (process.backend == .gui) + try expect(n[2] > 0 and n[3] > 0 and n[2] % n[1] == 0 and n[3] % n[0] == 0) + else + try expect(n[2] == n[1] * 8 and n[3] == n[0] * 16 and n[2] > 0); // A paging command run through pty/run does not wait in less: the // terminal's PAGER is `pardes -`, and its text is a +Pager pane. if (shell(a, "command -v git >/dev/null", .{}) == 0) { diff --git a/test/process/tree.zig b/test/process/tree.zig index 358e7040..ee39170b 100644 --- a/test/process/tree.zig +++ b/test/process/tree.zig @@ -480,12 +480,15 @@ fn cutWrites(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var k: usize = 0; while (body.next()) |line| : (k += 1) try expectEqualStrings(f.at("line {d:0>5} of the block", .{k}), line); try expectEqual(@as(usize, 2500), k); - // A detached session with no frontend is 160x50 until told. - const cols, const rows = try screenSize(f); - try expect(cols == 160 and rows == 50); - try c.write("/ctl", "size 132 40\n"); - const cols2, const rows2 = try screenSize(f); - try expect(cols2 == 132 and rows2 == 40); + // A detached session with no frontend is 160x50 until told; a + // window's size is its own, and refuses size. + if (process.backend == .tty) { + const cols, const rows = try screenSize(f); + try expect(cols == 160 and rows == 50); + try c.write("/ctl", "size 132 40\n"); + const cols2, const rows2 = try screenSize(f); + try expect(cols2 == 132 and rows2 == 40); + } } fn rootAndOs(gpa: std.mem.Allocator, exe: [:0]const u8) !void { diff --git a/test/process_test.zig b/test/process_test.zig index 2f1b5791..a870f13a 100644 --- a/test/process_test.zig +++ b/test/process_test.zig @@ -4,6 +4,7 @@ //! //! pardes-process-test [filter] const std = @import("std"); +const process = @import("process.zig"); const files = .{ @import("process/builtins.zig"), @@ -19,25 +20,42 @@ const files = .{ }; pub fn main(init: std.process.Init) !void { - const args = try init.minimal.args.toSlice(init.arena.allocator()); - if (args.len < 2) return error.Usage; - const exe = args[1]; - const filter: []const u8 = if (args.len > 2) args[2] else ""; + const all = try init.minimal.args.toSlice(init.arena.allocator()); + // [--backend=tty|gui] [filter] + var args: [3][:0]const u8 = undefined; + var n: usize = 0; + for (all[1..]) |a| { + if (std.mem.startsWith(u8, a, "--backend=")) { + process.backend = std.meta.stringToEnum(process.Backend, a["--backend=".len..]) orelse return error.Usage; + } else if (n < args.len) { + args[n] = a; + n += 1; + } + } + if (n < 1) return error.Usage; + const exe = args[0]; + const filter: []const u8 = if (n > 1) args[1] else ""; const gpa = std.heap.page_allocator; + const on = @tagName(process.backend); var failed: usize = 0; + var skipped: usize = 0; var ran: usize = 0; inline for (files) |file| inline for (file.cases) |case| { if (std.mem.indexOf(u8, case[0], filter) != null) { ran += 1; + const t0 = process.nowMs(); if (case[1](gpa, exe)) { - std.debug.print("PASS {s}\n", .{case[0]}); - } else |err| { + std.debug.print("PASS [{s}] {d} ms {s}\n", .{ on, process.nowMs() - t0, case[0] }); + } else |err| if (err == error.SkipZigTest) { + skipped += 1; + std.debug.print("SKIP [{s}] {s}\n", .{ on, case[0] }); + } else { failed += 1; if (@errorReturnTrace()) |t| std.debug.dumpErrorReturnTrace(t); - std.debug.print("FAIL {s}: {t}\n", .{ case[0], err }); + std.debug.print("FAIL [{s}] {d} ms {s}: {t}\n", .{ on, process.nowMs() - t0, case[0], err }); } } }; - std.debug.print("{d} of {d} process tests failed\n", .{ failed, ran }); + std.debug.print("{d} of {d} process tests failed on {s} ({d} skipped)\n", .{ failed, ran, on, skipped }); if (failed > 0 or ran == 0) std.process.exit(1); } -- cgit v1.3