-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.test.ts
More file actions
448 lines (375 loc) · 15.2 KB
/
search.test.ts
File metadata and controls
448 lines (375 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock dependencies before importing the module under test
vi.mock("child_process", () => ({
execSync: vi.fn(),
}));
vi.mock("fs", () => ({
existsSync: vi.fn(),
readFileSync: vi.fn(),
}));
vi.mock("globby", () => ({
globbySync: vi.fn(),
}));
vi.mock("../../src/utils/git.js", () => ({
REPOS_DIR: "/fake/repos",
getRepoPath: vi.fn((name: string) => `/fake/repos/${name}`),
}));
import { execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import { globbySync } from "globby";
import { getRepoPath } from "../../src/utils/git.js";
import {
searchCode,
searchDocs,
listExamples,
readFile,
findExample,
getFileType,
getResultPriority,
} from "../../src/utils/search.js";
const mockExecSync = vi.mocked(execSync);
const mockExistsSync = vi.mocked(existsSync);
const mockReadFileSync = vi.mocked(readFileSync);
const mockGlobbySync = vi.mocked(globbySync);
const mockGetRepoPath = vi.mocked(getRepoPath);
beforeEach(() => {
vi.clearAllMocks();
mockGetRepoPath.mockImplementation((name: string) => `/fake/repos/${name}`);
});
describe("getFileType", () => {
it('returns "contract" for .nr files', () => {
expect(getFileType("src/main.nr")).toBe("contract");
});
it('returns "test" for .nr files with "test" in path', () => {
expect(getFileType("tests/test_token.nr")).toBe("test");
expect(getFileType("src/test/main.nr")).toBe("test");
});
it('returns "typescript" for .ts and .tsx files', () => {
expect(getFileType("index.ts")).toBe("typescript");
expect(getFileType("component.tsx")).toBe("typescript");
});
it('returns "docs" for .md and .mdx files', () => {
expect(getFileType("README.md")).toBe("docs");
expect(getFileType("guide.mdx")).toBe("docs");
});
it('returns "other" for .json, .toml, and no extension', () => {
expect(getFileType("config.json")).toBe("other");
expect(getFileType("Nargo.toml")).toBe("other");
expect(getFileType("Makefile")).toBe("other");
});
});
describe("searchCode", () => {
it("returns [] when searchPath doesn't exist", () => {
mockExistsSync.mockReturnValue(false);
const results = searchCode("test");
expect(results).toEqual([]);
});
describe("ripgrep path", () => {
it("parses rg output correctly", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue(
"/fake/repos/aztec-packages/src/main.nr:10:fn main() {\n" +
"/fake/repos/aztec-packages/src/lib.nr:20:use dep::aztec;\n"
);
const results = searchCode("main");
expect(results).toHaveLength(2);
expect(results[0]).toEqual({
file: "aztec-packages/src/main.nr",
line: 10,
content: "fn main() {",
repo: "aztec-packages",
});
expect(results[1]).toEqual({
file: "aztec-packages/src/lib.nr",
line: 20,
content: "use dep::aztec;",
repo: "aztec-packages",
});
});
it("respects maxResults", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue(
"/fake/repos/r/a.nr:1:line1\n" +
"/fake/repos/r/b.nr:2:line2\n" +
"/fake/repos/r/c.nr:3:line3\n"
);
const results = searchCode("test", { maxResults: 2 });
expect(results).toHaveLength(2);
});
it("passes -i flag for case-insensitive search", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue("");
searchCode("test", { caseSensitive: false });
const call = mockExecSync.mock.calls[0][0] as string;
expect(call).toContain("-i");
});
it("does not pass -i flag when caseSensitive is true", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue("");
searchCode("test", { caseSensitive: true });
const call = mockExecSync.mock.calls[0][0] as string;
// The -i flag should not appear in the rg flags
// The call format is: rg <flags> "<query>" "<path>"
const flagsPart = call.split('"')[0];
expect(flagsPart).not.toContain("-i");
});
it("escapes shell-dangerous chars while preserving regex chars", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue("");
// Regex chars like |, *, + should be preserved
searchCode("foo|bar.*baz+");
let call = mockExecSync.mock.calls[0][0] as string;
expect(call).toContain("foo|bar.*baz+");
// Shell-dangerous chars should be escaped
mockExecSync.mockClear();
searchCode('test"$`\\!');
call = mockExecSync.mock.calls[0][0] as string;
expect(call).toContain('\\"');
expect(call).toContain("\\$");
expect(call).toContain("\\`");
expect(call).toContain("\\\\");
expect(call).toContain("\\!");
});
});
describe("manual fallback", () => {
it("activates when execSync throws", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockImplementation(() => {
throw new Error("rg not found");
});
mockGlobbySync.mockReturnValue([]);
const results = searchCode("test");
expect(results).toEqual([]);
expect(mockGlobbySync).toHaveBeenCalled();
});
it("uses globby to find and search files", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockImplementation(() => {
throw new Error("rg not found");
});
mockGlobbySync.mockReturnValue(["/fake/repos/myrepo/src/main.nr"]);
mockReadFileSync.mockReturnValue("line1\nfn test_func() {\nline3" as any);
const results = searchCode("test_func");
expect(results).toHaveLength(1);
expect(results[0].content).toBe("fn test_func() {");
expect(results[0].line).toBe(2);
});
it("handles invalid regex by escaping to literal", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockImplementation(() => {
throw new Error("rg not found");
});
mockGlobbySync.mockReturnValue(["/fake/repos/myrepo/src/main.nr"]);
mockReadFileSync.mockReturnValue("line with [invalid regex" as any);
// "[invalid regex" is invalid regex - should be escaped to literal
const results = searchCode("[invalid regex");
expect(results).toHaveLength(1);
});
it("skips unreadable files", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockImplementation(() => {
throw new Error("rg not found");
});
mockGlobbySync.mockReturnValue([
"/fake/repos/myrepo/a.nr",
"/fake/repos/myrepo/b.nr",
]);
mockReadFileSync
.mockImplementationOnce(() => {
throw new Error("EACCES");
})
.mockReturnValueOnce("fn test() {" as any);
const results = searchCode("test");
expect(results).toHaveLength(1);
expect(results[0].file).toBe("myrepo/b.nr");
});
});
});
describe("searchDocs", () => {
beforeEach(() => {
mockGetRepoPath.mockImplementation((name: string) => `/fake/repos/${name}`);
});
it("delegates to searchCode with *.{md,mdx} pattern", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue("");
searchDocs("tutorial");
const call = mockExecSync.mock.calls[0][0] as string;
expect(call).toContain("*.{md,mdx}");
});
it("narrows path when section exists", () => {
mockExistsSync.mockReturnValue(true);
mockGlobbySync.mockReturnValue(["version-v1.0.0"] as any);
mockExecSync.mockReturnValue("");
searchDocs("tutorial", { section: "tutorials" });
const call = mockExecSync.mock.calls[0][0] as string;
expect(call).toContain("aztec-packages-docs/docs/developer_versioned_docs/version-v1.0.0/tutorials");
});
it("falls back to aztec-packages-docs when section doesn't exist", () => {
// existsSync: first call for section path returns false, second for search path returns true
mockExistsSync
.mockReturnValueOnce(false) // section path doesn't exist
.mockReturnValueOnce(true); // search path exists
mockExecSync.mockReturnValue("");
searchDocs("tutorial", { section: "nonexistent" });
const call = mockExecSync.mock.calls[0][0] as string;
// Should search in aztec-packages-docs, not the nonexistent section
expect(call).toContain("/fake/repos/aztec-packages-docs");
});
});
describe("listExamples", () => {
beforeEach(() => {
mockGetRepoPath.mockImplementation((name: string) => `/fake/repos/${name}`);
});
it("finds contracts in both aztec-examples and aztec-packages/noir-contracts", () => {
mockExistsSync.mockReturnValue(true);
mockGlobbySync
.mockReturnValueOnce(["/fake/repos/aztec-examples/token/src/main.nr"])
.mockReturnValueOnce([
"/fake/repos/aztec-packages/noir-projects/noir-contracts/escrow/src/main.nr",
]);
const results = listExamples();
expect(results).toHaveLength(2);
expect(results[0].repo).toBe("aztec-examples");
expect(results[1].repo).toBe("aztec-packages");
});
it("filters by category (case-insensitive)", () => {
mockExistsSync.mockReturnValue(true);
mockGlobbySync
.mockReturnValueOnce(["/fake/repos/aztec-examples/token/src/main.nr"])
.mockReturnValueOnce([
"/fake/repos/aztec-packages/noir-projects/noir-contracts/escrow/src/main.nr",
]);
const results = listExamples("TOKEN");
expect(results).toHaveLength(1);
expect(results[0].name).toBe("token");
});
it("returns empty when repo paths don't exist", () => {
mockExistsSync.mockReturnValue(false);
const results = listExamples();
expect(results).toEqual([]);
});
});
describe("readFile", () => {
it("reads absolute paths directly", () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockReturnValue("file content" as any);
const result = readFile("/absolute/path/file.nr");
expect(result).toBe("file content");
expect(mockReadFileSync).toHaveBeenCalledWith("/absolute/path/file.nr", "utf-8");
});
it("prepends REPOS_DIR for relative paths", () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockReturnValue("file content" as any);
const result = readFile("aztec-packages/src/main.nr");
expect(result).toBe("file content");
expect(mockReadFileSync).toHaveBeenCalledWith(
"/fake/repos/aztec-packages/src/main.nr",
"utf-8"
);
});
it("returns null when file doesn't exist", () => {
mockExistsSync.mockReturnValue(false);
const result = readFile("nonexistent.nr");
expect(result).toBeNull();
});
it("returns null when read throws", () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockImplementation(() => {
throw new Error("EACCES");
});
const result = readFile("some/file.nr");
expect(result).toBeNull();
});
});
describe("findExample", () => {
beforeEach(() => {
mockGetRepoPath.mockImplementation((name: string) => `/fake/repos/${name}`);
});
it("exact name match takes priority over partial match", () => {
mockExistsSync.mockReturnValue(true);
mockGlobbySync
.mockReturnValueOnce([
"/fake/repos/aztec-examples/token/src/main.nr",
"/fake/repos/aztec-examples/token_bridge/src/main.nr",
])
.mockReturnValueOnce([]);
const result = findExample("token");
expect(result).not.toBeNull();
expect(result!.name).toBe("token");
});
it("returns null when no match", () => {
mockExistsSync.mockReturnValue(true);
mockGlobbySync.mockReturnValue([]);
const result = findExample("nonexistent");
expect(result).toBeNull();
});
});
describe("getResultPriority", () => {
it("ranks aztec-nr / yarn-project as highest priority (1)", () => {
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/yarn-project/aztec.js/src/main.ts", content: "", line: 1 })).toBe(1);
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/aztec-nr/aztec/src/lib.nr", content: "", line: 1 })).toBe(1);
});
it("ranks noir-contracts as priority 2", () => {
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/noir-projects/noir-contracts/token/src/main.nr", content: "", line: 1 })).toBe(2);
});
it("ranks noir_stdlib as priority 3", () => {
expect(getResultPriority({ repo: "noir", file: "noir/noir_stdlib/src/hash.nr", content: "", line: 1 })).toBe(3);
});
it("ranks other aztec-packages and noir paths as priority 4", () => {
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/boxes/token/src/main.nr", content: "", line: 1 })).toBe(4);
expect(getResultPriority({ repo: "noir", file: "noir/tooling/nargo/src/lib.rs", content: "", line: 1 })).toBe(4);
});
it("ranks example repos as lowest priority (5)", () => {
expect(getResultPriority({ repo: "aztec-examples", file: "aztec-examples/token/src/main.nr", content: "", line: 1 })).toBe(5);
expect(getResultPriority({ repo: "aztec-starter", file: "aztec-starter/src/main.nr", content: "", line: 1 })).toBe(5);
expect(getResultPriority({ repo: "gregoswap", file: "gregoswap/src/main.nr", content: "", line: 1 })).toBe(5);
});
});
describe("search result sorting", () => {
it("sorts ripgrep results by source priority", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue(
"/fake/repos/gregoswap/src/main.nr:1:fn transfer() {\n" +
"/fake/repos/aztec-packages/yarn-project/aztec.js/src/main.ts:5:fn transfer() {\n" +
"/fake/repos/aztec-examples/token/src/main.nr:3:fn transfer() {\n" +
"/fake/repos/aztec-packages/noir-projects/noir-contracts/token/src/main.nr:10:fn transfer() {\n" +
"/fake/repos/noir/noir_stdlib/src/hash.nr:7:fn transfer() {\n"
);
const results = searchCode("transfer");
expect(results[0].repo).toBe("aztec-packages");
expect(results[0].file).toContain("yarn-project");
expect(results[1].repo).toBe("aztec-packages");
expect(results[1].file).toContain("noir-contracts");
expect(results[2].repo).toBe("noir");
expect(results[2].file).toContain("noir_stdlib");
expect(results[3].repo).toBe("gregoswap");
expect(results[4].repo).toBe("aztec-examples");
});
it("sorts manual search results by source priority", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockImplementation(() => { throw new Error("rg not found"); });
mockGlobbySync.mockReturnValue([
"/fake/repos/aztec-starter/src/main.nr",
"/fake/repos/aztec-packages/yarn-project/aztec.js/src/lib.nr",
]);
mockReadFileSync
.mockReturnValueOnce("fn transfer() {" as any)
.mockReturnValueOnce("fn transfer() {" as any);
const results = searchCode("transfer");
expect(results).toHaveLength(2);
expect(results[0].repo).toBe("aztec-packages");
expect(results[1].repo).toBe("aztec-starter");
});
it("applies sorting before maxResults limit", () => {
mockExistsSync.mockReturnValue(true);
mockExecSync.mockReturnValue(
"/fake/repos/gregoswap/src/a.nr:1:match\n" +
"/fake/repos/aztec-examples/src/b.nr:2:match\n" +
"/fake/repos/aztec-packages/yarn-project/c.nr:3:match\n"
);
const results = searchCode("match", { maxResults: 2 });
expect(results).toHaveLength(2);
// SDK result should be kept even though it appeared last in raw output
expect(results[0].repo).toBe("aztec-packages");
});
});