diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/daemon.py | 32 | ||||
| -rw-r--r-- | test/e2e_harness.zig | 90 | ||||
| -rw-r--r-- | test/monkey.zig | 34 | ||||
| -rw-r--r-- | test/snapshot.zig | 92 | ||||
| -rw-r--r-- | test/snapshots/views.golden | 123 | ||||
| -rw-r--r-- | test/snapshots/views.snap | 57 |
6 files changed, 387 insertions, 41 deletions
diff --git a/test/daemon.py b/test/daemon.py index 9e9ea600..04455f0f 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -208,6 +208,38 @@ def main(binary, cycles): assert now == baseline, (cycle, now, baseline) print(f'daemon: {cycles} detach/attach cycles, fds/sockets/processes level: {baseline}') + # Another terminal while one is attached: a view of its own + # (src/views.zig), drawn beside the first. Detached from inside, its + # stub ends and the first draws on; closed outright, the same. + for how in ('detach', 'close'): + term2 = Term(20, 60) + stub2 = term2.spawn([binary, '--attach=work'], env) + started.append(stub2.pid) + wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) + wait_for('the second terminal\'s view live', lambda: client.screen()['cols'] == 60) + seen = len(term.text()) + run(client, shell, f'echo both-drawn-{how}\n'.encode()) + wait_for('the first terminal drawn', lambda: f'both-drawn-{how}'.encode() in term.text()[seen:]) + wait_for('the second terminal drawn', lambda: f'both-drawn-{how}'.encode() in term2.text()) + assert stub.poll() is None and stub2.poll() is None + if how == 'detach': + # Its view is the live one (its terminal spoke last). + client.write(f'/pane/{shell}/exec', b'Detach\n') + assert stub2.wait(timeout=10) == 0, stub2.returncode + assert b'detached from \'work\'' in term2.text(), term2.text()[-400:] + term2.close() + else: + term2.close() + assert stub2.wait(timeout=10) != 0 or True + wait_for('the first terminal\'s view live again', lambda: client.screen()['cols'] == 100) + seen = len(term.text()) + run(client, shell, f'echo first-on-{how}\n'.encode()) + wait_for('the first terminal drawn on', lambda: f'first-on-{how}'.encode() in term.text()[seen:]) + assert stub.poll() is None, 'the first terminal stays attached' + now = (fds(session), fds(daemon), len(list(root.iterdir())), len([p for p in processes(root) if alive(p)])) + assert now[:3] == baseline[:3], (how, now, baseline) + print('daemon: a second terminal on an attached session drawn beside the first, then detached and closed; the first drew on') + # Killed outright: its shells live on, their masters the daemon's. os.kill(session, signal.SIGKILL) stub.wait(timeout=10) diff --git a/test/e2e_harness.zig b/test/e2e_harness.zig index 7e29476b..6011ba16 100644 --- a/test/e2e_harness.zig +++ b/test/e2e_harness.zig @@ -12,6 +12,14 @@ extern "c" fn forkpty( termp: ?*const anyopaque, winp: ?*const posix.winsize, ) c_int; +extern "c" fn openpty( + amaster: *c_int, + aslave: *c_int, + name: ?[*]u8, + termp: ?*const anyopaque, + winp: ?*const posix.winsize, +) c_int; +extern "c" fn ttyname(fd: c_int) ?[*:0]const u8; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn _exit(status: c_int) noreturn; extern "c" fn unsetenv(name: [*:0]const u8) c_int; @@ -72,6 +80,12 @@ pub const Harness = struct { /// clipboard writes). Capture is bounded explicitly so a runaway child cannot /// consume unbounded test-runner memory. raw: *RawCapture, + /// Another view's terminal of the app this harness started (`view`): + /// it neither started the app nor kills it. + view: bool = false, + /// Every terminal of the app's, this one's too: each wait reads them + /// all, or the app would block writing a frame to one nobody reads. + views: []const *Harness = &.{}, pub fn init(gpa: std.mem.Allocator, exe: [*:0]const u8, rows: u16, cols: u16) !Harness { return initArgs(gpa, exe, rows, cols, null); @@ -129,8 +143,59 @@ pub const Harness = struct { }; } + /// A terminal for another view of the app `pid` (views.zig): a pty of + /// its own, whose path the app is given (PARDES_TEST_VIEWS) and opens. + pub fn initView(gpa: std.mem.Allocator, rows: u16, cols: u16, path: *[64]u8) !Harness { + var master: c_int = undefined; + var slave: c_int = undefined; + const ws = posix.winsize{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + if (openpty(&master, &slave, null, null, &ws) != 0) return error.NoPty; + defer _ = libc.close(slave); + const name = std.mem.span(ttyname(slave) orelse return error.NoPty); + if (name.len >= path.len) return error.NoPty; + @memcpy(path[0..name.len], name); + path[name.len] = 0; + const term = try gpa.create(ghostty_vt.Terminal); + errdefer gpa.destroy(term); + term.* = try ghostty_vt.Terminal.init(std.Io.Threaded.global_single_threaded.io(), gpa, .{ + .cols = cols, + .rows = rows, + .max_scrollback_bytes = 1024 * 1024, + }); + errdefer term.deinit(gpa); + const raw = try gpa.create(RawCapture); + raw.* = .{}; + return .{ .gpa = gpa, .master = master, .pid = 0, .term = term, .stream = term.vtStream(), .rows = rows, .cols = cols, .raw = raw, .view = true }; + } + + /// What the app wrote to its other terminals, read without waiting. + fn drainOthers(self: *Harness) void { + for (self.views) |other| if (other != self) while (other.pumpRead(0)) {}; + } + + fn pumpRead(self: *Harness, ms: i64) bool { + var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; + const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; + if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; + var buf: [4096]u8 = undefined; + const n = posix.read(self.master, &buf) catch return false; + if (n == 0) return false; + self.recordRaw(buf[0..n]); + self.feed(buf[0..n]); + return true; + } + + /// Bytes in every terminal of the app's (`pending`). + pub fn pendingAll(self: *Harness) usize { + var n = self.pending(); + for (self.views) |other| if (other != self) { + n += other.pending(); + }; + return n; + } + pub fn deinit(self: *Harness) void { - posix.kill(self.pid, posix.SIG.KILL) catch {}; + if (!self.view) posix.kill(self.pid, posix.SIG.KILL) catch {}; _ = libc.close(self.master); self.stream.deinit(); self.gpa.destroy(self.raw); @@ -175,8 +240,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; @@ -193,15 +259,14 @@ pub const Harness = struct { /// SETTLED grid sleeps in poll until the app actually writes, instead of /// burning a fixed window per check (see snapshot.zig's waitStable). pub fn pumpOnce(self: *Harness, ms: i64) !bool { - var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; - if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; - var buf: [4096]u8 = undefined; - const n = posix.read(self.master, &buf) catch return false; - if (n == 0) return false; - self.recordRaw(buf[0..n]); - self.feed(buf[0..n]); - return true; + if (self.views.len <= 1) return self.pumpRead(ms); + // Several terminals: each looked at every few milliseconds. + const deadline = nowMs() + ms; + while (true) { + self.drainOthers(); + if (self.pumpRead(@min(5, @max(0, deadline - nowMs())))) return true; + if (nowMs() >= deadline) return false; + } } /// Bytes sitting in the pty that nobody has picked up — keystrokes the app @@ -261,8 +326,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; diff --git a/test/monkey.zig b/test/monkey.zig index 7c9761ff..d35599ff 100644 --- a/test/monkey.zig +++ b/test/monkey.zig @@ -1,6 +1,6 @@ // pardes-monkey: random snapshot scripts against the tty, hunting panics. // -// zig build monkey -Dplatform=tty -- <seeds> [steps] [--from=N] [--out=DIR] [--keep] +// zig build monkey -Dplatform=tty -- <seeds> [steps] [--from=N] [--out=DIR] [--keep] [--views=N] // // Each seed makes one script (a fixed seed makes the same script, so a hit // replays): keys and leader bursts, typing, mouse presses, drags and wheel, @@ -9,7 +9,9 @@ // /tmp/pardes-monkey), and a panic is a record in the app's crash file // (src/crash.zig, in the script's fake home: found even when the crash cut // the script short) or a `.zig:N:N: 0x` stack frame in the report or the -// grid it captured (a trace renders INTO the screen). A hit +// grid it captured (a trace renders INTO the screen). --views=N gives the +// session N terminals, each a view of it (views.zig), and types into and +// sizes each in turn, a `view` switch among the steps. A hit // keeps DIR/seed-N.snap, its captures and its report; the rest are deleted. // A panic's trace can scroll off a small grid: replay the kept script, or // run the app in a larger pty. Every hit gets a fix and a pinned regression @@ -26,6 +28,9 @@ const Script = struct { gpa: std.mem.Allocator, rows: u16 = 24, cols: u16 = 80, + /// Each terminal's size, and the one typed into (`--views`). + sizes: [4][2]u16 = @splat(.{ 24, 80 }), + view: usize = 0, fn line(s: *Script, comptime fmt: []const u8, args: anytype) !void { try s.out.print(s.gpa, fmt ++ "\n", args); @@ -38,18 +43,24 @@ const commands = [_][]const u8{ "Newcol", "Find", "Grep", "Joincol", "Help", "Du const leader_keys = "abcdefghijklmnopqrstuvwxyz0123456789$^"; const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; -fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize) ![]u8 { +fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize, views: usize) ![]u8 { var prng = std.Random.DefaultPrng.init(seed); const random = prng.random(); var s: Script = .{ .gpa = gpa }; - try s.line("# pardes-monkey seed {d}, {d} steps", .{ seed, steps }); + try s.line("# pardes-monkey seed {d}, {d} steps, {d} views", .{ seed, steps, views }); try s.line("file bad.txt hello \\xff\\xfe world\\nlatin-1 caf\\xe9 tail\\ntruncated \\xe6\\x97 here\\nend\\n", .{}); try s.line("file wide.txt {s}tail\\nsecond line\\n", .{std.mem.asBytes(&@as([8][37]u8, @splat("\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaa\\x9e ".*)))}); try s.line("lines long.txt 400 some words after the number, long enough to wrap in a narrow pane", .{}); - try s.line("start 24 80 bad.txt", .{}); + if (views > 1) try s.line("start 24 80 views={d} bad.txt", .{views}) else try s.line("start 24 80 bad.txt", .{}); try s.line("wait 8000 Newcol Joincol", .{}); try s.line("stable 300 5000", .{}); for (0..steps) |step| { + if (views > 1 and random.uintLessThan(u8, 4) == 0) { + s.sizes[s.view] = .{ s.rows, s.cols }; + s.view = random.uintLessThan(usize, views); + s.rows, s.cols = s.sizes[s.view]; + try s.line("view {d}", .{s.view + 1}); + } const wide = s.cols >= 70 and s.rows >= 6; switch (random.uintLessThan(u8, 12)) { // A workspace word, run with a middle click: new columns make @@ -150,9 +161,9 @@ test "a panic trace is found, and ordinary colons are not" { } test "a seed makes the same script every time" { - const a = try generate(std.testing.allocator, 42, 50); + const a = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(a); - const b = try generate(std.testing.allocator, 42, 50); + const b = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(b); try std.testing.expectEqualStrings(a, b); } @@ -166,7 +177,7 @@ pub fn main(init: std.process.Init) !void { const io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len < 4) { - std.debug.print("usage: pardes-monkey <pardes-snap> <pardes> <seeds> [steps] [--from=N] [--out=DIR] [--keep] [--tape=DIR]\n", .{}); + std.debug.print("usage: pardes-monkey <pardes-snap> <pardes> <seeds> [steps] [--from=N] [--out=DIR] [--keep] [--tape=DIR] [--views=N]\n", .{}); std.process.exit(2); } const snap = args[1]; @@ -180,9 +191,12 @@ pub fn main(init: std.process.Init) !void { // --tape=DIR: each seed's sessions record their tapes there and the // seed is a hit if one does not replay (pardes-snap --tape). var tape: ?[]const u8 = null; + var views: usize = 1; var positional: usize = 0; for (args[3..]) |a| { - if (std.mem.startsWith(u8, a, "--from=")) { + if (std.mem.startsWith(u8, a, "--views=")) { + views = std.math.clamp(try std.fmt.parseInt(usize, a["--views=".len..], 10), 1, 4); + } else if (std.mem.startsWith(u8, a, "--from=")) { from = try std.fmt.parseInt(u64, a["--from=".len..], 10); } else if (std.mem.eql(u8, a, "--keep")) { keep = true; @@ -203,7 +217,7 @@ pub fn main(init: std.process.Init) !void { var hits: usize = 0; var seed = from; while (seed < from + seeds) : (seed += 1) { - const script = try generate(gpa, seed, steps); + const script = try generate(gpa, seed, steps, views); defer gpa.free(script); var path_buf: [4096]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/seed-{d}.snap", .{ out, seed }); diff --git a/test/snapshot.zig b/test/snapshot.zig index 255f55bf..8a0583e3 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; @@ -652,6 +657,14 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const 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 = .{}; @@ -717,15 +730,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()); @@ -735,49 +779,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; @@ -891,6 +938,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`", .{}); @@ -928,7 +982,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) { diff --git a/test/snapshots/views.golden b/test/snapshots/views.golden new file mode 100644 index 00000000..40f6a294 --- /dev/null +++ b/test/snapshots/views.golden @@ -0,0 +1,123 @@ +== snap one grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap two grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap one-edit grid=80x24 cursor=15,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-sees grid=80x24 cursor=7,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-down grid=80x24 cursor=7,6 +|3: 22 line 22 +|4: 23 line 23 +|5: 24 line 24 +|6: 25 line 25 +|7: 26 line 26 +|8: 27 line 27 +|9: 28 line 28 +|10: 29 line 29 +|11: 30 line 30 +|12: 31 line 31 +|13: 32 line 32 +|14: 33 line 33 +|15: 34 line 34 +|16: 35 line 35 +|17: 36 line 36 +|18: 37 line 37 +|19: 38 line 38 +|20: 39 line 39 +|21: 40 line 40 +|22: 41 line 41 +|23: 42 line 42 +== snap one-top grid=80x24 cursor=15,3 +== snap two-put grid=80x24 cursor=21,7 +|7: 26 EDIT-ONEline 1 +|8: 27 line 26 +|9: 28 line 27 +|10: 29 line 28 +|11: 30 line 29 +|12: 31 line 30 +|13: 32 line 31 +|14: 33 line 32 +|15: 34 line 33 +|16: 35 line 34 +|17: 36 line 35 +|18: 37 line 36 +|19: 38 line 37 +|20: 39 line 38 +|21: 40 line 39 +|22: 41 line 40 +|23: 42 line 41 +== snap two-col grid=80x24 cursor=42,1 +|1: New Tty Find Grep Joincol Delcol New Tty Find Grep Joincol Delcol +|2: */tmp/pardes-snap/views/cwd/v.txt Save +|3: Tty Collapse Del +|4: 22 line 22 +|5: 23 line 23 +|6: 24 line 24 +|7: 25 line 25 +|8: 26 EDIT-ONEline 1 +|9: 27 line 26 +|10: 28 line 27 +|11: 29 line 28 +|12: 30 line 29 +|13: 31 line 30 +|14: 32 line 31 +|15: 33 line 32 +|16: 34 line 33 +|17: 35 line 34 +|18: 36 line 35 +|19: 37 line 36 +|20: 38 line 37 +|21: 39 line 38 +|22: 40 line 39 +|23: 41 line 40 +== snap one-col grid=80x24 cursor=21,3 +== snap one-after grid=80x24 cursor=13,4 diff --git a/test/snapshots/views.snap b/test/snapshots/views.snap new file mode 100644 index 00000000..eba26bae --- /dev/null +++ b/test/snapshots/views.snap @@ -0,0 +1,57 @@ +# Two terminals on one session, each a view of it (src/views.zig): each its +# own layout, focus, cursor and scroll over the same panes. An edit typed in +# one is in the other; a line yanked in one is put in the other (registers +# are the session's); Newcol in one leaves the other's columns; Detach in +# one lets that terminal go and leaves the other drawing. +config Verbose off +lines v.txt 120 +start 24 80 views=2 v.txt +wait 8000 line 1 +stable 500 10000 +snap one +view 2 +wait 8000 line 1 +stable 500 10000 +snap two +# Typed in view 1: in view 2 too. +view 1 +key i +text EDIT-ONE +key esc +settle 100 +stable 400 8000 +snap one-edit +view 2 +stable 400 8000 +snap two-sees +# Scrolled in view 2: view 1 stays at the top. +key pgdn +stable 400 8000 +snap two-down +view 1 +stable 400 8000 +snap one-top +# Yanked in view 1, put in view 2. +key x y +stable 400 8000 +view 2 +key p +stable 400 8000 +snap two-put +# A column in view 2 only. +key space +text cn +stable 400 8000 +snap two-col +view 1 +stable 400 8000 +snap one-col +# Detached in view 2: its terminal let go, view 1 drawing on. +view 2 +key space +text sD +stable 400 8000 +view 1 +key j +stable 400 8000 +snap one-after |
