// The call recorder (#1471): `MemoryFileSystem.recorder()` is a full spy map // that appends every member call and its path arguments to one trace, and // `makeSpied` / `layerSpied` hang spies on ANY FileSystem. The real-disk run // is integration/recorder.int.test.ts. import { assert, describe, it } from "@effect/vitest"; import { Effect, FileSystem, Layer } from "effect"; import { MemoryFileSystem } from "../src/index.js"; const seeded = { "/repo/a.txt": "a", "/repo/secret.txt": "s", "/repo/dir/b.txt": "b" }; describe("MemoryFileSystem.recorder", () => { it.effect("records the member, its real arguments and its path", () => { const recorder = MemoryFileSystem.recorder(); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; yield* fs.stat("/repo/a.txt"); yield* fs.makeDirectory("/repo/new", { recursive: true }); const calls = recorder.calls; assert.deepStrictEqual(calls, [ { method: "stat", args: ["/repo/a.txt"], paths: ["/repo/a.txt"] }, { method: "makeDirectory", args: ["/repo/new", { recursive: true }], paths: ["/repo/new"] }, ]); const second = calls[1]; if (second?.method === "makeDirectory") { // Narrowing on `method` types `args` as that member's parameters. assert.strictEqual(second.args[1]?.recursive, true); } else { assert.fail("expected makeDirectory"); } }).pipe(Effect.provide(MemoryFileSystem.layerWith(seeded, { spies: recorder.spies }))); }); it.effect("names both paths of a two-path member, glob's root, and a temp directory's parent", () => { const recorder = MemoryFileSystem.recorder(); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; yield* fs.rename("/repo/a.txt", "/repo/c.txt"); yield* fs.symlink("c.txt", "/repo/link"); yield* fs.glob("*.txt", { root: "/repo" }); yield* fs.makeTempDirectory({ directory: "/repo" }); yield* fs.makeTempDirectory(); const byMethod = (method: string) => recorder.calls.filter((call) => call.method === method); assert.deepStrictEqual(byMethod("rename")[0]?.paths, ["/repo/a.txt", "/repo/c.txt"]); assert.deepStrictEqual(byMethod("symlink")[0]?.paths, ["c.txt", "/repo/link"]); assert.deepStrictEqual(byMethod("glob")[0]?.paths, ["*.txt", "/repo"]); assert.deepStrictEqual( byMethod("makeTempDirectory").map((call) => call.paths), [["/repo"], []], ); }).pipe(Effect.provide(MemoryFileSystem.layerWith(seeded, { spies: recorder.spies }))); }); it.effect("a derived member records itself, then the core member it runs through", () => { const recorder = MemoryFileSystem.recorder(); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; yield* fs.readFileString("/repo/a.txt"); yield* fs.exists("/repo/nope"); assert.deepStrictEqual( recorder.calls.map((call) => call.method), ["readFileString", "readFile", "exists", "access"], ); }).pipe(Effect.provide(MemoryFileSystem.layerWith(seeded, { spies: recorder.spies }))); }); it.effect("records a failing call too: a trace shows what was attempted", () => { const recorder = MemoryFileSystem.recorder(); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const error = yield* Effect.flip(fs.stat("/repo/missing")); assert.strictEqual(error.reason._tag, "NotFound"); assert.deepStrictEqual(recorder.paths(), ["/repo/missing"]); }).pipe(Effect.provide(MemoryFileSystem.layerWith(seeded, { spies: recorder.spies }))); }); it.effect("paths filters by member; calls is a copy; clear forgets", () => { const recorder = MemoryFileSystem.recorder(); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; yield* fs.stat("/repo/a.txt"); yield* fs.readDirectory("/repo/dir"); yield* fs.readFile("/repo/dir/b.txt"); assert.deepStrictEqual(recorder.paths("readDirectory", "readFile"), ["/repo/dir", "/repo/dir/b.txt"]); assert.notInclude(recorder.paths(), "/repo/secret.txt"); const before = recorder.calls; yield* fs.stat("/repo/a.txt"); assert.strictEqual(before.length, 3, "an earlier copy does not grow"); recorder.clear(); assert.deepStrictEqual(recorder.calls, []); yield* fs.lstat("/repo/a.txt"); assert.deepStrictEqual(recorder.paths(), ["/repo/a.txt"]); }).pipe(Effect.provide(MemoryFileSystem.layerWith(seeded, { spies: recorder.spies }))); }); }); describe("MemoryFileSystem.makeSpied / layerSpied", () => { it.effect("makeSpied observes any FileSystem and still performs the call", () => Effect.gen(function* () { const recorder = MemoryFileSystem.recorder(); const base = yield* MemoryFileSystem.makeWith(seeded); const fs = MemoryFileSystem.makeSpied(base, recorder.spies); yield* fs.writeFileString("/repo/w.txt", "w"); assert.strictEqual(yield* base.readFileString("/repo/w.txt"), "w", "the write reached the base"); assert.deepStrictEqual(recorder.paths("writeFile"), ["/repo/w.txt"]); }), ); it.effect("every function-valued member is intercepted and has a recorder spy", () => Effect.gen(function* () { const recorder = MemoryFileSystem.recorder(); const base = yield* MemoryFileSystem.makeWith(seeded); const fs = MemoryFileSystem.makeSpied(base, recorder.spies); const members = Object.keys(base).filter( (key) => typeof (base as unknown as Record)[key] === "function", ); assert.isAbove(members.length, 30); assert.sameMembers(Object.keys(recorder.spies), members); const forwardedUntouched = members.filter( (key) => (fs as unknown as Record)[key] === (base as unknown as Record)[key], ); assert.deepStrictEqual(forwardedUntouched, []); }), ); it("an unknown spy key throws RangeError at the call", () => { const base = Effect.runSync(MemoryFileSystem.make); assert.throws( () => MemoryFileSystem.makeSpied(base, { raed: () => undefined } as never), RangeError, /unknown fault key\(s\) "raed"/, ); }); it.effect("layerSpied decorates the FileSystem provided beneath it", () => { const recorder = MemoryFileSystem.recorder(); const Spied = MemoryFileSystem.layerSpied(recorder.spies).pipe(Layer.provide(MemoryFileSystem.layerWith(seeded))); return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; assert.strictEqual(yield* fs.readFileString("/repo/a.txt"), "a"); assert.deepStrictEqual(recorder.paths("readFile"), ["/repo/a.txt"]); }).pipe(Effect.provide(Spied)); }); });