// GitFixture.recorder over Git.makeTest doubles, no git: what it records and when, that it wraps every member the // shape has, the layer form, and the nested-provide trap its TSDoc warns about. Also GitFixtureError's message for a // cause `String` cannot convert. import { assert, describe, it } from "@effect/vitest"; import { Effect, Layer, Option, Result, Schema } from "effect"; import { Git } from "../src/Git.js"; import { GitFixture, GitFixtureError } from "../src/testing.js"; const double = Git.makeTest({ sparseCheckoutList: () => Effect.succeed({ enabled: true, cone: true, patterns: ["docs"] } as never), sparseCheckoutSet: () => Effect.succeed({ leftBehind: [], untrackedDirectories: [] } as never), currentBranch: () => Effect.succeed(Option.some("main")), }); describe("GitFixture.recorder", () => { it.effect("records each call with its arguments when the effect runs, and answers called and callsTo", () => Effect.gen(function* () { const recorder = GitFixture.recorder(); const git = recorder.wrap(double); yield* git.sparseCheckoutList("/work"); yield* git.currentBranch("/work"); assert.deepStrictEqual(recorder.calls, [ { member: "sparseCheckoutList", args: ["/work"] }, { member: "currentBranch", args: ["/work"] }, ]); assert.isTrue(recorder.called("sparseCheckoutList")); // The absence assertion the recorder exists for. assert.isFalse(recorder.called("sparseCheckoutSet")); assert.deepStrictEqual(recorder.callsTo("currentBranch"), [["/work"]]); assert.deepStrictEqual(recorder.callsTo("sparseCheckoutSet"), []); recorder.clear(); assert.deepStrictEqual(recorder.calls, []); }), ); it.effect("an effect built and never run leaves no trace", () => Effect.gen(function* () { const recorder = GitFixture.recorder(); const git = recorder.wrap(double); const unrun = git.sparseCheckoutSet("/work", ["docs"], { cone: true }); assert.isFalse(recorder.called("sparseCheckoutSet")); yield* unrun; assert.isTrue(recorder.called("sparseCheckoutSet")); }), ); it("wraps every member of the shape, so a member added to GitShape is recorded without an edit", () => { const wrapped = GitFixture.recorder().wrap(double); const members = Object.keys(double); // makeTest lists every GitShape member; a walk that found nothing would pass the comparison. assert.strictEqual(members.length, 97); assert.sameMembers(Object.keys(wrapped), members); for (const member of members) assert.notStrictEqual(Reflect.get(wrapped, member), Reflect.get(double, member)); }); it.effect("the layer wraps the Git it is given, provided at the outermost composition", () => Effect.gen(function* () { const recorder = GitFixture.recorder(); const TestLayer = recorder.layer.pipe(Layer.provide(Layer.succeed(Git, double))); yield* Effect.flatMap(Git, (git) => git.currentBranch("/work")).pipe(Effect.provide(TestLayer)); assert.isTrue(recorder.called("currentBranch")); }), ); it.effect("the trap: a service built over the outer Git captures the unwrapped one, and records nothing", () => Effect.gen(function* () { const recorder = GitFixture.recorder(); // A consumer service that captured Git when it was built, as a code-under-test layer does. const captured = yield* Effect.map(Git, (git) => () => git.currentBranch("/work")).pipe( Effect.provide(Layer.succeed(Git, double)), ); // Providing the recorder afterwards, nested, changes nothing for the captured instance. yield* captured().pipe(Effect.provide(recorder.layer.pipe(Layer.provide(Layer.succeed(Git, double))))); assert.isFalse(recorder.called("currentBranch")); }), ); }); describe("GitFixtureError.message", () => { it("renders a cause String cannot convert, such as a Schema issue, instead of throwing", () => { // The issue a failed decode carries: the kind of `cause` the trace reader once stored. const decoded = Schema.decodeUnknownResult(Schema.Struct({ a: Schema.String }))({ a: 1 }); const issue = Result.isFailure(decoded) ? decoded.failure.issue : undefined; assert.isDefined(issue); // An object with no prototype: `String(cause)` throws "Cannot convert object to primitive value". const bare = Object.assign(Object.create(null), { code: 1 }); assert.throws(() => String(bare), /Cannot convert object to primitive value/); for (const cause of >[bare, issue, "text"]) { const error = GitFixtureError.make({ code: "trace-unreadable", cwd: "/t", args: ["read", "/t"], cause }); assert.isString(error.message); assert.include(error.message, "a trace2 line is not an event"); } const io = GitFixtureError.make({ code: "io-failed", cwd: "/t", args: ["readFile", "/t"], cause: bare }); assert.include(io.message, '{"code":1}'); }); });