diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /test/snapshot.zig | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'test/snapshot.zig')
| -rw-r--r-- | test/snapshot.zig | 163 |
1 files changed, 144 insertions, 19 deletions
diff --git a/test/snapshot.zig b/test/snapshot.zig index 2d8a8664..1bd4dabf 100644 --- a/test/snapshot.zig +++ b/test/snapshot.zig @@ -29,7 +29,11 @@ // lines <name> <n> [tail] create file with n numbered lines, tail on each // dirmk <name> create a subdirectory // run <shell...> run a command in the script's cwd (NOT in a pane) -// start <rows> <cols> [arg] fork the app in a pty (optional extra CLI arg) +// start <rows> <cols> [views=N] [arg] fork the app in a pty (optional +// extra CLI arg); views=N gives it N-1 more +// terminals, each a view of the session +// view <k> type into and capture terminal k (1 is the +// app's own; a resize there SIGWINCHes the app) // wait <ms> <needle...> pump until needle appears on the grid (fails hard) // settle <ms> pump for a fixed duration // stable <quiet_ms> <timeout_ms> pump until the grid stops changing @@ -74,6 +78,7 @@ const Client = @import("9p_io").Client; pub const std_options: std.Options = .{ .log_level = .err }; extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; +extern "c" fn unsetenv(name: [*:0]const u8) c_int; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn system(cmd: [*:0]const u8) c_int; extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; @@ -86,6 +91,17 @@ var careful = false; /// (--lspmock=); null leaves the protocol client fully disabled for the run var lspmock_bin: ?[:0]const u8 = null; var record_dir: ?[]const u8 = null; +/// --tape=DIR: each session records its tape there (PARDES_RECORD, src/tape.zig) +/// and the script fails unless every tape replays without diverging. +var tape_dir: ?[]const u8 = null; +/// --replay-with=EXE (with --tape): each tape replayed by that build of +/// pardes too, a window's build replaying a terminal's sessions (a replica +/// of another build reads them so: docs/next-daemon.md). +var replay_with: ?[:0]const u8 = null; +/// --dump (with --tape): each replay also loads the session's dump at its +/// end, which must be the same session and dump to the same bytes +/// (PARDES_REPLAY_DUMP, src/tape.zig). +var dump_check = false; const gpa = std.heap.page_allocator; pub fn main(init: std.process.Init) !void { @@ -123,6 +139,12 @@ pub fn main(init: std.process.Init) !void { lspmock = try absPath(arena, orig_cwd, a["--lspmock=".len..]); } else if (std.mem.startsWith(u8, a, "--record=")) { record_dir = try absPath(arena, orig_cwd, a["--record=".len..]); + } else if (std.mem.startsWith(u8, a, "--tape=")) { + tape_dir = try absPath(arena, orig_cwd, a["--tape=".len..]); + } else if (std.mem.eql(u8, a, "--dump")) { + dump_check = true; + } else if (std.mem.startsWith(u8, a, "--replay-with=")) { + replay_with = try arena.dupeSentinel(u8, try absPath(arena, orig_cwd, a["--replay-with=".len..]), 0); } else if (std.mem.startsWith(u8, a, "--jobs=")) { jobs = @max(1, try std.fmt.parseInt(usize, a["--jobs=".len..], 10)); } else if (exe == null) { @@ -148,6 +170,9 @@ pub fn main(init: std.process.Init) !void { std.mem.eql(u8, a, "--trace-stable") or std.mem.startsWith(u8, a, "--lspmock=") or std.mem.startsWith(u8, a, "--record=") or + std.mem.startsWith(u8, a, "--tape=") or + std.mem.eql(u8, a, "--dump") or + std.mem.startsWith(u8, a, "--replay-with=") or std.mem.startsWith(u8, a, "--jobs=")) continue; if (!saw_exe) { saw_exe = true; @@ -179,6 +204,7 @@ pub fn main(init: std.process.Init) !void { if (update) fatal("--record and --update are separate modes", .{}); try mkdir(arena, dir, true); } + if (tape_dir) |dir| try mkdir(arena, dir, true); try mkdir(arena, SNAP_BASE, true); // one run at a time (this is the flock that used to be typed by hand) @@ -463,9 +489,15 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 const started = eh.nowMs(); const out = runScript(arena, exe_z, script_path, stem) catch |e| { std.debug.print("FAIL {s}: script error {s} ({d}ms)\n", .{ stem, @errorName(e), eh.nowMs() - started }); + // What the session did before the script gave up replays all the same. + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); return 1; }; const took = eh.nowMs() - started; + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| { + std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); + return 1; + }; if (record_dir) |dir| { const path = std.fmt.allocPrintSentinel(arena, "{s}/{s}.snapshot", .{ dir, stem }, 0) catch return 1; eh.writeFile(path, out) catch return 1; @@ -495,6 +527,44 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 return 1; } +/// Every tape the script's sessions wrote, replayed by the same build +/// (PARDES_REPLAY), and by --replay-with's: why the first that diverged +/// did, or null. Another build's replay need agree only in its replica that +/// draws nothing, as a replica client draws none of its session's frames: +/// the two that draw meet a window's drawing where the session had a +/// terminal's. +fn replayTapes(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8) ?[]const u8 { + if (replayTapesWith(arena, exe_z, stem, false)) |why| return why; + const other = replay_with orelse return null; + return replayTapesWith(arena, other, stem, true); +} + +fn replayTapesWith(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8, headless: bool) ?[]const u8 { + const dir = tape_dir.?; + const io = std.Io.Threaded.global_single_threaded.io(); + var d = std.Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }) catch return "the tape directory cannot be read"; + defer d.close(io); + var it = d.iterate(); + var tapes: usize = 0; + while (it.next(io) catch return "the tape directory cannot be listed") |entry| { + if (!std.mem.startsWith(u8, entry.name, stem) or !std.mem.endsWith(u8, entry.name, ".tape")) continue; + const pid = entry.name[stem.len..][0 .. entry.name.len - stem.len - ".tape".len]; + if (pid.len < 2 or pid[0] != '-' or std.mem.indexOfNone(u8, pid[1..], "0123456789") != null) continue; + tapes += 1; + const cmd = std.fmt.allocPrintSentinel(arena, "PARDES_RECORD= PARDES_REPLAY='{s}/{s}' {s}timeout 300 '{s}' > '{s}/{s}.replay' 2>&1", .{ dir, entry.name, if (dump_check and !headless) "PARDES_REPLAY_DUMP=1 " else "", exe_z, dir, entry.name }, 0) catch return "out of memory"; + if (system(cmd) != 0) { + const why_path = std.fmt.allocPrint(arena, "{s}/{s}.replay", .{ dir, entry.name }) catch return "out of memory"; + const said = readFileAlloc(arena, why_path) catch return "the replay failed and said nothing"; + if (headless) if (std.mem.indexOf(u8, said, ": headless replica: ")) |at| { + if (std.mem.startsWith(u8, std.mem.trimStart(u8, said[at + ": headless replica: ".len ..], "0123456789"), " digests agree")) continue; + }; + return said; + } + } + if (tapes == 0) return "no session wrote a tape"; + return null; +} + fn reportPath(arena: std.mem.Allocator, script_path: []const u8) ![]const u8 { return std.fmt.allocPrint(arena, "{s}/{s}.report", .{ SNAP_BASE, std.fs.path.stem(script_path) }); } @@ -603,12 +673,26 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const // together. Scripts name it `$DUMP`. const dump_path = try std.fmt.allocPrint(arena, "{s}/dump.zon", .{base}); _ = setenv("PARDES_DUMP", try arena.dupeSentinel(u8, dump_path, 0), 1); + // Each session the script starts (and each pardes run inside one) + // writes its own tape, <stem>-<pid>.tape. + if (tape_dir) |dir| { + _ = system(try std.fmt.allocPrintSentinel(arena, "rm -f '{s}/{s}-'[0-9]*.tape '{s}/{s}-'[0-9]*.tape.replay", .{ dir, stem, dir, stem }, 0)); + _ = setenv("PARDES_RECORD", try std.fmt.allocPrintSentinel(arena, "{s}/{s}-", .{ dir, stem }, 0), 1); + } if (chdirRc(try arena.dupeSentinel(u8, work, 0)) < 0) return error.ChdirFailed; const chunks = try arena.alloc([]const u8, countCaptures(src)); var chunk_len: usize = 0; var h: ?eh.Harness = null; defer if (h) |*hp| hp.deinit(); + // `views=N`: the app's other terminals, and the one driven now. + var others: [3]eh.Harness = undefined; + var nothers: usize = 0; + var group: [4]*eh.Harness = undefined; + var cur: usize = 0; + defer for (others[0..nothers]) |*o| o.deinit(); + // A capture is a delta against the previous one of its terminal's. + var prevs: [4]Prev = @splat(.{}); // captures are deltas against the previous one of the same kind (see Prev) var prev: Prev = .{}; @@ -674,15 +758,46 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); var extra: [6]?[*:0]const u8 = @splat(null); var nextra: usize = 0; - while (tok.next()) |a| : (nextra += 1) { + var nviews: usize = 1; + while (tok.next()) |a| { + if (std.mem.startsWith(u8, a, "views=")) { + nviews = try std.fmt.parseInt(usize, a["views=".len..], 10); + if (nviews < 1 or nviews > 4) return error.BadScript; + continue; + } if (nextra >= extra.len) return error.BadScript; extra[nextra] = (try arena.dupeSentinel(u8, a, 0)).ptr; + nextra += 1; } if (h) |*hp| hp.deinit(); // scripts may restart (e.g. dump then -l) + for (others[0..nothers]) |*o| o.deinit(); + nothers = 0; + cur = 0; // a new process is a new screen: capture it whole, or its first // frame would be a delta against a dead instance's rows prev = .{}; + prevs = @splat(.{}); + var paths: std.ArrayList(u8) = .empty; + while (nothers + 1 < nviews) : (nothers += 1) { + var path: [64]u8 = undefined; + others[nothers] = try eh.Harness.initView(gpa, rows, cols, &path); + try paths.print(arena, "{s}{s}", .{ if (paths.items.len > 0) " " else "", std.mem.sliceTo(&path, 0) }); + } + if (nothers > 0) _ = setenv("PARDES_TEST_VIEWS", try arena.dupeSentinel(u8, paths.items, 0), 1); h = try eh.Harness.initArgv(gpa, exe_z.ptr, rows, cols, extra[0..nextra]); + _ = unsetenv("PARDES_TEST_VIEWS"); + group[0] = &h.?; + for (others[0..nothers], 1..) |*o, i| { + o.pid = h.?.pid; + group[i] = o; + } + for (group[0 .. nothers + 1]) |v| v.views = group[0 .. nothers + 1]; + } else if (std.mem.eql(u8, cmd, "view")) { + const k = try std.fmt.parseInt(usize, tok.next() orelse return error.BadScript, 10); + if (k < 1 or k > nothers + 1) return error.BadScript; + prevs[cur] = prev; + cur = k - 1; + prev = prevs[cur]; } else if (std.mem.eql(u8, cmd, "snapfile")) { const label = tok.next() orelse return error.BadScript; const contents = try readFileAlloc(arena, tok.rest()); @@ -692,49 +807,52 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const try appendChunk(chunks, &chunk_len, try snapFile(arena, label, contents)); } else if (std.mem.eql(u8, cmd, "wait")) { const ms = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try live(&h).expectWaitFor(tok.rest(), ms, "snapshot wait"); + try on(&h, &group, nothers, cur).expectWaitFor(tok.rest(), ms, "snapshot wait"); } else if (std.mem.eql(u8, cmd, "settle")) { - try live(&h).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); + try on(&h, &group, nothers, cur).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); } else if (std.mem.eql(u8, cmd, "stable")) { const quiet = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); const timeout = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try waitStable(live(&h), quiet, timeout); + try waitStable(on(&h, &group, nothers, cur), quiet, timeout); } else if (std.mem.eql(u8, cmd, "text")) { - try live(&h).send(tok.rest()); + try on(&h, &group, nothers, cur).send(tok.rest()); } else if (std.mem.eql(u8, cmd, "send")) { - try live(&h).send(try unescape(arena, tok.rest())); + try on(&h, &group, nothers, cur).send(try unescape(arena, tok.rest())); } else if (std.mem.eql(u8, cmd, "key")) { - while (tok.next()) |name| try live(&h).send(keyBytes(name) orelse return error.BadKey); + while (tok.next()) |name| try on(&h, &group, nothers, cur).send(keyBytes(name) orelse return error.BadKey); } else if (std.mem.eql(u8, cmd, "press") or std.mem.eql(u8, cmd, "release") or std.mem.eql(u8, cmd, "drag")) { const btn = buttonCode(tok.next() orelse return error.BadScript) orelse return error.BadScript; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); const b: u16 = if (std.mem.eql(u8, cmd, "drag")) btn + 32 else btn; const fin: u8 = if (std.mem.eql(u8, cmd, "release")) 'm' else 'M'; - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); } else if (std.mem.eql(u8, cmd, "motion")) { const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); } else if (std.mem.eql(u8, cmd, "wheel")) { const dir = tok.next() orelse return error.BadScript; const b: u16 = if (std.mem.eql(u8, dir, "up")) 64 else 65; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); } else if (std.mem.eql(u8, cmd, "resize")) { const rows = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - try live(&h).resize(rows, cols); + try on(&h, &group, nothers, cur).resize(rows, cols); + // Another view's terminal is no one's controlling one: the app + // is told it changed, as a stub tells it of its own. + if (cur > 0) std.posix.kill(live(&h).pid, std.posix.SIG.WINCH) catch {}; } else if (std.mem.eql(u8, cmd, "snap")) { - try appendChunk(chunks, &chunk_len, try snapText(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapText(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snapstyle")) { - try appendChunk(chunks, &chunk_len, try snapStyle(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapStyle(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snap9p")) { - try appendChunk(chunks, &chunk_len, try snap9p(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snap9p(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else { std.debug.print("unknown command {s}\n", .{cmd}); return error.BadScript; @@ -848,6 +966,13 @@ fn snapFile(arena: std.mem.Allocator, label: []const u8, contents: []const u8) ! return out; } +/// The terminal the script drives now (`view`). +fn on(h: *?eh.Harness, group: *[4]*eh.Harness, nothers: usize, cur: usize) *eh.Harness { + const app = live(h); + if (cur == 0 or cur > nothers) return app; + return group[cur]; +} + fn live(h: *?eh.Harness) *eh.Harness { if (h.*) |*v| return v; fatal("script used the app before `start`", .{}); @@ -885,7 +1010,7 @@ fn waitStable(h: *eh.Harness, quiet: i64, timeout: i64) !void { // and nothing in its process tree is runnable. Held across two probes, // because a pane's shell can write and sleep a scheduler tick before // the app wakes to read it. - if (since >= probe_after_ms and h.pending() == 0 and treeAsleep(h.pid)) { + if (since >= probe_after_ms and h.pendingAll() == 0 and treeAsleep(h.pid)) { if (asleep_since == 0) { asleep_since = now; } else if (now - asleep_since >= probe_hold_ms) { |
