The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as GitHub from "@alchemy.run/github"; |
| 2 | import { Credentials, config } from "@alchemy.run/github/Credentials"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 7 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | |
| 11 | /** |
| 12 | * Run distilled GitHub calls with the provider's credentials, outside a |
| 13 | * provider test (out-of-band setup and verification). |
| 14 | */ |
| 15 | export const gh = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 16 | effect.pipe(Effect.provide(GitHub.providers({ baseUrl: "github.com" }))); |
| 17 | |
| 18 | /** |
| 19 | * Keep a provider's `list()` inside the test organization. GitHub's `list()` |
| 20 | * walks every repository the token can see (`/user/repos`); this rewrites |
| 21 | * that to `/orgs/{owner}/repos` and, with `only`, narrows the listing to one |
| 22 | * fixture repository and refuses any request outside it. |
| 23 | */ |
| 24 | export const scopedToOwner = |
| 25 | (owner: string, only?: { readonly repository: string; readonly path: string }) => |
| 26 | <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 27 | Effect.gen(function* () { |
| 28 | const client = yield* HttpClient.HttpClient; |
| 29 | const orgRepos = `/orgs/${owner}/repos`; |
| 30 | const scoped = client.pipe( |
| 31 | HttpClient.mapRequest((request) => { |
| 32 | const url = new URL(request.url); |
| 33 | if (url.pathname !== "/user/repos") return request; |
| 34 | url.pathname = orgRepos; |
| 35 | return HttpClientRequest.setUrl(request, url.href); |
| 36 | }), |
| 37 | HttpClient.transform((response, request) => { |
| 38 | if (only === undefined) return response; |
| 39 | const url = new URL(request.url); |
| 40 | const allowed = `/repos/${owner}/${only.repository}/${only.path}`; |
| 41 | if ( |
| 42 | url.origin !== "https://api.github.com" || |
| 43 | (url.pathname !== orgRepos && url.pathname !== allowed) |
| 44 | ) { |
| 45 | return Effect.die(`Unsafe GitHub list request: ${url}`); |
| 46 | } |
| 47 | if (url.pathname !== orgRepos) return response; |
| 48 | // Only the fixture repository, so enumeration stays on `allowed`. |
| 49 | return Effect.flatMap(response, (res) => |
| 50 | Effect.map(res.json, (repos) => |
| 51 | HttpClientResponse.fromWeb( |
| 52 | request, |
| 53 | new Response( |
| 54 | JSON.stringify( |
| 55 | (repos as Array<{ name: string }>).filter( |
| 56 | (repo) => repo.name === only.repository, |
| 57 | ), |
| 58 | ), |
| 59 | { status: res.status, headers: { "content-type": "application/json" } }, |
| 60 | ), |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | }), |
| 65 | ); |
| 66 | return yield* effect.pipe(Effect.provideService(HttpClient.HttpClient, scoped)); |
| 67 | }); |
| 68 | |
| 69 | /** |
| 70 | * A fake GitHub for provider unit tests: the provider's credentials are a |
| 71 | * dummy token and every request goes to `fetch` instead of the network. |
| 72 | * Overrides the `Fetch` reference the stack's HTTP client reads per request |
| 73 | * (the stack supplies its own `HttpClient`). |
| 74 | */ |
| 75 | export const mockGitHub = ( |
| 76 | fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>, |
| 77 | ) => |
| 78 | Layer.mergeAll( |
| 79 | Layer.succeed(Credentials, Effect.succeed(config(Redacted.make("test-token"), undefined))), |
| 80 | Layer.succeed(FetchHttpClient.Fetch, fetch as typeof globalThis.fetch), |
| 81 | ); |