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 { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as alerting from "@distilled.cloud/cloudflare/alerting"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as pathe from "pathe"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | import { expectUrlContains } from "../Utils/Http.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const main = pathe.resolve(import.meta.dirname, "fixtures/webhook-receiver.ts"); |
| 19 | |
| 20 | // Cloudflare fires a test POST at the webhook URL on create/update, so the |
| 21 | // destination worker must be deployed and serving before the webhook |
| 22 | // resource is created. |
| 23 | const Receiver = () => Cloudflare.Worker("WebhookReceiver", { main, workersDev: true }); |
| 24 | |
| 25 | test.provider( |
| 26 | "create, update, delete webhook destination", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 30 | |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | // Phase 1: deploy the receiving worker alone and wait until the |
| 34 | // workers.dev URL actually serves (fresh subdomains take a few |
| 35 | // seconds to propagate; the webhook test POST must hit a live 200). |
| 36 | const receiver = yield* stack.deploy(Receiver()); |
| 37 | expect(receiver.url).toBeDefined(); |
| 38 | yield* expectUrlContains(receiver.url!, "webhook-ok", { label: "webhook receiver" }); |
| 39 | |
| 40 | // Phase 2: create the webhook destination pointing at the worker. |
| 41 | const webhook = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const worker = yield* Receiver(); |
| 44 | return yield* Cloudflare.Alerting.NotificationWebhook("AlertWebhook", { |
| 45 | url: worker.url.as<string>(), |
| 46 | }); |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | expect(webhook.webhookId).toBeDefined(); |
| 51 | expect(webhook.accountId).toEqual(accountId); |
| 52 | expect(webhook.name).toBeDefined(); |
| 53 | expect(webhook.url).toEqual(receiver.url); |
| 54 | |
| 55 | // Verify out-of-band via the API. |
| 56 | const actual = yield* alerting.getDestinationWebhook({ |
| 57 | accountId, |
| 58 | webhookId: webhook.webhookId, |
| 59 | }); |
| 60 | expect(actual.name).toEqual(webhook.name); |
| 61 | expect(actual.url).toEqual(receiver.url); |
| 62 | |
| 63 | // Phase 3: rename (mutable prop) — same id, new name. |
| 64 | const renamed = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const worker = yield* Receiver(); |
| 67 | return yield* Cloudflare.Alerting.NotificationWebhook("AlertWebhook", { |
| 68 | name: testName("alchemy-test-alerting-webhook-renamed"), |
| 69 | url: worker.url.as<string>(), |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | expect(renamed.webhookId).toEqual(webhook.webhookId); |
| 74 | expect(renamed.name).toEqual(testName("alchemy-test-alerting-webhook-renamed")); |
| 75 | |
| 76 | const afterRename = yield* alerting.getDestinationWebhook({ |
| 77 | accountId, |
| 78 | webhookId: webhook.webhookId, |
| 79 | }); |
| 80 | expect(afterRename.name).toEqual(testName("alchemy-test-alerting-webhook-renamed")); |
| 81 | |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | yield* waitForWebhookDeleted(accountId, webhook.webhookId); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: [ |
| 88 | "provider:cloudflare", |
| 89 | "provider:cloudflare:alerting", |
| 90 | "provider:cloudflare:worker", |
| 91 | "live", |
| 92 | ], |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | test.provider( |
| 97 | "list enumerates the deployed webhook destination", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | // Deploy the receiving worker, wait for it to serve, then create the |
| 103 | // webhook destination pointing at it. |
| 104 | const receiver = yield* stack.deploy(Receiver()); |
| 105 | expect(receiver.url).toBeDefined(); |
| 106 | yield* expectUrlContains(receiver.url!, "webhook-ok", { label: "webhook receiver" }); |
| 107 | |
| 108 | const webhook = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | const worker = yield* Receiver(); |
| 111 | return yield* Cloudflare.Alerting.NotificationWebhook("ListWebhook", { |
| 112 | url: worker.url.as<string>(), |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | const provider = yield* Provider.findProvider(Cloudflare.Alerting.NotificationWebhook); |
| 118 | const all = yield* provider.list(); |
| 119 | |
| 120 | const match = all.find((w) => w.webhookId === webhook.webhookId); |
| 121 | expect(match).toBeDefined(); |
| 122 | expect(match!.name).toEqual(webhook.name); |
| 123 | expect(match!.url).toEqual(webhook.url); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | yield* waitForWebhookDeleted(webhook.accountId, webhook.webhookId); |
| 128 | }).pipe(logLevel), |
| 129 | { |
| 130 | tags: [ |
| 131 | "provider:cloudflare", |
| 132 | "provider:cloudflare:alerting", |
| 133 | "provider:cloudflare:worker", |
| 134 | "live", |
| 135 | ], |
| 136 | }, |
| 137 | ); |
| 138 | |
| 139 | const waitForWebhookDeleted = (accountId: string, webhookId: string) => |
| 140 | alerting.getDestinationWebhook({ accountId, webhookId }).pipe( |
| 141 | Effect.flatMap(() => Effect.fail({ _tag: "WebhookNotDeleted" } as const)), |
| 142 | Effect.catchTag("WebhookNotFound", () => Effect.void), |
| 143 | Effect.retry({ |
| 144 | while: (e) => e._tag === "WebhookNotDeleted", |
| 145 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 146 | }), |
| 147 | ); |