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

useThreadFindNavigation.browser.test.tsx

390 lines13.6 KB

1import type { LegendListRef } from "@legendapp/list/react";
2import { act, type ComponentProps } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5import { useThreadFindNavigation } from "./useThreadFindNavigation";
6​
7const renders = vi.fn();
8function Probe(props: Parameters<typeof useThreadFindNavigation>[0]) {
9 renders();
10 useThreadFindNavigation(props);
11 return null;
13​
14let root: Root;
15let host: HTMLDivElement;
16let container: HTMLDivElement;
17let rect: DOMRect;
18const scrollToIndex = vi.fn<LegendListRef["scrollToIndex"]>();
19const scrollToOffset = vi.fn<LegendListRef["scrollToOffset"]>();
20let props: ComponentProps<typeof Probe>;
21​
22beforeEach(() => {
23 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
24 vi.stubGlobal("CSS", { highlights: new Map() });
25 vi.stubGlobal(
26 "Highlight",
27 class extends Set<Range> {
28 constructor(...ranges: Range[]) {
29 super(ranges);
30 }
31 },
32 );
33 rect = new DOMRect(0, 200, 40, 20);
34 Object.defineProperty(Range.prototype, "getBoundingClientRect", {
35 configurable: true,
36 value: () => rect,
37 });
38 host = document.createElement("div");
39 container = document.createElement("div");
40 container.innerHTML =
41 '<div data-timeline-row-id="message"><p data-thread-find-text>COD4 and COD4</p></div>';
42 container.getBoundingClientRect = () => new DOMRect(0, 0, 800, 600);
43 document.body.append(host, container);
44 root = createRoot(host);
45 scrollToIndex.mockResolvedValue();
46 props = {
47 container,
48 query: "COD4",
49 match: { entryId: "message", runId: null, occurrence: 0 },
50 navigationId: 0,
51 rowIndex: 0,
52 entries: [{ id: "message" }],
53 listRef: {
54 current: {
55 scrollToIndex,
56 scrollToOffset,
57 getState: () => ({ scroll: 300 }),
58 } as unknown as LegendListRef,
59 },
60 contentInsetEndAdjustment: 100,
61 listReady: true,
62 };
63});
64​
65afterEach(async () => {
66 await act(async () => root.unmount());
67 host.remove();
68 container.remove();
69 Reflect.deleteProperty(Range.prototype, "getBoundingClientRect");
70 vi.clearAllMocks();
71 vi.unstubAllGlobals();
72});
73​
74describe("find result navigation", () => {
75 it("retargets a pending first jump when progressive history moves the matching row", async () => {
76 container.replaceChildren();
77 let first!: () => void;
78 let second!: () => void;
79 scrollToIndex
80 .mockImplementationOnce(
81 () =>
82 new Promise<void>((resolve) => {
83 first = resolve;
84 }),
85 )
86 .mockImplementationOnce(
87 () =>
88 new Promise<void>((resolve) => {
89 second = resolve;
90 }),
91 );
92 await act(async () => root.render(<Probe {...props} rowIndex={2} />));
93 await act(async () =>
94 root.render(<Probe {...props} rowIndex={6} entries={[{ id: "tool" }, ...props.entries]} />),
95 );
96 expect(scrollToIndex.mock.calls.map(([request]) => request.index)).toEqual([2, 6]);
97 await act(async () => first());
98 expect(scrollToOffset).not.toHaveBeenCalled();
99 await act(async () => {
100 container.innerHTML =
101 '<div data-timeline-row-id="message"><p data-thread-find-text>COD4</p></div>';
102 rect = new DOMRect(0, 450, 40, 20);
103 second();
104 await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
105 });
106 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
107 });
108​
109 it("keeps the highlighted text at the same position when progressive activity arrives", async () => {
110 const historyControls = {
111 hasMoreHistory: true,
112 loading: true,
113 error: null,
114 onLoadEarlier: vi.fn(),
115 };
116 await act(async () => root.render(<Probe {...props} historyControls={historyControls} />));
117 rect = new DOMRect(0, 368, 40, 20);
118 scrollToOffset.mockImplementationOnce(async () => {
119 rect = new DOMRect(0, 200, 40, 20);
120 });
121 await act(async () =>
122 root.render(
123 <Probe
124 {...props}
125 entries={[{ id: "tool" }, { id: "message" }]}
126 historyControls={{ ...historyControls, loading: false }}
127 />,
128 ),
129 );
130 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 468, animated: false });
131 expect(scrollToIndex).not.toHaveBeenCalled();
132 });
133​
134 it("respects scrolling away from a partial match before activity arrives", async () => {
135 const historyControls = {
136 hasMoreHistory: true,
137 loading: true,
138 error: null,
139 onLoadEarlier: vi.fn(),
140 };
141 await act(async () => root.render(<Probe {...props} historyControls={historyControls} />));
142 rect = new DOMRect(0, -600, 40, 20);
143 container.dispatchEvent(new Event("scroll"));
144 await act(async () =>
145 root.render(
146 <Probe
147 {...props}
148 entries={[{ id: "tool" }, { id: "message" }]}
149 historyControls={{ ...historyControls, loading: false }}
150 />,
151 ),
152 );
153 expect(scrollToOffset).not.toHaveBeenCalled();
154 });
155​
156 it("reveals settled virtual text without another timeline render", async () => {
157 let complete!: () => void;
158 scrollToIndex.mockImplementationOnce(
159 () =>
160 new Promise<void>((resolve) => {
161 complete = resolve;
162 }),
163 );
164 rect = new DOMRect(0, 1800, 40, 20);
165 await act(async () => root.render(<Probe {...props} />));
166 expect(renders).toHaveBeenCalledTimes(1);
167 expect(scrollToOffset).not.toHaveBeenCalled();
168 rect = new DOMRect(0, 450, 40, 20);
169 await act(async () => {
170 complete();
171 await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
172 });
173 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
174 expect(renders).toHaveBeenCalledTimes(1);
175 });
176​
177 it("ignores an older scroll completion after navigation moves to another occurrence", async () => {
178 let first!: () => void;
179 let second!: () => void;
180 scrollToIndex
181 .mockImplementationOnce(
182 () =>
183 new Promise<void>((resolve) => {
184 first = resolve;
185 }),
186 )
187 .mockImplementationOnce(
188 () =>
189 new Promise<void>((resolve) => {
190 second = resolve;
191 }),
192 );
193 rect = new DOMRect(0, 1800, 40, 20);
194 await act(async () => root.render(<Probe {...props} />));
195 await act(async () =>
196 root.render(
197 <Probe
198 {...props}
199 navigationId={1}
200 match={{ entryId: "message", runId: null, occurrence: 1 }}
201 />,
202 ),
203 );
204 rect = new DOMRect(0, 450, 40, 20);
205 await act(async () => first());
206 expect(scrollToOffset).not.toHaveBeenCalled();
207 await act(async () => {
208 second();
209 await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
210 });
211 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
212 });
213​
214 it("loads history until a distant match can be revealed in the conversation", async () => {
215 const onLoadEarlier = vi.fn();
216 const historyControls = { hasMoreHistory: true, loading: false, error: null, onLoadEarlier };
217 const pending = { ...props, rowIndex: -1, entries: [{ id: "recent" }], historyControls };
218 await act(async () => root.render(<Probe {...pending} />));
219 expect(onLoadEarlier).toHaveBeenCalledTimes(1);
220 expect(scrollToIndex).not.toHaveBeenCalled();
221 await act(async () =>
222 root.render(<Probe {...pending} historyControls={{ ...historyControls, loading: true }} />),
223 );
224 expect(onLoadEarlier).toHaveBeenCalledTimes(1);
225 await act(async () =>
226 root.render(<Probe {...pending} entries={[{ id: "older" }, ...pending.entries]} />),
227 );
228 expect(onLoadEarlier).toHaveBeenCalledTimes(2);
229 rect = new DOMRect(0, 1800, 40, 20);
230 scrollToIndex.mockImplementationOnce(async () => {
231 rect = new DOMRect(0, 200, 40, 20);
232 });
233 await act(async () => root.render(<Probe {...props} historyControls={historyControls} />));
234 expect(onLoadEarlier).toHaveBeenCalledTimes(2);
235 expect(scrollToIndex).toHaveBeenCalledTimes(1);
236 });
237​
238 it("waits for list bootstrap before positioning the first result", async () => {
239 rect = new DOMRect(0, 1800, 40, 20);
240 await act(async () => root.render(<Probe {...props} listReady={false} />));
241 expect(scrollToIndex).not.toHaveBeenCalled();
242 expect(scrollToOffset).not.toHaveBeenCalled();
243 scrollToIndex.mockImplementationOnce(async () => {
244 rect = new DOMRect(0, 200, 40, 20);
245 });
246 await act(async () => root.render(<Probe {...props} />));
247 expect(scrollToIndex).toHaveBeenCalledTimes(1);
248 expect(scrollToOffset).not.toHaveBeenCalled();
249 });
250 it("leaves the scroll position unchanged between already visible occurrences", async () => {
251 await act(async () => root.render(<Probe {...props} />));
252 await act(async () =>
253 root.render(
254 <Probe
255 {...props}
256 navigationId={1}
257 match={{ entryId: "message", runId: null, occurrence: 1 }}
258 />,
259 ),
260 );
261 expect(scrollToIndex).not.toHaveBeenCalled();
262 expect(scrollToOffset).not.toHaveBeenCalled();
263 const highlight = CSS.highlights.get("t3-thread-find-active");
264 const ranges = [...(highlight as unknown as Set<Range>)];
265 expect(ranges).toHaveLength(1);
266 expect(ranges[0]?.startOffset).toBe(9);
267 expect(ranges[0]?.toString()).toBe("COD4");
268 });
269​
270 it("reveals the same match again when search restarts after scrolling away", async () => {
271 await act(async () => root.render(<Probe {...props} />));
272 rect = new DOMRect(0, -1800, 40, 20);
273 props.listRef.current = {
274 scrollToIndex,
275 scrollToOffset,
276 getState: () => ({ scroll: 2400 }),
277 } as unknown as LegendListRef;
278 await act(async () => root.render(<Probe {...props} navigationId={1} />));
279 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 504, animated: false });
280 expect(scrollToIndex).not.toHaveBeenCalled();
281 expect(
282 Array.from(CSS.highlights.get("t3-thread-find-active") ?? [], (range) => range.toString()),
283 ).toEqual(["COD4"]);
284 });
285​
286 it("moves only far enough to reveal text below the composer", async () => {
287 await act(async () => root.render(<Probe {...props} />));
288 rect = new DOMRect(0, 450, 40, 20);
289 await act(async () =>
290 root.render(
291 <Probe
292 {...props}
293 navigationId={1}
294 match={{ entryId: "message", runId: null, occurrence: 1 }}
295 />,
296 ),
297 );
298 expect(scrollToIndex).not.toHaveBeenCalled();
299 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
300 });
301​
302 it("moves only far enough to reveal text above the viewport margin", async () => {
303 await act(async () => root.render(<Probe {...props} />));
304 rect = new DOMRect(0, 40, 40, 20);
305 await act(async () =>
306 root.render(
307 <Probe
308 {...props}
309 navigationId={1}
310 match={{ entryId: "message", runId: null, occurrence: 1 }}
311 />,
312 ),
313 );
314 expect(scrollToIndex).not.toHaveBeenCalled();
315 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 244, animated: false });
316 });
317​
318 it("keeps the selected text visible when newly measured rows shift during scrolling", async () => {
319 let scroll = 300;
320 props.listRef.current = {
321 scrollToIndex,
322 scrollToOffset,
323 getState: () => ({ scroll }),
324 } as unknown as LegendListRef;
325 scrollToOffset
326 .mockImplementationOnce(async ({ offset }) => {
327 scroll = offset;
328 })
329 .mockImplementationOnce(async ({ offset }) => {
330 scroll = offset;
331 rect = new DOMRect(0, 200, 40, 20);
332 });
333 await act(async () => root.render(<Probe {...props} />));
334 rect = new DOMRect(0, 450, 40, 20);
335 await act(async () =>
336 root.render(
337 <Probe
338 {...props}
339 navigationId={1}
340 match={{ entryId: "message", runId: null, occurrence: 1 }}
341 />,
342 ),
343 );
344 await act(async () => {
345 await new Promise<void>((resolve) =>
346 requestAnimationFrame(() => requestAnimationFrame(() => resolve())),
347 );
348 });
349 expect(rect.top).toBe(200);
350 expect(scrollToOffset.mock.calls).toEqual([
351 [{ offset: 366, animated: false }],
352 [{ offset: 432, animated: false }],
353 ]);
354 expect(renders).toHaveBeenCalledTimes(2);
355 });
356​
357 it("waits for a new context window to settle before revealing an offscreen result", async () => {
358 rect = new DOMRect(0, 1800, 40, 20);
359 scrollToIndex.mockImplementationOnce(async () => {
360 rect = new DOMRect(0, 450, 40, 20);
361 });
362 await act(async () => {
363 root.render(<Probe {...props} />);
364 });
365 await act(async () => {
366 await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
367 });
368 expect(scrollToIndex).toHaveBeenCalledTimes(1);
369 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
370 });
371​
372 it("materializes a virtualized message before revealing its occurrence", async () => {
373 container.replaceChildren();
374 await act(async () => root.render(<Probe {...props} rowIndex={4} />));
375 expect(scrollToIndex).toHaveBeenCalledExactlyOnceWith({
376 index: 4,
377 animated: false,
378 viewOffset: 96,
379 });
380 expect(scrollToOffset).not.toHaveBeenCalled();
381 rect = new DOMRect(0, 450, 40, 20);
382 await act(async () => {
383 container.innerHTML =
384 '<div data-timeline-row-id="message"><p data-thread-find-text>COD4</p></div>';
385 await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
386 });
387 expect(scrollToIndex).toHaveBeenCalledTimes(1);
388 expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false });
389 });
390});