diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 06:24:42 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 11:19:44 -0300 |
| commit | c1917b949b9580de8bf0977874a210f0266812da (patch) | |
| tree | 447dd2235ea00634f662cd3becf4bceb8a895f41 /test/panes.zig | |
| parent | 76c55b5bce0f181b0b15b16596c900397dfef544 (diff) | |
| download | pardes-c1917b949b9580de8bf0977874a210f0266812da.tar.gz pardes-c1917b949b9580de8bf0977874a210f0266812da.zip | |
kinds #14, the core's PDF code gone: pdf.zig and pdf_view.zig leave and every pdf_enabled branch with them; look, Jump, Recent, dumps (one from before the addon opens as its pane), the watch, mouse, keys, the rail and the tag go through Kind for any kind's pane; test/pdf.zig's behaviours are T1 cases over 9P (addons_pdf.zig) and pane cases (a Shadow over a replica, a file changed under it), the harnesses load pdf.so by path
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/panes.zig')
| -rw-r--r-- | test/panes.zig | 313 |
1 files changed, 163 insertions, 150 deletions
diff --git a/test/panes.zig b/test/panes.zig index 28a884ab..a19af1a6 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -2307,154 +2307,6 @@ const ImageTests = struct { } }; -const PdfTests = struct { - const enabled = panes.Pdf.enabled; - const pdf = panes.Pdf.pdf; - const Point = panes.Pdf.Point; - const Document = panes.Pdf.Document; - const State = panes.Pdf.State; - const probeWord = panes.Pdf.probeWord; - const pointAtGeometry = panes.Pdf.pointAtGeometry; - const SectionRows = panes.Pdf.SectionRows; - - test "feature-off PDF state is zero-sized" { - if (!enabled) try std.testing.expectEqual(@as(usize, 0), @sizeOf(State)); - } - - test "PDF section rows preserve DFS ordinals and sanitise hierarchy" { - if (!enabled) return; - const entries = [_]pdf.OutlineEntry{ - .{ .depth = 0, .title = null, .destination = .none }, - .{ .depth = 1, .title = " Child\n\tTitle\x01 Café \u{2028} Next ", .destination = .{ .internal = .{ .page = 2, .x = 12, .y = 34 } } }, - .{ .depth = 0, .title = "", .destination = .{ .internal = .{ .page = 0, .x = null, .y = null } } }, - .{ .depth = 0, .title = "External", .destination = .{ .external = "https://example.com/manual" } }, - .{ .depth = 0, .title = "Dead branch", .destination = .none }, - .{ .depth = 0, .title = "Unsafe", .destination = .{ .external = "javascript:alert" } }, - .{ .depth = 0, .title = "Linked branch", .destination = .none }, - .{ .depth = 1, .title = "Deep link", .destination = .{ .external = "https://example.com/deep" } }, - }; - const rows = try SectionRows.render(std.testing.allocator, "/tmp/manual.pdf", &entries); - defer std.testing.allocator.free(rows); - try std.testing.expectEqualStrings( - "manual.pdf:3:1 [untitled]\n" ++ - "manual.pdf:3:2 Child Title Café Next\n" ++ - "manual.pdf:1:3 [empty title]\n" ++ - "https://example.com/manual External\n" ++ - "https://example.com/deep Linked branch\n" ++ - "https://example.com/deep Deep link\n", - rows, - ); - try std.testing.expect(SectionRows.resolve(&entries, 4) == null); - try std.testing.expect(SectionRows.resolve(&entries, 5) == null); - const resolved = try SectionRows.resolveOrdinals(std.testing.allocator, &entries); - defer std.testing.allocator.free(resolved); - for (entries, 0..) |_, ordinal| { - const single = SectionRows.resolve(&entries, ordinal); - if (resolved[ordinal] == std.math.maxInt(usize)) { - try std.testing.expect(single == null); - } else { - const bulk = SectionRows.usableDestination(entries[resolved[ordinal]].destination) orelse - return error.MissingBulkPdfSectionDestination; - try std.testing.expect(single != null); - try std.testing.expect(std.meta.eql(single.?, bulk)); - } - } - - // Every allocation site in the ordinal table and output growth remains - // atomic: the testing allocator sees each induced failure cleaned up - // before the first index at which the whole render can succeed. - var rendered = false; - for (0..64) |fail_index| { - var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ - .fail_index = fail_index, - }); - const failure_gpa = failing.allocator(); - const attempt = SectionRows.render(failure_gpa, "/tmp/manual.pdf", &entries) catch |err| { - try std.testing.expectEqual(error.OutOfMemory, err); - continue; - }; - failure_gpa.free(attempt); - rendered = true; - break; - } - try std.testing.expect(rendered); - } - - test "bulk PDF section resolution matches single Look policy across DFS boundaries" { - if (!enabled) return; - const entries = [_]pdf.OutlineEntry{ - .{ .depth = 0, .title = "Resolved root", .destination = .none }, - .{ .depth = 1, .title = "Unsafe branch", .destination = .{ .external = "javascript:unsafe" } }, - .{ .depth = 2, .title = "Safe grandchild", .destination = .{ .internal = .{ .page = 1, .x = 4, .y = 8 } } }, - .{ .depth = 1, .title = "Unresolved child", .destination = .none }, - .{ .depth = 1, .title = "Sibling", .destination = .{ .internal = .{ .page = 2, .x = null, .y = null } } }, - .{ .depth = 0, .title = "Unresolved root", .destination = .none }, - .{ .depth = 0, .title = "Next root", .destination = .{ .internal = .{ .page = 3, .x = null, .y = null } } }, - }; - const bulk = try SectionRows.resolveOrdinals(std.testing.allocator, &entries); - defer std.testing.allocator.free(bulk); - const none = std.math.maxInt(usize); - try std.testing.expectEqualSlices(usize, &.{ 2, none, 2, none, 4, none, 6 }, bulk); - for (entries, 0..) |_, ordinal| { - const single = SectionRows.resolve(&entries, ordinal); - if (bulk[ordinal] == none) { - try std.testing.expect(single == null); - } else { - const destination = SectionRows.usableDestination(entries[bulk[ordinal]].destination) orelse - return error.MissingBoundaryPdfSectionDestination; - try std.testing.expect(single != null); - try std.testing.expect(std.meta.eql(single.?, destination)); - } - } - } - - test "PDF word probe owns quads and text without a selection object" { - if (comptime !enabled) return; - const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "docs/design.pdf", std.testing.allocator, .unlimited); - defer std.testing.allocator.free(bytes); - var document = try Document.openBytes(bytes); - defer document.deinit(); - var found = try document.search(std.testing.allocator, 0, "Pardes"); - defer found.deinit(std.testing.allocator); - const quad = found.quads[0].quad; - const point = Point{ - .x = (quad.ul.x + quad.ur.x + quad.ll.x + quad.lr.x) / 4, - .y = (quad.ul.y + quad.ur.y + quad.ll.y + quad.lr.y) / 4, - }; - - // Exactly one shared probe exercises the primitive used by both UI paths; - // click-vs-hover equivalence belongs to production structure, not a second - // test that can only restate this MuPDF result. - var probe = (try probeWord(&document, std.testing.allocator, 0, point)) orelse - return error.MissingPdfWordProbe; - defer probe.deinit(std.testing.allocator); - try std.testing.expectEqual(@as(usize, 0), probe.page); - try std.testing.expect(probe.quads.len > 0); - try std.testing.expect(std.ascii.findIgnoreCase(probe.text, "Pardes") != null); - } - - test "PDF point mapping restores band origin and fractional placement" { - if (comptime !enabled) return; - const point = pointAtGeometry( - .{ - .src = .{ .x = 20, .y = 10, .w = 40, .h = 20 }, - .dst = .{ .x = 100, .y = 30, .w = 80, .h = 20 }, - }, - -0.5, - 200, - 200, - 80, - 120, - 34, - false, - ) orelse return error.MissingPdfMappedPoint; - // x: src 30; y: band 80 + local src 14. The returned coordinates address - // pixel centers, matching MuPDF's normalized page-space contract. - try std.testing.expectApproxEqAbs(@as(f32, 30.5 / 200.0), point.x, 0.000_001); - try std.testing.expectApproxEqAbs(@as(f32, 94.5 / 200.0), point.y, 0.000_001); - } -}; - const TerminalTests = struct { // Tests asserting raw ANSI indices explicitly select helix, whose palette // is inherited. Native-palette coverage lives in the overlay/eviction and @@ -3636,13 +3488,11 @@ test { _ = @import("output.zig"); _ = @import("column_drag.zig"); _ = @import("mode.zig"); - _ = @import("pdf.zig"); _ = @import("fs_namespace.zig"); _ = @import("hxdiff.zig"); _ = FileTests; _ = OutputTests; _ = ImageTests; - _ = PdfTests; _ = TerminalTests; } @@ -3858,3 +3708,166 @@ test "a terminal keeps its tag and two body rows through a squeeze, a drag and a p.sync(); try std.testing.expect(p.view().rects[term].h >= p.panes[term].?.tag_rows + layout.min_body_rows); } + +// ---- a PDF: a pane of the pdf addon (addons/pdf/pdf.c) ---- + +const PdfAddon = struct { + const addons = @import("test_addons"); + const pdf_lib: []const u8 = if (addons.has_pdf) addons.pdf else ""; + + /// Its jobs, run here and answered as a host's workers answer: an + /// event `land` steps (a core's own update, or an authority's). An + /// authority's host takes them as a session's does (`job`): one run + /// inside the effect would be an update a replica cannot replay. + fn jobs(core: *Pardes, land: anytype) !void { + while (core.nextEffect()) |e| if (e == .job) asked.append(e.job); + while (asked.pop()) |a| { + const j = pardes.host_io.KindJob.take(core, a.pane, a.serial, a.token) orelse continue; + defer j.drop(); + j.run(); + try land.step(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); + } + } + + const Asked = struct { pane: u8, serial: u32, token: u32 }; + var asked: struct { + items: [16]Asked = undefined, + n: usize = 0, + fn append(q: *@This(), j: anytype) void { + if (q.n < q.items.len) q.items[q.n] = .{ .pane = j.pane, .serial = j.serial, .token = j.token }; + q.n += @intFromBool(q.n < q.items.len); + } + fn pop(q: *@This()) ?Asked { + if (q.n == 0) return null; + q.n -= 1; + return q.items[q.n]; + } + } = .{}; + + const host_vtable: pardes.Host.VTable = .{ + .watched = struct { + fn f(_: ?*anyopaque) bool { + return false; + } + }.f, + .job = struct { + fn f(_: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + asked.append(.{ .pane = pane, .serial = serial, .token = token }); + } + }.f, + }; + + test "a client without the pdf addon shows a PDF pane as the session's frames of it, its pages by key, and its wheel goes to the session" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + var screens: R.Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const Land = struct { + a: *R.Authority, + r: *R.Replica, + fn step(l: @This(), ev: pardes.Event) !void { + l.r.confirm(try l.a.local(ev)); + } + }; + const land: Land = .{ .a = a, .r = r }; + var words: [64]pardes.Kind.LoadedWord = undefined; + var why: pardes.Kind.Buf(160) = .{}; + const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); + try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null); + var buf: [4096]u8 = undefined; + const real = try std.Io.Dir.cwd().realPathFile(std.testing.io, "docs/design.pdf", &buf); + var line: [4200]u8 = undefined; + try land.step(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{buf[0..real]}) }); + for (0..3) |_| { + try land.step(.tick); + try jobs(a.core, land); + } + // Its frame crosses: the replica draws the pane as the session + // painted it, its pages placed by key, its page in its tag. + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const deliver = struct { + fn f(au: *R.Authority, sc: *R.Screens, pe: u16, re: *R.Replica) !void { + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| std.testing.allocator.free(m); + msgs.deinit(std.testing.allocator); + } + try sc.flush(au.core, pe, re.view, &msgs); + for (msgs.items) |m| if (re.screen(m)) |reply| { + _ = try sc.take(au.core, pe, reply); + }; + } + }.f; + _ = try a.core.render(arena.allocator()); + // (A kind's prefix is its last frame's, and the tag a step's: a + // Shadow's is the authority's last step's.) + try land.step(.tick); + try deliver(a, &screens, peer, r); + const id = for (r.core().panes, 0..) |slot, i| { + if (slot) |pane| if (pane.addon != null) break i; + } else return error.NoPdfPane; + const shown = try r.core().render(arena.allocator()); + var placed: usize = 0; + for (shown.placements.items) |pl| placed += @intFromBool(pl.pane == id); + try std.testing.expect(placed >= 1); + try std.testing.expect(std.mem.startsWith(u8, try pardes.tagline.tagPrefix(r.core(), r.core().panes[id].?), "[1/16]")); + // A wheel at the replica: the session's kind takes it, and the + // replica shows where the session's strip went. + const before = a.core.panes[id].?.addon.?.thumb_at; + const rect = r.core().view().rects[id]; + for (0..20) |_| r.confirm(try a.take(try r.input(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = rect.x + rect.w / 2, .row = rect.y + rect.h / 2 } }))); + try jobs(a.core, land); + _ = try a.core.render(arena.allocator()); + try std.testing.expect(a.core.panes[id].?.addon.?.thumb_at > before); + try deliver(a, &screens, peer, r); + r.settle(); + try std.testing.expect(r.diverged == null); + } + + test "a PDF pane whose file changes on disk opens again from it at its page, its fit kept" { + if (!addons.has_pdf) return error.SkipZigTest; + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "live.pdf", std.testing.io, .{}); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .tty_only = true }); + defer p.deinit(); + pardes.Addon.perform(p, .load, 0, pdf_lib); + var line: [4200]u8 = undefined; + try std.testing.expect(p.executeBuiltinLine(p.view().active, try std.fmt.bufPrint(&line, "Look {s}/live.pdf:5", .{dir}))); + const id = p.view().active; + try std.testing.expect(p.executeBuiltinLine(id, "PdfFit height")); + const Self = struct { + core: *Pardes, + fn step(l: @This(), ev: pardes.Event) !void { + l.core.update(ev); + } + }; + p.update(.tick); + try jobs(p, Self{ .core = p }); + // Another file under its name (an outline's three pages): opened + // again, on the page it can still be on, as height-fitted. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "live.pdf", .data = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", p.scratch.allocator(), .unlimited) }); + try std.testing.expect(p.reloadWatchedFile(@intCast(id), &.{})); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + try std.testing.expectEqualStrings("[3/3]", p.panes[id].?.addon.?.prefix.slice()); + var fields: [256]u8 = undefined; + try std.testing.expect(std.mem.indexOf(u8, pardes.Kind.ctlRead(p.panes[id].?, &fields), "fit:height") != null); + } +}; + +test { + _ = PdfAddon; +} |
