-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathindex.test.ts
More file actions
403 lines (340 loc) · 13.5 KB
/
Copy pathpathindex.test.ts
File metadata and controls
403 lines (340 loc) · 13.5 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
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
import { PathIndex, validateGlobs } from "./pathindex.ts";
import type { IndexEntryDir, IndexEntryFile, IndexEntrySymlink } from "./pathindex.ts";
describe("PathIndex", () => {
describe("construction", () => {
it("empty paths", () => {
const ix = PathIndex.from(".", []);
expect(ix.paths).toEqual([]);
expect(ix.length).toBe(0);
});
it("stores paths", () => {
const ix = PathIndex.from(".", ["a.go", "pkg/b.go", "pkg/sub/c.go"]);
expect(ix.paths).toEqual(["a.go", "pkg/b.go", "pkg/sub/c.go"]);
expect(ix.length).toBe(3);
});
it("builds tree from flat paths", () => {
const ix = PathIndex.from(".", ["src/a.go", "src/b.go", "lib/c.go"]);
const root = ix.dir(".")!;
expect(root.children.map((c) => c.name).sort()).toEqual(["lib", "src"]);
expect(root.children.every((c) => c.type === "dir")).toBe(true);
});
});
describe("globSearch", () => {
it("returns all paths with no filters", () => {
const ix = PathIndex.from(".", ["a.go", "b.go", "c.go"]);
expect(ix.globSearch()).toEqual(["a.go", "b.go", "c.go"]);
});
describe("excludes", () => {
it("excludes by directory glob", () => {
const ix = PathIndex.from(".", [".git/HEAD", ".git/config", "src/main.go"]);
expect(ix.globSearch({ excludes: [".git/**"] })).toEqual(["src/main.go"]);
});
it("excludes nested directory glob", () => {
const ix = PathIndex.from(".", ["src/.git/config", "src/main.go"]);
expect(ix.globSearch({ excludes: ["**/.git/**"] })).toEqual(["src/main.go"]);
});
it("excludes by extension glob", () => {
const ix = PathIndex.from(".", ["main.go", "test.go", "readme.md"]);
expect(ix.globSearch({ excludes: ["*.md"] })).toEqual(["main.go", "test.go"]);
});
it("handles multiple excludes", () => {
const ix = PathIndex.from(".", [".git/HEAD", "src/main.go", "vendor/pkg/a.go"]);
expect(ix.globSearch({ excludes: [".git/**", "vendor/**"] })).toEqual(["src/main.go"]);
});
it("returns all when excludes is empty", () => {
const ix = PathIndex.from(".", ["anything.go"]);
expect(ix.globSearch({ excludes: [] })).toEqual(["anything.go"]);
});
});
describe("includes", () => {
it("includes by directory glob", () => {
const ix = PathIndex.from(".", ["lib/cache.go", "src/main.go", "src/util.go"]);
expect(ix.globSearch({ includes: ["src/**"] })).toEqual(["src/main.go", "src/util.go"]);
});
it("includes by multiple globs", () => {
const ix = PathIndex.from(".", ["lib/foo.go", "src/main.go", "test/bar.go"]);
expect(ix.globSearch({ includes: ["src/**", "lib/**"] })).toEqual(["lib/foo.go", "src/main.go"]);
});
it("includes by extension glob", () => {
const ix = PathIndex.from(".", ["main.go", "app.tsx", "lib/util.go", "lib/util.tsx"]);
expect(ix.globSearch({ includes: ["*.tsx"] })).toEqual(["app.tsx", "lib/util.tsx"]);
});
it("returns empty when nothing matches", () => {
const ix = PathIndex.from(".", ["lib/main.go"]);
expect(ix.globSearch({ includes: ["src/**"] })).toEqual([]);
});
it("returns all when includes is empty", () => {
const ix = PathIndex.from(".", ["src/main.go"]);
expect(ix.globSearch({ includes: [] })).toEqual(["src/main.go"]);
});
});
it("caps results with maxResults", () => {
const ix = PathIndex.from(".", ["a.go", "b.go", "c.go", "d.go", "e.go"]);
expect(ix.globSearch({ maxResults: 3 })).toHaveLength(3);
});
it("maxResults zero means no limit", () => {
const ix = PathIndex.from(".", ["a.go", "b.go", "c.go"]);
expect(ix.globSearch({ maxResults: 0 })).toHaveLength(3);
});
it("combines includes and excludes", () => {
const ix = PathIndex.from(".", [
".git/config",
"src/api/handler.go",
"src/auth/handler.go",
"test/auth/handler_test.go",
]);
const results = ix.globSearch({
includes: ["src/**"],
excludes: [".git/**"],
maxResults: 10,
});
expect(results).toEqual(["src/api/handler.go", "src/auth/handler.go"]);
});
});
describe("fuzzySearch", () => {
it("finds by filename", () => {
const ix = PathIndex.from(".", [
"src/auth/handler.go",
"src/api/config.go",
"lib/cache/redis.go",
"pkg/auth/middleware.go",
]);
const results = ix.fuzzySearch("handler");
expect(results[0]).toBe("src/auth/handler.go");
});
it("multi-word waterfall narrows results", () => {
const ix = PathIndex.from(".", [
"src/auth/handler.go",
"src/api/handler.go",
"src/api/config.go",
"lib/auth/middleware.go",
]);
const results = ix.fuzzySearch("handler auth");
expect(results[0]).toBe("src/auth/handler.go");
expect(results).not.toContain("src/api/handler.go");
});
it("respects maxResults", () => {
const ix = PathIndex.from(".", ["a.go", "b.go", "c.go", "d.go", "e.go"]);
expect(ix.fuzzySearch("go", { maxResults: 2 })).toHaveLength(2);
});
it("applies excludes before fuzzy", () => {
const ix = PathIndex.from(".", ["src/handler.go", "vendor/handler.go"]);
const results = ix.fuzzySearch("handler", { excludes: ["vendor/**"] });
expect(results).toEqual(["src/handler.go"]);
});
it("applies includes before fuzzy", () => {
const ix = PathIndex.from(".", ["src/handler.go", "lib/handler.go"]);
const results = ix.fuzzySearch("handler", { includes: ["src/**"] });
expect(results).toEqual(["src/handler.go"]);
});
});
describe("dir", () => {
const ix = PathIndex.from(".", ["README.md", "lib/cache.go", "main.go", "src/bar/baz.go", "src/foo.go"]);
it("returns root via dot", () => {
const root = ix.dir(".");
expect(root).toBeDefined();
expect(root?.children.map((c) => c.name).sort()).toEqual(["README.md", "lib", "main.go", "src"]);
});
it("returns root via empty string", () => {
expect(ix.dir("")).toBe(ix.dir("."));
});
it("looks up subdirectory", () => {
const src = ix.dir("src");
expect(src?.name).toBe("src");
expect(src?.children.map((c) => c.name).sort()).toEqual(["bar", "foo.go"]);
});
it("looks up nested subdirectory", () => {
const bar = ix.dir("src/bar");
expect(bar?.name).toBe("bar");
});
it("strips trailing slash", () => {
expect(ix.dir("src/")).toBe(ix.dir("src"));
});
it("returns undefined for nonexistent path", () => {
expect(ix.dir("nope")).toBeUndefined();
});
it("returns undefined for file path", () => {
expect(ix.dir("README.md")).toBeUndefined();
});
it("explicit empty directory", () => {
const ix2 = PathIndex.from(".", ["src/", "src/sub/", "main.go"]);
const sub = ix2.dir("src/sub");
expect(sub).toBeDefined();
expect(sub?.children).toEqual([]);
});
it("explicit dir mixed with files", () => {
const ix2 = PathIndex.from(".", ["vendor/", "vendor/lib.go", "main.go"]);
const vendor = ix2.dir("vendor");
expect(vendor?.children.map((c) => c.name)).toEqual(["lib.go"]);
});
it("explicit dir in paths without trailing slash", () => {
const ix2 = PathIndex.from(".", ["empty/", "file.go"]);
expect(ix2.paths).toContain("empty");
expect(ix2.paths).not.toContain("empty/");
});
});
describe("get", () => {
const ix = PathIndex.from(".", ["src/main.go", "lib/util.go"]);
it("returns file entry by path", () => {
const entry = ix.get("src/main.go");
expect(entry?.type).toBe("file");
expect(entry?.name).toBe("main.go");
});
it("returns dir entry by path", () => {
const entry = ix.get("src");
expect(entry?.type).toBe("dir");
});
it("returns undefined for unknown path", () => {
expect(ix.get("nope")).toBeUndefined();
});
});
describe("validateGlobs", () => {
it("returns undefined for valid patterns", () => {
expect(validateGlobs(["*.go", "src/**", "**/*.tsx"])).toBeUndefined();
});
it("returns undefined for empty array", () => {
expect(validateGlobs([])).toBeUndefined();
});
});
});
async function createFiles(root: string, paths: string[]): Promise<void> {
for (const p of paths) {
const fullPath = join(root, p);
await mkdir(dirname(fullPath), { recursive: true });
await writeFile(fullPath, "x");
}
}
describe("PathIndex.new", () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "pathindex-"));
});
afterEach(async () => {
await rm(tempDir, { recursive: true });
});
it("empty directory", async () => {
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual([]);
expect(ix.length).toBe(0);
});
it("flat files", async () => {
await createFiles(tempDir, ["a.go", "README.md"]);
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual(expect.arrayContaining(["a.go", "README.md"]));
expect(ix.length).toBe(2);
});
it("nested files", async () => {
await createFiles(tempDir, ["top.go", "pkg/a.go", "pkg/sub/b.go", "pkg/sub/deeper/c.go"]);
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual(expect.arrayContaining(["top.go", "pkg/a.go", "pkg/sub/b.go", "pkg/sub/deeper/c.go"]));
expect(ix.length).toBe(4);
});
it("excludes .git directory", async () => {
await createFiles(tempDir, [
"src/main.go",
".git/HEAD",
".git/objects/ab/cdef",
"node_modules/lib/index.js",
".hidden",
]);
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual(expect.arrayContaining(["src/main.go", "node_modules/lib/index.js", ".hidden"]));
expect(ix.paths.filter((p) => p.startsWith(".git/"))).toHaveLength(0);
expect(ix.length).toBe(3);
});
it("returns paths in sorted order", async () => {
await createFiles(tempDir, ["c.go", "a.go", "b.go"]);
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual(["a.go", "b.go", "c.go"]);
});
it("sorts paths lexicographically", async () => {
await createFiles(tempDir, ["z.go", "a/y.go", "a/b/x.go"]);
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toEqual(["a/b/x.go", "a/y.go", "z.go"]);
});
it("tracks file sizes", async () => {
await writeFile(join(tempDir, "small.txt"), "hi");
await writeFile(join(tempDir, "big.txt"), "a".repeat(1000));
const ix = await PathIndex.new(tempDir);
const small = ix.get("small.txt") as IndexEntryFile;
const big = ix.get("big.txt") as IndexEntryFile;
expect(small.size).toBe(2);
expect(big.size).toBe(1000);
expect(ix.get("nonexistent")).toBeUndefined();
});
it("counts lines in text files", async () => {
await writeFile(join(tempDir, "three.txt"), "one\ntwo\nthree\n");
await writeFile(join(tempDir, "no-trailing.txt"), "one\ntwo");
const ix = await PathIndex.new(tempDir);
const three = ix.get("three.txt") as IndexEntryFile;
const noTrailing = ix.get("no-trailing.txt") as IndexEntryFile;
expect(three.lineCount).toBe(3);
expect(three.isBinary).toBe(false);
expect(noTrailing.lineCount).toBe(2);
expect(noTrailing.isBinary).toBe(false);
});
it("tracks max line length", async () => {
await writeFile(join(tempDir, "varied.txt"), "short\na longer line here\nhi\n");
const ix = await PathIndex.new(tempDir);
const entry = ix.get("varied.txt") as IndexEntryFile;
expect(entry.maxLineLen).toBe(Buffer.byteLength("a longer line here"));
});
it("detects binary files", async () => {
await writeFile(join(tempDir, "binary.bin"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0x0a, 0x1a]));
const ix = await PathIndex.new(tempDir);
const entry = ix.get("binary.bin") as IndexEntryFile;
expect(entry.isBinary).toBe(true);
expect(entry.lineCount).toBe(0);
expect(entry.maxLineLen).toBe(0);
});
it("handles empty files", async () => {
await writeFile(join(tempDir, "empty.txt"), "");
const ix = await PathIndex.new(tempDir);
const entry = ix.get("empty.txt") as IndexEntryFile;
expect(entry.lineCount).toBe(0);
expect(entry.maxLineLen).toBe(0);
expect(entry.isBinary).toBe(false);
});
it("builds tree structure", async () => {
await createFiles(tempDir, ["src/a.go", "src/b.go", "lib/c.go"]);
const ix = await PathIndex.new(tempDir);
const src = ix.dir("src") as IndexEntryDir;
expect(src.type).toBe("dir");
expect(src.children.filter((c) => c.type === "file")).toHaveLength(2);
});
it("indexes symlinks to files", async () => {
await writeFile(join(tempDir, "real.txt"), "content");
await symlink(join(tempDir, "real.txt"), join(tempDir, "link.txt"));
const ix = await PathIndex.new(tempDir);
expect(ix.paths).toContain("link.txt");
const entry = ix.get("link.txt") as IndexEntrySymlink;
expect(entry.type).toBe("symlink");
expect(entry.targetType).toBe("file");
expect(entry.size).toBe(7);
});
it("indexes symlinks to dirs without recursing", async () => {
await createFiles(tempDir, ["real/a.go", "real/b.go"]);
await symlink(join(tempDir, "real"), join(tempDir, "linked"));
const ix = await PathIndex.new(tempDir);
const entry = ix.get("linked") as IndexEntrySymlink;
expect(entry.type).toBe("symlink");
expect(entry.targetType).toBe("dir");
expect(ix.paths.filter((p) => p.startsWith("linked/"))).toHaveLength(0);
});
it("throws on aborted signal", async () => {
await createFiles(tempDir, ["a.go"]);
const controller = new AbortController();
controller.abort();
await expect(PathIndex.new(tempDir, controller.signal)).rejects.toThrow();
});
it("dir via tree lookup", async () => {
await createFiles(tempDir, ["main.go", "README.md", "src/foo.go", "src/bar/baz.go", "lib/cache.go"]);
const ix = await PathIndex.new(tempDir);
const root = ix.dir(".")!;
expect(root.children.map((c) => c.name).sort()).toEqual(["README.md", "lib", "main.go", "src"]);
});
});