Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId, ThreadId, type ProjectSearchEntriesResult } from "@t3tools/contracts";
3import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
4import { describe, expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6
7const searches = vi.hoisted(() => ({
8 atoms: new Map<string, unknown>(),
9 requested: [] as string[],
10}));
11
12const primaryEnvironmentId = EnvironmentId.make("primary");
13const activeThreadRef = {
14 environmentId: primaryEnvironmentId,
15 threadId: ThreadId.make("thread-1"),
16} as const;
17
18vi.mock(import("../../hooks/useActiveProjectTarget"), () => ({
19 useActiveProjectTarget: () => ({
20 environmentId: primaryEnvironmentId,
21 cwd: "/repo",
22 projectName: "demo-app",
23 threadRef: activeThreadRef,
24 }),
25}));
26vi.mock(import("../../state/filesystem"), async (original) => ({
27 ...(await original()),
28 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
29}));
30vi.mock(import("../../state/projects"), async (original) => {
31 const actual = await original();
32 return {
33 ...actual,
34 projectEnvironment: {
35 ...actual.projectEnvironment,
36 searchEntries: ((target: { input: { query: string } }) => {
37 searches.requested.push(target.input.query);
38 return searches.atoms.get(target.input.query);
39 }) as unknown as typeof actual.projectEnvironment.searchEntries,
40 },
41 };
42});
43
44import { render } from "../../../test/render";
45import { useRightPanelStore } from "../../rightPanelStore";
46import { ProjectFaviconPickerDialog } from "../settings/ProjectFaviconPickerDialog";
47import { ProjectFilePicker } from "./ProjectFilePicker";
48import { useProjectFilePickerQuery } from "./projectFilesQueryState";
49
50function searchAtom(query: string) {
51 const atom = Atom.make<AsyncResult.AsyncResult<ProjectSearchEntriesResult, Error>>(
52 AsyncResult.initial(true),
53 ).pipe(Atom.keepAlive);
54 searches.atoms.set(query, atom);
55 return atom;
56}
57
58function fileResults(...paths: ReadonlyArray<string>) {
59 return AsyncResult.success({
60 entries: paths.map((path) => ({ path, kind: "file" as const })),
61 truncated: false,
62 });
63}
64
65function resetSearches() {
66 searches.atoms.clear();
67 searches.requested.length = 0;
68 return AtomRegistry.make();
69}
70
71function Picker({ query }: { query: string }) {
72 const { entries, matchedQuery } = useProjectFilePickerQuery(
73 EnvironmentId.make("primary"),
74 "/repo",
75 query,
76 20,
77 );
78 const paths = entries.length === 0 ? "none" : entries.map((entry) => entry.path).join(",");
79 return <span>{`${matchedQuery}:${paths}`}</span>;
80}
81
82describe("useProjectFilePickerQuery", () => {
83 it("keeps the shown files while a narrower search is pending", async () => {
84 searches.atoms.clear();
85 searches.requested.length = 0;
86 const registry = AtomRegistry.make();
87 const first = searchAtom("app");
88 const narrower = searchAtom("appv");
89 const ui = (query: string) => (
90 <RegistryContext.Provider value={registry}>
91 <Picker query={query} />
92 </RegistryContext.Provider>
93 );
94 await using screen = await render(ui("app"), { router: false });
95 await vi.waitFor(() => expect(searches.requested).toContain("app"));
96 registry.set(
97 first,
98 AsyncResult.success({
99 entries: [
100 { path: "src/app.ts", kind: "file" },
101 { path: "src/appView.tsx", kind: "file" },
102 ],
103 truncated: false,
104 } as ProjectSearchEntriesResult),
105 );
106 await expect.element(screen.getByText("app:src/app.ts,src/appView.tsx")).toBeInTheDocument();
107
108 await screen.rerender(ui("appv"));
109 await vi.waitFor(() => expect(searches.requested).toContain("appv"));
110 await expect.element(screen.getByText("app:src/app.ts,src/appView.tsx")).toBeInTheDocument();
111
112 registry.set(
113 narrower,
114 AsyncResult.success({
115 entries: [{ path: "src/appView.tsx", kind: "file" }],
116 truncated: false,
117 } as ProjectSearchEntriesResult),
118 );
119 await expect.element(screen.getByText("appv:src/appView.tsx")).toBeInTheDocument();
120 });
121
122 it("opens the highlighted file on Enter after navigating while a narrower search loads", async () => {
123 const registry = resetSearches();
124 const initial = searchAtom("");
125 const narrowedMulti = searchAtom("d");
126 const narrowedSingle = searchAtom("de");
127 const setOpen = vi.fn();
128
129 // Isolated picker test; no router navigation is exercised.
130 await using screen = await render(
131 <RegistryContext.Provider value={registry}>
132 <ProjectFilePicker setOpen={setOpen} />
133 </RegistryContext.Provider>,
134 { router: false },
135 );
136 using openFile = vi
137 .spyOn(useRightPanelStore.getState(), "openFile")
138 .mockImplementation(() => {});
139
140 await vi.waitFor(() => expect(searches.requested).toContain(""));
141 registry.set(initial, fileResults("src/alpha.ts", "src/beta.ts", "src/gamma.ts"));
142 await expect.element(screen.getByRole("option", { name: /beta\.ts/ })).toBeVisible();
143
144 const search = screen.getByPlaceholder("Search files…");
145 await userEvent.type(search, "d");
146 await userEvent.keyboard("{ArrowDown}");
147 await vi.waitFor(() => expect(searches.requested).toContain("d"));
148
149 registry.set(narrowedMulti, fileResults("src/alpha.ts", "src/delta.ts"));
150 await expect
151 .element(screen.getByRole("option", { name: /delta\.ts/ }))
152 .toHaveAttribute("data-active", "true");
153
154 await userEvent.type(search, "e");
155 await userEvent.keyboard("{ArrowDown}");
156 await vi.waitFor(() => expect(searches.requested).toContain("de"));
157
158 registry.set(narrowedSingle, fileResults("src/delta.ts"));
159 await expect
160 .element(screen.getByRole("option", { name: /delta\.ts/ }))
161 .toHaveAttribute("data-active", "true");
162
163 await userEvent.keyboard("{Enter}");
164 expect(openFile).toHaveBeenCalledWith(activeThreadRef, "src/delta.ts");
165 expect(setOpen).toHaveBeenCalledWith(false);
166 });
167
168 it("selects the highlighted icon on Enter after navigating while a narrower search loads", async () => {
169 const registry = resetSearches();
170 const initial = searchAtom("");
171 const narrowed = searchAtom("logo");
172 const onOpenChange = vi.fn();
173 const onSelect = vi.fn();
174
175 // Isolated picker dialog test; no router navigation is exercised.
176 await using _screen = await render(
177 <RegistryContext.Provider value={registry}>
178 <ProjectFaviconPickerDialog
179 cwd="/repo"
180 environmentId={primaryEnvironmentId}
181 onOpenChange={onOpenChange}
182 onSelect={onSelect}
183 open
184 projectName="demo-app"
185 />
186 </RegistryContext.Provider>,
187 { router: false },
188 );
189
190 await vi.waitFor(() => expect(searches.requested).toContain(""));
191 registry.set(initial, fileResults("public/avatar.png", "public/banner.png"));
192 await expect.element(page.getByRole("option", { name: /banner\.png/ })).toBeVisible();
193
194 const search = page.getByPlaceholder("Search image files…");
195 await userEvent.type(search, "logo");
196 await userEvent.keyboard("{ArrowDown}");
197 await vi.waitFor(() => expect(searches.requested).toContain("logo"));
198
199 registry.set(narrowed, fileResults("public/logo.png"));
200 await expect
201 .element(page.getByRole("option", { name: /logo\.png/ }))
202 .toHaveAttribute("data-active", "true");
203
204 await userEvent.keyboard("{Enter}");
205 expect(onSelect).toHaveBeenCalledWith("public/logo.png");
206 expect(onOpenChange).toHaveBeenCalledWith(false);
207 });
208});