History

Use Effect FileSystem for workspace reads

Cursor Agent committed 4 months ago

Commit ddf731d1 parent cabc93b2 files · +107 −97Browse files

Showing the changes to apps/server/src/workspace/WorkspaceFileSystem.test.ts (1 of 2 files). Show all

Unified · Split

apps/server/src/workspace/WorkspaceFileSystem.test.ts+52−3
@@ -1,9 +1,10 @@
1import * as NodeServices from "@effect/platform-node/NodeServices";1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";2import { assert, it, describe, expect } from "@effect/vitest";
3import * as Effect from "effect/Effect";3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";6import * as Path from "effect/Path";
7import * as PlatformError from "effect/PlatformError";
7 8
8import * as ServerConfig from "../config.ts";9import * as ServerConfig from "../config.ts";
9import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";10import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
@@ -30,6 +31,34 @@ const TestLayer = Layer.empty.pipe( · 20 unmodified lines
30 Layer.provideMerge(NodeServices.layer),31 Layer.provideMerge(NodeServices.layer),
31);32);
32 33
34const OpenFailureFileSystemLayer = Layer.effect(
35 FileSystem.FileSystem,
36 Effect.gen(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38
39 return {
40 ...fileSystem,
41 open: (path, options) =>
42 Effect.fail(
43 PlatformError.systemError({
44 _tag: "PermissionDenied",
45 module: "FileSystem",
46 method: "open",
47 pathOrDescriptor: String(path),
48 description: `open intercepted by FileSystem test layer (options: ${String(Boolean(options))})`,
49 }),
50 ),
51 } satisfies FileSystem.FileSystem;
52 }),
53).pipe(Layer.provide(NodeServices.layer));
54
55const WorkspaceFileSystemOpenFailureLayer = WorkspaceFileSystem.layer.pipe(
56 Layer.provide(WorkspacePaths.layer),
57 Layer.provide(Layer.mock(WorkspaceEntries.WorkspaceEntries)({})),
58 Layer.provideMerge(OpenFailureFileSystemLayer),
59 Layer.provide(Path.layer),
60);
61
33const makeTempDir = Effect.gen(function* () {62const makeTempDir = Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;63 const fileSystem = yield* FileSystem.FileSystem;
35 return yield* fileSystem.makeTempDirectoryScoped({64 return yield* fileSystem.makeTempDirectoryScoped({
@@ -185,10 +214,30 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i · 149 unmodified lines
185 operationPath: resolvedPath,214 operationPath: resolvedPath,
186 operation: "realpath-target",215 operation: "realpath-target",
187 });216 });
188 expect(error.cause).toBeInstanceOf(Error);217 expect(error.cause).toBeInstanceOf(PlatformError.PlatformError);
189 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");218 expect((error.cause as PlatformError.PlatformError).reason._tag).toBe("NotFound");
190 }),219 }),
191 );220 );
221
222 it.effect("opens files through the injected FileSystem service", () =>
223 Effect.gen(function* () {
224 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
225 const cwd = yield* makeTempDir;
226 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
227 const fileSystem = yield* FileSystem.FileSystem;
228 const resolvedPath = yield* fileSystem.realPath(`${cwd}/src/index.ts`);
229
230 const error = yield* workspaceFileSystem
231 .readFile({ cwd, relativePath: "src/index.ts" })
232 .pipe(Effect.flip);
233
234 assert.instanceOf(error, WorkspaceFileSystem.WorkspaceFileSystemOperationError);
235 assert.equal(error.operation, "open");
236 assert.equal(error.operationPath, resolvedPath);
237 assert.instanceOf(error.cause, PlatformError.PlatformError);
238 assert.equal((error.cause as PlatformError.PlatformError).reason._tag, "PermissionDenied");
239 }).pipe(Effect.provide(WorkspaceFileSystemOpenFailureLayer)),
240 );
192 });241 });
193 242
194 describe("writeFile", () => {243 describe("writeFile", () => {