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

composerPathSearchState.test.ts

185 lines5.2 KB

1import { assert, beforeEach, it, vi } from "vite-plus/test";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { AtomRegistry } from "effect/unstable/reactivity";
4​
5import {
6 type ComposerPathSearchClient,
7 createComposerPathSearchManager,
8 EMPTY_COMPOSER_PATH_SEARCH_STATE,
9 getComposerPathSearchTargetKey,
10} from "./composerPathSearchState.ts";
11​
12let registry = AtomRegistry.make();
13​
14const noop = () => undefined;
15​
16beforeEach(() => {
17 registry.dispose();
18 registry = AtomRegistry.make();
19});
20​
21function flushAsyncWork(): Promise<void> {
22 return Promise.resolve().then(() => undefined);
24​
25const TARGET = {
26 environmentId: "env-local" as EnvironmentId,
27 cwd: "/repo",
28 query: "src",
29};
30​
31it("derives null keys for inactive path searches", () => {
32 assert.strictEqual(getComposerPathSearchTargetKey({ ...TARGET, query: "" }), null);
33 assert.strictEqual(getComposerPathSearchTargetKey({ ...TARGET, cwd: null }), null);
34 assert.strictEqual(getComposerPathSearchTargetKey({ ...TARGET, environmentId: null }), null);
35});
36​
37it("stores path search results in atom state", async () => {
38 const manager = createComposerPathSearchManager({
39 getRegistry: () => registry,
40 debounceMs: 0,
41 getClient: () => ({
42 searchEntries: async () => ({
43 entries: [
44 { path: "src/index.ts", kind: "file" },
45 { path: "src/components", kind: "directory" },
46 ],
47 truncated: false,
48 }),
49 }),
50 });
51​
52 manager.search(TARGET);
53 await flushAsyncWork();
54​
55 assert.deepStrictEqual(manager.getSnapshot(TARGET), {
56 entries: [
57 { path: "src/index.ts", kind: "file" },
58 { path: "src/components", kind: "directory" },
59 ],
60 isPending: false,
61 error: null,
62 });
63});
64​
65it("reuses fresh cached path search results", async () => {
66 const searchEntries = vi.fn(async () => ({
67 entries: [{ path: "src/index.ts", kind: "file" as const }],
68 truncated: false,
69 }));
70 const manager = createComposerPathSearchManager({
71 getRegistry: () => registry,
72 debounceMs: 0,
73 staleTimeMs: 15_000,
74 getClient: () => ({ searchEntries }),
75 });
76​
77 manager.search(TARGET);
78 await flushAsyncWork();
79 manager.search(TARGET);
80 await flushAsyncWork();
81​
82 assert.strictEqual(searchEntries.mock.calls.length, 1);
83 assert.deepStrictEqual(manager.getSnapshot(TARGET), {
84 entries: [{ path: "src/index.ts", kind: "file" }],
85 isPending: false,
86 error: null,
87 });
88});
89​
90it("invalidates watched path searches and refreshes without clearing entries", async () => {
91 type SearchResult = Awaited<ReturnType<ComposerPathSearchClient["searchEntries"]>>;
92​
93 let resolveSecond: (value: SearchResult) => void = noop;
94 let callCount = 0;
95 const searchEntries = vi.fn((() => {
96 callCount += 1;
97 if (callCount === 1) {
98 return Promise.resolve({
99 entries: [{ path: "src/old.ts", kind: "file" as const }],
100 truncated: false,
101 });
102 }
103 return new Promise((resolve) => {
104 resolveSecond = resolve;
105 });
106 }) satisfies ComposerPathSearchClient["searchEntries"]);
107 const manager = createComposerPathSearchManager({
108 getRegistry: () => registry,
109 debounceMs: 0,
110 staleTimeMs: 15_000,
111 getClient: () => ({ searchEntries }),
112 });
113​
114 const unwatch = manager.watch(TARGET);
115 await flushAsyncWork();
116 manager.invalidate();
117​
118 assert.deepStrictEqual(manager.getSnapshot(TARGET), {
119 entries: [{ path: "src/old.ts", kind: "file" }],
120 isPending: true,
121 error: null,
122 });
123​
124 resolveSecond({
125 entries: [{ path: "src/new.ts", kind: "file" }],
126 truncated: false,
127 });
128 await flushAsyncWork();
129​
130 assert.deepStrictEqual(manager.getSnapshot(TARGET), {
131 entries: [{ path: "src/new.ts", kind: "file" }],
132 isPending: false,
133 error: null,
134 });
135 assert.strictEqual(searchEntries.mock.calls.length, 2);
136 unwatch();
137});
138​
139it("ignores stale path search results after a newer request starts", async () => {
140 let resolveFirst: (value: {
141 entries: ReadonlyArray<{ path: string; kind: "file" | "directory" }>;
142 truncated: boolean;
143 }) => void = noop;
144 const manager = createComposerPathSearchManager({
145 getRegistry: () => registry,
146 debounceMs: 0,
147 getClient: () => ({
148 searchEntries: (input: Parameters<ComposerPathSearchClient["searchEntries"]>[0]) => {
149 if (input.query === "first") {
150 return new Promise((resolve) => {
151 resolveFirst = resolve;
152 });
153 }
154 return Promise.resolve({
155 entries: [{ path: "second.ts", kind: "file" }],
156 truncated: false,
157 });
158 },
159 }),
160 });
161​
162 manager.search({ ...TARGET, query: "first" });
163 manager.search({ ...TARGET, query: "second" });
164 await flushAsyncWork();
165 resolveFirst({ entries: [{ path: "first.ts", kind: "file" }], truncated: false });
166 await flushAsyncWork();
167​
168 assert.deepStrictEqual(manager.getSnapshot({ ...TARGET, query: "second" }), {
169 entries: [{ path: "second.ts", kind: "file" }],
170 isPending: false,
171 error: null,
172 });
173});
174​
175it("returns the empty snapshot for inactive targets", () => {
176 const manager = createComposerPathSearchManager({
177 getRegistry: () => registry,
178 getClient: () => null,
179 });
180​
181 assert.deepStrictEqual(
182 manager.getSnapshot({ environmentId: null, cwd: null, query: null }),
183 EMPTY_COMPOSER_PATH_SEARCH_STATE,
184 );
185});