summaryrefslogtreecommitdiff
path: root/test/process/flags.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 16:46:18 -0300
committerGabriel Schneider <[email protected]>2026-11-04 16:55:32 -0300
commit6bcb18e021a09d51539e628106a594748637ead2 (patch)
tree954996560c694dd7f0ab410aa01def00d25c33fe /test/process/flags.zig
parentbe3985092df1c3388cfa09ca5e989d2fa66f55ec (diff)
downloadpardes-6bcb18e021a09d51539e628106a594748637ead2.tar.gz
pardes-6bcb18e021a09d51539e628106a594748637ead2.zip
test: fs.py's inline checks, quic_test, workflow_canary, python_repl_runs_one_line and signal_cleans_up moved to Zig, deleted from fs.py (its named cases stay): 21 process cases in test/process/ (flags: bad options refused in words, --9p naming the socket, forwarding a launch inside a session and the launches that start their own (mount, name, tcp, load, shells, toggle, quic), --wait and its refusals, pardes - and its +Pager (escapes, inks and Filter, progress, 11 MiB, the 256 MiB cut), $winid, the stdin boot, no terminal to draw on, a killed tty editor and its swept sockets, a signal unlinking the socket; mounts: --mount at startup, Mount and Unmount at run time, QUIC and TCP v4 and v6 detached and in a pty (skipped without -Dquic=true, passed with it); tree: the root and /os, 256-byte frames, a held ctl line, a frozen /screen, Look's precedence, 255-byte names; root-ctl: Restore detached and in a pty; builtins: Lspinfo; look-and-exec: the canary; repls: Python's pyrepl) and 3 behaviour tests (a 60 KB body write, truncated, a missing pane refused; a body write's EI record; keyword colour, not bold); process.zig gains, under fsb:, Conn.dialSized, dialTcp, replace, newPane, serials, look, pane, exec, execRefused, removePane, Files.fg, Detached.exists, abs, startTty, and Child (a program in a root's environment, its stdin fed, on a pty if asked, its stderr kept), run, code, entries, canonical, quicIncluded, nowMs, nap, has, check; each checked to fail against a break (no such option reworded, SyntaxBold on by default, a body insert's record not EI, Grep's miss reworded, already inside pardes reworded, a session already running reworded, Lspinfo's servers line reworded, no terminal to draw on reworded, unknown control message reworded); behaviour, process, fs-unit and core tests and the rest of fs.py pass
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/process/flags.zig')
-rw-r--r--test/process/flags.zig778
1 files changed, 778 insertions, 0 deletions
diff --git a/test/process/flags.zig b/test/process/flags.zig
new file mode 100644
index 00000000..2ae8d0c7
--- /dev/null
+++ b/test/process/flags.zig
@@ -0,0 +1,778 @@
+//! docs/typ/reference.typ, Flags <flags>: a real pardes launched with
+//! them, inside a session and out (test/fs.py's inline checks).
+const std = @import("std");
+const libc = std.c;
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const Child = process.Child;
+
+pub const cases = .{
+ .{ "flags: a bad option, socket name, dial or mount is refused in words, exit 1", refusals },
+ .{ "flags: --9p=<name> names the socket, not --detach's name", explicitName },
+ .{ "flags: a launch inside a session forwards its file there, refuses what it cannot, and one with flags of a session's own starts its own", forwarding },
+ .{ "flags: --wait lasts until its pane goes; a forwarded name is opened, made or refused in words; pardes - pages stdin into a +Pager", waitAndPager },
+ .{ "flags: a command from a pane's tag gets $winid, one from the workspace's tag none", winid },
+ .{ "flags: pardes - outside a session boots on stdin's +Pager; a launch with no terminal to draw on says so", outside },
+ .{ "flags: a tty editor killed by SIGTERM or SIGHUP puts its terminal back and its socket goes; a dead editor's socket is swept", killedTty },
+ .{ "flags: a detached session told to stop by a signal unlinks its socket; a hangup does not stop it", signalCleansUp },
+};
+
+extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]const u8;
+
+const long_name: [:0]const u8 = blk: {
+ var name: [304:0]u8 = @splat('x');
+ @memcpy(name[300..], ".txt");
+ const done = name;
+ break :blk &done;
+};
+
+const has = process.has;
+const check = process.check;
+
+fn z(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) [:0]const u8 {
+ return std.fmt.allocPrintSentinel(a, fmt, args, 0) catch @panic("out of memory");
+}
+
+fn contains(set: []const u32, n: u32) bool {
+ return std.mem.indexOfScalar(u32, set, n) != null;
+}
+
+/// The serials in `after` not in `before`.
+fn added(a: std.mem.Allocator, before: []const u32, after: []const u32) []const u32 {
+ var out: std.ArrayList(u32) = .empty;
+ for (after) |n| if (!contains(before, n)) out.append(a, n) catch @panic("out of memory");
+ return out.items;
+}
+
+/// The one pane made since `before`.
+fn madeOne(a: std.mem.Allocator, c: *Conn, before: []const u32) !u32 {
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 1, "made {any}, not one pane", .{new});
+ return new[0];
+}
+
+fn same(a: []const u32, b: []const u32) bool {
+ return std.mem.eql(u32, a, b);
+}
+
+/// The session's pid, from /status.
+fn statusPid(a: std.mem.Allocator, c: *Conn) ![:0]const u8 {
+ var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n');
+ while (lines.next()) |line| if (std.mem.startsWith(u8, line, "pid ")) return z(a, "{s}", .{line[4..]});
+ return error.NoPid;
+}
+
+/// Waits up to `ms` for the session's log to hold `text`.
+fn waitLog(a: std.mem.Allocator, c: *Conn, text: []const u8, ms: i64) !void {
+ const deadline = process.nowMs() + ms;
+ while (!has(try c.read(a, "/log"), text)) {
+ if (process.nowMs() > deadline) {
+ std.debug.print("not in the log: {s}\n", .{text});
+ return error.Timeout;
+ }
+ process.nap(20);
+ }
+}
+
+fn isSocketName(name: []const u8) bool {
+ return std.mem.startsWith(u8, name, "pardes-9p-") and std.mem.endsWith(u8, name, ".sock");
+}
+
+/// The socket a launched session made in `root`, none of `existing`: one
+/// named by its pid, as fs.py's glob found it.
+fn newSocket(a: std.mem.Allocator, root: []const u8, existing: []const []const u8, child: *Child) ![]const u8 {
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ for (try process.entries(a, root)) |name| {
+ if (!isSocketName(name)) continue;
+ for (existing) |old| {
+ if (std.mem.eql(u8, old, name)) break;
+ } else return std.fmt.allocPrint(a, "{s}/{s}", .{ root, name });
+ }
+ if (child.wait(0)) |status| {
+ std.debug.print("exited {d} before its socket: {s}\n", .{ status, child.said(a) catch "" });
+ return error.Exited;
+ }
+ if (process.nowMs() > deadline) return error.NoSocket;
+ process.nap(5);
+ }
+}
+
+fn refusals(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const Row = struct { []const [:0]const u8, []const u8 };
+ for ([_]Row{
+ .{ &.{"--fs"}, "no such option" },
+ .{ &.{"--fs9"}, "no such option" },
+ .{ &.{ "--", "a", "b" }, "is the second" },
+ .{ &.{"--9p"}, "--9p needs a socket name" },
+ .{ &.{"--9p="}, "invalid 9P socket name" },
+ .{ &.{"--9p=a/b"}, "invalid 9P socket name" },
+ .{ &.{ "--attach=work", "--9p=work" }, "--attach has none" },
+ .{ &.{"--9p-tcp"}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp="}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp=tcp!localhost!5640"}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp=quic!127.0.0.1!5640"}, "--9p-tcp needs" },
+ .{ &.{ "--9p-tcp=tcp!127.0.0.1!5640", "--9p-tcp=tcp!127.0.0.1!5641" }, "specified twice" },
+ .{ &.{ "--attach=work", "--9p-tcp=tcp!127.0.0.1!5640" }, "--attach has none" },
+ .{ &.{"--9p-quic"}, "--9p-quic needs" },
+ .{ &.{"--9p-quic="}, "--9p-quic needs" },
+ .{ &.{"--9p-quic=tcp!127.0.0.1!5640"}, "--9p-quic needs" },
+ .{ &.{"--mount=os=work"}, "invalid mount name" },
+ .{ &.{"--mount=peer="}, "invalid mount name or dial" },
+ .{ &.{ "--mount=peer=one", "--mount=peer=two" }, "duplicate mount" },
+ .{ &.{"--mount=peer=tcp!127.0.0.1!0"}, "invalid mount dial" },
+ .{ &.{ "--attach=work", "--mount=peer=one" }, "--attach has none" },
+ }) |row| {
+ var argv: [4][:0]const u8 = undefined;
+ argv[0] = exe;
+ for (row[0], 1..) |arg, i| argv[i] = arg;
+ const r = try process.run(a, argv[0 .. 1 + row[0].len], .{});
+ try check(r.code == 1 and has(r.said, row[1]), "{any}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ }
+}
+
+fn explicitName(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "renamed", .{ .args = &.{"--9p=explicit"}, .socket = "explicit" });
+ defer d.deinit();
+ try std.testing.expect(d.exists("pardes-9p-explicit.sock"));
+ try std.testing.expect(!d.exists("pardes-9p-renamed.sock"));
+ try d.files.expect("/pane/1/body", "initial\n");
+}
+
+fn forwarding(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "forwarding", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const address = d.abs("pardes-9p-forwarding.sock");
+ _ = libc.mkdir(d.abs("child-working-directory"), 0o755);
+ const child_dir = d.abs("child-working-directory");
+ const spaced_text = "first line\nsecond line\n";
+ try d.put("child-working-directory/space name.txt", spaced_text);
+ const spaced = d.abs("child-working-directory/space name.txt");
+ // PARDES_PID says "you are inside a pardes"; the socket and the pane
+ // only say how to reach it.
+ const pid = try statusPid(a, c);
+ const inherited = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", pid } };
+ const opts: Child.Options = .{ .root = d.root(), .cwd = child_dir, .env = &inherited };
+ var opened: u32 = 0;
+ const Word = struct { [:0]const u8, []const u8, ?[2]u32, bool };
+ for ([_]Word{
+ .{ "space name.txt:2:4", spaced_text, .{ 14, 14 }, false },
+ .{ "/n/self/pane/1/body", "initial\n", null, false },
+ .{ "/virtual/pane/1/body:1:2-4", "initial\n", .{ 1, 4 }, true },
+ }) |row| {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "--tty", row[0] }, opts);
+ try check(r.code == 0, "{s}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ if (row[3]) {
+ try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]});
+ } else opened = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(row[1], try c.pane(a, opened, "body"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ // The selection is dot; reading it takes no command at all.
+ if (row[2]) |sel| {
+ var it = std.mem.tokenizeScalar(u8, try c.pane(a, opened, "dot"), ' ');
+ const got = [2]u32{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, std.mem.trim(u8, it.next().?, " \n"), 10) };
+ try check(std.mem.eql(u32, &got, &sel), "{s}: dot {any}, not {any}", .{ row[0], got, sel });
+ }
+ }
+ // A launch with no file inside a session is refused, and never clears
+ // the address limit its parent's pane holds.
+ const addr = try c.hold("/pane/1/addr", 2);
+ const limit = try c.hold("/pane/1/limit", 2);
+ _ = try c.writeFid(addr, 0, "#0,#3");
+ _ = try c.writeFid(limit, 0, "#0,#3");
+ _ = try c.writeFid(addr, 0, "#0");
+ for (0..8) |_| {
+ const r = try process.run(a, &.{exe}, opts);
+ try check(r.code == 1 and has(r.said, "already inside pardes"), "no-argument child: exit {d}: {s}", .{ r.code, r.said });
+ if (c.writeFid(addr, 0, "/tial/")) |_| {
+ return error.LimitCleared;
+ } else |_| {}
+ }
+ c.clunk(addr);
+ c.clunk(limit);
+ _ = try c.read(a, "/pane/1/ctl");
+
+ const control = try c.newPane(a);
+ try c.exec(control, "Dump\n");
+ const deadline = process.nowMs() + 3_000;
+ while (!d.exists("forwarding.dump.zon")) {
+ if (process.nowMs() > deadline) return error.NoDump;
+ process.nap(5);
+ }
+ const saved = d.abs("forwarding.dump.zon");
+ const before = try c.read(a, "/index");
+ // Flags of a session's own start one, never forwarded.
+ const Launch = struct { []const u8, []const [:0]const u8, ?[]const u8 };
+ for ([_]Launch{
+ .{ "mount", &.{ "--tty", z(a, "--mount=peer={s}", .{address}), "/n/peer/pane/1/body" }, null },
+ .{ "name", &.{ "--tty", "--9p=forwarded-name" }, "forwarded-name" },
+ .{ "tcp", &.{ "--tty", "--9p-tcp=tcp!127.0.0.1!0" }, null },
+ .{ "load", &.{ "--tty", "-l", saved }, null },
+ .{ "shells", &.{ "-n", "3" }, null },
+ .{ "toggle", &.{ "--tty", "--tty-toggle=q" }, null },
+ .{ "toggle-arg", &.{ "--tty", "--tty-toggle", "q" }, null },
+ .{ "quic", &.{ "--tty", "--9p-quic=quic!127.0.0.1!0" }, null },
+ }) |row| {
+ if (std.mem.eql(u8, row[0], "quic") and !try process.quicIncluded(a, exe)) continue;
+ var argv: [8][:0]const u8 = undefined;
+ argv[0] = exe;
+ for (row[1], 1..) |arg, i| argv[i] = arg;
+ const existing = try process.entries(a, d.root());
+ var child = try Child.spawn(argv[0 .. 1 + row[1].len], .{ .root = d.root(), .env = &inherited, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "forward-{s}.pty", .{row[0]})) } });
+ defer child.deinit();
+ const sock = if (row[2]) |name| blk: {
+ const p = try std.fmt.allocPrint(a, "{s}/pardes-9p-{s}.sock", .{ d.root(), name });
+ try child.waitPath(p, 10_000);
+ break :blk p;
+ } else try newSocket(a, d.root(), existing, &child);
+ try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]});
+ var local: process.Files = .init(gpa, sock);
+ defer local.deinit();
+ const index = try local.read("/index");
+ const rows = std.mem.count(u8, index, "\n");
+ if (std.mem.eql(u8, row[0], "mount")) {
+ try check(rows == 1 and has(index, "/n/peer/"), "mount: {s}", .{index});
+ try local.expect("/pane/1/body", "initial\n");
+ } else if (std.mem.eql(u8, row[0], "load")) {
+ try std.testing.expectEqualStrings(before, index);
+ try local.expect("/pane/1/body", "initial\n");
+ } else {
+ // A bare tty launch is a shell plus the empty text pane the
+ // boot puts under it; --shells=3 is three shells, no scratch.
+ try check(rows == @as(usize, if (std.mem.eql(u8, row[0], "shells")) 3 else 2), "{s}: {s}", .{ row[0], index });
+ }
+ if (std.mem.eql(u8, row[0], "tcp") or std.mem.eql(u8, row[0], "quic"))
+ try check(has(try local.read("/listeners"), try std.fmt.allocPrint(a, "{s}!127.0.0.1!", .{row[0]})), "{s}: no listener", .{row[0]});
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ }
+
+ // A session that answers takes the file or says why, as acme's B does:
+ // a pane it has not is its refusal and exit 1, never a nested editor.
+ const gone = try process.run(a, &.{ exe, "--tty", spaced }, .{ .root = d.root(), .env = &.{ inherited[0], .{ "PARDES_PANE", "4294967295" }, inherited[2] } });
+ try check(gone.code == 1 and std.mem.startsWith(u8, gone.said, "pardes: "), "gone pane: exit {d}: {s}", .{ gone.code, gone.said });
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ const absent = d.abs("absent.sock");
+ const stale = [_][2][:0]const u8{ .{ "PARDES_9P", absent }, inherited[1], inherited[2] };
+ const Stale = struct { []const u8, []const [2][:0]const u8, ?[:0]const u8 };
+ for ([_]Stale{
+ .{ "stale-file", &stale, spaced },
+ .{ "stale-missing", &stale, "missing-child-file.txt" },
+ .{ "stale-noarg", &stale, null },
+ // No pid is "not inside a pardes at all", as a plain shell has.
+ .{ "not-nested", inherited[0..2], spaced },
+ }) |row| {
+ const existing = try process.entries(a, d.root());
+ var argv: [3][:0]const u8 = .{ exe, "--tty", undefined };
+ if (row[2]) |w| argv[2] = w;
+ var child = try Child.spawn(argv[0..if (row[2] != null) 3 else 2], .{ .root = d.root(), .env = row[1], .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{row[0]})) } });
+ defer child.deinit();
+ const sock = try newSocket(a, d.root(), existing, &child);
+ try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]});
+ var local: process.Files = .init(gpa, sock);
+ defer local.deinit();
+ if (row[2]) |w| {
+ if (std.mem.eql(u8, w, spaced)) {
+ try local.expect("/pane/1/body", spaced_text);
+ } else try check(has(try local.read("/index"), w) or has(try local.screen(), w), "{s}: {s} not shown", .{ row[0], w });
+ }
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ }
+}
+
+/// `pardes <flag> <file>` started, back once it has made a pane or exited.
+fn launch(a: std.mem.Allocator, c: *Conn, argv: []const [:0]const u8, opts: Child.Options) !Child {
+ const before = try c.serials(a);
+ var child = try Child.spawn(argv, opts);
+ const deadline = process.nowMs() + 3_000;
+ while (child.wait(0) == null and same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10);
+ return child;
+}
+
+fn running(a: std.mem.Allocator, child: *Child) !void {
+ if (child.wait(0)) |status| {
+ std.debug.print("exited {d}: {s}\n", .{ status, child.said(a) catch "" });
+ return error.Exited;
+ }
+}
+
+fn exitsWith(child: *Child, want: c_int, ms: i64) !void {
+ const status = child.wait(ms) orelse return error.StillRunning;
+ try std.testing.expectEqual(want, process.code(status));
+}
+
+fn waitAndPager(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "wait", .{});
+ var ended = false;
+ defer if (!ended) d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ // A second --detach of a name a live session holds: said, exit 1, no
+ // Zig error trace.
+ const second_detach = try process.run(a, &.{ exe, "--detach=wait" }, .{ .root = root });
+ try std.testing.expectEqual(@as(c_int, 1), second_detach.code);
+ try std.testing.expectEqualStrings("pardes: a session named wait is already running\n", second_detach.said);
+ const address = d.abs("pardes-9p-wait.sock");
+ const env = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", try statusPid(a, c) } };
+ const opts: Child.Options = .{ .root = root, .env = &env };
+ try d.put("wait edit.txt", "edit me\n");
+ const edited = d.abs("wait edit.txt");
+
+ // --wait (acme's E): a launch inside a pane lasts until the pane it
+ // opened is deleted; one on a file already open waits on that pane.
+ var first = try launch(a, c, &.{ exe, "--wait", edited }, opts);
+ defer first.deinit();
+ const opened = blk: {
+ const now = try c.serials(a);
+ try check(now.len == 2 and now[0] == 1, "--wait: {any}", .{now});
+ break :blk now[1];
+ };
+ var second = try launch(a, c, &.{ exe, "-w", edited }, opts);
+ defer second.deinit();
+ process.nap(500);
+ try running(a, &first);
+ try running(a, &second);
+ try check(same(try c.serials(a), &.{ 1, opened }), "-w made a pane", .{});
+ try c.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "edited\n");
+ try c.exec(opened, "Save\n");
+ try c.removePane(opened);
+ const deleted = process.nowMs();
+ try exitsWith(&first, 0, 5_000);
+ // Woken by the `del` record, not a poll.
+ try check(process.nowMs() - deleted < 50, "--wait woke {d} ms after the del", .{process.nowMs() - deleted});
+ try exitsWith(&second, 0, 5_000);
+ try std.testing.expectEqualStrings("edited\n", try d.get(a, "wait edit.txt"));
+
+ // --wait from outside the session's panes, $PARDES_9P alone (an
+ // agent's EDITOR): asked of the session, not a screen of its own.
+ const outside_env = env[0..1];
+ try d.put("wait far.txt", "far\n");
+ const far = d.abs("wait far.txt");
+ {
+ const before = try c.serials(a);
+ var waiting = try Child.spawn(&.{ exe, "--wait", far }, .{ .root = root, .env = outside_env });
+ defer waiting.deinit();
+ const deadline = process.nowMs() + 5_000;
+ while (same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10);
+ const far_pane = try madeOne(a, c, before);
+ process.nap(300);
+ try running(a, &waiting);
+ try c.removePane(far_pane);
+ try exitsWith(&waiting, 0, 5_000);
+ }
+ // --wait with a $PARDES_9P that answers no more: said, exit 1.
+ const dead = try process.run(a, &.{ exe, "--wait", far }, .{ .root = root, .env = &.{.{ "PARDES_9P", d.abs("pardes-9p-gone.sock") }} });
+ try check(dead.code == 1 and has(dead.said, "does not answer"), "dead: exit {d}: {s}", .{ dead.code, dead.said });
+ // --wait with no FILE says it needs one, and waits on nothing.
+ const bare = try process.run(a, &.{ exe, "--wait" }, opts);
+ try check(bare.code == 1 and has(bare.said, "--wait needs a FILE"), "bare: exit {d}: {s}", .{ bare.code, bare.said });
+ // `--` ends the options: a name starting with a dash is a name.
+ try d.put("-dash.txt", "dash\n");
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "--", "-dash.txt" }, opts);
+ try check(r.code == 0, "--: exit {d}: {s}", .{ r.code, r.said });
+ const dashed = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(d.abs("-dash.txt"), std.mem.trimEnd(u8, try c.pane(a, dashed, "name"), "\n"));
+ try c.removePane(dashed);
+ }
+ // A name under a directory that may not be searched is refused,
+ // permission denied, exit 1, no pane. (Root searches any directory.)
+ if (libc.geteuid() != 0) {
+ _ = libc.mkdir(d.abs("noperm"), 0);
+ const before = try c.serials(a);
+ const denied = process.run(a, &.{ exe, "noperm/a/b/c" }, opts);
+ _ = libc.chmod(d.abs("noperm"), 0o755);
+ const r = try denied;
+ try check(r.code == 1 and has(r.said, "permission denied"), "noperm: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "noperm made a pane", .{});
+ }
+ // --wait waits on the pane its look answered: a name /index shows
+ // escaped (a backslash is `\\` there) is still the pane waited on.
+ try d.put("wait\\odd.txt", "odd\n");
+ {
+ const before = try c.serials(a);
+ var waiting = try launch(a, c, &.{ exe, "--wait", d.abs("wait\\odd.txt") }, opts);
+ defer waiting.deinit();
+ const odd_pane = try madeOne(a, c, before);
+ process.nap(500);
+ try running(a, &waiting);
+ try c.removePane(odd_pane);
+ try exitsWith(&waiting, 0, 5_000);
+ }
+ // A name not there yet opens a pane named it, as acme's B does, and
+ // Save creates it: git's and fish's temporary files are.
+ {
+ const before = try c.serials(a);
+ var made = try Child.spawn(&.{ exe, "--wait", "wait new.txt" }, opts);
+ defer made.deinit();
+ // The launch makes the pane, then names it: wait for the name.
+ const deadline = process.nowMs() + 3_000;
+ while (!has(try c.read(a, "/index"), d.abs("wait new.txt"))) {
+ try running(a, &made);
+ if (process.nowMs() > deadline) return error.NoPaneNamed;
+ process.nap(10);
+ }
+ const named = try madeOne(a, c, before);
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "new\n");
+ try c.exec(named, "Save\n");
+ try c.removePane(named);
+ try exitsWith(&made, 0, 5_000);
+ try std.testing.expectEqualStrings("new\n", try d.get(a, "wait new.txt"));
+ }
+ // ...in a directory not there either: forwarded all the same, and Save
+ // makes the directories.
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "wait-missing/sub/new.txt" }, opts);
+ try check(r.code == 0, "deep: exit {d}: {s}", .{ r.code, r.said });
+ const deadline = process.nowMs() + 3_000;
+ while (!has(try c.read(a, "/index"), d.abs("wait-missing/sub/new.txt"))) {
+ if (process.nowMs() > deadline) return error.NoPaneNamed;
+ process.nap(10);
+ }
+ const named = try madeOne(a, c, before);
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "deep\n");
+ try c.exec(named, "Save\n");
+ try c.removePane(named);
+ try std.testing.expectEqualStrings("deep\n", try d.get(a, "wait-missing/sub/new.txt"));
+ }
+ // `pardes '~/x'`, quoted so the shell left the `~`: the launching
+ // shell's HOME all the same.
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "~/fwd-tilde.txt" }, opts);
+ try check(r.code == 0, "tilde: exit {d}: {s}", .{ r.code, r.said });
+ try check(has(try c.read(a, "/index"), d.abs("fwd-tilde.txt")), "no ~ pane", .{});
+ for (added(a, before, try c.serials(a))) |n| try c.removePane(n);
+ }
+ // A name the session refuses: its reason and exit 1, no pane left.
+ const before = try c.serials(a);
+ {
+ const r = try process.run(a, &.{ exe, long_name }, opts);
+ try check(r.code == 1 and has(r.said, "a component over 255 bytes"), "long: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "a long name made a pane", .{});
+ }
+ // `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a
+ // +Pager pane of the session, clean, and back at once.
+ const pager_name = try std.fmt.allocPrint(a, "{s}/+Pager\n", .{root});
+ const made_now = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b[1mbold\x1b[0m text\n" });
+ try check(r.code == 0, "pager: exit {d}: {s}", .{ r.code, r.said });
+ break :blk try madeOne(a, c, before);
+ };
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, made_now, "name"));
+ try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body"));
+ {
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ const row = while (rows.next()) |r| {
+ if (std.mem.startsWith(u8, r, try std.fmt.allocPrint(a, "{d} ", .{made_now}))) break r;
+ } else return error.NotListed;
+ var words = std.mem.tokenizeScalar(u8, row, ' ');
+ _ = words.next();
+ _ = words.next();
+ try check(std.mem.eql(u8, words.next().?, "0"), "a +Pager is dirty: {s}", .{row}); // clean: nothing to ask at Exit
+ // Its `new` record names it as it is: no +New and a rename.
+ const log = try std.fmt.allocPrint(a, "\n{s}", .{try c.read(a, "/log")});
+ try check(has(log, try std.fmt.allocPrint(a, "\nnew {d} {s}/+Pager\n", .{ made_now, root })), "no new record: {s}", .{log});
+ try check(!has(log, try std.fmt.allocPrint(a, "\nrename {d} ", .{made_now})), "a +Pager renamed", .{});
+ }
+ // Its colours are the pane's, drawn and never read: mapped through the
+ // theme, as a terminal's are; Filter shows them as sent. Each run is its
+ // own +Pager; one there already keeps its page.
+ const coloured = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n" });
+ try check(r.code == 0, "colours: exit {d}: {s}", .{ r.code, r.said });
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 2 and new[0] == made_now, "colours: {any}", .{new});
+ break :blk new[1];
+ };
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, coloured, "name"));
+ try std.testing.expectEqualStrings("red rgb plain\n", try c.pane(a, coloured, "body"));
+ try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body"));
+ const rgb = "{\"rgb\":[1,2,3]}";
+ {
+ const ink = try inks(&d.files);
+ try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[1], rgb), "drawn inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] });
+ }
+ try c.exec(coloured, "Filter\n");
+ {
+ const ink = try inks(&d.files);
+ try std.testing.expectEqualStrings(rgb, ink[1]);
+ try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[0], ink[1]), "filtered inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] });
+ }
+ // A progress line keeps its last state.
+ const again = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "again\r\nprogress 10%\rprogress 100%\n" });
+ try check(r.code == 0, "progress: exit {d}: {s}", .{ r.code, r.said });
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 3, "progress: {any}", .{new});
+ break :blk new[2];
+ };
+ try std.testing.expectEqualStrings("again\nprogress 100%\n", try c.pane(a, again, "body"));
+ for ([_]u32{ made_now, coloured, again }) |n| try c.removePane(n);
+ // More than one 8 MiB write's worth arrives whole, byte for byte.
+ {
+ var big: std.ArrayList(u8) = .empty;
+ defer big.deinit(gpa);
+ for (0..400_000) |i| try big.print(gpa, "line {d:0>8} of the paged text\n", .{i}); // ~11 MiB
+ try std.testing.expect(big.items.len > 8 * 1024 * 1024);
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = big.items });
+ try check(r.code == 0, "big: exit {d}: {s}", .{ r.code, r.said });
+ const big_pane = try madeOne(a, c, before);
+ const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane}));
+ defer gpa.free(got);
+ try check(std.mem.eql(u8, got, big.items), "the paged body is not the text", .{});
+ // Past the 256 MiB cap: the first 256 MiB, then the cut note.
+ const cap = 256 * 1024 * 1024;
+ var exe_buf: [4096]u8 = undefined;
+ const huge = try process.run(a, &.{ "sh", "-c", z(a, "yes 0123456789abcdef | head -c {d} | \"$0\" -", .{cap + 4096}), std.mem.span(realpath(exe, &exe_buf) orelse return error.NoBinary) }, .{ .root = root, .env = &env });
+ try check(huge.code == 0, "huge: exit {d}: {s}", .{ huge.code, huge.said });
+ try c.removePane(big_pane);
+ }
+ {
+ const cap = 256 * 1024 * 1024;
+ const big_pane = try madeOne(a, c, before);
+ const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane}));
+ defer gpa.free(got);
+ const note = "\n[pardes: the rest of stdin is cut: a pane holds at most a file's limit]\n";
+ try check(std.mem.endsWith(u8, got, note) and got.len == cap + note.len, "huge: {d} bytes", .{got.len});
+ const line = "0123456789abcdef\n";
+ var i: usize = 0;
+ while (i < cap) : (i += line.len) {
+ const n = @min(line.len, cap - i);
+ if (!std.mem.eql(u8, got[i..][0..n], line[0..n])) return error.NotTheText;
+ }
+ try c.removePane(big_pane);
+ }
+ // From a directory whose name could not be a ctl line: the session's
+ // directory's +Pager, the text whole, no stray +New.
+ _ = libc.mkdir(d.abs("two\nlines"), 0o755);
+ {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .cwd = d.abs("two\nlines"), .env = &env, .stdin = "odd dir text\n" });
+ try check(r.code == 0, "odd dir: exit {d}: {s}", .{ r.code, r.said });
+ const odd_pane = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, odd_pane, "name"));
+ try std.testing.expectEqualStrings("odd dir text\n", try c.pane(a, odd_pane, "body"));
+ try c.removePane(odd_pane);
+ }
+ // Nothing on stdin: no pane, and done.
+ {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "" });
+ try check(r.code == 0 and same(before, try c.serials(a)), "empty stdin: exit {d}: {s}", .{ r.code, r.said });
+ }
+ // A new name in a directory there but not writable: refused now, not a
+ // pane that only fails at Save. (Root writes anywhere.)
+ _ = libc.mkdir(d.abs("shut-dir"), 0o500);
+ if (libc.geteuid() != 0) {
+ const r = try process.run(a, &.{ exe, "shut-dir/new.txt" }, opts);
+ try check(r.code == 1 and has(r.said, "may not be read or written"), "shut-dir: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "shut-dir made a pane", .{});
+ }
+ _ = libc.chmod(d.abs("shut-dir"), 0o755);
+ // A pane address the session has not is refused in words, exit 1, no
+ // pane made for a file of that name; one it has is looked at. A name
+ // with a newline is said in words, not an error name.
+ for ([_][2][:0]const u8{
+ .{ "@p999:1", "pardes: @p999:1: this session has no such pane" },
+ .{ "two\nlines.txt", "pardes: two\\nlines.txt: a file name is one line" },
+ }) |row| {
+ const r = try process.run(a, &.{ exe, row[0] }, opts);
+ try check(r.code == 1 and has(r.said, row[1]), "{s}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ try check(!has(r.said, "NotOneLine") and !has(r.said, "NoSuchPane"), "an error name: {s}", .{r.said});
+ try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]});
+ }
+ const looked = try process.run(a, &.{ exe, "@p1:1" }, opts);
+ try check(looked.code == 0, "@p1:1: exit {d}: {s}", .{ looked.code, looked.said });
+ try check(same(before, try c.serials(a)), "@p1:1 made a pane", .{});
+ // A session that goes away ends a --wait on it with 1.
+ var orphan = try launch(a, c, &.{ exe, "--wait", edited }, opts);
+ defer orphan.deinit();
+ try running(a, &orphan);
+ d.deinit();
+ ended = true;
+ try exitsWith(&orphan, 1, 5_000);
+}
+
+/// The inks of `red rgb plain`'s three words, as /screen draws them.
+fn inks(f: *process.Files) ![3][]const u8 {
+ const col, const row = try f.where("red rgb plain");
+ return .{ try f.fg(col, row), try f.fg(col + 4, row), try f.fg(col + 8, row) };
+}
+
+fn winid(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "winid", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const first = std.mem.min(u32, try c.serials(a));
+ try check(has(try commandBody(a, c, try std.fmt.allocPrint(a, "/pane/{d}/tagexec", .{first})), try std.fmt.allocPrint(a, "winid={d}\n", .{first})), "a pane's tag: no $winid", .{});
+ try check(has(try commandBody(a, c, "/tagexec"), "winid=\n"), "the workspace's tag: a $winid", .{});
+}
+
+/// What `echo winid=$winid` written to `node` printed, its pane removed.
+fn commandBody(a: std.mem.Allocator, c: *Conn, node: []const u8) ![]const u8 {
+ try c.write(node, "echo winid=$winid\n");
+ var words = std.mem.tokenizeAny(u8, try c.read(a, node), " \n");
+ var last: []const u8 = "";
+ while (words.next()) |w| last = w;
+ const serial = try std.fmt.parseInt(u32, last, 10);
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ const log = try c.read(a, "/log");
+ if (std.mem.lastIndexOf(u8, log, try std.fmt.allocPrint(a, "\nrun {d} ", .{serial}))) |ran| {
+ if (has(log[ran..], try std.fmt.allocPrint(a, "\nexit {d} ", .{serial}))) {
+ const body = try c.pane(a, serial, "body");
+ var parts = std.mem.splitSequence(u8, body, "% echo winid");
+ var tail: []const u8 = "";
+ while (parts.next()) |p| tail = p;
+ try c.removePane(serial);
+ return tail;
+ }
+ }
+ if (process.nowMs() > deadline) return error.NeverExited;
+ process.nap(50);
+ }
+}
+
+fn outside(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // A session for its root alone.
+ var d = try process.Detached.start(gpa, exe, "outside", .{});
+ defer d.deinit();
+ // `pardes -` outside a session: an editor whose first pane is a +Pager
+ // of stdin, as `vim -`; here a detached one, to read it.
+ {
+ var boot = try Child.spawn(&.{ exe, "--detach=stdinboot", "-" }, .{ .root = d.root(), .stdin = "\x1b[32mfrom stdin\x1b[m\nsecond\n" });
+ defer boot.deinit();
+ const sock = d.abs("pardes-9p-stdinboot.sock");
+ try boot.waitPath(sock, 10_000);
+ var booted: process.Files = .init(gpa, sock);
+ defer booted.deinit();
+ var first = std.mem.tokenizeScalar(u8, std.mem.sliceTo(try booted.read("/index"), '\n'), ' ');
+ const serial = first.next().?;
+ _ = first.next();
+ _ = first.next();
+ try std.testing.expectEqualStrings(d.abs("+Pager"), first.next().?);
+ try booted.expect(booted.at("/pane/{s}/body", .{serial}), "from stdin\nsecond\n");
+ }
+ // A launch with no terminal to draw on (a pty that is no one's
+ // controlling terminal) says so and exits 1, no error trace.
+ var tty = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .pty = .{ .log = d.abs("noctty.pty"), .ctty = false } });
+ defer tty.deinit();
+ const words = try tty.said(a);
+ try exitsWith(&tty, 1, 10_000);
+ try check(has(words, "pardes: no terminal to draw on") and !has(words, ".zig:"), "no terminal: {s}", .{words});
+}
+
+/// A unix socket's file at `path`, bound and closed, as a dead editor
+/// leaves one.
+fn socketFile(path: []const u8) !void {
+ const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0);
+ if (fd < 0) return error.NoSocket;
+ defer _ = libc.close(fd);
+ var addr: libc.sockaddr.un = .{ .path = undefined };
+ @memset(&addr.path, 0);
+ @memcpy(addr.path[0..path.len], path);
+ if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.NoBind;
+}
+
+fn killedTty(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "killed", .{});
+ defer d.deinit();
+ // Killed with SIGTERM or SIGHUP, a tty editor puts its terminal back
+ // (main screen, cooked mode) before it dies, and its 9P socket goes
+ // with it. One a dead editor left (no process has its pid) is swept at
+ // the next start; a live pid's, here this test's own, never is.
+ for ([_]libc.SIG{ .TERM, .HUP }) |sig| {
+ const dead = "pardes-9p-2147483647.sock";
+ const live = try std.fmt.allocPrint(a, "pardes-9p-{d}.sock", .{libc.getpid()});
+ for ([_][]const u8{ dead, live }) |name| if (!d.exists(name)) try socketFile(d.abs(name));
+ const existing = try process.entries(a, d.root());
+ const log = d.abs(try std.fmt.allocPrint(a, "killed-{t}.pty", .{sig}));
+ // The daemon's, as a user's editor has: a hangup leaves the session.
+ var killed = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .env = &.{.{ "PARDES_DAEMON", "on" }}, .pty = .{ .log = log } });
+ defer killed.deinit();
+ const deadline = process.nowMs() + 10_000;
+ while (!has(d.get(a, std.fs.path.basename(log)) catch "", "\x1b[?1049h")) {
+ if (process.nowMs() > deadline) return error.NeverDrew;
+ process.nap(20);
+ }
+ process.nap(300);
+ // The session's own, named by its pid: forked off the launched
+ // process, which stays as its stub.
+ const own = try newSocket(a, d.root(), existing, &killed);
+ try check(!d.exists(dead), "a dead editor's socket kept", .{});
+ try check(d.exists(live), "a live pid's socket swept", .{});
+ killed.kill(sig);
+ try exitsWith(&killed, -@as(c_int, @intCast(@intFromEnum(sig))), 5_000);
+ const own_z = z(a, "{s}", .{own});
+ if (sig == .HUP) {
+ // A hangup is the terminal's end: the session carries on
+ // detached (the daemon keeps it) until told to end.
+ try check(libc.access(own_z, 0) == 0, "a hangup ended the session", .{});
+ const name = std.fs.path.basename(own);
+ _ = libc.kill(try std.fmt.parseInt(libc.pid_t, name["pardes-9p-".len .. name.len - ".sock".len], 10), .TERM);
+ }
+ const gone_by = process.nowMs() + 5_000;
+ while (libc.access(own_z, 0) == 0 and process.nowMs() < gone_by) process.nap(10);
+ try check(libc.access(own_z, 0) != 0, "{s} left behind", .{own});
+ process.nap(200);
+ const seen = try d.get(a, std.fs.path.basename(log));
+ const tail = seen[std.mem.lastIndexOf(u8, seen, "\x1b[?1049h").?..];
+ try check(has(tail, "\x1b[?1049l"), "the main screen not put back", .{});
+ try check(try process.canonical(killed.slave), "the terminal left raw", .{});
+ daemonGone(&d);
+ }
+}
+
+/// The daemon a session had (PARDES_DAEMON=on) outlives it a little and,
+/// going, makes its runtime directory (the root) again: waited for.
+fn daemonGone(d: *const process.Detached) void {
+ const deadline = process.nowMs() + 5_000;
+ while (d.exists("pardes-daemon.sock") and process.nowMs() < deadline) process.nap(20);
+ process.nap(100);
+}
+
+fn signalCleansUp(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ // 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| {
+ var d = try process.Detached.start(gpa, exe, "sig", .{ .env = &.{.{ "PARDES_DAEMON", "on" }} });
+ defer d.deinit();
+ try std.testing.expect(d.exists("pardes-9p-sig.sock"));
+ var sig = first;
+ if (sig == .HUP) {
+ _ = libc.kill(d.pid, sig);
+ process.nap(300);
+ try check(d.exists("pardes-9p-sig.sock") and (try d.files.read("/index")).len > 0, "a hangup ended a kept session", .{});
+ sig = .TERM;
+ }
+ _ = libc.kill(d.pid, sig);
+ const deadline = process.nowMs() + 5_000;
+ while (d.exists("pardes-9p-sig.sock") and process.nowMs() < deadline) process.nap(10);
+ try check(!d.exists("pardes-9p-sig.sock"), "{t}: the socket left", .{first});
+ daemonGone(&d);
+ }
+}