Open History to see the last commit
1import fs from "node:fs";
2import fsPromises from "node:fs/promises";
3import os from "node:os";
4import path from "node:path";
5import { spawnSync } from "node:child_process";
6
7import { afterEach, assert, describe, it, vi } from "vitest";
8
9import { searchWorkspaceEntries } from "./workspaceEntries";
10
11const tempDirs: string[] = [];
12
13function makeTempDir(prefix: string): string {
14 const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
15 tempDirs.push(dir);
16 return dir;
17}
18
19function writeFile(cwd: string, relativePath: string, contents = ""): void {
20 const absolutePath = path.join(cwd, relativePath);
21 fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
22 fs.writeFileSync(absolutePath, contents, "utf8");
23}
24
25function runGit(cwd: string, args: string[]): void {
26 const result = spawnSync("git", args, { cwd, encoding: "utf8" });
27 if (result.status !== 0) {
28 throw new Error(result.stderr || `git ${args.join(" ")} failed`);
29 }
30}
31
32describe("searchWorkspaceEntries", () => {
33 afterEach(() => {
34 vi.restoreAllMocks();
35 for (const dir of tempDirs.splice(0, tempDirs.length)) {
36 fs.rmSync(dir, { recursive: true, force: true });
37 }
38 });
39
40 it("returns files and directories relative to cwd", async () => {
41 const cwd = makeTempDir("t3code-workspace-entries-");
42 writeFile(cwd, "src/components/Composer.tsx");
43 writeFile(cwd, "src/index.ts");
44 writeFile(cwd, "README.md");
45 writeFile(cwd, ".git/HEAD");
46 writeFile(cwd, "node_modules/pkg/index.js");
47
48 const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
49 const paths = result.entries.map((entry) => entry.path);
50
51 assert.include(paths, "src");
52 assert.include(paths, "src/components");
53 assert.include(paths, "src/components/Composer.tsx");
54 assert.include(paths, "README.md");
55 assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".git")));
56 assert.isFalse(paths.some((entryPath) => entryPath.startsWith("node_modules")));
57 assert.isFalse(result.truncated);
58 });
59
60 it("filters and ranks entries by query", async () => {
61 const cwd = makeTempDir("t3code-workspace-query-");
62 writeFile(cwd, "src/components/Composer.tsx");
63 writeFile(cwd, "src/components/composePrompt.ts");
64 writeFile(cwd, "docs/composition.md");
65
66 const result = await searchWorkspaceEntries({ cwd, query: "compo", limit: 5 });
67
68 assert.isAbove(result.entries.length, 0);
69 assert.isTrue(result.entries.some((entry) => entry.path === "src/components"));
70 assert.isTrue(result.entries.every((entry) => entry.path.toLowerCase().includes("compo")));
71 });
72
73 it("excludes gitignored paths for git repositories", async () => {
74 const cwd = makeTempDir("t3code-workspace-gitignore-");
75 runGit(cwd, ["init"]);
76 writeFile(cwd, ".gitignore", ".convex/\nconvex/\nignored.txt\n");
77 writeFile(cwd, "src/keep.ts", "export {};");
78 writeFile(cwd, "ignored.txt", "ignore me");
79 writeFile(cwd, ".convex/local-storage/data.json", "{}");
80 writeFile(cwd, "convex/UOoS-l/convex_local_storage/modules/data.json", "{}");
81
82 const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
83 const paths = result.entries.map((entry) => entry.path);
84
85 assert.include(paths, "src");
86 assert.include(paths, "src/keep.ts");
87 assert.notInclude(paths, "ignored.txt");
88 assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
89 assert.isFalse(paths.some((entryPath) => entryPath.startsWith("convex/")));
90 });
91
92 it("excludes tracked paths that match ignore rules", async () => {
93 const cwd = makeTempDir("t3code-workspace-tracked-gitignore-");
94 runGit(cwd, ["init"]);
95 writeFile(cwd, ".convex/local-storage/data.json", "{}");
96 writeFile(cwd, "src/keep.ts", "export {};");
97 runGit(cwd, ["add", ".convex/local-storage/data.json", "src/keep.ts"]);
98 writeFile(cwd, ".gitignore", ".convex/\n");
99
100 const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
101 const paths = result.entries.map((entry) => entry.path);
102
103 assert.include(paths, "src");
104 assert.include(paths, "src/keep.ts");
105 assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
106 });
107
108 it("excludes .convex in non-git workspaces", async () => {
109 const cwd = makeTempDir("t3code-workspace-non-git-convex-");
110 writeFile(cwd, ".convex/local-storage/data.json", "{}");
111 writeFile(cwd, "src/keep.ts", "export {};");
112
113 const result = await searchWorkspaceEntries({ cwd, query: "", limit: 100 });
114 const paths = result.entries.map((entry) => entry.path);
115
116 assert.include(paths, "src");
117 assert.include(paths, "src/keep.ts");
118 assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
119 });
120
121 it("deduplicates concurrent index builds for the same cwd", async () => {
122 const cwd = makeTempDir("t3code-workspace-concurrent-build-");
123 writeFile(cwd, "src/components/Composer.tsx");
124
125 let rootReadCount = 0;
126 const originalReaddir = fsPromises.readdir.bind(fsPromises);
127 vi.spyOn(fsPromises, "readdir").mockImplementation((async (
128 ...args: Parameters<typeof fsPromises.readdir>
129 ) => {
130 if (args[0] === cwd) {
131 rootReadCount += 1;
132 await new Promise((resolve) => setTimeout(resolve, 20));
133 }
134 return originalReaddir(...args);
135 }) as typeof fsPromises.readdir);
136
137 await Promise.all([
138 searchWorkspaceEntries({ cwd, query: "", limit: 100 }),
139 searchWorkspaceEntries({ cwd, query: "comp", limit: 100 }),
140 searchWorkspaceEntries({ cwd, query: "src", limit: 100 }),
141 ]);
142
143 assert.equal(rootReadCount, 1);
144 });
145
146 it("limits concurrent directory reads while walking the filesystem", async () => {
147 const cwd = makeTempDir("t3code-workspace-read-concurrency-");
148 for (let index = 0; index < 80; index += 1) {
149 writeFile(cwd, `group-${index}/entry-${index}.ts`, "export {};");
150 }
151
152 let activeReads = 0;
153 let peakReads = 0;
154 const originalReaddir = fsPromises.readdir.bind(fsPromises);
155 vi.spyOn(fsPromises, "readdir").mockImplementation((async (
156 ...args: Parameters<typeof fsPromises.readdir>
157 ) => {
158 const target = args[0];
159 if (typeof target === "string" && target.startsWith(cwd)) {
160 activeReads += 1;
161 peakReads = Math.max(peakReads, activeReads);
162 await new Promise((resolve) => setTimeout(resolve, 4));
163 try {
164 return await originalReaddir(...args);
165 } finally {
166 activeReads -= 1;
167 }
168 }
169 return originalReaddir(...args);
170 }) as typeof fsPromises.readdir);
171
172 await searchWorkspaceEntries({ cwd, query: "", limit: 200 });
173
174 assert.isAtMost(peakReads, 32);
175 });
176});