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

WorkspaceFileSystem.test.ts

334 lines12.2 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";
17​
18const ProjectLayer = WorkspaceFileSystem.layer.pipe(
19 Layer.provide(WorkspacePaths.layer),
20 Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
21);
22​
23const TestLayer = Layer.empty.pipe(
24 Layer.provideMerge(ProjectLayer),
25 Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
26 Layer.provideMerge(WorkspacePaths.layer),
27 Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
28 Layer.provide(
29 ServerConfig.ServerConfig.layerTest(process.cwd(), {
30 prefix: "t3-workspace-files-test-",
31 }),
32 ),
33 Layer.provideMerge(NodeServices.layer),
34);
35​
36const makeTempDir = Effect.gen(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38 return yield* fileSystem.makeTempDirectoryScoped({
39 prefix: "t3code-workspace-files-",
40 });
41});
42​
43const writeTextFile = Effect.fn("writeTextFile")(function* (
44 cwd: string,
45 relativePath: string,
46 contents = "",
47) {
48 const fileSystem = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
50 const absolutePath = path.join(cwd, relativePath);
51 yield* fileSystem
52 .makeDirectory(path.dirname(absolutePath), { recursive: true })
53 .pipe(Effect.orDie);
54 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
55});
56​
57it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
58 describe("readFile", () => {
59 it.effect("reads UTF-8 files relative to the workspace root", () =>
60 Effect.gen(function* () {
61 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
62 const cwd = yield* makeTempDir;
63 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
64​
65 const result = yield* workspaceFileSystem.readFile({
66 cwd,
67 relativePath: "src/index.ts",
68 });
69​
70 expect(result).toEqual({
71 relativePath: "src/index.ts",
72 contents: "export const answer = 42;\n",
73 byteLength: 26,
74 truncated: false,
75 });
76 }),
77 );
78​
79 it.effect("reads host files outside the workspace root by absolute path", () =>
80 Effect.gen(function* () {
81 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
82 const path = yield* Path.Path;
83 const cwd = yield* makeTempDir;
84 const outsideDir = yield* makeTempDir;
85 yield* writeTextFile(outsideDir, "cleanup-report.md", "# Report\n");
86 const absolutePath = path.join(outsideDir, "cleanup-report.md");
87​
88 const result = yield* workspaceFileSystem.readFile({
89 cwd,
90 relativePath: absolutePath,
91 });
92​
93 expect(result).toEqual({
94 relativePath: absolutePath,
95 contents: "# Report\n",
96 byteLength: 9,
97 truncated: false,
98 });
99 }),
100 );
101​
102 it.effect("rejects a FIFO without blocking on open", () =>
103 Effect.gen(function* () {
104 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
105 const path = yield* Path.Path;
106 const cwd = yield* makeTempDir;
107 const outsideDir = yield* makeTempDir;
108 const fifoPath = path.join(outsideDir, "pipe");
109 yield* Effect.promise(
110 () =>
111 new Promise<void>((resolve, reject) =>
112 NodeChildProcess.execFile("mkfifo", [fifoPath], (error) =>
113 error ? reject(error) : resolve(),
114 ),
115 ),
116 );
117​
118 const error = yield* workspaceFileSystem
119 .readFile({ cwd, relativePath: fifoPath })
120 .pipe(Effect.flip);
121​
122 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
123 }),
124 );
125​
126 it.effect("rejects reads outside the workspace root", () =>
127 Effect.gen(function* () {
128 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
129 const cwd = yield* makeTempDir;
130​
131 const error = yield* workspaceFileSystem
132 .readFile({ cwd, relativePath: "../escape.md" })
133 .pipe(Effect.flip);
134​
135 expect(error.message).toContain(
136 "Workspace file path must be relative to the project root: ../escape.md",
137 );
138 }),
139 );
140​
141 it.effect("rejects symlinks that resolve outside the workspace root", () =>
142 Effect.gen(function* () {
143 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
144 const fileSystem = yield* FileSystem.FileSystem;
145 const path = yield* Path.Path;
146 const cwd = yield* makeTempDir;
147 const outsideDir = yield* makeTempDir;
148 yield* writeTextFile(outsideDir, "secret.txt", "outside\n");
149 yield* fileSystem.symlink(
150 path.join(outsideDir, "secret.txt"),
151 path.join(cwd, "linked-secret.txt"),
152 );
153​
154 const error = yield* workspaceFileSystem
155 .readFile({ cwd, relativePath: "linked-secret.txt" })
156 .pipe(Effect.flip);
157 const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd);
158 const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt"));
159​
160 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
161 expect(error).toMatchObject({
162 workspaceRoot: cwd,
163 relativePath: "linked-secret.txt",
164 resolvedWorkspaceRoot,
165 resolvedPath,
166 });
167 expect("cause" in error).toBe(false);
168 }),
169 );
170​
171 it.effect("rejects directories without manufacturing an I/O cause", () =>
172 Effect.gen(function* () {
173 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
174 const fileSystem = yield* FileSystem.FileSystem;
175 const path = yield* Path.Path;
176 const cwd = yield* makeTempDir;
177 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
178​
179 const error = yield* workspaceFileSystem
180 .readFile({ cwd, relativePath: "src" })
181 .pipe(Effect.flip);
182 const resolvedPath = yield* fileSystem.realPath(path.join(cwd, "src"));
183​
184 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
185 expect(error).toMatchObject({
186 workspaceRoot: cwd,
187 relativePath: "src",
188 resolvedPath,
189 });
190 expect("cause" in error).toBe(false);
191 }),
192 );
193​
194 it.effect("rejects binary files without leaking their contents into the error", () =>
195 Effect.gen(function* () {
196 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
197 const fileSystem = yield* FileSystem.FileSystem;
198 const path = yield* Path.Path;
199 const cwd = yield* makeTempDir;
200 const absolutePath = path.join(cwd, "asset.bin");
201 yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
202​
203 const error = yield* workspaceFileSystem
204 .readFile({ cwd, relativePath: "asset.bin" })
205 .pipe(Effect.flip);
206 const resolvedPath = yield* fileSystem.realPath(absolutePath);
207​
208 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
209 expect(error).toMatchObject({
210 workspaceRoot: cwd,
211 relativePath: "asset.bin",
212 resolvedPath,
213 });
214 expect("cause" in error).toBe(false);
215 expect("contents" in error).toBe(false);
216 }),
217 );
218​
219 it.effect("preserves the real cause and path for I/O failures", () =>
220 Effect.gen(function* () {
221 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
222 const path = yield* Path.Path;
223 const cwd = yield* makeTempDir;
224 const resolvedPath = path.join(cwd, "missing.txt");
225​
226 const error = yield* workspaceFileSystem
227 .readFile({ cwd, relativePath: "missing.txt" })
228 .pipe(Effect.flip);
229​
230 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFileSystemOperationError);
231 expect(error).toMatchObject({
232 workspaceRoot: cwd,
233 relativePath: "missing.txt",
234 resolvedPath,
235 operationPath: resolvedPath,
236 operation: "realpath-target",
237 });
238 expect(error.cause).toBeInstanceOf(Error);
239 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
240 }),
241 );
242 });
243​
244 describe("writeFile", () => {
245 it.effect("writes files relative to the workspace root", () =>
246 Effect.gen(function* () {
247 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
248 const cwd = yield* makeTempDir;
249 const fileSystem = yield* FileSystem.FileSystem;
250 const path = yield* Path.Path;
251 const result = yield* workspaceFileSystem.writeFile({
252 cwd,
253 relativePath: "plans/effect-rpc.md",
254 contents: "# Plan\n",
255 });
256 const saved = yield* fileSystem
257 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
258 .pipe(Effect.orDie);
259​
260 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
261 expect(saved).toBe("# Plan\n");
262 }),
263 );
264​
265 it.effect("rejects writes by absolute path", () =>
266 Effect.gen(function* () {
267 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
268 const path = yield* Path.Path;
269 const cwd = yield* makeTempDir;
270 const outsideDir = yield* makeTempDir;
271 const absolutePath = path.join(outsideDir, "cleanup-report.md");
272​
273 const error = yield* workspaceFileSystem
274 .writeFile({ cwd, relativePath: absolutePath, contents: "# Edited\n" })
275 .pipe(Effect.flip);
276​
277 expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
278 }),
279 );
280​
281 it.effect("invalidates workspace entry search cache after writes", () =>
282 Effect.gen(function* () {
283 const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
284 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
285 const cwd = yield* makeTempDir;
286 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
287​
288 const beforeWrite = yield* workspaceEntries.list({ cwd });
289 expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe(
290 false,
291 );
292​
293 yield* workspaceFileSystem.writeFile({
294 cwd,
295 relativePath: "plans/effect-rpc.md",
296 contents: "# Plan\n",
297 });
298​
299 const afterWrite = yield* workspaceEntries.list({ cwd });
300 expect(afterWrite.entries).toEqual(
301 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
302 );
303 expect(afterWrite.truncated).toBe(false);
304 }),
305 );
306​
307 it.effect("rejects writes outside the workspace root", () =>
308 Effect.gen(function* () {
309 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
310 const cwd = yield* makeTempDir;
311 const path = yield* Path.Path;
312 const fileSystem = yield* FileSystem.FileSystem;
313​
314 const error = yield* workspaceFileSystem
315 .writeFile({
316 cwd,
317 relativePath: "../escape.md",
318 contents: "# nope\n",
319 })
320 .pipe(Effect.flip);
321​
322 expect(error.message).toContain(
323 "Workspace file path must be relative to the project root: ../escape.md",
324 );
325​
326 const escapedPath = path.resolve(cwd, "..", "escape.md");
327 const escapedStat = yield* fileSystem
328 .stat(escapedPath)
329 .pipe(Effect.orElseSucceed(() => null));
330 expect(escapedStat).toBeNull();
331 }),
332 );
333 });
334});