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

queries.pathSearch.browser.test.tsx

150 lines5.3 KB

1import { RegistryContext } from "@effect/atom-react";
2import {
3 EnvironmentId,
4 type ProjectSearchContentsResult,
5 type ProjectSearchEntriesResult,
6} from "@t3tools/contracts";
7import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
8import { describe, expect, it, vi } from "vite-plus/test";
9​
10const searches = vi.hoisted(() => ({
11 atoms: new Map<string, unknown>(),
12 requested: [] as string[],
13}));
14​
15vi.mock(import("./filesystem"), async (original) => ({
16 ...(await original()),
17 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
18}));
19vi.mock(import("./projects"), async (original) => {
20 const actual = await original();
21 return {
22 ...actual,
23 projectEnvironment: {
24 ...actual.projectEnvironment,
25 searchEntries: ((target: { input: { query: string } }) => {
26 searches.requested.push(target.input.query);
27 return searches.atoms.get(target.input.query);
28 }) as unknown as typeof actual.projectEnvironment.searchEntries,
29 },
30 projectContentSearch: ((target: { input: { query: string } }) => {
31 searches.requested.push(target.input.query);
32 return searches.atoms.get(target.input.query);
33 }) as unknown as typeof actual.projectContentSearch,
34 };
35});
36​
37import { render } from "../../test/render";
38import { useProjectContentSearch, useProjectPathSearch } from "./queries";
39​
40function setup() {
41 searches.atoms.clear();
42 searches.requested.length = 0;
43 return AtomRegistry.make();
45​
46function searchAtom<A = ProjectSearchEntriesResult>(query: string) {
47 const atom = Atom.make<AsyncResult.AsyncResult<A, Error>>(AsyncResult.initial(true)).pipe(
48 Atom.keepAlive,
49 );
50 searches.atoms.set(query, atom);
51 return atom;
53​
54function Entries({ query }: { query: string }) {
55 const { entries, searchedTarget } = useProjectPathSearch(
56 { environmentId: EnvironmentId.make("primary"), cwd: "/repo", query },
57 20,
58 );
59 const paths = entries.length === 0 ? "none" : entries.map((entry) => entry.path).join(",");
60 return <span>{`${searchedTarget.query}:${paths}`}</span>;
62​
63describe("useProjectPathSearch", () => {
64 it("keeps the shown entries while a narrower search is pending", async () => {
65 const registry = setup();
66 const first = searchAtom("app");
67 const narrower = searchAtom("appv");
68 const ui = (query: string) => (
69 <RegistryContext.Provider value={registry}>
70 <Entries query={query} />
71 </RegistryContext.Provider>
72 );
73 await using screen = await render(ui("app"), { router: false });
74 await vi.waitFor(() => expect(searches.requested).toContain("app"));
75 registry.set(
76 first,
77 AsyncResult.success({
78 entries: [
79 { path: "src/app.ts", kind: "file" },
80 { path: "src/appView.tsx", kind: "file" },
81 ],
82 truncated: false,
83 } as ProjectSearchEntriesResult),
84 );
85 await expect.element(screen.getByText("app:src/app.ts,src/appView.tsx")).toBeInTheDocument();
86​
87 await screen.rerender(ui("appv"));
88 await vi.waitFor(() => expect(searches.requested).toContain("appv"));
89 // Kept entries still name the query they answered, so a stale pick is rejected.
90 await expect.element(screen.getByText("app:src/app.ts,src/appView.tsx")).toBeInTheDocument();
91​
92 registry.set(
93 narrower,
94 AsyncResult.success({
95 entries: [{ path: "src/appView.tsx", kind: "file" }],
96 truncated: false,
97 } as ProjectSearchEntriesResult),
98 );
99 await expect.element(screen.getByText("appv:src/appView.tsx")).toBeInTheDocument();
100 });
101});
102​
103function ContentMatches({ query }: { query: string }) {
104 const { matches, isPending } = useProjectContentSearch({
105 environmentId: EnvironmentId.make("primary"),
106 cwd: "/repo",
107 query,
108 caseSensitive: false,
109 wholeWord: false,
110 useRegex: false,
111 });
112 const paths = matches.length === 0 ? "none" : matches.map((match) => match.path).join(",");
113 return <span>{`${isPending ? "pending" : "settled"}:${paths}`}</span>;
115​
116describe("useProjectContentSearch", () => {
117 it("keeps the shown matches while a longer search is pending", async () => {
118 const registry = setup();
119 const first = searchAtom<ProjectSearchContentsResult>("todo");
120 const longer = searchAtom<ProjectSearchContentsResult>("todo:");
121 const ui = (query: string) => (
122 <RegistryContext.Provider value={registry}>
123 <ContentMatches query={query} />
124 </RegistryContext.Provider>
125 );
126 await using screen = await render(ui("todo"), { router: false });
127 await vi.waitFor(() => expect(searches.requested).toContain("todo"));
128 registry.set(
129 first,
130 AsyncResult.success({
131 matches: [{ path: "src/app.ts", lineNumber: 1, lineContent: "// todo", matchRanges: [] }],
132 truncated: false,
133 }),
134 );
135 await expect.element(screen.getByText("settled:src/app.ts")).toBeInTheDocument();
136​
137 await screen.rerender(ui("todo:"));
138 await vi.waitFor(() => expect(searches.requested).toContain("todo:"));
139 await expect.element(screen.getByText("pending:src/app.ts")).toBeInTheDocument();
140​
141 registry.set(
142 longer,
143 AsyncResult.success({
144 matches: [{ path: "src/b.ts", lineNumber: 2, lineContent: "// todo: b", matchRanges: [] }],
145 truncated: false,
146 }),
147 );
148 await expect.element(screen.getByText("settled:src/b.ts")).toBeInTheDocument();
149 });
150});