@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 817 lines 32.4 KB
Raw
1 import {
2 FileFinder,
3 type FileItem,
4 type GrepCursor,
5 type GrepOptions,
6 type GrepResult,
7 type WatchEvent,
8 } from "@ff-labs/fff-node";
9 import * as NodeServices from "@effect/platform-node/NodeServices";
10 import { afterEach, expect, it } from "@effect/vitest";
11 import * as HostProcess from "@t3tools/shared/HostProcess";
12 import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
13 import * as Deferred from "effect/Deferred";
14 import * as Effect from "effect/Effect";
15 import { ChildProcessSpawner } from "effect/process";
16 import * as Fiber from "effect/Fiber";
17 import * as FileSystem from "effect/FileSystem";
18 import * as Layer from "effect/Layer";
19 import * as Path from "effect/Path";
20 import * as TestClock from "effect/testing/TestClock";
21 import { vi } from "vite-plus/test";
22
23 import * as VcsProcess from "../vcs/VcsProcess.ts";
24 import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
25
26 const testLayer = VcsProcess.layer.pipe(Layer.provideMerge(NodeServices.layer));
27
28 afterEach(() => {
29 vi.restoreAllMocks();
30 });
31
32 function fileItem(relativePath: string): FileItem {
33 return {
34 relativePath,
35 fileName: relativePath.slice(relativePath.lastIndexOf("/") + 1),
36 size: 1,
37 modified: 0,
38 accessFrecencyScore: 0,
39 modificationFrecencyScore: 0,
40 totalFrecencyScore: 0,
41 gitStatus: "clean",
42 };
43 }
44
45 it.live("image searches match the image subset of a file search without fetching every file", () =>
46 Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const vcsProcess = yield* VcsProcess.VcsProcess;
50 const writeFiles = Effect.fn(function* (root: string, relativePaths: ReadonlyArray<string>) {
51 for (const relativePath of relativePaths) {
52 yield* fileSystem.makeDirectory(path.dirname(path.join(root, relativePath)), {
53 recursive: true,
54 });
55 yield* fileSystem.writeFileString(path.join(root, relativePath), "");
56 yield* fileSystem.utimes(path.join(root, relativePath), 0, 0);
57 }
58 });
59
60 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-search-" });
61 yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd });
62 yield* fileSystem.writeFileString(path.join(cwd, ".git/info/exclude"), "hidden/\n");
63 yield* writeFiles(cwd, [
64 ...Array.from({ length: 220 }, (_, index) => `assets/source-${index}.ts`),
65 ...PROJECT_FAVICON_EXTENSIONS.flatMap((extension) => [
66 `assets/logo${extension}`,
67 `other/logo${extension.toUpperCase()}`,
68 ]),
69 "assets/logo.ts",
70 "assets/logotype.png",
71 "assetss/logotype.png",
72 "assets/images/logo.PNG",
73 "assets/foo{bar}.png",
74 "assets/[id].png",
75 "hidden/logo.png",
76 ]);
77
78 const fileSearch = vi.spyOn(FileFinder.prototype, "fileSearchAsync");
79 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
80 yield* searchIndex.search("", 200, undefined, true);
81 const nativeResult = yield* Effect.promise(
82 (): ReturnType<FileFinder["fileSearchAsync"]> => fileSearch.mock.results[0]?.value,
83 );
84 if (!nativeResult.ok) return yield* Effect.die(nativeResult.error);
85 expect(nativeResult.value.items.length).toBeLessThanOrEqual(201);
86
87 // The image glob must not change fff's parse of fuzzy paths, globs, negations or locations.
88 for (const query of [
89 "",
90 "logo",
91 "lgo",
92 "*.png",
93 "!*.png",
94 "assets/**",
95 "!assets/",
96 "assets*/",
97 "/assets",
98 "assets/",
99 "logo:12",
100 "/assets/logotype.png",
101 "assets/images/",
102 "logo *.svg !other",
103 "foo{bar}.png",
104 "[id].png",
105 "*.{png,svg}",
106 "hidden/",
107 ]) {
108 const ordinary = yield* searchIndex.search(query, 1000, "file");
109 const expected = ordinary.entries.filter((entry) => isProjectFaviconPath(entry.path));
110 for (const limit of [1, 200]) {
111 const images = yield* searchIndex.search(query, limit, "directory", true);
112 expect(images, `query=${query} limit=${limit}`).toEqual({
113 entries: expected.slice(0, limit),
114 truncated: expected.length > limit,
115 });
116 }
117 }
118
119 // Past 15,000 matches fff drops its filename fallback, which the image glob would otherwise
120 // reorder: "x/logo/logo.png" must keep its place relative to "a/xxlogo.png".
121 const largeCwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-rank-" });
122 yield* writeFiles(largeCwd, [
123 ...Array.from({ length: 14_998 }, (_, index) => `b/logo-${index}.png`),
124 "x/logo/source.ts",
125 "a/xxlogo.png",
126 "x/logo/logo.png",
127 ]);
128 const largeIndex = yield* WorkspaceSearchIndex.make(largeCwd);
129 const pairPaths = ["a/xxlogo.png", "x/logo/logo.png"];
130 const ordinary = yield* largeIndex.search("logo", 20_000, "file");
131 const images = yield* largeIndex.search("logo", 20_000, "file", true);
132 expect(images.entries).toHaveLength(15_000);
133 expect(images.entries.filter((entry) => pairPaths.includes(entry.path))).toEqual(
134 ordinary.entries.filter((entry) => pairPaths.includes(entry.path)),
135 );
136 }).pipe(Effect.scoped, Effect.provide(testLayer)),
137 );
138
139 it.effect("fetches past a first page of Git-ignored results", () =>
140 Effect.scoped(
141 Effect.gen(function* () {
142 const fs = yield* FileSystem.FileSystem;
143 const path = yield* Path.Path;
144 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-search-ignored-" });
145 const vcsProcess = yield* VcsProcess.VcsProcess;
146 yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd });
147 yield* fs.writeFileString(path.join(cwd, ".git", "info", "exclude"), "hidden/\n");
148 yield* fs.makeDirectory(path.join(cwd, "hidden"));
149 yield* fs.writeFileString(path.join(cwd, "hidden", "a.ts"), "");
150
151 const items = [
152 fileItem("hidden/a.ts"),
153 fileItem("hidden/b.ts"),
154 fileItem("hidden/c.ts"),
155 fileItem("src/a.ts"),
156 ];
157 const fileSearch = vi.fn(async (_query: string, options?: { pageSize?: number }) => ({
158 ok: true as const,
159 value: {
160 items: items.slice(0, options?.pageSize),
161 scores: [],
162 totalMatched: items.length,
163 totalFiles: items.length,
164 },
165 }));
166 const finder = {
167 destroy: vi.fn(),
168 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
169 getScanProgressAsync: async () => ({
170 ok: true as const,
171 value: { isScanning: false },
172 }),
173 watchAsync: async () => ({ ok: true as const, value: () => undefined }),
174 fileSearchAsync: fileSearch,
175 } as unknown as FileFinder;
176 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
177
178 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
179 const result = yield* searchIndex.search("a", 1, "file", false);
180
181 expect(result).toEqual({ entries: [{ kind: "file", path: "src/a.ts" }], truncated: false });
182 }),
183 ).pipe(Effect.provide(testLayer)),
184 );
185
186 // A backslash is a file name character on Linux and macOS, so only Windows may treat it as a separator.
187 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
188 "keeps a backslash in a POSIX folder name as part of the search result path",
189 () =>
190 Effect.scoped(
191 Effect.gen(function* () {
192 const items = [fileItem("dir\\one/report.txt")];
193 const finder = {
194 destroy: vi.fn(),
195 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
196 getScanProgressAsync: async () => ({
197 ok: true as const,
198 value: { isScanning: false },
199 }),
200 watchAsync: async () => ({ ok: true as const, value: () => undefined }),
201 fileSearchAsync: vi.fn(async () => ({
202 ok: true as const,
203 value: { items, scores: [], totalMatched: 1, totalFiles: 1 },
204 })),
205 } as unknown as FileFinder;
206 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
207
208 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
209 const result = yield* searchIndex.search("report", 50, "file");
210
211 expect(result.entries).toEqual([{ kind: "file", path: "dir\\one/report.txt" }]);
212 }),
213 ).pipe(Effect.provide(testLayer)),
214 );
215
216 it.effect("continues whole-word searches after a filtered grep page", () =>
217 Effect.scoped(
218 Effect.gen(function* () {
219 const nextCursor = {
220 __brand: "GrepCursor",
221 _offset: 1,
222 } as GrepCursor;
223 const grepResult = (
224 lineContent: string,
225 matchRanges: Array<[number, number]>,
226 cursor: GrepCursor | null,
227 ): GrepResult => ({
228 items: [
229 {
230 relativePath: "src/words.ts",
231 fileName: "words.ts",
232 gitStatus: "unmodified",
233 size: lineContent.length,
234 modified: 0,
235 isBinary: false,
236 totalFrecencyScore: 0,
237 accessFrecencyScore: 0,
238 modificationFrecencyScore: 0,
239 lineNumber: 1,
240 col: 0,
241 byteOffset: 0,
242 lineContent,
243 matchRanges,
244 },
245 ],
246 totalMatched: 1,
247 totalFilesSearched: 1,
248 totalFiles: 1,
249 filteredFileCount: 1,
250 nextCursor: cursor,
251 });
252 const grep = vi.fn(async (_query: string, options?: GrepOptions) =>
253 options?.cursor
254 ? { ok: true as const, value: grepResult("needle", [[0, 6]], null) }
255 : {
256 ok: true as const,
257 value: grepResult("needleSuffix", [[0, 6]], nextCursor),
258 },
259 );
260 const finder = {
261 destroy: vi.fn(),
262 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
263 getScanProgressAsync: async () => ({
264 ok: true as const,
265 value: { isScanning: false },
266 }),
267 watchAsync: async () => ({ ok: true as const, value: () => undefined }),
268 grepAsync: grep,
269 } as unknown as FileFinder;
270 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
271
272 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
273 const result = yield* searchIndex.searchContents({
274 query: "needle",
275 limit: 1,
276 caseSensitive: true,
277 wholeWord: true,
278 useRegex: false,
279 });
280
281 expect(result).toEqual({
282 matches: [
283 {
284 path: "src/words.ts",
285 lineNumber: 1,
286 lineContent: "needle",
287 matchRanges: [{ start: 0, end: 6 }],
288 },
289 ],
290 truncated: false,
291 });
292 expect(grep).toHaveBeenCalledTimes(2);
293 expect(grep.mock.calls[0]?.[0]).toBe(String.raw`(?-u)\bneedle\b`);
294 expect(grep.mock.calls[0]?.[1]?.mode).toBe("regex");
295 expect(grep.mock.calls[1]?.[1]?.cursor).toBe(nextCursor);
296 }),
297 ).pipe(Effect.provide(testLayer)),
298 );
299
300 it.live("answers content searches with git grep while a slow content index builds", () =>
301 Effect.gen(function* () {
302 const fileSystem = yield* FileSystem.FileSystem;
303 const path = yield* Path.Path;
304 const vcsProcess = yield* VcsProcess.VcsProcess;
305 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-content-warmup-" });
306 yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd });
307 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), "ignored.txt\n");
308 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
309 yield* fileSystem.writeFileString(
310 path.join(cwd, "src/a.ts"),
311 "const Needle = 1;\r\nneedles(a.b);\nnone\n",
312 );
313 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "needle\n");
314 // fff leaves out what `.ignore` excludes, so git results must leave it out too.
315 yield* fileSystem.writeFileString(path.join(cwd, ".ignore"), "out/\n");
316 yield* fileSystem.makeDirectory(path.join(cwd, "out/deep"), { recursive: true });
317 yield* fileSystem.writeFileString(path.join(cwd, "out/deep/g.txt"), "needle\n");
318
319 let isWarmupComplete = false;
320 const grep = vi.fn(async () => ({
321 ok: true as const,
322 value: {
323 items: [],
324 totalMatched: 0,
325 totalFilesSearched: 0,
326 totalFiles: 0,
327 filteredFileCount: 0,
328 nextCursor: null,
329 } satisfies GrepResult,
330 }));
331 const finder = {
332 destroy: vi.fn(),
333 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: false })),
334 getScanProgressAsync: async () => ({
335 ok: true as const,
336 value: { isScanning: false, isWarmupComplete },
337 }),
338 watchAsync: async () => ({ ok: true as const, value: () => undefined }),
339 grepAsync: grep,
340 } as unknown as FileFinder;
341 const noPcreFinder = {
342 ...finder,
343 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
344 } as unknown as FileFinder;
345 vi.spyOn(FileFinder, "create")
346 .mockReturnValueOnce({ ok: true, value: finder })
347 .mockReturnValueOnce({ ok: true, value: noPcreFinder })
348 .mockReturnValueOnce({ ok: true, value: noPcreFinder })
349 .mockReturnValueOnce({ ok: true, value: noPcreFinder });
350
351 const searchIndex = yield* WorkspaceSearchIndex.make(cwd, "content");
352 const search = (input: Partial<Parameters<typeof searchIndex.searchContents>[0]>) =>
353 searchIndex.searchContents({
354 query: "needle",
355 limit: 10,
356 caseSensitive: false,
357 wholeWord: false,
358 useRegex: false,
359 ...input,
360 });
361
362 expect(yield* search({})).toEqual({
363 matches: [
364 {
365 path: "src/a.ts",
366 lineNumber: 1,
367 lineContent: "const Needle = 1;",
368 matchRanges: [{ start: 6, end: 12 }],
369 },
370 {
371 path: "src/a.ts",
372 lineNumber: 2,
373 lineContent: "needles(a.b);",
374 matchRanges: [{ start: 0, end: 6 }],
375 },
376 ],
377 truncated: false,
378 });
379 expect((yield* search({ caseSensitive: true, wholeWord: true })).matches).toEqual([]);
380 expect((yield* search({ query: "a.b" })).matches.map((match) => match.matchRanges)).toEqual([
381 [{ start: 8, end: 11 }],
382 ]);
383 expect(
384 (yield* search({ query: "ne+dles?", useRegex: true })).matches.map(
385 (match) => match.matchRanges,
386 ),
387 ).toEqual([[{ start: 6, end: 12 }], [{ start: 0, end: 7 }]]);
388 expect(
389 (yield* search({ query: "[a-z]e", useRegex: true })).matches.map(
390 (match) => match.matchRanges,
391 ),
392 ).toEqual([
393 [{ start: 4, end: 6 }],
394 [
395 { start: 6, end: 8 },
396 { start: 10, end: 12 },
397 ],
398 [
399 { start: 0, end: 2 },
400 { start: 4, end: 6 },
401 ],
402 [{ start: 2, end: 4 }],
403 ]);
404 const invalid = yield* search({ query: "a.b(", useRegex: true });
405 expect(invalid.matches).toEqual([]);
406 expect(invalid.regexFallbackError).toBeDefined();
407 expect(yield* search({ limit: 1 })).toMatchObject({ truncated: true });
408 expect(grep).not.toHaveBeenCalled();
409
410 // When git cannot give regex results or their ranges, fff answers once ready instead.
411 const failingGitIndex = (
412 failingArgument: string,
413 stderr: string,
414 output: { readonly exitCode: number; readonly stdout: string } = {
415 exitCode: 128,
416 stdout: "",
417 },
418 ) =>
419 WorkspaceSearchIndex.make(cwd, "content").pipe(
420 Effect.provideService(
421 VcsProcess.VcsProcess,
422 VcsProcess.VcsProcess.of({
423 run: (input) =>
424 input.args.includes(failingArgument)
425 ? Effect.succeed({
426 exitCode: ChildProcessSpawner.ExitCode(output.exitCode),
427 stdout: output.stdout,
428 stderr,
429 stdoutTruncated: output.exitCode === 0,
430 stderrTruncated: false,
431 })
432 : vcsProcess.run(input),
433 }),
434 ),
435 );
436 const regexInput = {
437 query: "ne+dles?",
438 limit: 10,
439 caseSensitive: false,
440 wholeWord: true,
441 useRegex: true,
442 };
443 const noPcreIndex = yield* failingGitIndex(
444 "-P",
445 "fatal: cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE\n",
446 );
447 expect((yield* noPcreIndex.searchContents(regexInput)).regexFallbackError).toBeUndefined();
448 expect(grep).toHaveBeenCalledTimes(1);
449 const noRangesIndex = yield* failingGitIndex("-o", "fatal: unexpected failure\n");
450 yield* noRangesIndex.searchContents(regexInput);
451 expect(grep).toHaveBeenCalledTimes(2);
452 // Ranges output cut off after line 1: line 2 is left out and the result says so.
453 const cutRangesIndex = yield* failingGitIndex("-o", "", {
454 exitCode: 0,
455 stdout: "src/a.ts\u00001\u00007\u0000Needle\nsrc/a.ts\u00002",
456 });
457 expect(yield* cutRangesIndex.searchContents(regexInput)).toEqual({
458 matches: [
459 {
460 path: "src/a.ts",
461 lineNumber: 1,
462 lineContent: "const Needle = 1;",
463 matchRanges: [{ start: 6, end: 12 }],
464 },
465 ],
466 truncated: true,
467 });
468
469 isWarmupComplete = true;
470 yield* search({});
471 expect(grep).toHaveBeenCalledTimes(3);
472 expect(finder.destroy).not.toHaveBeenCalled();
473 }).pipe(Effect.scoped, Effect.provide(testLayer)),
474 );
475
476 function fakeSymlinkFinder(
477 directories: ReadonlyArray<string>,
478 options: {
479 readonly indexed?: ReadonlyArray<{
480 readonly path: string;
481 readonly kind: "file" | "directory";
482 }>;
483 readonly watchFailures?: number;
484 } = {},
485 ) {
486 const watchers: Array<(events: ReadonlyArray<WatchEvent>) => void> = [];
487 let watchFailures = options.watchFailures ?? 0;
488 // fff's pageIndex is an offset into the results, not a page number.
489 const directorySearch = vi.fn(
490 async (_query: string, searchOptions?: { pageIndex?: number; pageSize?: number }) => {
491 const offset = searchOptions?.pageIndex ?? 0;
492 const items = directories.slice(offset, offset + (searchOptions?.pageSize ?? 100));
493 return {
494 ok: true as const,
495 value: {
496 items: items.map((relativePath) => ({ relativePath })),
497 totalMatched: directories.length,
498 },
499 };
500 },
501 );
502 const indexedItems = (options.indexed ?? []).map((entry) => ({
503 type: entry.kind,
504 item: { relativePath: entry.path, fileName: entry.path.split("/").at(-1) },
505 }));
506 const finder = {
507 destroy: vi.fn(),
508 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
509 // fff's isScanning waits on the picker lock on the JS thread.
510 isScanning: () => {
511 throw new Error("isScanning must not run on the JS thread");
512 },
513 getScanProgressAsync: async () => ({
514 ok: true as const,
515 value: { isScanning: false },
516 }),
517 // fff's watch waits on the picker lock on the JS thread.
518 watch: () => {
519 throw new Error("watch must not run on the JS thread");
520 },
521 watchAsync: async (callback: (events: ReadonlyArray<WatchEvent>) => void) => {
522 if (watchFailures > 0) {
523 watchFailures -= 1;
524 return { ok: false as const, error: "File system watcher is not ready" };
525 }
526 watchers.push(callback);
527 return { ok: true as const, value: () => undefined };
528 },
529 directorySearchAsync: directorySearch,
530 fileSearchAsync: async () => {
531 const items = indexedItems.flatMap((entry) => (entry.type === "file" ? [entry.item] : []));
532 return { ok: true as const, value: { items, scores: [], totalMatched: items.length } };
533 },
534 mixedSearchAsync: async () => ({
535 ok: true as const,
536 value: { items: indexedItems, totalMatched: indexedItems.length },
537 }),
538 } as unknown as FileFinder;
539 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
540 return {
541 directorySearch,
542 notifyWatchers: (events: ReadonlyArray<WatchEvent>) =>
543 watchers.forEach((watcher) => watcher(events)),
544 };
545 }
546
547 const listedPaths = (searchIndex: WorkspaceSearchIndex.WorkspaceSearchIndex["Service"]) =>
548 searchIndex.list().pipe(Effect.map((result) => result.entries.map((entry) => entry.path)));
549
550 it.effect("reads symlinks again after the watcher reports a change", () =>
551 Effect.scoped(
552 Effect.gen(function* () {
553 if ((yield* HostProcess.Platform) === "win32") return;
554 const fileSystem = yield* FileSystem.FileSystem;
555 const path = yield* Path.Path;
556 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-symlink-watch-" });
557 const outside = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-outside-" });
558 yield* fileSystem.writeFileString(path.join(cwd, "a.ts"), "");
559 yield* fileSystem.symlink("a.ts", path.join(cwd, "old-link.ts"));
560 const { directorySearch, notifyWatchers } = fakeSymlinkFinder([]);
561
562 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
563 const concurrent = yield* Effect.all(
564 Array.from({ length: 8 }, () => listedPaths(searchIndex)),
565 { concurrency: "unbounded" },
566 );
567 yield* fileSystem.remove(path.join(cwd, "old-link.ts"));
568 yield* fileSystem.symlink("a.ts", path.join(cwd, "new-link.ts"));
569 const beforeNotice = yield* listedPaths(searchIndex);
570 // Editing a file cannot move a link, so it does not cost a rescan.
571 notifyWatchers([{ kind: "modified", path: path.join(cwd, "a.ts") }]);
572 const afterEdit = yield* listedPaths(searchIndex);
573 notifyWatchers([{ kind: "created", path: path.join(cwd, "new-link.ts") }]);
574 const afterNotice = yield* listedPaths(searchIndex);
575 // fff reports a link replaced in place as modified.
576 yield* fileSystem.remove(path.join(cwd, "new-link.ts"));
577 yield* fileSystem.symlink(outside, path.join(cwd, "new-link.ts"));
578 notifyWatchers([{ kind: "modified", path: path.join(cwd, "new-link.ts") }]);
579 const afterReplace = yield* listedPaths(searchIndex);
580
581 expect(concurrent).toEqual(Array.from({ length: 8 }, () => ["old-link.ts"]));
582 expect(beforeNotice).toEqual(["old-link.ts"]);
583 expect(afterEdit).toEqual(["old-link.ts"]);
584 expect(afterNotice).toEqual(["new-link.ts"]);
585 expect(afterReplace).toEqual([]);
586 expect(directorySearch).toHaveBeenCalledTimes(3);
587 }),
588 ).pipe(Effect.provide(testLayer)),
589 );
590
591 it.effect("answers from the last symlink scan while a slow rescan runs", () =>
592 Effect.scoped(
593 Effect.gen(function* () {
594 if ((yield* HostProcess.Platform) === "win32") return;
595 const fileSystem = yield* FileSystem.FileSystem;
596 const path = yield* Path.Path;
597 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-symlink-stale-" });
598 yield* fileSystem.writeFileString(path.join(cwd, "a.ts"), "");
599 yield* fileSystem.symlink("a.ts", path.join(cwd, "old-link.ts"));
600 const { notifyWatchers } = fakeSymlinkFinder([]);
601 // Holding git's ignore check open keeps the rescan from finishing.
602 const gate = yield* Deferred.make<void>();
603 let holdIgnoreChecks = false;
604 const vcsProcess = yield* VcsProcess.VcsProcess;
605 const searchIndex = yield* WorkspaceSearchIndex.make(cwd).pipe(
606 Effect.provideService(VcsProcess.VcsProcess, {
607 ...vcsProcess,
608 run: (input) =>
609 holdIgnoreChecks && input.operation === "WorkspaceSearchIndex.listGitIgnored"
610 ? Deferred.await(gate).pipe(Effect.andThen(vcsProcess.run(input)))
611 : vcsProcess.run(input),
612 }),
613 );
614 expect(yield* listedPaths(searchIndex)).toEqual(["old-link.ts"]);
615
616 holdIgnoreChecks = true;
617 yield* fileSystem.symlink("a.ts", path.join(cwd, "new-link.ts"));
618 notifyWatchers([{ kind: "created", path: path.join(cwd, "new-link.ts") }]);
619 const duringRescan = yield* Effect.forkChild(listedPaths(searchIndex));
620 yield* TestClock.adjust("1 second");
621 expect(yield* Fiber.join(duringRescan)).toEqual(["old-link.ts"]);
622
623 yield* Deferred.succeed(gate, undefined);
624 expect(yield* listedPaths(searchIndex)).toEqual(["new-link.ts", "old-link.ts"]);
625 }),
626 ).pipe(Effect.provide(testLayer)),
627 );
628
629 it.effect("keeps symlinks once the watcher subscribes after starting late", () =>
630 Effect.scoped(
631 Effect.gen(function* () {
632 if ((yield* HostProcess.Platform) === "win32") return;
633 const fileSystem = yield* FileSystem.FileSystem;
634 const path = yield* Path.Path;
635 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-symlink-watch-" });
636 yield* fileSystem.writeFileString(path.join(cwd, "a.ts"), "");
637 yield* fileSystem.symlink("a.ts", path.join(cwd, "link.ts"));
638 const { directorySearch } = fakeSymlinkFinder([], { watchFailures: 2 });
639
640 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
641 const scansAfterRound: number[] = [];
642 for (let round = 0; round < 5; round += 1) {
643 expect(yield* listedPaths(searchIndex)).toEqual(["link.ts"]);
644 expect(yield* listedPaths(searchIndex)).toEqual(["link.ts"]);
645 scansAfterRound.push(directorySearch.mock.calls.length);
646 yield* TestClock.adjust("1 minute");
647 }
648
649 // An unwatched scan serves reads only briefly; the third has a watcher and is kept.
650 expect(scansAfterRound).toEqual([1, 2, 3, 3, 3]);
651 }),
652 ).pipe(Effect.provide(testLayer)),
653 );
654
655 it.effect(
656 "lists a symlink the watcher indexed once, with the link's kind, and drops rejected ones",
657 () =>
658 Effect.scoped(
659 Effect.gen(function* () {
660 if ((yield* HostProcess.Platform) === "win32") return;
661 const fileSystem = yield* FileSystem.FileSystem;
662 const path = yield* Path.Path;
663 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-symlink-dupe-" });
664 const outside = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-outside-" });
665 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
666 yield* fileSystem.symlink("src", path.join(cwd, "src-link"));
667 // A link the scan rejects stays out even when the watcher indexed it.
668 yield* fileSystem.symlink(outside, path.join(cwd, "outside-link"));
669 fakeSymlinkFinder([], {
670 indexed: [
671 { path: "outside-link", kind: "file" },
672 { path: "src-link", kind: "file" },
673 { path: "link-file.ts", kind: "file" },
674 ],
675 });
676
677 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
678 const searched = yield* searchIndex.search("link", 10);
679 const files = yield* searchIndex.search("link", 1, "file");
680 const listed = yield* searchIndex.list();
681
682 expect(searched.entries).toEqual([
683 { path: "src-link", kind: "directory" },
684 { path: "link-file.ts", kind: "file" },
685 ]);
686 // The dropped link does not take the only slot.
687 expect(files).toEqual({
688 entries: [{ path: "link-file.ts", kind: "file" }],
689 truncated: false,
690 });
691 expect(listed.entries).toEqual([
692 { path: "link-file.ts", kind: "file" },
693 { path: "src-link", kind: "directory" },
694 ]);
695 }),
696 ).pipe(Effect.provide(testLayer)),
697 );
698
699 it.effect("finds symlinks in folders past the first page of the index", () =>
700 Effect.scoped(
701 Effect.gen(function* () {
702 if ((yield* HostProcess.Platform) === "win32") return;
703 const fileSystem = yield* FileSystem.FileSystem;
704 const path = yield* Path.Path;
705 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-symlink-pages-" });
706 yield* fileSystem.makeDirectory(path.join(cwd, "late"));
707 yield* fileSystem.writeFileString(path.join(cwd, "late/a.ts"), "");
708 yield* fileSystem.symlink("a.ts", path.join(cwd, "late/link.ts"));
709 // Folders fff lists but that are gone by the time they are read are skipped.
710 const firstPage = Array.from({ length: 1_000 }, (_, index) => `missing-${index}`);
711 const { directorySearch } = fakeSymlinkFinder([...firstPage, "late"]);
712
713 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
714
715 expect(yield* listedPaths(searchIndex)).toEqual(["late", "late/link.ts"]);
716 expect(directorySearch.mock.calls.map(([, options]) => options?.pageIndex)).toEqual([
717 0, 1_000,
718 ]);
719 }),
720 ).pipe(Effect.provide(testLayer)),
721 );
722
723 // fff reads HOME on Linux and macOS but the user profile on Windows, so a temp HOME cannot stand in there.
724 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
725 "refuses to index a workspace rooted at the home directory",
726 () =>
727 Effect.scoped(
728 Effect.gen(function* () {
729 const fs = yield* FileSystem.FileSystem;
730 const path = yield* Path.Path;
731 const home = yield* fs.realPath(
732 yield* fs.makeTempDirectoryScoped({ prefix: "t3-search-home-" }),
733 );
734 yield* fs.makeDirectory(path.join(home, "OneDrive", "Remix"), { recursive: true });
735 yield* fs.writeFileString(path.join(home, "OneDrive", "Remix", "part.stl"), "solid part\n");
736 yield* Effect.acquireRelease(
737 Effect.sync(() => vi.stubEnv("HOME", home)),
738 () => Effect.sync(() => vi.unstubAllEnvs()),
739 );
740
741 const result = yield* WorkspaceSearchIndex.make(home).pipe(
742 Effect.flatMap((searchIndex) => searchIndex.search("stl", 50, undefined, false)),
743 Effect.map(({ entries }) => entries),
744 Effect.catchTags({
745 WorkspaceSearchIndexCreateFailed: (error) => Effect.succeed(error._tag),
746 }),
747 );
748
749 expect(result).toBe("WorkspaceSearchIndexCreateFailed");
750 }),
751 ).pipe(Effect.provide(testLayer)),
752 );
753
754 // ripgrep and fff let a matching `.ignore` decide ahead of `.gitignore`, so Git's list must not hide it.
755 it.live("finds Git-ignored files that a root .ignore re-includes", () =>
756 Effect.gen(function* () {
757 const fileSystem = yield* FileSystem.FileSystem;
758 const path = yield* Path.Path;
759 const vcsProcess = yield* VcsProcess.VcsProcess;
760 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-search-reinclude-" });
761 yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd });
762 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), "notes/\n");
763 yield* fileSystem.writeFileString(
764 path.join(cwd, ".ignore"),
765 "!notes/\nnotes/**/*\n!notes/**/\n!notes/**/*.md\n",
766 );
767 yield* fileSystem.makeDirectory(path.join(cwd, "notes", "daily"), { recursive: true });
768 yield* fileSystem.writeFileString(path.join(cwd, "notes", "daily", "standup.md"), "");
769 yield* fileSystem.writeFileString(path.join(cwd, "notes", "daily", "standup.txt"), "");
770
771 const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
772 const result = yield* searchIndex.search("standup", 50, "file");
773
774 expect(result.entries).toEqual([{ kind: "file", path: "notes/daily/standup.md" }]);
775 }).pipe(Effect.scoped, Effect.provide(testLayer)),
776 );
777
778 // Real fff: the native whole-word pattern must return what the plain search and the JS word check did.
779 it.live.each([
780 { query: "needle", caseSensitive: true, expected: ["notes.md:1:0-6", "words.ts:2:0-6,8-14"] },
781 { query: "NEEDLE", caseSensitive: false, expected: ["notes.md:1:0-6", "words.ts:2:0-6,8-14"] },
782 // fff reads `*.ts` as a file constraint, which a regex built around the whole query would lose.
783 { query: "*.ts needle", caseSensitive: true, expected: ["words.ts:2:0-6,8-14"] },
784 // Plain search folds ASCII case only: `ſ` is not an `s`, and the Kelvin sign is not a `k`.
785 { query: "s", caseSensitive: false, expected: ["fold.txt:2:0-1"] },
786 { query: "k", caseSensitive: false, expected: ["fold.txt:4:0-1"] },
787 ])("matches whole words in a real workspace for $query", ({ query, caseSensitive, expected }) =>
788 Effect.gen(function* () {
789 const fileSystem = yield* FileSystem.FileSystem;
790 const path = yield* Path.Path;
791 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-search-whole-word-" });
792 yield* fileSystem.writeFileString(
793 path.join(cwd, "words.ts"),
794 "needleSuffix\nneedle, needle.\n\u00e9needle\n",
795 );
796 yield* fileSystem.writeFileString(path.join(cwd, "notes.md"), "needle *.ts\n");
797 yield* fileSystem.writeFileString(path.join(cwd, "fold.txt"), "\u017f\ns\n\u212a\nk\n");
798
799 const searchIndex = yield* WorkspaceSearchIndex.make(cwd, "content");
800 const result = yield* searchIndex.searchContents({
801 query,
802 limit: 500,
803 caseSensitive,
804 wholeWord: true,
805 useRegex: false,
806 });
807
808 expect(
809 result.matches
810 .map(
811 (match) =>
812 `${match.path}:${match.lineNumber}:${match.matchRanges.map((range) => `${range.start}-${range.end}`).join(",")}`,
813 )
814 .toSorted(),
815 ).toEqual(expected);
816 }).pipe(Effect.scoped, Effect.provide(testLayer)),
817 );