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

ThreadFindProvider.browser.test.tsx

133 lines4.4 KB

1import { act, createRef, useContext, useState, type ReactNode } from "react";
2import { createRoot } from "react-dom/client";
3import { afterEach, expect, it, vi } from "vite-plus/test";
4import {
5 EnvironmentId,
6 ThreadId,
7 type OrchestrationV2SearchThreadResult,
8} from "@t3tools/contracts";
9import {
10 ThreadFind,
11 ThreadFindProvider,
12 ThreadFindTimelineContext,
13 type ThreadFindControls,
14} from "./ThreadFindProvider";
15import type { ThreadFindBar } from "./ThreadFindBar";
16​
17const query = vi.hoisted(() => ({
18 value: {
19 data: null as OrchestrationV2SearchThreadResult | null,
20 isPending: false,
21 error: null,
22 refresh: vi.fn(),
23 },
24 listeners: new Set<() => void>(),
25}));
26let bar: Parameters<typeof ThreadFindBar>[0];
27vi.mock("./ThreadFindBar", () => ({
28 ThreadFindBar: (props: typeof bar) => {
29 bar = props;
30 return null;
31 },
32}));
33vi.mock("~/state/orchestration", () => ({
34 orchestrationEnvironment: { threadFind: (target: unknown) => target },
35}));
36vi.mock("~/state/queries", () => ({ useDebouncedValue: <T,>(value: T) => value }));
37vi.mock("~/state/query", async () => {
38 const { useSyncExternalStore } = await import("react");
39 return {
40 useEnvironmentQuery: (atom: unknown) => {
41 const value = useSyncExternalStore(
42 (listener) => {
43 query.listeners.add(listener);
44 return () => query.listeners.delete(listener);
45 },
46 () => query.value,
47 );
48 return { ...value, data: atom ? value.data : null };
49 },
50 };
51});
52vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
53​
54type Rendered = { update: (node: ReactNode) => void; unmount: () => void };
55function render(node: ReactNode): Rendered {
56 const root = createRoot(document.createElement("div"));
57 root.render(node);
58 return { update: (next) => root.render(next), unmount: () => root.unmount() };
60​
61let renderer: Rendered | undefined;
62afterEach(async () => {
63 if (renderer) await act(async () => renderer?.unmount());
64 renderer = undefined;
65 query.listeners.clear();
66 vi.unstubAllGlobals();
67});
68​
69it("keeps navigation and pending responses out of the surrounding chat and unrelated children", async () => {
70 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
71 const controls = createRef<ThreadFindControls>();
72 const parentRender = vi.fn();
73 const composerRender = vi.fn();
74 const timelineRender = vi.fn();
75 function Composer() {
76 composerRender();
77 return null;
78 }
79 function Timeline() {
80 timelineRender(useContext(ThreadFindTimelineContext));
81 return null;
82 }
83 function Chat() {
84 parentRender();
85 const [, setOpen] = useState(false);
86 return (
87 <ThreadFindProvider
88 thread={{ environmentId: EnvironmentId.make("dev"), threadId: ThreadId.make("thread") }}
89 enabled
90 content={undefined}
91 controlsRef={controls}
92 onOpenChange={setOpen}
93 >
94 <ThreadFind onClose={() => {}} />
95 <Timeline />
96 <Composer />
97 </ThreadFindProvider>
98 );
99 }
100 const response = (index: number): OrchestrationV2SearchThreadResult => ({
101 totalMatches: 3,
102 activeIndex: index,
103 snapshotSequence: 1,
104 match: { entryId: "message", runId: null, occurrence: index },
105 });
106 query.value = { ...query.value, data: response(0), isPending: false };
107 await act(async () => {
108 renderer = render(<Chat />);
109 });
110 await act(async () => controls.current?.open());
111 await act(async () => bar.onQueryChange("COD4"));
112 const parentCount = parentRender.mock.calls.length;
113 const composerCount = composerRender.mock.calls.length;
114 const timelineCount = timelineRender.mock.calls.length;
115 await act(async () => {
116 query.value = { ...query.value, data: null, isPending: true };
117 for (const listener of query.listeners) listener();
118 bar.onNext();
119 });
120 expect(parentRender).toHaveBeenCalledTimes(parentCount);
121 expect(composerRender).toHaveBeenCalledTimes(composerCount);
122 expect(timelineRender).toHaveBeenCalledTimes(timelineCount);
123 await act(async () => {
124 query.value = { ...query.value, data: response(1), isPending: false };
125 for (const listener of query.listeners) listener();
126 });
127 expect(parentRender).toHaveBeenCalledTimes(parentCount);
128 expect(composerRender).toHaveBeenCalledTimes(composerCount);
129 expect(timelineRender).toHaveBeenCalledTimes(timelineCount + 1);
130 expect(timelineRender.mock.lastCall?.[0]?.activeFindMatch?.occurrence).toBe(1);
131 await act(async () => controls.current?.close());
132 expect(parentRender.mock.calls.length).toBeGreaterThan(parentCount);
133});