Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pdfLayout.test.ts

144 lines4.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 currentPdfPage,
4 pdfFindIncludes,
5 pdfFindMatchRuns,
6 pdfPageOffsets,
7 pdfPageScrollTop,
8 pdfPageText,
9 pdfPageWidth,
10 stepPdfZoom,
11 visiblePdfPages,
12} from "./pdfLayout";
13​
14describe("pdfPageWidth", () => {
15 it("fits the page to the viewport minus its gutters until a zoom is chosen", () => {
16 expect(pdfPageWidth(500, 612, null)).toEqual({ width: 468, scale: 468 / 612 });
17 expect(pdfPageWidth(500, 612, 1.5)).toEqual({ width: 918, scale: 1.5 });
18 });
19​
20 it("never collapses below one pixel in a tiny viewport", () => {
21 expect(pdfPageWidth(10, 612, null).width).toBe(1);
22 });
23});
24​
25describe("stepPdfZoom", () => {
26 it("steps by a quarter and clamps to 25%..300%", () => {
27 expect(stepPdfZoom(1, 1)).toBe(1.25);
28 expect(stepPdfZoom(1, -1)).toBe(0.75);
29 expect(stepPdfZoom(2.9, 1)).toBe(3);
30 expect(stepPdfZoom(0.3, -1)).toBe(0.25);
31 });
32​
33 it("starts from the fit-width scale when no zoom is set", () => {
34 const { scale } = pdfPageWidth(500, 612, null);
35 expect(stepPdfZoom(scale, 1)).toBeCloseTo(468 / 612 + 0.25);
36 });
37});
38​
39describe("mixed-height page layout", () => {
40 // A short banner page, a letter page, then a page half as tall as it is wide.
41 const ratios = [140 / 612, 792 / 612, 306 / 612];
42 const width = 612;
43 const offsets = pdfPageOffsets(ratios, width);
44​
45 it("stacks pages with a gap before, between, and after them", () => {
46 expect(offsets).toEqual([16, 172, 980, 1302]);
47 });
48​
49 it("reports the page whose bottom is past the top gutter as current", () => {
50 expect(currentPdfPage(offsets, 3, 0)).toBe(0);
51 expect(currentPdfPage(offsets, 3, 154)).toBe(0);
52 expect(currentPdfPage(offsets, 3, 155)).toBe(1);
53 expect(currentPdfPage(offsets, 3, 962)).toBe(1);
54 expect(currentPdfPage(offsets, 3, 964)).toBe(2);
55 });
56​
57 it("jumps to each page so that page becomes current", () => {
58 for (const index of [0, 1, 2])
59 expect(currentPdfPage(offsets, 3, pdfPageScrollTop(offsets, index))).toBe(index);
60 expect(pdfPageScrollTop(offsets, 7)).toBe(0);
61 });
62​
63 it("mounts pages within a viewport above and two below", () => {
64 const indexes = (scrollTop: number, height: number) =>
65 visiblePdfPages(ratios, offsets, scrollTop, height).map((page) => page.index);
66 expect(indexes(0, 100)).toEqual([0, 1]);
67 expect(indexes(1100, 100)).toEqual([2]);
68 expect(indexes(1100, 900)).toEqual([1, 2]);
69 expect(visiblePdfPages(ratios, offsets, 0, 100)[0]).toEqual({ index: 0, ratio: ratios[0] });
70 });
71​
72 it("scales offsets with the page width when zoomed", () => {
73 expect(pdfPageOffsets(ratios, width * 2)).toEqual([16, 312, 1912, 2540]);
74 });
75});
76​
77describe("pdfPageText", () => {
78 it("joins text items, adds line ends, and skips marked content", () => {
79 expect(
80 pdfPageText([
81 { str: "Quarter", hasEOL: false },
82 { type: "beginMarkedContent" },
83 { str: "ly outlook", hasEOL: true },
84 { str: "Next", hasEOL: false },
85 ]),
86 ).toBe("Quarterly outlook\nNext");
87 });
88});
89​
90describe("pdfFindMatchRuns", () => {
91 it("highlights every run a match spans when a word is split across spans", () => {
92 expect(pdfFindMatchRuns(["Quarter", "ly outlook", " for Q3"], "quarterly OUTLOOK")).toEqual([
93 true,
94 true,
95 false,
96 ]);
97 });
98​
99 it("highlights each separate match", () => {
100 expect(pdfFindMatchRuns(["risk", " and ", "budget", " ", "Risk"], "risk")).toEqual([
101 true,
102 false,
103 false,
104 false,
105 true,
106 ]);
107 });
108​
109 it("matches a space in the query across a line end", () => {
110 expect(
111 pdfFindMatchRuns(["Q3 quarterly", "\n", "outlook", " ahead"], "quarterly outlook"),
112 ).toEqual([true, true, true, false]);
113 });
114​
115 it("matches a word hyphenated across a line end", () => {
116 expect(pdfFindMatchRuns(["the quar-", "\n", "terly report"], "quarterly")).toEqual([
117 true,
118 false,
119 true,
120 ]);
121 });
122​
123 it("drops a line-end hyphen after a letter outside the BMP", () => {
124 expect(pdfFindMatchRuns(["\u{10400}-", "\n", "\u{10401}"], "\u{10400}\u{10401}")).toEqual([
125 true,
126 false,
127 true,
128 ]);
129 });
130​
131 it("finds the same pages the highlights do", () => {
132 const page = "Q3 quarterly\noutlook ahead, the quar-\nterly report";
133 expect(pdfFindIncludes(page, "Quarterly OUTLOOK")).toBe(true);
134 expect(pdfFindIncludes(page, "quarterly report")).toBe(true);
135 expect(pdfFindIncludes(page, "bud-get")).toBe(false);
136 expect(pdfFindIncludes(page, " ")).toBe(false);
137 });
138​
139 it("highlights nothing for a blank or missing query", () => {
140 expect(pdfFindMatchRuns(["a b", "c"], " ")).toEqual([false, false]);
141 expect(pdfFindMatchRuns(["a b", "c"], "")).toEqual([false, false]);
142 expect(pdfFindMatchRuns(["a b", "c"], "zzz")).toEqual([false, false]);
143 });
144});