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 { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Core from "@alchemy.run/test/Core"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | import Stack from "./fixtures/stack.ts"; |
| 13 | |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 15 | providers: Cloudflare.providers(), |
| 16 | }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 21 | |
| 22 | const stack = beforeAll(deploy(Stack)); |
| 23 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 24 | |
| 25 | // Resolve the test zone id via the SDK. `withProviders` supplies the Cloudflare |
| 26 | // credentials + account id the lookup needs inside a plain test body. |
| 27 | const resolveZoneId = Core.withProviders( |
| 28 | Effect.gen(function* () { |
| 29 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 30 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 31 | return zone?.id; |
| 32 | }), |
| 33 | { providers: Cloudflare.providers() }, |
| 34 | "DnsTestStack", |
| 35 | ); |
| 36 | |
| 37 | test( |
| 38 | "deployed worker drives the full DNS record CRUD surface via DnsReadWrite", |
| 39 | Effect.gen(function* () { |
| 40 | const { effectUrl } = yield* stack; |
| 41 | expect(effectUrl).toBeTypeOf("string"); |
| 42 | |
| 43 | const zoneId = yield* resolveZoneId; |
| 44 | expect(zoneId, `zone "${zoneName}" not found in account`).toBeTypeOf("string"); |
| 45 | |
| 46 | // Deterministic record name — the same on every run (never |
| 47 | // Date.now()/random). The fixture's /dns route deletes any leftover |
| 48 | // record with this name before creating, so a crashed run self-heals |
| 49 | // instead of leaking records into the zone. |
| 50 | const name = `${testName("alchemy-dns-test-crud")}.${zoneName}`; |
| 51 | |
| 52 | const client = yield* HttpClient.HttpClient; |
| 53 | const res = yield* client.get(`${effectUrl}/dns?name=${encodeURIComponent(name)}`).pipe( |
| 54 | // A cold-starting or briefly-unhealthy edge returns 5xx — often a |
| 55 | // Cloudflare HTML error page, NOT the worker's structured JSON, so we |
| 56 | // must never try to parse it. Treat any non-200 as transient and ride |
| 57 | // it out: this covers both cold start and eventual-consistency blips |
| 58 | // in the scoped API-token propagation the worker depends on. |
| 59 | Effect.flatMap((res) => |
| 60 | res.status === 200 |
| 61 | ? Effect.succeed(res) |
| 62 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 63 | ), |
| 64 | // Cap exponential backoff at 3s so retries stay bounded. |
| 65 | Effect.retry({ |
| 66 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]), |
| 67 | times: 20, |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | const body = (yield* res.json) as { |
| 72 | id: string; |
| 73 | getName: string; |
| 74 | count: number; |
| 75 | updatedId: string; |
| 76 | deleted: boolean; |
| 77 | }; |
| 78 | expect(body.id.length).toBeGreaterThan(0); |
| 79 | expect(body.getName).toBe(name); |
| 80 | expect(body.count).toBeGreaterThan(0); |
| 81 | expect(body.updatedId).toBe(body.id); |
| 82 | expect(body.deleted).toBe(true); |
| 83 | }).pipe(logLevel), |
| 84 | { |
| 85 | tags: [ |
| 86 | "provider:cloudflare", |
| 87 | "provider:cloudflare:dns", |
| 88 | "provider:cloudflare:worker", |
| 89 | "provider:cloudflare:zone", |
| 90 | "live", |
| 91 | ], |
| 92 | timeout: 180_000, |
| 93 | }, |
| 94 | ); |