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 { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 7 | import * as emailRouting from "@distilled.cloud/cloudflare/email-routing"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { EMAIL_ROUTING_ZONE, emailRoutingScoped } from "./scope.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 zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 19 | |
| 20 | // Deterministic per-stage subdomains, reused on every run. |
| 21 | const subdomainOf = (stage: string, label: string) => |
| 22 | `alchemy-routesub-${label}-${stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}.${zoneName}`; |
| 23 | |
| 24 | const resolveZoneId = Effect.gen(function* () { |
| 25 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 26 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 27 | if (!zone) { |
| 28 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 29 | } |
| 30 | return zone.id; |
| 31 | }); |
| 32 | |
| 33 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 34 | |
| 35 | const retryForbidden = <A, E extends { readonly _tag: string }, R>( |
| 36 | effect: Effect.Effect<A, E, R>, |
| 37 | ) => |
| 38 | effect.pipe( |
| 39 | Effect.retry({ |
| 40 | while: (e) => e._tag === "Forbidden", |
| 41 | schedule: forbiddenRetrySchedule, |
| 42 | times: 8, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | const routingMx = (zoneId: string, name: string) => |
| 47 | retryForbidden( |
| 48 | dns.listRecords.items({ zoneId, name: { exact: name }, type: "MX" }).pipe( |
| 49 | Stream.filter((record) => /^route\d+\.mx\.cloudflare\.net\.?$/iu.test(record.content ?? "")), |
| 50 | Stream.runCollect, |
| 51 | Effect.map((chunk) => Array.from(chunk)), |
| 52 | ), |
| 53 | ); |
| 54 | |
| 55 | // Every record at the name, so a leftover SPF TXT also fails the assertion. |
| 56 | const recordsAt = (zoneId: string, name: string) => |
| 57 | retryForbidden( |
| 58 | dns.listRecords.items({ zoneId, name: { exact: name } }).pipe( |
| 59 | Stream.map((record) => `${record.type} ${record.content}`), |
| 60 | Stream.runCollect, |
| 61 | Effect.map((chunk) => Array.from(chunk)), |
| 62 | ), |
| 63 | ); |
| 64 | |
| 65 | describe.skipIf(!emailRoutingScoped)( |
| 66 | "RoutingSubdomain", |
| 67 | { |
| 68 | tags: ["provider:cloudflare", "provider:cloudflare:email", "provider:cloudflare:zone", "live"], |
| 69 | }, |
| 70 | () => { |
| 71 | test.provider( |
| 72 | "create, rename and destroy a routing subdomain, leaving the apex routing", |
| 73 | (stack) => |
| 74 | Effect.gen(function* () { |
| 75 | const zoneId = yield* resolveZoneId; |
| 76 | const nameA = subdomainOf(stack.stage, "a"); |
| 77 | const nameB = subdomainOf(stack.stage, "b"); |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | // The subdomain needs Email Routing on the zone; capture it to restore. |
| 81 | const before = yield* retryForbidden(emailRouting.getEmailRouting({ zoneId })); |
| 82 | if (!before.enabled) yield* retryForbidden(emailRouting.enableEmailRouting({ zoneId })); |
| 83 | |
| 84 | const subdomainProgram = (name: string) => |
| 85 | Effect.gen(function* () { |
| 86 | return yield* Cloudflare.Email.RoutingSubdomain("Inbound", { zoneId, name }); |
| 87 | }); |
| 88 | const deploy = (name: string) => stack.deploy(subdomainProgram(name)); |
| 89 | |
| 90 | const subdomain = yield* deploy(nameA); |
| 91 | expect(subdomain.zoneId).toEqual(zoneId); |
| 92 | expect(subdomain.name).toEqual(nameA); |
| 93 | expect(subdomain.mx.length).toBeGreaterThan(0); |
| 94 | |
| 95 | // Out-of-band: the subdomain has Email Routing MX records. |
| 96 | expect((yield* routingMx(zoneId, nameA)).length).toBeGreaterThan(0); |
| 97 | |
| 98 | // A second deploy with the same props is a no-op; a rename replaces. |
| 99 | expect((yield* stack.plan(subdomainProgram(nameA))).resources.Inbound?.action).toBe( |
| 100 | "noop", |
| 101 | ); |
| 102 | expect((yield* stack.plan(subdomainProgram(nameB))).resources.Inbound?.action).toBe( |
| 103 | "replace", |
| 104 | ); |
| 105 | const again = yield* deploy(nameA); |
| 106 | expect(again.mx).toEqual(subdomain.mx); |
| 107 | |
| 108 | // Renaming replaces it: the new name routes mail, the old one no longer does. |
| 109 | const renamed = yield* deploy(nameB); |
| 110 | expect(renamed.name).toEqual(nameB); |
| 111 | expect((yield* routingMx(zoneId, nameB)).length).toBeGreaterThan(0); |
| 112 | expect(yield* recordsAt(zoneId, nameA)).toEqual([]); |
| 113 | |
| 114 | yield* stack.destroy(); |
| 115 | |
| 116 | // The subdomain's records are gone, and the apex still routes mail. |
| 117 | expect(yield* recordsAt(zoneId, nameB)).toEqual([]); |
| 118 | const after = yield* retryForbidden(emailRouting.getEmailRouting({ zoneId })); |
| 119 | expect(after.enabled).toEqual(true); |
| 120 | |
| 121 | if (!before.enabled) yield* retryForbidden(emailRouting.disableEmailRouting({ zoneId })); |
| 122 | }).pipe(logLevel), |
| 123 | { timeout: 120_000, exclusive: EMAIL_ROUTING_ZONE }, |
| 124 | ); |
| 125 | }, |
| 126 | ); |