Open History to see the last commit
1import {
2 FileFinder,
3 type FileItem,
4 type GrepCursor,
5 type GrepOptions,
6 type GrepResult,
7} from "@ff-labs/fff-node";
8import { afterEach, expect, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import { vi } from "vite-plus/test";
11
12import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
13
14afterEach(() => {
15 vi.restoreAllMocks();
16});
17
18function fileItem(relativePath: string): FileItem {
19 return {
20 relativePath,
21 fileName: relativePath.slice(relativePath.lastIndexOf("/") + 1),
22 size: 1,
23 modified: 0,
24 accessFrecencyScore: 0,
25 modificationFrecencyScore: 0,
26 totalFrecencyScore: 0,
27 gitStatus: "clean",
28 };
29}
30
31it.effect("filters image searches before applying the result limit", () =>
32 Effect.scoped(
33 Effect.gen(function* () {
34 const items = [
35 ...Array.from({ length: 200 }, (_, index) => fileItem(`src/file-${index}.ts`)),
36 fileItem("public/icon.svg"),
37 ];
38 const fileSearch = vi.fn(() => ({
39 ok: true as const,
40 value: {
41 items,
42 scores: [],
43 totalMatched: items.length,
44 totalFiles: items.length,
45 },
46 }));
47 const finder = {
48 destroy: vi.fn(),
49 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
50 fileSearch,
51 } as unknown as FileFinder;
52 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
53
54 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
55 const resultWithoutKind = yield* searchIndex.search("", 200, undefined, true);
56 const resultWithDirectoryKind = yield* searchIndex.search("", 200, "directory", true);
57
58 expect(resultWithoutKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]);
59 expect(resultWithDirectoryKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]);
60 expect(fileSearch).toHaveBeenCalledTimes(2);
61 expect(fileSearch).toHaveBeenCalledWith("", { pageSize: 25_002 });
62 }),
63 ),
64);
65
66it.effect("continues whole-word searches after a filtered grep page", () =>
67 Effect.scoped(
68 Effect.gen(function* () {
69 const nextCursor = {
70 __brand: "GrepCursor",
71 _offset: 1,
72 } as GrepCursor;
73 const grepResult = (
74 lineContent: string,
75 matchRanges: Array<[number, number]>,
76 cursor: GrepCursor | null,
77 ): GrepResult => ({
78 items: [
79 {
80 relativePath: "src/words.ts",
81 fileName: "words.ts",
82 gitStatus: "unmodified",
83 size: lineContent.length,
84 modified: 0,
85 isBinary: false,
86 totalFrecencyScore: 0,
87 accessFrecencyScore: 0,
88 modificationFrecencyScore: 0,
89 lineNumber: 1,
90 col: 0,
91 byteOffset: 0,
92 lineContent,
93 matchRanges,
94 },
95 ],
96 totalMatched: 1,
97 totalFilesSearched: 1,
98 totalFiles: 1,
99 filteredFileCount: 1,
100 nextCursor: cursor,
101 });
102 const grep = vi.fn((_query: string, options?: GrepOptions) =>
103 options?.cursor
104 ? { ok: true as const, value: grepResult("needle", [[0, 6]], null) }
105 : {
106 ok: true as const,
107 value: grepResult("needleSuffix", [[0, 6]], nextCursor),
108 },
109 );
110 const finder = {
111 destroy: vi.fn(),
112 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
113 grep,
114 } as unknown as FileFinder;
115 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
116
117 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
118 const result = yield* searchIndex.searchContents({
119 query: "needle",
120 limit: 1,
121 caseSensitive: true,
122 wholeWord: true,
123 useRegex: false,
124 });
125
126 expect(result).toEqual({
127 matches: [
128 {
129 path: "src/words.ts",
130 lineNumber: 1,
131 lineContent: "needle",
132 matchRanges: [{ start: 0, end: 6 }],
133 },
134 ],
135 truncated: false,
136 });
137 expect(grep).toHaveBeenCalledTimes(2);
138 expect(grep.mock.calls[1]?.[1]?.cursor).toBe(nextCursor);
139 }),
140 ),
141);