@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 176 lines 6.65 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2
3 import { render } from "../../../test/render";
4
5 import { collectThreadFindRanges, useThreadFindHighlights } from "./threadFindHighlights";
6
7 function row(html: string) {
8 const container = document.createElement("div");
9 container.innerHTML = `<div data-timeline-row-id="row"><div data-thread-find-text>${html}</div></div>`;
10 return container;
11 }
12
13 describe("collectThreadFindRanges", () => {
14 it("finds matches split across inline token spans, in order", () => {
15 const container = row(
16 "<pre><code><span>con</span><span>st</span> a = <span>c</span>onst; const</code></pre>",
17 );
18 const ranges = collectThreadFindRanges(container, "const");
19 expect(ranges.map((match) => [match.occurrence, match.range.toString()])).toEqual([
20 [0, "const"],
21 [1, "const"],
22 [2, "const"],
23 ]);
24 });
25 });
26
27 function Highlights(props: Parameters<typeof useThreadFindHighlights>[0]) {
28 useThreadFindHighlights(props);
29 return null;
30 }
31
32 function mountTimeline(html: string) {
33 const container = document.createElement("div");
34 container.style.cssText = "height: 200px; width: 300px; overflow: auto";
35 container.innerHTML = html;
36 document.body.append(container);
37 return { container, [Symbol.dispose]: () => container.remove() };
38 }
39
40 const painted = () => [...(CSS.highlights.get("t3-thread-find") ?? [])] as Range[];
41 const paintedIn = (element: Element) =>
42 painted().filter((range) => element.contains(range.startContainer)).length;
43
44 it("paints only matches near the viewport plus the active one, in a row with more matches than a call can take as arguments", async () => {
45 const count = 200_000;
46 const onActiveRange = vi.fn<(range: Range | null) => void>();
47 using timeline = mountTimeline(
48 `<div data-timeline-row-id="row"><div data-thread-find-text>${"e x".repeat(count)}</div></div>`,
49 );
50 await using _screen = await render(
51 <Highlights
52 container={timeline.container}
53 query="e"
54 activeRowId="row"
55 activeOccurrence={count - 1}
56 onActiveRange={onActiveRange}
57 />,
58 );
59 const visible = painted();
60 expect(visible.length).toBeGreaterThan(0);
61 expect(visible.length).toBeLessThan(5_000);
62 expect(Math.max(...visible.map((range) => range.startOffset))).toBeLessThan(count);
63 expect(CSS.highlights.get("t3-thread-find-active")?.size).toBe(1);
64 expect(onActiveRange.mock.lastCall?.[0]?.startOffset).toBe((count - 1) * 3);
65
66 timeline.container.scrollTop = timeline.container.scrollHeight;
67 await vi.waitFor(() =>
68 expect(Math.max(...painted().map((range) => range.startOffset))).toBe((count - 2) * 3),
69 );
70 expect(painted().length).toBeLessThan(5_000);
71 });
72
73 it("paints every match near the viewport when cells lay text out in a different order than the DOM", async () => {
74 using timeline = mountTimeline(
75 `<div data-timeline-row-id="row"><div data-thread-find-text><table style="width: 100%"><tr>
76 <td style="vertical-align: top">${"e x ".repeat(5_000)}</td>
77 <td style="vertical-align: top">${"e x ".repeat(5_000)}</td>
78 </tr></table></div></div>`,
79 );
80 const cells = [...timeline.container.querySelectorAll("td")];
81 const ranges = collectThreadFindRanges(timeline.container, "e");
82 const key = (range: Range) =>
83 `${cells.indexOf(range.startContainer.parentElement as HTMLTableCellElement)}:${range.startOffset}`;
84 const missing = () => {
85 const viewport = timeline.container.getBoundingClientRect();
86 const [top, bottom] = [viewport.top - viewport.height, viewport.bottom + viewport.height];
87 const shown = new Set(painted().map(key));
88 return ranges.filter(({ range }) => {
89 const rect = range.getBoundingClientRect();
90 return rect.bottom >= top && rect.top <= bottom && !shown.has(key(range));
91 }).length;
92 };
93 await using _screen = await render(
94 <Highlights
95 container={timeline.container}
96 query="e"
97 activeRowId={null}
98 activeOccurrence={0}
99 onActiveRange={() => {}}
100 />,
101 );
102 expect(missing()).toBe(0);
103
104 timeline.container.scrollTop = 1_000;
105 await vi.waitFor(() => expect(missing()).toBe(0));
106 expect(painted().length).toBeLessThan(ranges.length / 2);
107 });
108
109 it("skips a hidden block's matches without measuring each one", async () => {
110 using timeline = mountTimeline(
111 `<div data-timeline-row-id="row"><div data-thread-find-text>
112 <div style="display: none">${"e x ".repeat(10_000)}</div><p>e</p>
113 </div></div>`,
114 );
115 const visible = timeline.container.querySelector("p")!;
116 using measure = vi.spyOn(Range.prototype, "getBoundingClientRect");
117 await using _screen = await render(
118 <Highlights
119 container={timeline.container}
120 query="e"
121 activeRowId={null}
122 activeOccurrence={0}
123 onActiveRange={() => {}}
124 />,
125 );
126 expect([painted().length, paintedIn(visible)]).toEqual([1, 1]);
127 expect(measure.mock.calls.length).toBeLessThan(100);
128 });
129
130 it("registers a row's matches only while it is near the viewport, and again after its text changes", async () => {
131 using timeline = mountTimeline(
132 `<div data-timeline-row-id="near"><div data-thread-find-text>e e e</div></div>
133 <div style="height: 5000px"></div>
134 <div data-timeline-row-id="far"><div data-thread-find-text>e e</div></div>`,
135 );
136 const [near, far] = [...timeline.container.querySelectorAll("[data-thread-find-text]")];
137 const onActiveRange = vi.fn<(range: Range | null) => void>();
138 await using _screen = await render(
139 <Highlights
140 container={timeline.container}
141 query="e"
142 activeRowId="far"
143 activeOccurrence={1}
144 onActiveRange={onActiveRange}
145 />,
146 );
147 expect([paintedIn(near!), paintedIn(far!)]).toEqual([3, 0]);
148 expect(CSS.highlights.get("t3-thread-find-active")?.size).toBe(1);
149 expect(far!.contains(onActiveRange.mock.lastCall?.[0]?.startContainer ?? null)).toBe(true);
150
151 near!.append(" e");
152 await vi.waitFor(() => expect(paintedIn(near!)).toBe(4));
153
154 timeline.container.scrollTop = timeline.container.scrollHeight;
155 await vi.waitFor(() => expect([paintedIn(near!), paintedIn(far!)]).toEqual([0, 1]));
156 });
157
158 it("paints a row's matches once the hidden wrapper around it is revealed", async () => {
159 using timeline = mountTimeline(
160 `<div id="wrap" hidden><div data-timeline-row-id="row"><div data-thread-find-text>e e</div></div></div>`,
161 );
162 const text = timeline.container.querySelector("[data-thread-find-text]")!;
163 await using _screen = await render(
164 <Highlights
165 container={timeline.container}
166 query="e"
167 activeRowId={null}
168 activeOccurrence={0}
169 onActiveRange={() => {}}
170 />,
171 );
172 expect(paintedIn(text)).toBe(0);
173
174 timeline.container.querySelector("#wrap")!.removeAttribute("hidden");
175 await vi.waitFor(() => expect(paintedIn(text)).toBe(2));
176 });