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 customHostnames from "@distilled.cloud/cloudflare/custom-hostnames"; |
| 7 | import { adopt } from "alchemy/AdoptPolicy"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { testName } from "../../names.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | // Cloudflare for SaaS (SSL for SaaS) is NOT provisioned on the test account: |
| 17 | // every fallback-origin API call fails with `SaasAccessNotGranted` |
| 18 | // (code 1456): "Access to configure this resource has not been granted for |
| 19 | // this zone. This feature is available with SSL for SaaS." |
| 20 | // Enabling Cloudflare for SaaS is a one-time dashboard action (SSL/TLS → |
| 21 | // Custom Hostnames → Enable) with no public API — verified directly against |
| 22 | // the API: PUT /custom_hostnames/fallback_origin is rejected with the same |
| 23 | // entitlement error. Set CLOUDFLARE_SAAS_ENABLED=1 once the zone is |
| 24 | // provisioned to run these tests. |
| 25 | const saasEnabled = !!process.env.CLOUDFLARE_SAAS_ENABLED; |
| 26 | const testSaas = test.provider.skipIf(!saasEnabled); |
| 27 | |
| 28 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 29 | |
| 30 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 31 | |
| 32 | // Deterministic record names for the origin DNS records the fallback origin |
| 33 | // points at (the API requires the origin to be a DNS record in the zone). |
| 34 | const ORIGIN_A = `${testName("alchemy-fallback-origin-a")}.${zoneName}`; |
| 35 | const ORIGIN_B = `${testName("alchemy-fallback-origin-b")}.${zoneName}`; |
| 36 | |
| 37 | const resolveZoneId = Effect.gen(function* () { |
| 38 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 39 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 40 | if (!zone) { |
| 41 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 42 | } |
| 43 | return zone.id; |
| 44 | }); |
| 45 | |
| 46 | // The scoped API token the test harness mints propagates eventually- |
| 47 | // consistently across Cloudflare's edge — a fresh token intermittently 403s |
| 48 | // (typed as `Forbidden` in the distilled unions). Ride out the blips on the |
| 49 | // test's own out-of-band verification calls. |
| 50 | const forbiddenBlips = Schedule.exponential("500 millis"); |
| 51 | |
| 52 | // Observe the zone's fallback origin out-of-band; `undefined` when it is not |
| 53 | // configured (`FallbackOriginNotFound`) or is mid-deletion. |
| 54 | const observeFallbackOrigin = (zoneId: string) => |
| 55 | customHostnames.getFallbackOrigin({ zoneId }).pipe( |
| 56 | Effect.retry({ while: (e) => e._tag === "Forbidden", schedule: forbiddenBlips, times: 8 }), |
| 57 | Effect.map((r) => ({ origin: r.origin ?? undefined, status: r.status ?? undefined })), |
| 58 | Effect.catchTag("FallbackOriginNotFound", () => Effect.succeed(undefined)), |
| 59 | ); |
| 60 | |
| 61 | // Typed entitlement probe: every fallback-origin call on an unprovisioned |
| 62 | // zone is rejected with `SaasAccessNotGranted` (code 1456). When the suite |
| 63 | // is enabled but the zone still lacks the entitlement, fail fast with a |
| 64 | // clear message instead of surfacing raw entitlement errors mid-deploy. |
| 65 | const probeSaasEntitlement = (zoneId: string) => |
| 66 | customHostnames.getFallbackOrigin({ zoneId }).pipe( |
| 67 | Effect.asVoid, |
| 68 | Effect.retry({ while: (e) => e._tag === "Forbidden", schedule: forbiddenBlips, times: 8 }), |
| 69 | Effect.catchTag("FallbackOriginNotFound", () => Effect.void), |
| 70 | Effect.catchTag("SaasAccessNotGranted", (e) => |
| 71 | Effect.die( |
| 72 | new Error( |
| 73 | `Cloudflare for SaaS is not provisioned on zone "${zoneName}" — ` + |
| 74 | `unset CLOUDFLARE_SAAS_ENABLED or enable SSL for SaaS first: ${e.message}`, |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | ); |
| 79 | |
| 80 | const isGone = ( |
| 81 | observed: { origin: string | undefined; status: string | undefined } | undefined, |
| 82 | ): boolean => |
| 83 | observed === undefined || |
| 84 | observed.origin === undefined || |
| 85 | observed.status === "pending_deletion" || |
| 86 | observed.status === "deletion_timed_out"; |
| 87 | |
| 88 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 89 | // API for the fallback origin, so `list()` enumerates every zone via |
| 90 | // `listAllZones` and reads the singleton in each, skipping zones without |
| 91 | // Cloudflare for SaaS (`SaasAccessNotGranted` / `Forbidden`). This read-only |
| 92 | // assertion ALWAYS runs — on an unentitled account every zone is skipped and |
| 93 | // the result is a well-typed empty `FallbackOriginAttributes[]`. |
| 94 | test.provider( |
| 95 | "list enumerates fallback origins across all zones", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const provider = yield* Provider.findProvider(Cloudflare.CustomHostname.FallbackOrigin); |
| 101 | const all = yield* provider.list(); |
| 102 | |
| 103 | // Well-typed `FallbackOriginAttributes[]`: each element matches the |
| 104 | // shape `read` produces. |
| 105 | expect(Array.isArray(all)).toBe(true); |
| 106 | for (const item of all) { |
| 107 | expect(typeof item.zoneId).toBe("string"); |
| 108 | expect(typeof item.origin).toBe("string"); |
| 109 | } |
| 110 | |
| 111 | yield* stack.destroy(); |
| 112 | }).pipe(logLevel), |
| 113 | { tags: ["provider:cloudflare", "provider:cloudflare:customhostname", "live"] }, |
| 114 | ); |
| 115 | |
| 116 | // Entitlement-gated: when Cloudflare for SaaS is provisioned, deploy a |
| 117 | // fallback origin and assert `list()` surfaces it. Skipped on the standing |
| 118 | // test account, where every fallback-origin call returns |
| 119 | // `SaasAccessNotGranted` (code 1456). |
| 120 | testSaas( |
| 121 | "list surfaces a deployed fallback origin", |
| 122 | (stack) => |
| 123 | Effect.gen(function* () { |
| 124 | const zoneId = yield* resolveZoneId; |
| 125 | yield* probeSaasEntitlement(zoneId); |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | |
| 129 | yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | const record = yield* Cloudflare.DNS.Record("OriginA", { |
| 132 | zoneId, |
| 133 | name: ORIGIN_A, |
| 134 | type: "A", |
| 135 | content: "203.0.113.50", |
| 136 | proxied: true, |
| 137 | }).pipe(adopt(true)); |
| 138 | return yield* Cloudflare.CustomHostname.FallbackOrigin("Fallback", { |
| 139 | zoneId, |
| 140 | origin: record.name, |
| 141 | }).pipe(adopt(true)); |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | const provider = yield* Provider.findProvider(Cloudflare.CustomHostname.FallbackOrigin); |
| 146 | const all = yield* provider.list(); |
| 147 | expect(all.some((o) => o.zoneId === zoneId && o.origin === ORIGIN_A)).toBe(true); |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | }).pipe(logLevel), |
| 151 | { tags: ["provider:cloudflare", "provider:cloudflare:customhostname", "live"], timeout: 300_000 }, |
| 152 | ); |
| 153 | |
| 154 | testSaas( |
| 155 | "set, update and delete the zone fallback origin", |
| 156 | (stack) => |
| 157 | Effect.gen(function* () { |
| 158 | const zoneId = yield* resolveZoneId; |
| 159 | yield* probeSaasEntitlement(zoneId); |
| 160 | |
| 161 | yield* stack.destroy(); |
| 162 | |
| 163 | // Create: an origin DNS record plus the fallback origin pointing at it. |
| 164 | const initial = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | const record = yield* Cloudflare.DNS.Record("OriginA", { |
| 167 | zoneId, |
| 168 | name: ORIGIN_A, |
| 169 | type: "A", |
| 170 | content: "203.0.113.50", |
| 171 | proxied: true, |
| 172 | }).pipe(adopt(true)); |
| 173 | const fallback = yield* Cloudflare.CustomHostname.FallbackOrigin("Fallback", { |
| 174 | zoneId, |
| 175 | origin: record.name, |
| 176 | }).pipe(adopt(true)); |
| 177 | return { record, fallback }; |
| 178 | }), |
| 179 | ); |
| 180 | |
| 181 | expect(initial.fallback.zoneId).toEqual(zoneId); |
| 182 | expect(initial.fallback.origin).toEqual(ORIGIN_A); |
| 183 | expect(initial.fallback.status).toBeDefined(); |
| 184 | |
| 185 | const liveA = yield* observeFallbackOrigin(zoneId); |
| 186 | expect(liveA?.origin).toEqual(ORIGIN_A); |
| 187 | |
| 188 | // Re-deploying the same desired state is a no-op. |
| 189 | const again = yield* stack.deploy( |
| 190 | Effect.gen(function* () { |
| 191 | const record = yield* Cloudflare.DNS.Record("OriginA", { |
| 192 | zoneId, |
| 193 | name: ORIGIN_A, |
| 194 | type: "A", |
| 195 | content: "203.0.113.50", |
| 196 | proxied: true, |
| 197 | }).pipe(adopt(true)); |
| 198 | const fallback = yield* Cloudflare.CustomHostname.FallbackOrigin("Fallback", { |
| 199 | zoneId, |
| 200 | origin: record.name, |
| 201 | }).pipe(adopt(true)); |
| 202 | return { record, fallback }; |
| 203 | }), |
| 204 | ); |
| 205 | expect(again.fallback.origin).toEqual(ORIGIN_A); |
| 206 | |
| 207 | // Update: point at a different origin record (PUT is an upsert — the |
| 208 | // singleton is updated in place). |
| 209 | const updated = yield* stack.deploy( |
| 210 | Effect.gen(function* () { |
| 211 | const record = yield* Cloudflare.DNS.Record("OriginB", { |
| 212 | zoneId, |
| 213 | name: ORIGIN_B, |
| 214 | type: "A", |
| 215 | content: "203.0.113.51", |
| 216 | proxied: true, |
| 217 | }).pipe(adopt(true)); |
| 218 | const fallback = yield* Cloudflare.CustomHostname.FallbackOrigin("Fallback", { |
| 219 | zoneId, |
| 220 | origin: record.name, |
| 221 | }).pipe(adopt(true)); |
| 222 | return { record, fallback }; |
| 223 | }), |
| 224 | ); |
| 225 | |
| 226 | expect(updated.fallback.origin).toEqual(ORIGIN_B); |
| 227 | |
| 228 | const liveB = yield* observeFallbackOrigin(zoneId); |
| 229 | expect(liveB?.origin).toEqual(ORIGIN_B); |
| 230 | |
| 231 | yield* stack.destroy(); |
| 232 | |
| 233 | // Deletion is asynchronous — poll (bounded) until the fallback origin |
| 234 | // is gone or mid-deletion. |
| 235 | const gone = yield* observeFallbackOrigin(zoneId).pipe( |
| 236 | Effect.repeat({ schedule: Schedule.spaced("3 seconds"), until: isGone, times: 20 }), |
| 237 | ); |
| 238 | expect(isGone(gone)).toBe(true); |
| 239 | }).pipe(logLevel), |
| 240 | { tags: ["provider:cloudflare", "provider:cloudflare:customhostname", "live"], timeout: 300_000 }, |
| 241 | ); |