diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig index 57dd4eed..f62cd629 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -4491,6 +4491,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; |
