The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as GitHub from "@alchemy.run/github"; |
| 3 | import { webhookPath } from "@alchemy.run/github/RepositoryEventSource"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Repos from "@distilled.cloud/github/repos"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import GitHubEventSourceWorker, { owner, repository } from "./fixtures/event-source-worker.ts"; |
| 14 | import { gh } from "./scope.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ |
| 17 | providers: Layer.mergeAll(Cloudflare.providers(), GitHub.providers()), |
| 18 | }); |
| 19 | const { executeWhenReady, getWhenReady } = Test; |
| 20 | |
| 21 | // Public and retained across runs: the gh token lacks delete_repo. |
| 22 | const Repository = GitHub.Repository("Repo", { |
| 23 | owner, |
| 24 | name: repository, |
| 25 | visibility: "public", |
| 26 | autoInit: true, |
| 27 | }); |
| 28 | |
| 29 | const listHooks = Effect.gen(function* () { |
| 30 | return yield* gh( |
| 31 | Stream.runCollect(Repos.listWebhooks.items({ owner, repo: repository, per_page: 100 })), |
| 32 | ); |
| 33 | }); |
| 34 | |
| 35 | // Ping the webhook and return the status the Worker answered GitHub with. |
| 36 | const pingStatus = Effect.fn(function* (hookId: number) { |
| 37 | const pings = gh(Repos.listWebhookDeliveries({ owner, repo: repository, hook_id: hookId })).pipe( |
| 38 | Effect.map((deliveries) => deliveries.filter((delivery) => delivery.event === "ping")), |
| 39 | ); |
| 40 | const before = (yield* pings).length; |
| 41 | yield* gh(Repos.pingWebhook({ owner, repo: repository, hook_id: hookId })); |
| 42 | // Deliveries are listed newest first. |
| 43 | const after = yield* pings.pipe( |
| 44 | Effect.repeat({ |
| 45 | schedule: Schedule.spaced("2 seconds"), |
| 46 | until: (deliveries) => deliveries.length > before, |
| 47 | times: 15, |
| 48 | }), |
| 49 | ); |
| 50 | return after.length > before ? after[0]!.status_code : undefined; |
| 51 | }); |
| 52 | |
| 53 | test.provider( |
| 54 | "consumeRepositoryEvents without a secret verifies every delivery", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | // The webhook only depends on the Worker's URL, so the repository has |
| 59 | // to exist before the Worker deploys. |
| 60 | yield* stack.deploy(Repository); |
| 61 | const { url } = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | yield* Repository; |
| 64 | const worker = yield* GitHubEventSourceWorker; |
| 65 | return { url: worker.url.as<string>() }; |
| 66 | }), |
| 67 | ); |
| 68 | const base = url.replace(/\/+$/, ""); |
| 69 | const deliveryUrl = `${base}${webhookPath({ owner, repository })}`; |
| 70 | yield* getWhenReady(base); |
| 71 | |
| 72 | // GitHub masks a configured secret as "********" and omits it otherwise. |
| 73 | const hook = (yield* listHooks).find((hook) => hook.config.url === deliveryUrl); |
| 74 | const secret = hook?.config.secret; |
| 75 | expect(Redacted.isRedacted(secret) ? Redacted.value(secret) : secret).toBe("********"); |
| 76 | |
| 77 | // An unsigned delivery is rejected. |
| 78 | const forged = yield* executeWhenReady( |
| 79 | HttpClientRequest.post(deliveryUrl).pipe( |
| 80 | HttpClientRequest.setHeader("x-github-event", "push"), |
| 81 | HttpClientRequest.bodyJsonUnsafe({ ref: "refs/heads/main" }), |
| 82 | ), |
| 83 | ); |
| 84 | expect(forged.status).toBe(401); |
| 85 | |
| 86 | // A delivery GitHub signs with the generated secret is accepted. GitHub |
| 87 | // can reach the fresh workers.dev URL before it serves there (404), so |
| 88 | // ping again until a delivery gets past the cold start. |
| 89 | const status = yield* pingStatus(hook!.id).pipe( |
| 90 | Effect.repeat({ |
| 91 | schedule: Schedule.spaced("3 seconds"), |
| 92 | until: (status) => status !== undefined && status !== 404 && status < 500, |
| 93 | times: 10, |
| 94 | }), |
| 95 | ); |
| 96 | expect(status).toBe(202); |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | expect((yield* listHooks).filter((hook) => hook.config.url === deliveryUrl)).toEqual([]); |
| 100 | }), |
| 101 | { |
| 102 | timeout: 180_000, |
| 103 | tags: [ |
| 104 | "provider:cloudflare", |
| 105 | "provider:cloudflare:worker", |
| 106 | "provider:github", |
| 107 | "provider:github:webhook", |
| 108 | "live", |
| 109 | ], |
| 110 | }, |
| 111 | ); |