diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig index 8b0f8c8e..330c3204 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -4265,3 +4265,67 @@ test "a replica with no code for a kind draws its pane from the authority's fram try std.testing.expect(r.diverged == null); try std.testing.expectEqual(digestOf(a), r.digest()); } + +/// A kind whose place (`where`) is the rows its last view was told: a +/// session's step that tells it moves its place, never a key's. +const Placed = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + const n: *u32 = @ptrCast(@alignCast(pardes.Kind.api.alloc.?(4))); + n.* = 0; + return n; + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn view(st: ?*anyopaque, v: [*c]const pa.pa_view) callconv(.c) void { + const n: *u32 = @ptrCast(@alignCast(st.?)); + n.* = v.*.rows; + } + fn where(st: ?*anyopaque, buf: [*c]u8, len: u32) callconv(.c) u32 { + const n: *u32 = @ptrCast(@alignCast(st.?)); + return @intCast((std.fmt.bufPrint(buf[0..len], "1:0 {d}", .{n.*}) catch return 0).len); + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Placed", .opens = 1, .doc = "a place" })}; + 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 = "placed".* ++ @as([10]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .view = view, + .where = where, + }); +}; + +test "a kind's place moved in a step of another view's: a replica's key that reads it guesses the authority's" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } })); + // The replica's view opens the kind's pane, and an Esc goes back to + // the file (Last): view 0 has the file. + const own = try a.openView(1); + r.view = own.view; + r.confirm(own.unit); + for ([_]pardes.Event{ .{ .command = "Placed" }, .{ .key = .{ .cp = pardes.Key.escape } } }) |ev| { + r.confirm(try a.take(try r.input(ev))); + r.settle(); + } + // View 0 resized, and a step of the session's own tells the kind: its + // place moves in a step whose view has the file, which reads it not. + var buf: [256]u8 = undefined; + r.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 80, .rows = 20 } }))); + r.confirm(try a.local(.tick)); + // The replica's Esc back to the kind's pane: its jump reads the place. + const fast = r.stats.fast; + const missed = r.stats.mispredicted; + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = pardes.Key.escape } })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(missed, r.stats.mispredicted); + try std.testing.expectEqual(fast + 1, r.stats.fast); + try agrees(a, r); +} |
