import { assert, describe, it } from "@effect/vitest"; import { Effect, Layer, Option } from "effect"; import { GitHubError } from "../src/GitHubError.js"; import { GitHubRepository } from "../src/GitHubRepository.js"; import type { Repo } from "../src/Repo.js"; import type { Reply } from "./fixtures.js"; import { harness } from "./harness.js"; const drive = ( replies: ReadonlyArray, use: (repository: GitHubRepository["Service"]) => Effect.Effect, ) => Effect.gen(function* () { const { script, base } = harness(replies); const value = yield* Effect.provide( Effect.flatMap(GitHubRepository, use), GitHubRepository.layer.pipe(Layer.provideMerge(base)), ); return { value, script }; }); const driveFailure = (reply: Reply) => Effect.gen(function* () { const { base } = harness([reply]); return yield* Effect.flip( Effect.provide( Effect.flatMap(GitHubRepository, (repository) => repository.customProperties), GitHubRepository.layer.pipe(Layer.provideMerge(base)), ), ); }); const VALUES = [ { property_name: "team", value: "platform" }, { property_name: "languages", value: ["ts", "zig"] }, { property_name: "tier", value: null }, ]; describe("GitHubRepository.customProperties", () => { it.effect("reads the property-values route and keeps each value's shape", () => Effect.gen(function* () { const { value, script } = yield* drive( [{ status: 200, body: VALUES }], (repository) => repository.customProperties, ); assert.strictEqual(script.count(), 1); assert.strictEqual(script.calls[0]?.method, "GET"); assert.strictEqual(script.calls[0]?.path, "/repos/acme/widget/properties/values"); assert.deepStrictEqual( value.map((entry) => [entry.propertyName, entry.value]), [ ["team", "platform"], ["languages", ["ts", "zig"]], ["tier", null], ], ); }), ); it.effect("fails kind notFound on a 404, the answer for a user-owned repository", () => Effect.gen(function* () { const error = yield* driveFailure({ status: 404, body: { message: "Not Found" } }); assert.strictEqual(error.kind, "notFound"); assert.isTrue(GitHubError.hasKind("notFound", "unauthorized")(error)); }), ); it.effect("fails kind unauthorized on a 403", () => Effect.gen(function* () { const error = yield* driveFailure({ status: 403, body: { message: "Resource not accessible by integration" } }); assert.strictEqual(error.kind, "unauthorized"); }), ); it.effect("fails kind transport on a 5xx", () => Effect.gen(function* () { const error = yield* driveFailure({ status: 503, body: { message: "Unavailable" } }); assert.strictEqual(error.kind, "transport"); assert.isFalse(GitHubError.hasKind("notFound", "unauthorized")(error)); }), ); it.effect("fails kind decode on a value outside string, string array or null", () => Effect.gen(function* () { const error = yield* driveFailure({ status: 200, body: [{ property_name: "count", value: 3 }] }); assert.strictEqual(error.kind, "decode"); assert.strictEqual(error.operation, "GitHubRepository.customProperties"); }), ); }); describe("GitHubRepository.customProperty", () => { it.effect("finds one property by exact name", () => Effect.gen(function* () { const { value } = yield* drive([{ status: 200, body: VALUES }], (repository) => repository.customProperty("languages"), ); assert.deepStrictEqual( Option.map(value, (entry) => entry.value), Option.some(["ts", "zig"]), ); }), ); it.effect("answers some with a null value for a listed property with no value", () => Effect.gen(function* () { const { value } = yield* drive([{ status: 200, body: VALUES }], (repository) => repository.customProperty("tier"), ); assert.deepStrictEqual( Option.map(value, (entry) => entry.value), Option.some(null), ); }), ); it.effect("answers none for a property the repository does not list", () => Effect.gen(function* () { const { value } = yield* drive([{ status: 200, body: VALUES }], (repository) => repository.customProperty("Team"), ); assert.isTrue(Option.isNone(value)); }), ); });