diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 17:01:35 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 19:38:53 -0300 |
| commit | 2cce341e2c94fb3150dc1f3e7911c26502a70487 (patch) | |
| tree | c637fcfa9a52aa66f6c34600f4cdea4020088e01 /test/e2e_harness.zig | |
| parent | c2b4f235e78735102c691677f08bd1b2403168df (diff) | |
| download | pardes-2cce341e2c94fb3150dc1f3e7911c26502a70487.tar.gz pardes-2cce341e2c94fb3150dc1f3e7911c26502a70487.zip | |
views: several clients on one session, each its own layout, focus, cursors and scroll over the panes they share; a second `pardes --attach` draws on its own terminal beside the first, and `pardes-gui --attach` opens another window
The core still sees one view, the live one, in the fields it always had (src/views.zig): a view's share of the session (`views.fields`: its screen, columns, rects, focus, pointer and gestures, jumps, key prefixes, column tags, presentation and animations, windowed and native_images) and of each pane (`PaneView`: its place in the layout, which text has the keyboard, the body's cursor, selections and mode, its scroll, a document's size, what the step measured of it) is copied out when another view goes live and back when it is live again. One view never parks: the single client pays a test of one byte a step. `toView`, `openView` and `closeView` are taped inputs; the pump draws each other view by going into it and back, which leaves every digested field as it was, so the replica that draws nothing skips it. A parked view is in no digest (its slot is written whenever it goes out); what drawing it made of its own (animations, presentation) is kept, whatever its unpark brought inside a changed text is done again when it is next live.
Shared: the panes' text, terminals, tags, prompts, messages, registers (yanked in one view, put in another), the workspace tag and settings. A terminal shown at two sizes takes the size of the view that typed into it last (`Pane.sized_by`), and any view's once that one is gone; the others show it at that size. A text edited in one view moves the other views' cursors and scroll with the splice, as a Zerox twin's (File.ParkedPlaces). A pane closed in one view leaves the others' layouts (by its slot's serial); a pane focused in a view that does not show it is placed in its active column (views.show).
tty: each terminal is a Screen of its own (its vaxis, input reader, frame answer, kitty images and shm probe, glide, trail and flash); its input reaches the loop as its view's (a `from` ahead of it when another terminal posted last, so one terminal posts none). A SIGWINCH asks every terminal its size; a SIGHUP asks each whether it hung up or lost its stub. Detach, or a terminal's end, with other views left closes only that view, its stub told `d`; the session's end tells every stub. daemon.zig: an attach names an attached session too (or the only one there is when none is detached).
gui: the Gui holds the live window's share of itself (window_fields: the SDL window and its presenting, scroll, glide, scene and post chain, pointer, blink, captures); the others wait in Shell.windows, swapped in (`follow`) as the core's live view changes, and each SDL event first takes its window's view live (`route`). Closing a window while others stay closes its view.
Harnesses: `start <rows> <cols> views=N` in a snapshot script gives the app N-1 more terminals (PARDES_TEST_VIEWS, opened as attached ones), `view <k>` types into and captures terminal k, every wait reads them all; `pardes-monkey --views=N` switches among N; `zig build replay` runs its seeds at three views too. views.snap pins two terminals: an edit seen in both, a scroll in one, a line yanked in one and put in the other, Newcol in one, Detach in one. Unit tests: own cursor, scroll, mode, layout and focus; edits shared; registers shared; terminal size by last typist; closing views; drawing the others leaves the digest; a view keeps its place in a text another edits.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/e2e_harness.zig')
| -rw-r--r-- | test/e2e_harness.zig | 90 |
1 files changed, 78 insertions, 12 deletions
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; |
