diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 213 |
1 files changed, 211 insertions, 2 deletions
diff --git a/core/replica.zig b/core/replica.zig index 829972c2..2a66cc3d 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1911,6 +1911,18 @@ pub const Peers = struct { return n; } + /// The time the session's pump reads after its wait, when that wait + /// took a client's post and nothing else (`only_mail`, the shell's word + /// for its wait): the core's clock as it stands, so the pump's unit + /// moves nothing a client's speculation stands on; null otherwise (the + /// shell's clock). Held only while a post waits (`mail`): a wait that + /// ran out took none, and the pump after it is the one its deadline + /// asked for (a window's wait that ran out was held, 2026-11-24: a + /// deadline never met, the session stepped a millisecond for ever). + pub fn heldNow(ps: *const Peers, core: *const Pardes, only_mail: bool) ?u64 { + return if (only_mail and ps.recording and ps.mail.load(.acquire) and core.clock.started) core.clock.now_ns else null; + } + pub fn post(ps: *Peers, note: Note) void { ps.lock.lockUncancelable(ps.io); if (note == .join and ps.upgrading.load(.monotonic)) { @@ -3100,7 +3112,7 @@ fn loadAgain(gpa: Allocator, j: *Joined, have: ?*Have, why: []u8, session: []con /// The fields a replica loaded from the join's dump `bytes` has other than /// the session as it dumped them (the digest a field at a time the dump /// ends with: dump.joinParts), named in `out`. -fn loadDiffers(r: *Replica, bytes: []const u8, out: []u8) []const u8 { +pub fn loadDiffers(r: *Replica, bytes: []const u8, out: []u8) []const u8 { const want = pardes.dump.joinParts(r.gpa, bytes) orelse return "(the dump names no field) "; defer r.gpa.free(want); var arena: std.heap.ArenaAllocator = .init(r.gpa); @@ -4534,6 +4546,160 @@ test "bytes: a client's wheel over a terminal is its own view's: applied at once try std.testing.expect(placeIn(b.r.core(), b.r.view, id).? < back); } +test "a view's top is a line and a row of its block: a client's wheel into a line wrapped over rows moves it a row a notch, the session agrees through a rollback, and a dump keeps the row" { + const gpa = std.testing.allocator; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + // Line 3 is 60 words: four rows at this width. + for (0..40) |i| if (i == 3) { + for (0..60) |w| try text.print(gpa, "w{d:0>3} ", .{w}); + try text.append(gpa, '\n'); + } else try text.print(gpa, "line {d}\n", .{i}); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + _ = try a.core.setTestFile(text.items); + was.end(); + _ = try a.unit(0, 0); + const id = a.core.view().active; + // A client joined from the session's dump: the text is in it. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const rect = a.core.view().rects[id]; + const notch: pardes.Event = .{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = rect.x + 10, .row = rect.y + 5 } }; + // Three notches to line 3's first row, two into its block. + for (0..5) |_| { + const m = try gpa.dupe(u8, try r.input(notch)); + defer gpa.free(m); + r.confirm(try a.take(m)); + r.settle(); + try agrees(a, r); + } + for ([_]*Pardes{ a.core, r.core() }) |core| { + try std.testing.expectEqual(@as(usize, 3), core.panes[id].?.file.?.scroll); + try std.testing.expectEqual(@as(u32, 2), core.panes[id].?.file.?.scroll_row); + } + // A notch of the client's on top of the session's own key (one that + // leaves the view be): the copy put back, the notch applied over it. + const again = try gpa.dupe(u8, try r.input(notch)); + defer gpa.free(again); + r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } })); + r.settle(); + r.confirm(try a.take(again)); + r.settle(); + try agrees(a, r); + try std.testing.expectEqual(@as(u32, 3), r.core().panes[id].?.file.?.scroll_row); + // A client joining over the view scrolled into the block has the row. + const late = try joinedNow(a, 2, 0); + defer late.deinit(); + try agrees(a, late); + try std.testing.expectEqual(@as(u32, 3), late.core().panes[id].?.file.?.scroll_row); + // The client's view keys, each after the session drew (its geometry + // made for the frame; the client's is not): what a key reads of the + // body's rows is made of the state alone (the soak's divergence at a + // Ctrl-b, before File.shownRows laid the geometry out first). + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + for ([_]pardes.Key{ .{ .cp = 'b', .ctrl = true }, .{ .cp = 'f', .ctrl = true }, .{ .cp = 'd', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, .{ .cp = 'e', .ctrl = true } }) |k| { + for (0..3) |_| { + { + const drew = Stepping.begin(&a.t, null); + defer drew.end(); + _ = try a.core.render(arena.allocator()); + } + const m = try gpa.dupe(u8, try r.input(.{ .key = k })); + defer gpa.free(m); + r.confirm(try a.take(m)); + r.settle(); + try agrees(a, r); + } + } + // A session's dump keeps the row. + const bytes = try pardes.dump.dumpBytes(a.core); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + try std.testing.expectEqual(walk.digest(Pardes, a.core), walk.digest(Pardes, loaded)); + try std.testing.expectEqual(a.core.panes[id].?.file.?.scroll_row, loaded.panes[id].?.file.?.scroll_row); +} + +test "a text's views under random orders of the session's and a client's keys, wheels and draws, keys of the session's between a client's and its confirm (rollbacks): every side agrees (an explorer of a view's rows)" { + const gpa = std.testing.allocator; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + // Lines of one row and of several (wrapped at 80 columns). + for (0..120) |i| if (i % 7 == 3) { + for (0..20 + i % 40) |w| try text.print(gpa, "w{d:0>3} ", .{w}); + try text.append(gpa, '\n'); + } else try text.print(gpa, "line {d}\n", .{i}); + const keys = [_]pardes.Key{ + .{ .cp = 'j', .text = "j" }, .{ .cp = 'k', .text = "k" }, + .{ .cp = 'w', .text = "w" }, .{ .cp = 'b', .text = "b" }, + .{ .cp = 'b', .ctrl = true }, .{ .cp = 'f', .ctrl = true }, + .{ .cp = 'd', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, + .{ .cp = 'G', .text = "G" }, .{ .cp = 'z', .text = "z" }, + .{ .cp = 't', .text = "t" }, .{ .cp = 'i', .text = "i" }, + .{ .cp = 'x', .text = "x" }, .{ .cp = pardes.Key.escape }, + .{ .cp = '/', .text = "/" }, .{ .cp = pardes.Key.enter }, + }; + for (0..view_orders) |seed| { + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + _ = try a.core.setTestFile(text.items); + was.end(); + _ = try a.unit(0, 0); + const id = a.core.view().active; + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const opened = try a.openView(1); + r.view = opened.view; + r.confirm(opened.unit); + r.settle(); + try agrees(a, r); + var prng: std.Random.DefaultPrng = .init(seed); + const rand = prng.random(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + for (0..150) |_| { + const rect = a.core.views[r.view].?.rects[id]; + const at = .{ .col = rect.x + 10, .row = rect.y + rect.h / 2 }; + const ev: pardes.Event = switch (rand.uintLessThan(u8, 10)) { + 0 => .{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = at.col, .row = at.row } }, + 1 => .{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = at.col, .row = at.row } }, + else => .{ .key = keys[rand.uintLessThan(usize, keys.len)] }, + }; + if (rand.boolean()) { + const drew = Stepping.begin(&a.t, null); + defer drew.end(); + _ = arena.reset(.retain_capacity); + _ = try a.core.render(arena.allocator()); + } + if (rand.uintLessThan(u8, 4) == 0) _ = r.draw(.{ .ctx = null, .f = null }); + if (rand.uintLessThan(u8, 3) == 0) { + // The session's own key, then nothing of the client's. + r.confirm(try a.local(.{ .key = keys[rand.uintLessThan(usize, keys.len)] })); + r.settle(); + } else { + const m = try gpa.dupe(u8, try r.input(ev)); + defer gpa.free(m); + // Sometimes the session's own key first: the client's + // applied at once is rolled back and replayed. + if (rand.uintLessThan(u8, 3) == 0) { + r.confirm(try a.local(.{ .key = keys[rand.uintLessThan(usize, keys.len)] })); + r.settle(); + } + r.confirm(try a.take(m)); + r.settle(); + } + agrees(a, r) catch |err| { + std.debug.print("seed {d}: diverged\n", .{seed}); + return err; + }; + } + } +} + /// Seeds of the explorer below a run of the suite takes; 3,000 were run /// once (reports/pardes-term-join-10.md). const view_orders = 30; @@ -6461,6 +6627,45 @@ test "a kind's place moved in a step of another view's: a replica's key that rea try agrees(a, r); } +/// Placed, plain (PA_PLAIN): a client runs it, and its state is the rows +/// its last view was told. +const Viewed = struct { + const pa = pardes.Kind.c; + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Viewed", .opens = 1, .doc = "the rows its view was told" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "viewed".* ++ @as([10]u8, @splat(0)), .version = 1, .exts = "", .flags = pa.PA_PLAIN, .words = &words, .nwords = 1 }), + .open = Placed.open, + .paint = Placed.paint, + .view = Placed.view, + }); +}; + +test "a client joins with its session's geometry as the session holds it: the view opened for it, not laid out yet, is not laid out in the client either, and a plain kind is told the same views on both" { + // The soak's (test/replicas.zig): a client parted at its first unit + // after its join, `addon pdf: its state after view is not the + // session's`: it had laid every view out at its load, its own new one + // too, which the session lays out at that view's first input. + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const viewed = pardes.Kind.loadedOf(&Viewed.ops, "/x/viewed.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &viewed } }); + _ = try a.local(.{ .command = "Viewed" }); + const kid = a.core.view().active; + const joined = (try a.join(1, null)).?; + defer gpa.free(joined.dump); + const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + try std.testing.expect(r.loaded(joined.digest)); + try runsHere(r, kid, &Viewed.ops); + try std.testing.expectEqual(a.core.views[joined.view].?.rects[kid], r.core().views[joined.view].?.rects[kid]); + // The session's own step tells the kind each view whose facts changed. + r.confirm(try a.local(.tick)); + r.settle(); + try agrees(a, r); +} + /// A kind whose every key asks a job, for the job tokens' test. const Jobber = struct { const pa = pardes.Kind.c; @@ -7172,12 +7377,16 @@ const may_wait = [_]Wait{ .{ .what = "DumpThemes", .on = .any, .stop = "the authority's own state", .why = "the theme files written" }, // A +Preview's text is read from the session's disk. .{ .what = "Search *", .on = .any, .stop = "preview", .why = "a +Preview's file, from the session's disk" }, + // jj run in the session's tree (jj.zig `show`, `diffChange`, + // `diffPair`): what its repository holds, the session's disk. + .{ .what = "At *", .on = .any, .stop = "jj", .why = "a file at a revision, asked of the session's repository" }, // A kind's own state on the session (a PDF's document): asking it. .{ .what = "*", .on = .addon, .stop = "a kind's search", .why = "a search in a kind's pane: its own state, the session's" }, .{ .what = "*", .on = .addon, .stop = "a kind's address", .why = "a place in a kind's pane (`Look doc.pdf:3`): its own state" }, .{ .what = "*", .on = .addon, .stop = "a kind's close", .why = "a kind's pane closed: its state let go on the session" }, .{ .what = "*", .on = .addon, .stop = "a kind's word", .why = "a word to a kind (a key it takes as one): its own state" }, .{ .what = "*", .on = .addon, .stop = "a kind's open", .why = "a kind's pane opened (`Life`): its state made on the session" }, + .{ .what = "*", .on = .addon, .stop = "a key into a kind's pane", .why = "a key to a kind this client does not run (no library for it here, or not PA_PLAIN): its state the session's" }, } ++ (if (pardes.builtins.capabilities.font_picker) [_]Wait{ // A window's view's words, which a terminal's build has too: a window // client of its session runs them. @@ -7233,7 +7442,7 @@ const walk_samples = [_]struct { []const u8, []const u8 }{ .{ "Msg", "hi" }, .{ "ThemeFile", "t.zon" }, .{ "Edit", "s/a/b/" }, .{ "Mini", "x" }, .{ "Find", "x" }, .{ "Grep", "x" }, .{ "Rename", "x" }, .{ "WsSymbols", "x" }, .{ "Theme", "acme" }, .{ "Font", "Mono 12" }, .{ "TaglineSize", "100" }, .{ "WindowOpacity", "100" }, - .{ "MessageLinger", "1000" }, .{ "InactiveDim", "0" }, .{ "GripWidth", "100" }, + .{ "MessageLinger", "1000" }, .{ "InactiveDim", "0" }, .{ "GripWidth", "100" }, .{ "At", "@-" }, }; fn keyName(buf: []u8, k: pardes.Key) []const u8 { |
