Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceFolders.test.ts

424 lines16.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { ProjectId, type ProjectTrackedFolder } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8​
9import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
10import * as SqlitePersistence from "../persistence/Sqlite.ts";
11import * as TaskFolderStore from "./TaskFolderStore.ts";
12import * as WorkspaceFolders from "./WorkspaceFolders.ts";
13​
14const projectId = ProjectId.make("project:workspace-folders");
15​
16const layerFor = (project?: {
17 readonly workspaceRoot: string;
18 readonly trackedFolders: ReadonlyArray<ProjectTrackedFolder>;
19}) =>
20 WorkspaceFolders.layer.pipe(
21 Layer.provide(
22 Layer.mock(ProjectStore.ProjectStoreV2)({
23 get: () =>
24 project === undefined
25 ? Effect.succeedNone
26 : Effect.succeedSome({
27 projectId,
28 title: "Project",
29 hasCustomTitle: false,
30 defaultModelSelection: null,
31 defaultThreadEnvMode: null,
32 autoPull: false,
33 faviconPath: null,
34 projectIcon: null,
35 scripts: [],
36 pinnedAt: null,
37 archivedAt: null,
38 createdAt: "2026-06-21T00:00:00.000Z",
39 updatedAt: "2026-06-21T00:00:00.000Z",
40 deletedAt: null,
41 ...project,
42 }),
43 }),
44 ),
45 Layer.provideMerge(TaskFolderStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory))),
46 );
47​
48// Resolved, so a symlinked temp directory does not show up as a second spelling of each path.
49const makeTempDir = Effect.gen(function* () {
50 const fileSystem = yield* FileSystem.FileSystem;
51 return yield* fileSystem.realPath(
52 yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-workspace-folders-" }),
53 );
54});
55​
56it.layer(NodeServices.layer)("WorkspaceFolders", (it) => {
57 it.effect("scans child repositories and the folders a code-workspace file lists", () =>
58 Effect.gen(function* () {
59 const fileSystem = yield* FileSystem.FileSystem;
60 const path = yield* Path.Path;
61 const tmp = yield* makeTempDir;
62 const root = path.join(tmp, "wrapper");
63 const outside = path.join(tmp, "outside");
64 for (const folder of [
65 ".git",
66 "api/.git",
67 "web/.git",
68 "sub/.git",
69 "notes",
70 "src/svc/.git",
71 "tree",
72 ".hidden/.git",
73 "node_modules/.git",
74 "../outside/.git",
75 ]) {
76 yield* fileSystem.makeDirectory(path.join(root, folder), { recursive: true });
77 }
78 yield* fileSystem.symlink(outside, path.join(root, "linked"));
79 yield* fileSystem.symlink(root, path.join(root, "self"));
80 // A worktree of `api`: its history is that repository's.
81 yield* fileSystem.writeFileString(
82 path.join(root, "tree", ".git"),
83 `gitdir: ${path.join(root, "api", ".git", "worktrees", "tree")}\n`,
84 );
85 yield* fileSystem.writeFileString(
86 path.join(root, ".gitmodules"),
87 '[submodule "sub"]\n\tpath = sub\n\turl = https://example.test/sub.git\n',
88 );
89 yield* fileSystem.writeFileString(
90 path.join(root, "team.code-workspace"),
91 `{
92 // Editors write comments and trailing commas here.
93 "folders": [
94 // Entries that lead nowhere take none of the room the real ones need.
95 ${Array.from({ length: 130 }, (_, index) => `{ "path": "gone-${index}" },`).join("")}
96 { "path": "api", "name": "API" },
97 { "path": "notes" },
98 { "path": "../outside" },
99 { "path": "gone" },
100 { "path": "." },
101 ],
102 }`,
103 );
104​
105 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
106 assert.deepEqual(yield* workspaceFolders.scan({ workspaceRoot: `${root}${path.sep}` }), {
107 workspaceRoot: root,
108 isGitRepository: true,
109 codeWorkspaceFile: path.join(root, "team.code-workspace"),
110 folders: [
111 {
112 path: path.join(root, "api"),
113 name: "API",
114 isGitRepository: true,
115 source: "code-workspace",
116 },
117 {
118 path: path.join(root, "notes"),
119 name: "notes",
120 isGitRepository: false,
121 source: "code-workspace",
122 },
123 { path: outside, name: "outside", isGitRepository: true, source: "code-workspace" },
124 // One level below a plain folder is looked into.
125 {
126 path: path.join(root, "src", "svc"),
127 name: "svc",
128 isGitRepository: true,
129 source: "child",
130 },
131 { path: path.join(root, "web"), name: "web", isGitRepository: true, source: "child" },
132 ],
133 });
134 assert.equal(
135 (yield* workspaceFolders.scan({ workspaceRoot: path.join(root, "gone") }).pipe(Effect.flip))
136 ._tag,
137 "WorkspaceFoldersError",
138 );
139 }).pipe(Effect.provide(layerFor())),
140 );
141​
142 it.effect("finds the repositories inside a project folder from disk each time", () =>
143 Effect.gen(function* () {
144 const fileSystem = yield* FileSystem.FileSystem;
145 const path = yield* Path.Path;
146 const tmp = yield* makeTempDir;
147 const workspaceRoot = path.join(tmp, "wrapper");
148 const outside = path.join(tmp, "outside");
149 const repository = (name: string) => path.join(workspaceRoot, "src", name);
150 for (const folder of ["src/api/.git", "src/notes", "docs/.git", "vendor/big/.git", "tree"]) {
151 yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true });
152 }
153 // A worktree of `docs` follows that repository, even when a workspace file lists it.
154 yield* fileSystem.writeFileString(
155 path.join(workspaceRoot, "tree", ".git"),
156 `gitdir: ${path.join(workspaceRoot, "docs", ".git", "worktrees", "tree")}\n`,
157 );
158 yield* fileSystem.writeFileString(
159 path.join(workspaceRoot, "team.code-workspace"),
160 JSON.stringify({ folders: [{ path: "tree" }] }),
161 );
162 yield* fileSystem.makeDirectory(path.join(outside, ".git"), { recursive: true });
163 // A link to a repository elsewhere is not inside the project.
164 yield* fileSystem.symlink(outside, path.join(workspaceRoot, "linked"));
165 yield* fileSystem.writeFileString(
166 path.join(workspaceRoot, "t3.json"),
167 JSON.stringify({ ignoreRepositories: ["vendor/big"] }),
168 );
169 // The stored list is only trusted for what someone added.
170 const trackedFolders = [
171 { path: path.join(workspaceRoot, "gone"), name: "gone", discovered: true },
172 { path: outside, name: "Outside" },
173 ];
174​
175 yield* Effect.gen(function* () {
176 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
177 const listed = Effect.map(workspaceFolders.list({ projectId }), ({ folders }) =>
178 folders.map((folder) => [folder.name, folder.path]),
179 );
180 assert.deepEqual(yield* listed, [
181 ["docs", path.join(workspaceRoot, "docs")],
182 ["api", repository("api")],
183 ["Outside", outside],
184 ]);
185 yield* fileSystem.makeDirectory(path.join(repository("web"), ".git"), { recursive: true });
186 yield* fileSystem.remove(repository("api"), { recursive: true });
187 assert.deepEqual(yield* listed, [
188 ["docs", path.join(workspaceRoot, "docs")],
189 ["web", repository("web")],
190 ["Outside", outside],
191 ]);
192 }).pipe(Effect.provide(layerFor({ workspaceRoot, trackedFolders })));
193 }),
194 );
195​
196 it.effect("keeps the folders someone added when a project holds more than fit", () =>
197 Effect.gen(function* () {
198 const fileSystem = yield* FileSystem.FileSystem;
199 const path = yield* Path.Path;
200 const tmp = yield* makeTempDir;
201 const workspaceRoot = path.join(tmp, "wrapper");
202 const outside = path.join(tmp, "outside");
203 const longName = "r".repeat(200);
204 yield* Effect.forEach(
205 [longName, ...Array.from({ length: 130 }, (_, index) => `s${index}`)],
206 (name) =>
207 fileSystem.makeDirectory(path.join(workspaceRoot, "src", name, ".git"), {
208 recursive: true,
209 }),
210 { discard: true },
211 );
212 yield* fileSystem.makeDirectory(outside, { recursive: true });
213​
214 yield* Effect.gen(function* () {
215 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
216 const { folders } = yield* workspaceFolders.list({ projectId });
217 assert.equal(folders.length, 128);
218 assert.deepEqual(
219 [folders[0]!.name, folders[0]!.path],
220 ["r".repeat(128), path.join(workspaceRoot, "src", longName)],
221 );
222 assert.deepEqual([folders.at(-1)!.name, folders.at(-1)!.path], ["Outside", outside]);
223 }).pipe(
224 Effect.provide(
225 layerFor({ workspaceRoot, trackedFolders: [{ path: outside, name: "Outside" }] }),
226 ),
227 );
228 }),
229 );
230​
231 it.effect("reports each tracked folder's state and grants a thread the ones outside", () =>
232 Effect.gen(function* () {
233 const fileSystem = yield* FileSystem.FileSystem;
234 const path = yield* Path.Path;
235 const tmp = yield* makeTempDir;
236 const workspaceRoot = path.join(tmp, "app");
237 const folder = (name: string, ...segments: ReadonlyArray<string>) => ({
238 name,
239 path: path.join(tmp, ...segments),
240 });
241 const inside = folder("web", "app", "packages", "web");
242 const repository = folder("api", "api");
243 const missing = folder("gone", "gone");
244 const linked = folder("shared", "app", "linked");
245 const shared = path.join(tmp, "shared");
246 const worktree = path.join(tmp, "tree");
247 // `lib` is checked out with a submodule of its own; `docs` was never initialized. A path
248 // listed twice is one submodule.
249 // `ext` is a symlink out of the repository, so what it leads to is no submodule of it.
250 const gitmodules =
251 '[submodule "lib"]\n\tpath = vendor/lib\n[submodule "docs"]\n\tpath = docs\n[submodule "again"]\n\tpath = vendor/lib\n[submodule "out"]\n\tpath = ext/x\n';
252 for (const root of [workspaceRoot, worktree]) {
253 yield* fileSystem.makeDirectory(path.join(root, "vendor", "lib", "deep", ".git"), {
254 recursive: true,
255 });
256 yield* fileSystem.makeDirectory(path.join(root, "docs"), { recursive: true });
257 yield* fileSystem.writeFileString(path.join(root, "vendor", "lib", ".git"), "gitdir: x\n");
258 yield* fileSystem.writeFileString(path.join(root, ".gitmodules"), gitmodules);
259 yield* fileSystem.writeFileString(
260 path.join(root, "vendor", "lib", ".gitmodules"),
261 '[submodule "deep"]\n\tpath = deep\n',
262 );
263 }
264 for (const directory of [inside.path, path.join(repository.path, ".git"), shared]) {
265 yield* fileSystem.makeDirectory(directory, { recursive: true });
266 }
267 yield* fileSystem.symlink(shared, linked.path);
268 yield* fileSystem.makeDirectory(path.join(tmp, "elsewhere", "x", ".git"), {
269 recursive: true,
270 });
271 yield* fileSystem.symlink(path.join(tmp, "elsewhere"), path.join(workspaceRoot, "ext"));
272​
273 yield* Effect.gen(function* () {
274 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
275 assert.deepEqual(yield* workspaceFolders.list({ projectId }), {
276 folders: [
277 { ...inside, state: "plain" },
278 { ...repository, state: "git" },
279 { ...missing, state: "missing" },
280 { ...linked, state: "plain" },
281 ],
282 });
283 assert.deepEqual(yield* workspaceFolders.forThread({ projectId, worktreePath: null }), {
284 folders: [
285 { ...inside, isGitRepository: false },
286 { ...repository, isGitRepository: true },
287 { ...linked, isGitRepository: false },
288 ],
289 // A symlink inside the project folder still leads outside it.
290 extraRoots: [repository.path, linked.path, shared],
291 });
292 assert.deepEqual(yield* workspaceFolders.forThread({ projectId, worktreePath: worktree }), {
293 folders: [],
294 extraRoots: [],
295 });
296 assert.deepEqual(yield* workspaceFolders.submodules({ projectId, worktreePath: null }), [
297 { name: "lib", path: path.join(workspaceRoot, "vendor", "lib"), isGitRepository: true },
298 {
299 name: "deep",
300 path: path.join(workspaceRoot, "vendor", "lib", "deep"),
301 isGitRepository: true,
302 },
303 ]);
304 // A worktree reaches its own submodules, named by where the project folder has them.
305 assert.deepEqual(
306 yield* workspaceFolders.submodules({ projectId, worktreePath: worktree }),
307 [
308 {
309 name: "lib",
310 path: path.join(worktree, "vendor", "lib"),
311 isGitRepository: true,
312 repositoryPath: path.join(workspaceRoot, "vendor", "lib"),
313 },
314 {
315 name: "deep",
316 path: path.join(worktree, "vendor", "lib", "deep"),
317 isGitRepository: true,
318 repositoryPath: path.join(workspaceRoot, "vendor", "lib", "deep"),
319 },
320 ],
321 );
322 }).pipe(
323 Effect.provide(
324 layerFor({ workspaceRoot, trackedFolders: [inside, repository, missing, linked] }),
325 ),
326 );
327 }),
328 );
329 it.effect("gives a task folder thread its worktrees, never the checkouts they came from", () =>
330 Effect.gen(function* () {
331 const fileSystem = yield* FileSystem.FileSystem;
332 const path = yield* Path.Path;
333 const tmp = yield* makeTempDir;
334 const ctx = path.join(tmp, "x", "ctx");
335 const app = path.join(tmp, "x", "app");
336 const lib = path.join(ctx, "vendor", "lib");
337 // The worktrees directory is reached through a symlink, as on a machine that moved it.
338 const realWorktrees = path.join(tmp, "disk", "worktrees");
339 const worktrees = path.join(tmp, "worktrees");
340 const taskRoot = path.join(worktrees, "x", "feature-0123");
341 for (const directory of [
342 lib,
343 app,
344 path.join(realWorktrees, "x", "feature-0123", "ctx", "vendor", "lib"),
345 path.join(realWorktrees, "x", "feature-0123", "app"),
346 path.join(realWorktrees, "x", "single"),
347 ]) {
348 yield* fileSystem.makeDirectory(directory, { recursive: true });
349 }
350 yield* fileSystem.symlink(realWorktrees, worktrees);
351 const trackedFolders = [
352 { name: "app", path: app },
353 { name: "lib", path: lib },
354 ];
355​
356 yield* Effect.gen(function* () {
357 const taskFolders = yield* TaskFolderStore.TaskFolderStore;
358 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
359 const taskFolder = {
360 root: taskRoot,
361 projectId,
362 projectRoot: ctx,
363 anchor: path.join(tmp, "x"),
364 projectPath: path.join(taskRoot, "ctx"),
365 projectIsWorktree: true,
366 repositories: [
367 { repositoryPath: app, worktreePath: path.join(taskRoot, "app"), name: "app" },
368 {
369 repositoryPath: lib,
370 worktreePath: path.join(taskRoot, "ctx", "vendor", "lib"),
371 name: "lib",
372 },
373 ],
374 };
375 yield* taskFolders.put(taskFolder);
376 const expected = {
377 folders: [
378 {
379 name: "app",
380 path: path.join(taskRoot, "app"),
381 isGitRepository: true,
382 repositoryPath: app,
383 },
384 {
385 name: "lib",
386 path: path.join(taskRoot, "ctx", "vendor", "lib"),
387 isGitRepository: true,
388 repositoryPath: lib,
389 },
390 ],
391 // Only the sibling lies outside the thread's folder, under both of its spellings.
392 extraRoots: [
393 path.join(taskRoot, "app"),
394 path.join(realWorktrees, "x", "feature-0123", "app"),
395 ],
396 };
397 // The thread stores the path as named; a sandbox or Git may hand back the resolved one.
398 for (const worktreePath of [
399 path.join(taskRoot, "ctx"),
400 path.join(realWorktrees, "x", "feature-0123", "ctx"),
401 ]) {
402 assert.deepEqual(
403 yield* workspaceFolders.forThread({ projectId, worktreePath }),
404 expected,
405 );
406 }
407 assert.deepEqual(
408 yield* taskFolders.findContaining(
409 path.join(realWorktrees, "x", "feature-0123", "app", "src"),
410 ),
411 taskFolder,
412 );
413 // A single worktree beside it is no task folder.
414 assert.deepEqual(
415 yield* workspaceFolders.forThread({
416 projectId,
417 worktreePath: path.join(worktrees, "x", "single"),
418 }),
419 { folders: [], extraRoots: [] },
420 );
421 }).pipe(Effect.provide(layerFor({ workspaceRoot: ctx, trackedFolders })));
422 }),
423 );
424});