forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspaceEntries.test.ts
More file actions
279 lines (228 loc) · 10.5 KB
/
workspaceEntries.test.ts
File metadata and controls
279 lines (228 loc) · 10.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
import fs from "node:fs";
import fsPromises from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { afterEach, assert, describe, it, vi } from "vitest";
import { browseDirectories, searchWorkspaceEntries } from "./workspaceEntries";
const tempDirs: string[] = [];
function makeTempDir(prefix: string): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
function writeFile(cwd: string, relativePath: string, contents = ""): void {
const absolutePath = path.join(cwd, relativePath);
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
fs.writeFileSync(absolutePath, contents, "utf8");
}
function runGit(cwd: string, args: string[]): void {
const result = spawnSync("git", args, { cwd, encoding: "utf8" });
if (result.status !== 0) {
throw new Error(result.stderr || `git ${args.join(" ")} failed`);
}
}
describe("searchWorkspaceEntries", () => {
afterEach(() => {
vi.restoreAllMocks();
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it("returns files and directories relative to cwd", async () => {
const cwd = makeTempDir("marcode-workspace-entries-");
writeFile(cwd, "src/components/Composer.tsx");
writeFile(cwd, "src/index.ts");
writeFile(cwd, "README.md");
writeFile(cwd, ".git/HEAD");
writeFile(cwd, "node_modules/pkg/index.js");
const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
const paths = result.entries.map((entry) => entry.path);
assert.include(paths, "src");
assert.include(paths, "src/components");
assert.include(paths, "src/components/Composer.tsx");
assert.include(paths, "README.md");
assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".git")));
assert.isFalse(paths.some((entryPath) => entryPath.startsWith("node_modules")));
assert.isFalse(result.truncated);
});
it("filters and ranks entries by query", async () => {
const cwd = makeTempDir("marcode-workspace-query-");
writeFile(cwd, "src/components/Composer.tsx");
writeFile(cwd, "src/components/composePrompt.ts");
writeFile(cwd, "docs/composition.md");
const result = await searchWorkspaceEntries({ cwd, query: "compo", limit: 5 });
assert.isAbove(result.entries.length, 0);
assert.isTrue(result.entries.some((entry) => entry.path === "src/components"));
assert.isTrue(result.entries.every((entry) => entry.path.toLowerCase().includes("compo")));
});
it("supports fuzzy subsequence queries for composer path search", async () => {
const cwd = makeTempDir("marcode-workspace-fuzzy-query-");
writeFile(cwd, "src/components/Composer.tsx");
writeFile(cwd, "src/components/composePrompt.ts");
writeFile(cwd, "docs/composition.md");
const result = await searchWorkspaceEntries({ cwd, query: "cmp", limit: 10 });
const paths = result.entries.map((entry) => entry.path);
assert.isAbove(result.entries.length, 0);
assert.include(paths, "src/components");
assert.include(paths, "src/components/Composer.tsx");
});
it("tracks truncation without sorting every fuzzy match", async () => {
const cwd = makeTempDir("marcode-workspace-fuzzy-limit-");
writeFile(cwd, "src/components/Composer.tsx");
writeFile(cwd, "src/components/composePrompt.ts");
writeFile(cwd, "docs/composition.md");
const result = await searchWorkspaceEntries({ cwd, query: "cmp", limit: 1 });
assert.lengthOf(result.entries, 1);
assert.isTrue(result.truncated);
});
it("excludes gitignored paths for git repositories", async () => {
const cwd = makeTempDir("marcode-workspace-gitignore-");
runGit(cwd, ["init"]);
writeFile(cwd, ".gitignore", ".convex/\nconvex/\nignored.txt\n");
writeFile(cwd, "src/keep.ts", "export {};");
writeFile(cwd, "ignored.txt", "ignore me");
writeFile(cwd, ".convex/local-storage/data.json", "{}");
writeFile(cwd, "convex/UOoS-l/convex_local_storage/modules/data.json", "{}");
const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
const paths = result.entries.map((entry) => entry.path);
assert.include(paths, "src");
assert.include(paths, "src/keep.ts");
assert.notInclude(paths, "ignored.txt");
assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
assert.isFalse(paths.some((entryPath) => entryPath.startsWith("convex/")));
});
it("excludes tracked paths that match ignore rules", async () => {
const cwd = makeTempDir("marcode-workspace-tracked-gitignore-");
runGit(cwd, ["init"]);
writeFile(cwd, ".convex/local-storage/data.json", "{}");
writeFile(cwd, "src/keep.ts", "export {};");
runGit(cwd, ["add", ".convex/local-storage/data.json", "src/keep.ts"]);
writeFile(cwd, ".gitignore", ".convex/\n");
const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
const paths = result.entries.map((entry) => entry.path);
assert.include(paths, "src");
assert.include(paths, "src/keep.ts");
assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
});
it("excludes .convex in non-git workspaces", async () => {
const cwd = makeTempDir("marcode-workspace-non-git-convex-");
writeFile(cwd, ".convex/local-storage/data.json", "{}");
writeFile(cwd, "src/keep.ts", "export {};");
const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
const paths = result.entries.map((entry) => entry.path);
assert.include(paths, "src");
assert.include(paths, "src/keep.ts");
assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
});
it("deduplicates concurrent index builds for the same cwd", async () => {
const cwd = makeTempDir("marcode-workspace-concurrent-build-");
writeFile(cwd, "src/components/Composer.tsx");
let rootReadCount = 0;
const originalReaddir = fsPromises.readdir.bind(fsPromises);
vi.spyOn(fsPromises, "readdir").mockImplementation((async (
...args: Parameters<typeof fsPromises.readdir>
) => {
if (args[0] === cwd) {
rootReadCount += 1;
await new Promise((resolve) => setTimeout(resolve, 20));
}
return originalReaddir(...args);
}) as typeof fsPromises.readdir);
await Promise.all([
searchWorkspaceEntries({ cwd, query: "", limit: 100 }),
searchWorkspaceEntries({ cwd, query: "comp", limit: 100 }),
searchWorkspaceEntries({ cwd, query: "src", limit: 100 }),
]);
assert.equal(rootReadCount, 1);
});
it("limits concurrent directory reads while walking the filesystem", async () => {
const cwd = makeTempDir("marcode-workspace-read-concurrency-");
for (let index = 0; index < 80; index += 1) {
writeFile(cwd, `group-${index}/entry-${index}.ts`, "export {};");
}
let activeReads = 0;
let peakReads = 0;
const originalReaddir = fsPromises.readdir.bind(fsPromises);
vi.spyOn(fsPromises, "readdir").mockImplementation((async (
...args: Parameters<typeof fsPromises.readdir>
) => {
const target = args[0];
if (typeof target === "string" && target.startsWith(cwd)) {
activeReads += 1;
peakReads = Math.max(peakReads, activeReads);
await new Promise((resolve) => setTimeout(resolve, 4));
try {
return await originalReaddir(...args);
} finally {
activeReads -= 1;
}
}
return originalReaddir(...args);
}) as typeof fsPromises.readdir);
await searchWorkspaceEntries({ cwd, query: "", limit: 200 });
assert.isAtMost(peakReads, 32);
});
});
describe("browseDirectories", () => {
afterEach(() => {
vi.restoreAllMocks();
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it("returns directory entries relative to cwd with resolvedParent", async () => {
const cwd = makeTempDir("marcode-browse-basic-");
fs.mkdirSync(path.join(cwd, "alpha"));
fs.mkdirSync(path.join(cwd, "beta"));
writeFile(cwd, "readme.md", "");
const result = await browseDirectories({ cwd, pathQuery: "", limit: 100 });
const names = result.entries.map((entry) => entry.path);
assert.sameMembers(names, ["alpha", "beta"]);
assert.equal(result.resolvedParent, path.resolve(cwd));
assert.isFalse(result.truncated);
});
it("resolves absolute paths in pathQuery regardless of cwd", async () => {
const cwd = makeTempDir("marcode-browse-cwd-");
const other = makeTempDir("marcode-browse-abs-");
fs.mkdirSync(path.join(other, "nested"));
const result = await browseDirectories({ cwd, pathQuery: `${other}/`, limit: 100 });
const names = result.entries.map((entry) => entry.path);
assert.equal(result.resolvedParent, path.resolve(other));
assert.include(
names.map((name) => path.basename(name)),
"nested",
);
});
it("expands ~/ in cwd to the user's home directory", async () => {
const homeDir = os.homedir();
const sentinel = `marcode-browse-home-sentinel-${process.pid}-${Date.now()}`;
const sentinelPath = path.join(homeDir, sentinel);
fs.mkdirSync(sentinelPath);
try {
const result = await browseDirectories({ cwd: "~/", pathQuery: "", limit: 100 });
assert.equal(result.resolvedParent, path.resolve(homeDir));
assert.isTrue(result.entries.some((entry) => path.basename(entry.path) === sentinel));
} finally {
fs.rmSync(sentinelPath, { recursive: true, force: true });
}
});
it("resolves ../ relative to cwd", async () => {
const parent = makeTempDir("marcode-browse-parent-");
const child = path.join(parent, "child");
fs.mkdirSync(child);
fs.mkdirSync(path.join(parent, "sibling"));
const result = await browseDirectories({ cwd: child, pathQuery: "../", limit: 100 });
assert.equal(result.resolvedParent, path.resolve(parent));
const names = result.entries.map((entry) => path.basename(entry.path));
assert.includeMembers(names, ["child", "sibling"]);
});
it("returns empty entries and resolvedParent when directory does not exist", async () => {
const cwd = makeTempDir("marcode-browse-missing-");
const missing = path.join(cwd, "does-not-exist");
const result = await browseDirectories({ cwd: missing, pathQuery: "", limit: 100 });
assert.deepEqual([...result.entries], []);
assert.equal(result.resolvedParent, path.resolve(missing));
assert.isFalse(result.truncated);
});
});