summaryrefslogtreecommitdiff
path: root/test/process/root_ctl.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/root_ctl.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/root_ctl.zig')
-rw-r--r--test/process/root_ctl.zig87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/process/root_ctl.zig b/test/process/root_ctl.zig
new file mode 100644
index 00000000..87c9e777
--- /dev/null
+++ b/test/process/root_ctl.zig
@@ -0,0 +1,87 @@
+//! docs/typ/reference.typ, The root ctl <root-ctl>: Dump and Restore in a
+//! real session, detached and in a pty.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "root-ctl: Restore asks once about unsaved text, is answered before it cuts every connection, and the dump's panes come back under their serials", restore },
+};
+
+/// A connection to `path`, waiting up to 5 s for it to answer.
+fn redial(gpa: std.mem.Allocator, path: []const u8) !*Conn {
+ const deadline = process.nowMs() + 5_000;
+ while (true) {
+ if (Conn.dial(gpa, path)) |c| return c else |err| if (process.nowMs() > deadline) return err;
+ process.nap(20);
+ }
+}
+
+fn restore(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ for ([_]bool{ false, true }) |tty| {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const name = if (tty) "restore tty space" else "restore detached space";
+ var d = try process.Detached.start(gpa, exe, if (tty) "restore-root" else name, .{});
+ defer d.deinit();
+ var term: ?process.Child = null;
+ defer if (term) |*t| t.deinit();
+ if (tty) term = try d.startTty(exe, name, &.{});
+ const address = d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name}));
+ const old = try Conn.dial(gpa, address);
+ defer old.deinit(gpa);
+ try old.replace("/pane/1/body", "dumped state\n");
+ const control = try old.newPane(a);
+ try old.execRefused(control, try std.fmt.allocPrint(a, "Restore {s}\n", .{d.abs("missing dump.zon")}), "no such file");
+ try std.testing.expectEqualStrings("dumped state\n", try old.read(a, "/pane/1/body"));
+ try old.exec(control, try std.fmt.allocPrint(a, "Mount own {s}\n", .{address}));
+ try old.exec(control, "Dump\n");
+ const saved_rel = try std.fmt.allocPrint(a, "{s}.dump.zon", .{name});
+ const saved = d.abs(saved_rel);
+ const deadline = process.nowMs() + 5_000;
+ while (!d.exists(saved_rel)) {
+ if (process.nowMs() > deadline) return error.NoDump;
+ process.nap(5);
+ }
+ const held_body = try old.hold("/pane/1/body", 0);
+ const held_screen = try old.hold("/screen", 0);
+ const old_ids = try old.serials(a);
+ try std.testing.expectEqualStrings("dumped state\n", try old.readFid(held_body, 0, 65536));
+ try std.testing.expectEqualStrings("{", try old.readFid(held_screen, 0, 1));
+ try old.replace("/pane/1/body", "changed after dump\n");
+ // Unsaved text is asked about once, as Exit asks; the second Restore
+ // goes ahead, and is answered before the hang-up.
+ const line = try std.fmt.allocPrint(a, "Restore {s}\n", .{saved});
+ if (old.exec(control, line)) |_| {} else |err| {
+ if (err != error.Refused) return err;
+ try check(has(old.said, "Modified"), "Restore refused: {s}", .{old.said});
+ }
+ try std.testing.expectEqualStrings("changed after dump\n", try old.read(a, "/pane/1/body"));
+ // Answered before the hang-up: the write's reply comes; the clunk
+ // after it may meet the connection already cut.
+ const restore_fid = try old.hold(try std.fmt.allocPrint(a, "/pane/{d}/exec", .{control}), 1);
+ _ = try old.writeFid(restore_fid, 0, line);
+ old.clunk(restore_fid);
+ // Refused while the connections are cut, or cut already.
+ if (old.readFid(held_body, 0, 65536)) |_| return error.OldFidUsable else |_| {}
+ const restored = try redial(gpa, address);
+ defer restored.deinit(gpa);
+ // The new log names its panes, then where they came from.
+ const log = try restored.read(a, "/log");
+ try check(has(log, try std.fmt.allocPrint(a, "\nrestore {s}\n", .{saved})), "no restore logged: {s}", .{log});
+ // Its panes are the dump's, their serials too.
+ const ids = try restored.serials(a);
+ try check(ids.len == 2 and std.mem.eql(u32, ids, old_ids), "restored {any}, dumped {any}", .{ ids, old_ids });
+ try std.testing.expectEqualStrings("dumped state\n", try restored.pane(a, ids[0], "body"));
+ try check((try restored.pane(a, ids[1], "body")).len == 0, "commands ran through exec, not the body", .{});
+ 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
+ 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"));
+ }
+}