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

WorkspaceFileSystem.test.ts

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