The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** Git's native Effect HTTP contracts, group registration, and server assembly. */ |
| 2 | import * as Git from "@alchemy.run/git"; |
| 3 | import { HASH_ROUTE } from "@alchemy.run/git/Hasher/Protocol"; |
| 4 | import { describe, expect, it } from "@alchemy.run/test"; |
| 5 | import * as Http from "alchemy/Http"; |
| 6 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 9 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 10 | import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint"; |
| 11 | import * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 12 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 13 | import * as HttpServerError from "effect/http/HttpServerError"; |
| 14 | import * as HttpServerRequest from "effect/http/HttpServerRequest"; |
| 15 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 16 | import * as Layer from "effect/Layer"; |
| 17 | import * as Schema from "effect/Schema"; |
| 18 | |
| 19 | const oid = "a".repeat(40) as Git.Oid; |
| 20 | const unexpected = () => Effect.die("Unexpected handler invocation"); |
| 21 | const echoBody = () => |
| 22 | Effect.gen(function* () { |
| 23 | const request = yield* HttpServerRequest.HttpServerRequest; |
| 24 | const bytes = yield* request.arrayBuffer.pipe(Effect.orDie); |
| 25 | return HttpServerResponse.uint8Array(new Uint8Array(bytes), { |
| 26 | contentType: "application/x-git-upload-pack-result", |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | // The actual ApiLive registers every group against these handler implementations. |
| 31 | const FakeHandlers = Layer.succeed(Git.Handlers, { |
| 32 | repos: { |
| 33 | create: unexpected, |
| 34 | get: ({ params }) => Effect.fail(new Git.RepoNotFound(params)), |
| 35 | update: unexpected, |
| 36 | list: unexpected, |
| 37 | delete: unexpected, |
| 38 | fork: unexpected, |
| 39 | compact: unexpected, |
| 40 | import: unexpected, |
| 41 | }, |
| 42 | refs: { |
| 43 | list: unexpected, |
| 44 | get: ({ query }) => Effect.succeed(new Git.Ref({ name: query.name, oid })), |
| 45 | update: unexpected, |
| 46 | remove: unexpected, |
| 47 | }, |
| 48 | objects: { |
| 49 | commit: unexpected, |
| 50 | log: unexpected, |
| 51 | tree: unexpected, |
| 52 | blob: unexpected, |
| 53 | diff: unexpected, |
| 54 | compare: unexpected, |
| 55 | blobRaw: unexpected, |
| 56 | file: unexpected, |
| 57 | }, |
| 58 | pulls: { |
| 59 | create: unexpected, |
| 60 | list: unexpected, |
| 61 | get: unexpected, |
| 62 | update: unexpected, |
| 63 | merge: unexpected, |
| 64 | }, |
| 65 | protocol: { infoRefs: unexpected, uploadPack: echoBody, receivePack: echoBody }, |
| 66 | github: { |
| 67 | user: () => Effect.succeed(HttpServerResponse.jsonUnsafe({ login: "default" })), |
| 68 | repo: unexpected, |
| 69 | branches: unexpected, |
| 70 | commits: unexpected, |
| 71 | commit: unexpected, |
| 72 | contents: unexpected, |
| 73 | pulls: unexpected, |
| 74 | createPull: unexpected, |
| 75 | pull: unexpected, |
| 76 | updatePull: unexpected, |
| 77 | mergePull: unexpected, |
| 78 | pullFiles: unexpected, |
| 79 | }, |
| 80 | internal: { hashPart: echoBody }, |
| 81 | }); |
| 82 | |
| 83 | const Authentication = HttpRouter.middleware((httpEffect) => |
| 84 | Effect.gen(function* () { |
| 85 | const request = yield* HttpServerRequest.HttpServerRequest; |
| 86 | if (request.headers.authorization !== "Bearer test") { |
| 87 | return HttpServerResponse.empty({ status: 401 }); |
| 88 | } |
| 89 | const runtime = yield* RuntimeContext; |
| 90 | return HttpServerResponse.setHeader(yield* httpEffect, "x-runtime-id", runtime.id); |
| 91 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 92 | ); |
| 93 | class AppRoutes extends HttpApiGroup.make("app").add( |
| 94 | HttpApiEndpoint.get("health", "/health", { success: Schema.String }), |
| 95 | ) {} |
| 96 | class AppApi extends HttpApi.make("app").add(AppRoutes) {} |
| 97 | const AppApiLive = HttpApiBuilder.layer(AppApi).pipe( |
| 98 | Layer.provide( |
| 99 | HttpApiBuilder.group(AppApi, "app", (h) => h.handle("health", () => Effect.succeed("ok"))), |
| 100 | ), |
| 101 | ); |
| 102 | const GitHubLive = HttpApiBuilder.group(Git.Api, "github", (h) => |
| 103 | Effect.map(Git.Handlers, (git) => |
| 104 | h.handleAll({ |
| 105 | ...git.github, |
| 106 | user: () => Effect.succeed(HttpServerResponse.jsonUnsafe({ login: "application" })), |
| 107 | }), |
| 108 | ), |
| 109 | ); |
| 110 | const OpenRoutes = Git.ApiLive.pipe(Layer.provide(FakeHandlers), Layer.provide(Http.Platform)); |
| 111 | const ProtectedRoutes = Layer.mergeAll( |
| 112 | Layer.mergeAll( |
| 113 | AppApiLive, |
| 114 | HttpApiBuilder.layer(Git.Api).pipe(Layer.provide(Layer.mergeAll(Git.GroupsLive, GitHubLive))), |
| 115 | ).pipe(Layer.provide(Authentication.layer)), |
| 116 | Git.InternalApiLive, |
| 117 | ).pipe(Layer.provide(FakeHandlers), Layer.provide(Http.Platform)); |
| 118 | class PrefixedApi extends HttpApi.make("custom-git").add(Git.Refs).prefix("/git") {} |
| 119 | const PrefixedRoutes = Layer.mergeAll( |
| 120 | AppApiLive, |
| 121 | HttpApiBuilder.layer(PrefixedApi).pipe( |
| 122 | Layer.provide( |
| 123 | HttpApiBuilder.group(PrefixedApi, "refs", (h) => |
| 124 | Effect.map(Git.Handlers, (git) => h.handleAll(git.refs)), |
| 125 | ), |
| 126 | ), |
| 127 | ), |
| 128 | ).pipe( |
| 129 | Layer.provide(Authentication.layer), |
| 130 | Layer.provide(FakeHandlers), |
| 131 | Layer.provide(Http.Platform), |
| 132 | ); |
| 133 | |
| 134 | const request = (fetch: Http.HttpEffect<RuntimeContext>, path: string, init?: RequestInit) => |
| 135 | fetch.pipe( |
| 136 | Effect.provideService( |
| 137 | HttpServerRequest.HttpServerRequest, |
| 138 | HttpServerRequest.fromWeb(new Request(`http://git.test${path}`, init)), |
| 139 | ), |
| 140 | Effect.provideService(RuntimeContext, { |
| 141 | Type: "test", |
| 142 | id: "request-runtime", |
| 143 | env: {}, |
| 144 | get: () => Effect.succeed(undefined), |
| 145 | set: () => Effect.succeed(""), |
| 146 | }), |
| 147 | Effect.catchCause((cause) => |
| 148 | HttpServerError.causeResponse(cause).pipe(Effect.map(([response]) => response)), |
| 149 | ), |
| 150 | Effect.map(HttpServerResponse.toWeb), |
| 151 | Effect.scoped, |
| 152 | ); |
| 153 | |
| 154 | describe("Git HTTP composition", { tags: ["unit", "local"] }, () => { |
| 155 | it.effect("registers every default group and preserves decoded requests and typed errors", () => |
| 156 | Effect.gen(function* () { |
| 157 | const server = yield* HttpRouter.toHttpEffect(OpenRoutes); |
| 158 | const ref = yield* request(server, "/api/v1/repos/acme/repo/ref?name=refs%2Fheads%2Fmain"); |
| 159 | expect(ref.status).toBe(200); |
| 160 | expect(yield* Effect.promise(() => ref.json())).toEqual({ name: "refs/heads/main", oid }); |
| 161 | |
| 162 | const missing = yield* request(server, "/api/v1/repos/acme/missing"); |
| 163 | expect(missing.status).toBe(404); |
| 164 | expect(yield* Effect.promise(() => missing.json())).toMatchObject({ |
| 165 | _tag: "RepoNotFound", |
| 166 | owner: "acme", |
| 167 | repo: "missing", |
| 168 | }); |
| 169 | |
| 170 | const invalid = yield* request(server, "/api/v1/repos/acme/repo/ref"); |
| 171 | expect(invalid.status).toBe(400); |
| 172 | }).pipe(Effect.scoped), |
| 173 | ); |
| 174 | |
| 175 | it.effect("preserves binary protocol responses", () => |
| 176 | Effect.gen(function* () { |
| 177 | const server = yield* HttpRouter.toHttpEffect(OpenRoutes); |
| 178 | const bytes = new Uint8Array([0, 255, 1, 128, 10]); |
| 179 | const response = yield* request(server, "/acme/repo.git/git-upload-pack", { |
| 180 | method: "POST", |
| 181 | body: bytes, |
| 182 | }); |
| 183 | expect(response.status).toBe(200); |
| 184 | expect(response.headers.get("content-type")).toBe("application/x-git-upload-pack-result"); |
| 185 | expect(new Uint8Array(yield* Effect.promise(() => response.arrayBuffer()))).toEqual(bytes); |
| 186 | }).pipe(Effect.scoped), |
| 187 | ); |
| 188 | |
| 189 | it.effect( |
| 190 | "applies app middleware and handler overrides while keeping internal routes separate", |
| 191 | () => |
| 192 | Effect.gen(function* () { |
| 193 | const server = yield* HttpRouter.toHttpEffect(ProtectedRoutes); |
| 194 | const denied = yield* request(server, "/api/v3/user"); |
| 195 | expect(denied.status).toBe(401); |
| 196 | const wireDenied = yield* request(server, "/acme/repo.git/git-upload-pack", { |
| 197 | method: "POST", |
| 198 | }); |
| 199 | expect(wireDenied.status).toBe(401); |
| 200 | const allowed = yield* request(server, "/api/v3/user", { |
| 201 | headers: { authorization: "Bearer test" }, |
| 202 | }); |
| 203 | expect(allowed.status).toBe(200); |
| 204 | expect(allowed.headers.get("x-runtime-id")).toBe("request-runtime"); |
| 205 | expect(yield* Effect.promise(() => allowed.json())).toEqual({ login: "application" }); |
| 206 | const health = yield* request(server, "/health", { |
| 207 | headers: { authorization: "Bearer test" }, |
| 208 | }); |
| 209 | expect(health.status).toBe(200); |
| 210 | expect(yield* Effect.promise(() => health.json())).toBe("ok"); |
| 211 | const internal = yield* request(server, HASH_ROUTE, { method: "POST", body: "hash input" }); |
| 212 | expect(internal.status).toBe(200); |
| 213 | expect(yield* Effect.promise(() => internal.text())).toBe("hash input"); |
| 214 | }).pipe(Effect.scoped), |
| 215 | ); |
| 216 | it.effect("adds app endpoints beside prefixed Git defaults on a subset API", () => |
| 217 | Effect.gen(function* () { |
| 218 | const server = yield* HttpRouter.toHttpEffect(PrefixedRoutes); |
| 219 | const init = { headers: { authorization: "Bearer test" } }; |
| 220 | const denied = yield* request(server, "/git/api/v1/repos/acme/repo/ref?name=refs/heads/main"); |
| 221 | expect(denied.status).toBe(401); |
| 222 | const ref = yield* request( |
| 223 | server, |
| 224 | "/git/api/v1/repos/acme/repo/ref?name=refs/heads/main", |
| 225 | init, |
| 226 | ); |
| 227 | expect(ref.status).toBe(200); |
| 228 | expect(yield* Effect.promise(() => ref.json())).toEqual({ name: "refs/heads/main", oid }); |
| 229 | expect(ref.headers.get("x-runtime-id")).toBe("request-runtime"); |
| 230 | const health = yield* request(server, "/health", init); |
| 231 | expect(health.status).toBe(200); |
| 232 | expect(yield* Effect.promise(() => health.json())).toBe("ok"); |
| 233 | const unprefixed = yield* request( |
| 234 | server, |
| 235 | "/api/v1/repos/acme/repo/ref?name=refs/heads/main", |
| 236 | init, |
| 237 | ); |
| 238 | expect(unprefixed.status).toBe(404); |
| 239 | }).pipe(Effect.scoped), |
| 240 | ); |
| 241 | }); |