The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * The Repository block's seams, as the docs show them: a route of your |
| 3 | * own over `Git.GitRepo`, and a decorated namespace that logs commits. |
| 4 | * Compile-checked against the real types; run against a fake stub. |
| 5 | */ |
| 6 | import * as Git from "@alchemy.run/git"; |
| 7 | import { describe, expect, it } from "@alchemy.run/test"; |
| 8 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 11 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 12 | import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint"; |
| 13 | import * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | |
| 16 | // ── a route of your own ────────────────────────────────────────────────────── |
| 17 | |
| 18 | export const Tip = HttpApiEndpoint.get("tip", "/api/v1/repos/:owner/:repo/tip", { |
| 19 | params: Git.RepoPath, |
| 20 | success: Git.Ref, |
| 21 | error: [Git.RepoNotFound, Git.RefNotFound], |
| 22 | }); |
| 23 | |
| 24 | class TipApi extends HttpApi.make("tip").add(HttpApiGroup.make("tip").add(Tip)) {} |
| 25 | |
| 26 | export const TipLive = HttpApiBuilder.group(TipApi, "tip", (h) => |
| 27 | Effect.gen(function* () { |
| 28 | const registry = yield* Git.RegistryStore; |
| 29 | const repos = yield* Git.RepoStore; |
| 30 | return h.handle( |
| 31 | "tip", |
| 32 | Effect.fn(function* ({ params }) { |
| 33 | const entry = yield* registry |
| 34 | .resolve(params.owner, params.repo) |
| 35 | .pipe(Effect.catchTag("StoreError", (error) => Effect.die(error))); |
| 36 | if (entry === undefined) return yield* new Git.RepoNotFound(params); |
| 37 | const repo = repos.getByName(entry.repoId); |
| 38 | const meta = yield* repo |
| 39 | .getRepoMeta() |
| 40 | .pipe(Effect.catchTag("StoreError", (error) => Effect.die(error))); |
| 41 | const ref = yield* repo |
| 42 | .getRef(`refs/heads/${meta.defaultBranch}`) |
| 43 | .pipe(Effect.catchTag("StoreError", (error) => Effect.die(error))); |
| 44 | return new Git.Ref({ |
| 45 | name: ref.name, |
| 46 | oid: ref.oid as Git.Oid, |
| 47 | ...(ref.peeled === null ? {} : { peeled: ref.peeled as Git.Oid }), |
| 48 | }); |
| 49 | }), |
| 50 | ); |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | // ── wrap it ────────────────────────────────────────────────────────────────── |
| 55 | |
| 56 | const seen: Array<{ repoId: string; refs: ReadonlyArray<string> }> = []; |
| 57 | |
| 58 | const afterPush = (repoId: string, result: Git.CommitPushResult) => |
| 59 | Effect.sync(() => { |
| 60 | seen.push({ repoId, refs: result.results.map((r) => r.ref) }); |
| 61 | }); |
| 62 | |
| 63 | export const ReposWithLogging = Layer.effect( |
| 64 | Git.RepoStore, |
| 65 | Effect.map(Git.RepoStore, (repos) => ({ |
| 66 | getByName: (repoId) => { |
| 67 | const stub = repos.getByName(repoId); |
| 68 | return new Proxy(stub, { |
| 69 | get: (target, key, receiver) => |
| 70 | key === "commitPush" |
| 71 | ? (input: Git.CommitPushInput) => |
| 72 | target.commitPush(input).pipe(Effect.tap((result) => afterPush(repoId, result))) |
| 73 | : Reflect.get(target, key, receiver), |
| 74 | }); |
| 75 | }, |
| 76 | })), |
| 77 | ); |
| 78 | |
| 79 | describe("Repository seams", { tags: ["unit", "local"] }, () => { |
| 80 | it.effect("a decorated namespace observes every commitPush", () => |
| 81 | Effect.gen(function* () { |
| 82 | const fakeStub = { |
| 83 | commitPush: () => |
| 84 | Effect.succeed({ |
| 85 | unpack: "ok", |
| 86 | results: [{ ref: "refs/heads/main", status: "ok" }], |
| 87 | }), |
| 88 | readMeta: () => Effect.succeed(undefined), |
| 89 | }; |
| 90 | const FakeRepos = Layer.succeed(Git.RepoStore, { |
| 91 | getByName: () => fakeStub as never, |
| 92 | }); |
| 93 | |
| 94 | const repos = yield* Git.RepoStore.pipe( |
| 95 | Effect.provide(ReposWithLogging.pipe(Layer.provide(FakeRepos))), |
| 96 | ); |
| 97 | const stub = repos.getByName("01ARZ3NDEKTSV4RRFFQ69G5FAV"); |
| 98 | const result = yield* stub.commitPush({} as Git.CommitPushInput); |
| 99 | expect(result.unpack).toBe("ok"); |
| 100 | // other methods pass through the proxy untouched |
| 101 | expect(yield* stub.readMeta()).toBeUndefined(); |
| 102 | expect(seen).toEqual([{ repoId: "01ARZ3NDEKTSV4RRFFQ69G5FAV", refs: ["refs/heads/main"] }]); |
| 103 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 104 | ); |
| 105 | }); |