Showing the changes to apps/server/src/workspace/WorkspaceFileSystem.test.ts (1 of 2 files). Show all
Unified · Split
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | −import { it, describe, expect } from "@effect/vitest"; | |
| 2 | +import { assert, it, describe, expect } from "@effect/vitest"; | |
| 3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | 5 | import * as Layer from "effect/Layer"; |
| 6 | 6 | import * as Path from "effect/Path"; |
| 7 | +import * as PlatformError from "effect/PlatformError"; | |
| 7 | 8 | |
| 8 | 9 | import * as ServerConfig from "../config.ts"; |
| 9 | 10 | import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| @@ -30,6 +31,34 @@ const TestLayer = Layer.empty.pipe( · 20 unmodified lines | ||
| 30 | 31 | Layer.provideMerge(NodeServices.layer), |
| 31 | 32 | ); |
| 32 | 33 | |
| 34 | +const 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 | + | |
| 55 | +const 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 | + | |
| 33 | 62 | const makeTempDir = Effect.gen(function* () { |
| 34 | 63 | const fileSystem = yield* FileSystem.FileSystem; |
| 35 | 64 | return yield* fileSystem.makeTempDirectoryScoped({ |
| @@ -185,10 +214,30 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i · 149 unmodified lines | ||
| 185 | 214 | operationPath: resolvedPath, |
| 186 | 215 | operation: "realpath-target", |
| 187 | 216 | }); |
| 188 | − expect(error.cause).toBeInstanceOf(Error); | |
| 189 | − expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT"); | |
| 217 | + expect(error.cause).toBeInstanceOf(PlatformError.PlatformError); | |
| 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 | 243 | describe("writeFile", () => { |