// GitFixture.faults over Git.makeTest doubles, no git: a handler replaces one member and receives the original, the // other members are untouched, the handler runs when the call's effect runs, the layer form, its composition with a // recorder, a wiring mistake dying, and the nested-provide trap its TSDoc warns about. import { assert, describe, it } from "@effect/vitest"; import { Effect, Layer, Option } from "effect"; import { Git, GitCommandError } from "../src/Git.js"; import { GitFixture } from "../src/testing.js"; const double = Git.makeTest({ currentBranch: () => Effect.succeed(Option.some("main")), refExists: () => Effect.succeed(true), }); describe("GitFixture.faults", () => { it.effect("replaces a handled member, hands it the original and the arguments, and leaves the rest alone", () => Effect.gen(function* () { const seen: Array> = []; const faults = GitFixture.faults({ currentBranch: (original, cwd) => Effect.map(original(cwd), (branch) => { seen.push([cwd]); return Option.map(branch, (name) => `${name}-moved`); }), }); const git = faults.wrap(double); assert.deepStrictEqual(yield* git.currentBranch("/work"), Option.some("main-moved")); assert.deepStrictEqual(seen, [["/work"]]); assert.strictEqual(git.refExists, double.refExists); assert.isTrue(yield* git.refExists("/work", "main")); }), ); it.effect("a handler can fail with an error the member declares, without calling the original", () => Effect.gen(function* () { let originalRan = false; const git = GitFixture.faults({ currentBranch: (original, cwd) => Effect.andThen( Effect.fail(GitCommandError.make({ kind: "failed", args: ["branch"], cwd, exitCode: 1, stderr: "racy" })), Effect.andThen( Effect.sync(() => (originalRan = true)), original(cwd), ), ), }).wrap(double); const error = yield* Effect.flip(git.currentBranch("/work")); assert.isTrue(error._tag === "GitCommandError" && error.stderr === "racy"); assert.isFalse(originalRan); }), ); it.effect("the handler runs when the call's effect runs, not when the member is called", () => Effect.gen(function* () { let ran = 0; const git = GitFixture.faults({ currentBranch: (original, cwd) => Effect.andThen( Effect.sync(() => ran++), original(cwd), ), }).wrap(double); const unrun = git.currentBranch("/work"); assert.strictEqual(ran, 0); yield* unrun; yield* unrun; assert.strictEqual(ran, 2); }), ); it("dies on a handler naming a member the service does not have", () => { const faults = GitFixture.faults({ notAMember: () => Effect.void } as never); assert.throws(() => faults.wrap(double), TypeError, /notAMember/); }); it.effect("the layer faults the Git it is given, and composes with a recorder over it", () => Effect.gen(function* () { let faulted = 0; const faults = GitFixture.faults({ currentBranch: () => Effect.sync(() => { faulted++; return Option.none(); }), }); const recorder = GitFixture.recorder(); const TestLayer = recorder.layer.pipe(Layer.provide(faults.layer), Layer.provide(Layer.succeed(Git, double))); const branch = yield* Effect.flatMap(Git, (git) => git.currentBranch("/work")).pipe(Effect.provide(TestLayer)); assert.isTrue(Option.isNone(branch)); assert.strictEqual(faulted, 1); assert.deepStrictEqual(recorder.callsTo("currentBranch"), [["/work"]]); }), ); it.effect("the trap: a service built over the outer Git captured the unfaulted one, and the fault never fires", () => Effect.gen(function* () { let faulted = 0; const faults = GitFixture.faults({ currentBranch: () => Effect.sync(() => { faulted++; return Option.none(); }), }); const captured = yield* Effect.map(Git, (git) => () => git.currentBranch("/work")).pipe( Effect.provide(Layer.succeed(Git, double)), ); const branch = yield* captured().pipe( Effect.provide(faults.layer.pipe(Layer.provide(Layer.succeed(Git, double)))), ); assert.deepStrictEqual(branch, Option.some("main")); assert.strictEqual(faulted, 0); }), ); });