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

WorkspaceFileSystem.test.ts

432 lines16.1 KB

1// @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
2import * as NodeChildProcess from "node:child_process";
3​
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { it, describe, expect } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10​
11import * as ServerConfig from "../config.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as WorkspaceEntries from "./WorkspaceEntries.ts";
15import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts";
16import * as WorkspacePaths from "./WorkspacePaths.ts";
17import * as HostProcess from "@t3tools/shared/HostProcess";
18import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
19​
20const layerProject = WorkspaceFileSystem.layer.pipe(
21 Layer.provide(WorkspacePaths.layer),
22 Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
23);
24​
25const layerTest = Layer.empty.pipe(
26 Layer.provideMerge(layerProject),
27 Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
28 Layer.provideMerge(WorkspacePaths.layer),
29 Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
30 Layer.provide(
31 ServerConfig.ServerConfig.layerTest(process.cwd(), {
32 prefix: "t3-workspace-files-test-",
33 }),
34 ),
35 Layer.provideMerge(NodeServices.layer),
36);
37​
38const makeTempDir = Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 return yield* fileSystem.makeTempDirectoryScoped({
41 prefix: "t3code-workspace-files-",
42 });
43});
44​
45const writeTextFile = Effect.fn("writeTextFile")(function* (
46 cwd: string,
47 relativePath: string,
48 contents = "",
49) {
50 const fileSystem = yield* FileSystem.FileSystem;
51 const path = yield* Path.Path;
52 const absolutePath = path.join(cwd, relativePath);
53 yield* fileSystem
54 .makeDirectory(path.dirname(absolutePath), { recursive: true })
55 .pipe(Effect.orDie);
56 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
57});
58​
59it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
60 describe("getMetadata", () => {
61 it.effect("expands home-relative paths on the server", () =>
62 Effect.gen(function* () {
63 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
64 const home = yield* makeTempDir;
65 yield* writeTextFile(home, "file", "hello");
66 expect(
67 (yield* service
68 .getMetadata({ paths: ["~/file"] })
69 .pipe(Effect.provideService(HostProcess.HomeDirectory, home))).entries,
70 ).toEqual([{ kind: "file", byteLength: 5 }]);
71 }),
72 );
73​
74 it.effect(
75 "reports actual kinds for extensionless files, dotfiles, and dotted directories",
76 () =>
77 Effect.gen(function* () {
78 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const cwd = yield* makeTempDir;
82 yield* writeTextFile(cwd, "custom-name", "hello");
83 yield* writeTextFile(cwd, ".npmrc", "registry=test");
84 yield* fs.makeDirectory(path.join(cwd, "folder.ts"));
85 const file = path.join(cwd, "custom-name");
86 const result = yield* service.getMetadata({
87 paths: [
88 file,
89 path.join(cwd, ".npmrc"),
90 path.join(cwd, "folder.ts"),
91 file,
92 path.join(cwd, "missing"),
93 "relative-path",
94 ],
95 });
96 expect(result.entries).toEqual([
97 { kind: "file", byteLength: 5 },
98 { kind: "file", byteLength: 13 },
99 { kind: "directory" },
100 { kind: "file", byteLength: 5 },
101 null,
102 null,
103 ]);
104 }),
105 );
106​
107 it.effect("detects an extensionless script and image without returning their contents", () =>
108 Effect.gen(function* () {
109 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
110 const fs = yield* FileSystem.FileSystem;
111 const path = yield* Path.Path;
112 const cwd = yield* makeTempDir;
113 const script = "#!/usr/bin/env python3\nprint('hello')\n";
114 yield* writeTextFile(cwd, "run", script);
115 const image = path.join(cwd, "image");
116 yield* fs.writeFile(
117 image,
118 new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
119 );
120 expect(
121 (yield* service.getMetadata({ paths: [path.join(cwd, "run"), image] })).entries,
122 ).toEqual([
123 { kind: "file", byteLength: script.length, mimeType: "text/x-python" },
124 { kind: "file", byteLength: 8, mimeType: "image/png" },
125 ]);
126 }),
127 );
128​
129 it.effect.skipIf(!symlinksSupported)("follows file and directory symlinks", () =>
130 Effect.gen(function* () {
131 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
132 const fs = yield* FileSystem.FileSystem;
133 const path = yield* Path.Path;
134 const cwd = yield* makeTempDir;
135 yield* writeTextFile(cwd, "target", "text");
136 yield* fs.makeDirectory(path.join(cwd, "directory"));
137 const fileLink = path.join(cwd, "file-link");
138 const directoryLink = path.join(cwd, "directory-link");
139 yield* fs.symlink(path.join(cwd, "target"), fileLink);
140 yield* fs.symlink(path.join(cwd, "directory"), directoryLink);
141 expect((yield* service.getMetadata({ paths: [fileLink, directoryLink] })).entries).toEqual([
142 { kind: "file", byteLength: 4 },
143 { kind: "directory" },
144 ]);
145 }),
146 );
147 });
148​
149 describe("readFile", () => {
150 it.effect("reads UTF-8 files relative to the workspace root", () =>
151 Effect.gen(function* () {
152 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
153 const cwd = yield* makeTempDir;
154 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
155​
156 const result = yield* workspaceFileSystem.readFile({
157 cwd,
158 relativePath: "src/index.ts",
159 });
160​
161 expect(result).toEqual({
162 relativePath: "src/index.ts",
163 contents: "export const answer = 42;\n",
164 byteLength: 26,
165 truncated: false,
166 });
167 }),
168 );
169​
170 it.effect("reads host files outside the workspace root by absolute path", () =>
171 Effect.gen(function* () {
172 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
173 const path = yield* Path.Path;
174 const cwd = yield* makeTempDir;
175 const outsideDir = yield* makeTempDir;
176 yield* writeTextFile(outsideDir, "cleanup-report.md", "# Report\n");
177 const absolutePath = path.join(outsideDir, "cleanup-report.md");
178​
179 const result = yield* workspaceFileSystem.readFile({
180 cwd,
181 relativePath: absolutePath,
182 });
183​
184 expect(result).toEqual({
185 relativePath: absolutePath,
186 contents: "# Report\n",
187 byteLength: 9,
188 truncated: false,
189 });
190 }),
191 );
192​
193 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
194 "rejects a FIFO without blocking on open",
195 () =>
196 Effect.gen(function* () {
197 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
198 const path = yield* Path.Path;
199 const cwd = yield* makeTempDir;
200 const outsideDir = yield* makeTempDir;
201 const fifoPath = path.join(outsideDir, "pipe");
202 yield* Effect.promise(
203 () =>
204 new Promise<void>((resolve, reject) =>
205 NodeChildProcess.execFile("mkfifo", [fifoPath], (error) =>
206 error ? reject(error) : resolve(),
207 ),
208 ),
209 );
210​
211 const error = yield* workspaceFileSystem
212 .readFile({ cwd, relativePath: fifoPath })
213 .pipe(Effect.flip);
214​
215 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
216 expect((yield* workspaceFileSystem.getMetadata({ paths: [fifoPath] })).entries).toEqual([
217 { kind: "other" },
218 ]);
219 }),
220 );
221​
222 it.effect("rejects reads outside the workspace root", () =>
223 Effect.gen(function* () {
224 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
225 const cwd = yield* makeTempDir;
226​
227 const error = yield* workspaceFileSystem
228 .readFile({ cwd, relativePath: "../escape.md" })
229 .pipe(Effect.flip);
230​
231 expect(error.message).toContain(
232 "Workspace file path must be relative to the project root: ../escape.md",
233 );
234 }),
235 );
236​
237 it.effect.skipIf(!symlinksSupported)(
238 "rejects symlinks that resolve outside the workspace root",
239 () =>
240 Effect.gen(function* () {
241 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
242 const fileSystem = yield* FileSystem.FileSystem;
243 const path = yield* Path.Path;
244 const cwd = yield* makeTempDir;
245 const outsideDir = yield* makeTempDir;
246 yield* writeTextFile(outsideDir, "secret.txt", "outside\n");
247 yield* fileSystem.symlink(
248 path.join(outsideDir, "secret.txt"),
249 path.join(cwd, "linked-secret.txt"),
250 );
251​
252 const error = yield* workspaceFileSystem
253 .readFile({ cwd, relativePath: "linked-secret.txt" })
254 .pipe(Effect.flip);
255 const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd);
256 const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt"));
257​
258 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
259 expect(error).toMatchObject({
260 workspaceRoot: cwd,
261 relativePath: "linked-secret.txt",
262 resolvedWorkspaceRoot,
263 resolvedPath,
264 });
265 expect("cause" in error).toBe(false);
266 }),
267 );
268​
269 it.effect("rejects directories without manufacturing an I/O cause", () =>
270 Effect.gen(function* () {
271 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
272 const fileSystem = yield* FileSystem.FileSystem;
273 const path = yield* Path.Path;
274 const cwd = yield* makeTempDir;
275 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
276​
277 const error = yield* workspaceFileSystem
278 .readFile({ cwd, relativePath: "src" })
279 .pipe(Effect.flip);
280 const resolvedPath = yield* fileSystem.realPath(path.join(cwd, "src"));
281​
282 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
283 expect(error).toMatchObject({
284 workspaceRoot: cwd,
285 relativePath: "src",
286 resolvedPath,
287 });
288 expect("cause" in error).toBe(false);
289 }),
290 );
291​
292 it.effect("rejects binary files without leaking their contents into the error", () =>
293 Effect.gen(function* () {
294 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
295 const fileSystem = yield* FileSystem.FileSystem;
296 const path = yield* Path.Path;
297 const cwd = yield* makeTempDir;
298 const absolutePath = path.join(cwd, "asset.bin");
299 yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
300​
301 const error = yield* workspaceFileSystem
302 .readFile({ cwd, relativePath: "asset.bin" })
303 .pipe(Effect.flip);
304 const resolvedPath = yield* fileSystem.realPath(absolutePath);
305​
306 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
307 expect(error).toMatchObject({
308 workspaceRoot: cwd,
309 relativePath: "asset.bin",
310 resolvedPath,
311 });
312 expect("cause" in error).toBe(false);
313 expect("contents" in error).toBe(false);
314 }),
315 );
316​
317 it.effect("preserves the real cause and path for I/O failures", () =>
318 Effect.gen(function* () {
319 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
320 const path = yield* Path.Path;
321 const cwd = yield* makeTempDir;
322 const resolvedPath = path.join(cwd, "missing.txt");
323​
324 const error = yield* workspaceFileSystem
325 .readFile({ cwd, relativePath: "missing.txt" })
326 .pipe(Effect.flip);
327​
328 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFileSystemOperationError);
329 expect(error).toMatchObject({
330 workspaceRoot: cwd,
331 relativePath: "missing.txt",
332 resolvedPath,
333 operationPath: resolvedPath,
334 operation: "realpath-target",
335 });
336 expect(error.cause).toBeInstanceOf(Error);
337 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
338 }),
339 );
340 });
341​
342 describe("writeFile", () => {
343 it.effect("writes files relative to the workspace root", () =>
344 Effect.gen(function* () {
345 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
346 const cwd = yield* makeTempDir;
347 const fileSystem = yield* FileSystem.FileSystem;
348 const path = yield* Path.Path;
349 const result = yield* workspaceFileSystem.writeFile({
350 cwd,
351 relativePath: "plans/effect-rpc.md",
352 contents: "# Plan\n",
353 });
354 const saved = yield* fileSystem
355 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
356 .pipe(Effect.orDie);
357​
358 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
359 expect(saved).toBe("# Plan\n");
360 }),
361 );
362​
363 it.effect("rejects writes by absolute path", () =>
364 Effect.gen(function* () {
365 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
366 const path = yield* Path.Path;
367 const cwd = yield* makeTempDir;
368 const outsideDir = yield* makeTempDir;
369 const absolutePath = path.join(outsideDir, "cleanup-report.md");
370​
371 const error = yield* workspaceFileSystem
372 .writeFile({ cwd, relativePath: absolutePath, contents: "# Edited\n" })
373 .pipe(Effect.flip);
374​
375 expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
376 }),
377 );
378​
379 it.effect("invalidates workspace entry search cache after writes", () =>
380 Effect.gen(function* () {
381 const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
382 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
383 const cwd = yield* makeTempDir;
384 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
385​
386 const beforeWrite = yield* workspaceEntries.list({ cwd });
387 expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe(
388 false,
389 );
390​
391 yield* workspaceFileSystem.writeFile({
392 cwd,
393 relativePath: "plans/effect-rpc.md",
394 contents: "# Plan\n",
395 });
396​
397 const afterWrite = yield* workspaceEntries.list({ cwd });
398 expect(afterWrite.entries).toEqual(
399 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
400 );
401 expect(afterWrite.truncated).toBe(false);
402 }),
403 );
404​
405 it.effect("rejects writes outside the workspace root", () =>
406 Effect.gen(function* () {
407 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
408 const cwd = yield* makeTempDir;
409 const path = yield* Path.Path;
410 const fileSystem = yield* FileSystem.FileSystem;
411​
412 const error = yield* workspaceFileSystem
413 .writeFile({
414 cwd,
415 relativePath: "../escape.md",
416 contents: "# nope\n",
417 })
418 .pipe(Effect.flip);
419​
420 expect(error.message).toContain(
421 "Workspace file path must be relative to the project root: ../escape.md",
422 );
423​
424 const escapedPath = path.resolve(cwd, "..", "escape.md");
425 const escapedStat = yield* fileSystem
426 .stat(escapedPath)
427 .pipe(Effect.orElseSucceed(() => null));
428 expect(escapedStat).toBeNull();
429 }),
430 );
431 });
432});