summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig162
1 files changed, 159 insertions, 3 deletions
diff --git a/core/replica.zig b/core/replica.zig
index cbb64363..e26a9f17 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -2362,6 +2362,10 @@ pub const Peers = struct {
ps.recording = true;
ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
}
+ // A join with no room lets go a view kept past its bound by the
+ // core's clock (Core.openView), which a wake by mail alone, as this
+ // join's, does not move (tui.zig `clockNow`): moved now, on the tape.
+ if (@popCount(core.views_open) == pardes.views.max) if (core.host.call(.now, .{})) |now| core.advance(now);
// A pair's first client, or the first back after a Restore with no
// client and no view kept for one: the view the session has is the
// joiner's. Any other attach (a detached session's too) opens one.
@@ -2370,7 +2374,14 @@ pub const Peers = struct {
const k = viewFor(core, joining.view, own) orelse {
core.unattended = ps.count() == 0 and ps.detached;
if (ps.count() == 0) ps.stop(core);
- return daemon.refuseReplica(fd, "that session has as many views as it takes");
+ const full = "that session has as many views as it takes";
+ var left: u64 = 0;
+ for (0..pardes.views.max) |i| if (pardes.views.isAway(core, @intCast(i))) {
+ const n = (core.views[i].?.away_ns + pardes.views.away_kept_ns) -| core.clock.now_ns;
+ left = if (left == 0) n else @min(left, n);
+ };
+ var why: [128]u8 = undefined;
+ return daemon.refuseReplica(fd, if (left == 0) full else std.fmt.bufPrint(&why, full ++ " (one is kept {d} s more for a client joining again)", .{(left + std.time.ns_per_s - 1) / std.time.ns_per_s}) catch full);
};
ps.seq += 1;
ps.unit(0, ps.seq);
@@ -4797,6 +4808,40 @@ test "bytes: a session's output that moves nothing the digest covers (a colour s
try sameTerminals(b.a, b.r);
}
+test "bytes: a terminal's view a rollback puts back is the session's through a reflow: a view on a wrapped line's second row, joined into the row above by the session's widening, is the same row after its narrowing on every side" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const b = try BytesPair.init();
+ defer b.deinit();
+ const id = b.shell.index();
+ // Lines a row and a half long, the view on the first's second row (put
+ // there alike on both sides: no step of either moved it).
+ const cols = pardes.Term.grid(b.a.core.panes[id].?.terminal).cols;
+ var line: [512]u8 = undefined;
+ const text = line[0 .. cols + cols / 2];
+ @memset(text, 'x');
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = "\x1bc" } });
+ for (0..40) |_| {
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = text } });
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = "\r\n" } });
+ }
+ b.r.confirm((try b.a.pending()).?);
+ b.r.settle();
+ for ([_]*Pardes{ b.a.core, b.r.core() }) |core| pardes.Term.viewTo(core.panes[id].?.terminal.?, 1);
+ // The session's frontend widens: the line one row, the view at row 0,
+ // its cell mid-row.
+ b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 200, .rows = 24 } }));
+ b.r.settle();
+ try agrees(b.a, b.r);
+ try std.testing.expectEqual(@as(?usize, 0), pardes.Term.grid(b.a.core.panes[id].?.terminal).view);
+ // A key of the client's on top, the session's first: the copy put back.
+ try b.rollback();
+ try agrees(b.a, b.r);
+ // The session narrows again: every side's view the same row.
+ b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 80, .rows = 24 } }));
+ b.r.settle();
+ try agrees(b.a, b.r);
+}
+
test "bytes: a mouse motion a speculation told a terminal's program is told again when its input is replayed: the cell last told is the pane's, put back with it" {
if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const gpa = std.testing.allocator;
@@ -6453,6 +6498,107 @@ test "a closed terminal with a kitty image brought back on a client: its image d
try agrees(a, r);
}
+/// The core's fields whose digests differ between the session and a
+/// client, named (tape.partsDiffer); empty when they agree.
+fn partsApart(a: *Authority, r: *Replica, arena: std.mem.Allocator) ![]const u8 {
+ const want = try arena.create(tape.Parts);
+ const got = try arena.create(tape.Parts);
+ const w = digested: {
+ const was = Stepping.begin(&a.t, null);
+ defer was.end();
+ break :digested tape.digestParts(a.core, want);
+ };
+ const g = digested: {
+ const was = Stepping.begin(&r.t, &r.epoch);
+ defer was.end();
+ break :digested tape.digestParts(r.core(), got);
+ };
+ return if (std.mem.eql(u64, w, g)) "" else tape.partsDiffer(g, w, arena);
+}
+
+test "a terminal's program shows a kitty image under screen state: the client's emulator never has it, and its tag says what the session's does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), true);
+ defer r.deinit();
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // In the session's order: the pump's unit, then what its terminals came
+ // to; then a step after both, on every side.
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ r.confirm(try a.local(.{ .focus = true }));
+ r.settle();
+ try std.testing.expect(pardes.Term.hasImages(a.core.panes[shell].?.terminal));
+ try std.testing.expect(!pardes.Term.hasImages(r.core().panes[shell].?.terminal));
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ try agrees(a, r);
+ const prefix = try pardes.tagline.prefixIn(r.core(), arena.allocator(), r.core().panes[shell].?);
+ try std.testing.expect(std.mem.indexOf(u8, prefix, " petscii:") != null);
+}
+
+test "a command typed into a shell with prompt marks under screen state: every side notes it as the session's emulator numbers it, and Kill finds it" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const ShellHost = struct {
+ fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool {
+ return false;
+ }
+ fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write };
+ };
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &ShellHost.vtable }, true);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), true);
+ defer r.deinit();
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ // pardes's own bash marks (host_io.zig): one command run, a prompt.
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ a.core.view().active = shell;
+ r.confirm(try a.local(.{ .focus = true }));
+ r.settle();
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // Exec'd in the shell: typed at its prompt, the command noted.
+ r.confirm(try a.local(.{ .command = "Exec sleep 9" }));
+ r.settle();
+ // Its marks cross with the cells (Term.marksOf): numbered alike.
+ try std.testing.expectEqual(@as(u32, 2), a.core.panes[shell].?.sent_command.?.seq);
+ try std.testing.expectEqual(@as(u32, 2), r.core().panes[shell].?.sent_command.?.seq);
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // Its echo and its C mark: running. Kill is asked for it.
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = "sleep 9\r\n\x1b]133;C;aid=pardes\x07" } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ r.confirm(try a.local(.{ .command = "Kill" }));
+ r.settle();
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ try agrees(a, r);
+}
+
test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
@@ -6719,7 +6865,7 @@ test "a client's n in a kind's pane is a motion: it steps to the next place a re
/// `addon`: a kind whose state is its own on the session (a PDF's), which
/// the walk does not stand in (its Still has no search, place or word):
/// the soak's (test/replicas.zig) PDF and pictures are, judged by name.
-const On = enum { any, file, kind, shell, shell_normal, addon };
+const On = enum { any, file, kind, shell, shell_normal, addon, dir };
/// What a far client may wait a round trip for (the user's line, P14: a
/// side effect on the system may be slow; motions, edits and layout are
@@ -6740,6 +6886,12 @@ const may_wait = [_]Wait{
.{ .what = "LookPrev", .on = .any, .stop = "resolve", .why = "a Look's walk: each word asked of the disk" },
.{ .what = "Mini *", .on = .any, .stop = "resolve", .why = "a file opened by name, from the session's disk" },
.{ .what = "Get", .on = .any, .stop = "read_file", .why = "the file read again from the session's disk" },
+ .{ .what = "Get", .on = .dir, .stop = "dir_listing", .why = "a directory listed again from the session's disk" },
+ // A directory's pane (Dir.zig): its entries are names on the session's disk.
+ .{ .what = "Enter", .on = .dir, .stop = "resolve", .why = "the entry under the cursor opened, from the session's disk" },
+ .{ .what = "* Enter", .on = .dir, .stop = "resolve", .why = "the entry opened (Enter after a prefix still opens it)" },
+ .{ .what = "Tab", .on = .dir, .stop = "kind", .why = "what the entry under the cursor is, asked of the session's disk" },
+ .{ .what = "* Tab", .on = .dir, .stop = "kind", .why = "what the entry is (Tab after a prefix still asks)" },
.{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" },
// A program run, a shell opened or closed: the session's processes.
.{ .what = "Exec *", .on = .any, .stop = "kind", .why = "a program run: its name asked of the disk first" },
@@ -7015,12 +7167,16 @@ test "a far client's every word and key, on a file's pane, a kind's and a shell'
_ = try a.local(.{ .command = "Still" });
const kind = a.core.view().active;
try std.testing.expect(file != kind and a.core.panes[file].?.file != null and a.core.panes[kind].?.addon != null);
+ // A directory's pane (Dir.zig), listed from the disk.
+ _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/", .{dir}) });
+ const listing = a.core.view().active;
+ try std.testing.expect(a.core.panes[listing].?.file.?.listing != null);
var seen: [may_wait.len]bool = @splat(false);
var hit: [may_wait.len]bool = @splat(false);
var unlisted: u32 = 0;
var tried: u32 = 0;
- const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell } };
+ const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell }, .{ .dir, listing } };
for (stands) |stand| {
const on, const at = stand;
const id = at orelse continue;