// @vitest-environment jsdom import type { LegendListRef } from "@legendapp/list/react"; import { act, type ComponentProps } from "react"; import { createRoot, type Root } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; import { useThreadFindNavigation } from "./useThreadFindNavigation"; const renders = vi.fn(); function Probe(props: Parameters[0]) { renders(); useThreadFindNavigation(props); return null; } let root: Root; let host: HTMLDivElement; let container: HTMLDivElement; let rect: DOMRect; const scrollToIndex = vi.fn(); const scrollToOffset = vi.fn(); let props: ComponentProps; beforeEach(() => { vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); vi.stubGlobal("CSS", { highlights: new Map() }); vi.stubGlobal( "Highlight", class extends Set { constructor(...ranges: Range[]) { super(ranges); } }, ); rect = new DOMRect(0, 200, 40, 20); Object.defineProperty(Range.prototype, "getBoundingClientRect", { configurable: true, value: () => rect, }); host = document.createElement("div"); container = document.createElement("div"); container.innerHTML = '

COD4 and COD4

'; container.getBoundingClientRect = () => new DOMRect(0, 0, 800, 600); document.body.append(host, container); root = createRoot(host); scrollToIndex.mockResolvedValue(); props = { container, query: "COD4", match: { entryId: "message", runId: null, occurrence: 0 }, navigationId: 0, rowIndex: 0, entries: [{ id: "message" }], listRef: { current: { scrollToIndex, scrollToOffset, getState: () => ({ scroll: 300 }), } as unknown as LegendListRef, }, contentInsetEndAdjustment: 100, listReady: true, }; }); afterEach(async () => { await act(async () => root.unmount()); host.remove(); container.remove(); Reflect.deleteProperty(Range.prototype, "getBoundingClientRect"); vi.clearAllMocks(); vi.unstubAllGlobals(); }); describe("find result navigation", () => { it("retargets a pending first jump when progressive history moves the matching row", async () => { container.replaceChildren(); let first!: () => void; let second!: () => void; scrollToIndex .mockImplementationOnce( () => new Promise((resolve) => { first = resolve; }), ) .mockImplementationOnce( () => new Promise((resolve) => { second = resolve; }), ); await act(async () => root.render()); await act(async () => root.render(), ); expect(scrollToIndex.mock.calls.map(([request]) => request.index)).toEqual([2, 6]); await act(async () => first()); expect(scrollToOffset).not.toHaveBeenCalled(); await act(async () => { container.innerHTML = '

COD4

'; rect = new DOMRect(0, 450, 40, 20); second(); await new Promise((resolve) => requestAnimationFrame(() => resolve())); }); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); }); it("keeps the highlighted text at the same position when progressive activity arrives", async () => { const historyControls = { hasMoreHistory: true, loading: true, error: null, onLoadEarlier: vi.fn(), }; await act(async () => root.render()); rect = new DOMRect(0, 368, 40, 20); scrollToOffset.mockImplementationOnce(async () => { rect = new DOMRect(0, 200, 40, 20); }); await act(async () => root.render( , ), ); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 468, animated: false }); expect(scrollToIndex).not.toHaveBeenCalled(); }); it("respects scrolling away from a partial match before activity arrives", async () => { const historyControls = { hasMoreHistory: true, loading: true, error: null, onLoadEarlier: vi.fn(), }; await act(async () => root.render()); rect = new DOMRect(0, -600, 40, 20); container.dispatchEvent(new Event("scroll")); await act(async () => root.render( , ), ); expect(scrollToOffset).not.toHaveBeenCalled(); }); it("reveals settled virtual text without another timeline render", async () => { let complete!: () => void; scrollToIndex.mockImplementationOnce( () => new Promise((resolve) => { complete = resolve; }), ); rect = new DOMRect(0, 1800, 40, 20); await act(async () => root.render()); expect(renders).toHaveBeenCalledTimes(1); expect(scrollToOffset).not.toHaveBeenCalled(); rect = new DOMRect(0, 450, 40, 20); await act(async () => { complete(); await new Promise((resolve) => requestAnimationFrame(() => resolve())); }); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); expect(renders).toHaveBeenCalledTimes(1); }); it("ignores an older scroll completion after navigation moves to another occurrence", async () => { let first!: () => void; let second!: () => void; scrollToIndex .mockImplementationOnce( () => new Promise((resolve) => { first = resolve; }), ) .mockImplementationOnce( () => new Promise((resolve) => { second = resolve; }), ); rect = new DOMRect(0, 1800, 40, 20); await act(async () => root.render()); await act(async () => root.render( , ), ); rect = new DOMRect(0, 450, 40, 20); await act(async () => first()); expect(scrollToOffset).not.toHaveBeenCalled(); await act(async () => { second(); await new Promise((resolve) => requestAnimationFrame(() => resolve())); }); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); }); it("loads history until a distant match can be revealed in the conversation", async () => { const onLoadEarlier = vi.fn(); const historyControls = { hasMoreHistory: true, loading: false, error: null, onLoadEarlier }; const pending = { ...props, rowIndex: -1, entries: [{ id: "recent" }], historyControls }; await act(async () => root.render()); expect(onLoadEarlier).toHaveBeenCalledTimes(1); expect(scrollToIndex).not.toHaveBeenCalled(); await act(async () => root.render(), ); expect(onLoadEarlier).toHaveBeenCalledTimes(1); await act(async () => root.render(), ); expect(onLoadEarlier).toHaveBeenCalledTimes(2); rect = new DOMRect(0, 1800, 40, 20); scrollToIndex.mockImplementationOnce(async () => { rect = new DOMRect(0, 200, 40, 20); }); await act(async () => root.render()); expect(onLoadEarlier).toHaveBeenCalledTimes(2); expect(scrollToIndex).toHaveBeenCalledTimes(1); }); it("waits for list bootstrap before positioning the first result", async () => { rect = new DOMRect(0, 1800, 40, 20); await act(async () => root.render()); expect(scrollToIndex).not.toHaveBeenCalled(); expect(scrollToOffset).not.toHaveBeenCalled(); scrollToIndex.mockImplementationOnce(async () => { rect = new DOMRect(0, 200, 40, 20); }); await act(async () => root.render()); expect(scrollToIndex).toHaveBeenCalledTimes(1); expect(scrollToOffset).not.toHaveBeenCalled(); }); it("leaves the scroll position unchanged between already visible occurrences", async () => { await act(async () => root.render()); await act(async () => root.render( , ), ); expect(scrollToIndex).not.toHaveBeenCalled(); expect(scrollToOffset).not.toHaveBeenCalled(); const highlight = CSS.highlights.get("t3-thread-find-active"); const ranges = [...(highlight as unknown as Set)]; expect(ranges).toHaveLength(1); expect(ranges[0]?.startOffset).toBe(9); expect(ranges[0]?.toString()).toBe("COD4"); }); it("reveals the same match again when search restarts after scrolling away", async () => { await act(async () => root.render()); rect = new DOMRect(0, -1800, 40, 20); props.listRef.current = { scrollToIndex, scrollToOffset, getState: () => ({ scroll: 2400 }), } as unknown as LegendListRef; await act(async () => root.render()); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 504, animated: false }); expect(scrollToIndex).not.toHaveBeenCalled(); expect( Array.from(CSS.highlights.get("t3-thread-find-active") ?? [], (range) => range.toString()), ).toEqual(["COD4"]); }); it("moves only far enough to reveal text below the composer", async () => { await act(async () => root.render()); rect = new DOMRect(0, 450, 40, 20); await act(async () => root.render( , ), ); expect(scrollToIndex).not.toHaveBeenCalled(); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); }); it("moves only far enough to reveal text above the viewport margin", async () => { await act(async () => root.render()); rect = new DOMRect(0, 40, 40, 20); await act(async () => root.render( , ), ); expect(scrollToIndex).not.toHaveBeenCalled(); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 244, animated: false }); }); it("keeps the selected text visible when newly measured rows shift during scrolling", async () => { let scroll = 300; props.listRef.current = { scrollToIndex, scrollToOffset, getState: () => ({ scroll }), } as unknown as LegendListRef; scrollToOffset .mockImplementationOnce(async ({ offset }) => { scroll = offset; // Newly mounted row measurements offset the first scroll movement. }) .mockImplementationOnce(async ({ offset }) => { scroll = offset; rect = new DOMRect(0, 200, 40, 20); }); await act(async () => root.render()); rect = new DOMRect(0, 450, 40, 20); await act(async () => root.render( , ), ); await act(async () => { await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve())), ); }); expect(rect.top).toBe(200); expect(scrollToOffset.mock.calls).toEqual([ [{ offset: 366, animated: false }], [{ offset: 432, animated: false }], ]); expect(renders).toHaveBeenCalledTimes(2); }); it("waits for a new context window to settle before revealing an offscreen result", async () => { rect = new DOMRect(0, 1800, 40, 20); scrollToIndex.mockImplementationOnce(async () => { rect = new DOMRect(0, 450, 40, 20); }); await act(async () => { root.render(); }); await act(async () => { await new Promise((resolve) => requestAnimationFrame(() => resolve())); }); expect(scrollToIndex).toHaveBeenCalledTimes(1); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); }); it("materializes a virtualized message before revealing its occurrence", async () => { container.replaceChildren(); await act(async () => root.render()); expect(scrollToIndex).toHaveBeenCalledExactlyOnceWith({ index: 4, animated: false, viewOffset: 96, }); expect(scrollToOffset).not.toHaveBeenCalled(); rect = new DOMRect(0, 450, 40, 20); await act(async () => { container.innerHTML = '

COD4

'; await new Promise((resolve) => requestAnimationFrame(() => resolve())); }); expect(scrollToIndex).toHaveBeenCalledTimes(1); expect(scrollToOffset).toHaveBeenCalledExactlyOnceWith({ offset: 366, animated: false }); }); });