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 emailRouting from "@distilled.cloud/cloudflare/email-routing"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 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 | import { EMAIL_ROUTING_ZONE, emailRoutingScoped } from "./scope.ts"; |
| 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 | const resolveZoneId = Effect.gen(function* () { |
| 21 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 22 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 23 | if (!zone) { |
| 24 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 25 | } |
| 26 | return zone.id; |
| 27 | }); |
| 28 | |
| 29 | // The scoped API token the test harness mints propagates eventually- |
| 30 | // consistently across Cloudflare's edge — a fresh token intermittently 403s |
| 31 | // with "Unable to authenticate request". Ride out the blips on the test's |
| 32 | // own out-of-band calls by retrying the typed `Forbidden` error (part of |
| 33 | // each email-routing operation's error union via distilled patches). |
| 34 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 35 | |
| 36 | const getCatchAll = (zoneId: string) => |
| 37 | emailRouting.getRuleCatchAll({ zoneId }).pipe( |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "Forbidden", |
| 40 | schedule: forbiddenRetrySchedule, |
| 41 | times: 8, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // Normalize the zone to a known baseline so each run starts from the same |
| 46 | // cloud state regardless of what a previous (possibly interrupted) run left |
| 47 | // behind: Email Routing enabled, catch-all back at the Cloudflare default |
| 48 | // (disabled, drop, no name). |
| 49 | const setBaseline = (zoneId: string) => |
| 50 | emailRouting.enableEmailRouting({ zoneId }).pipe( |
| 51 | Effect.andThen( |
| 52 | emailRouting.putRuleCatchAll({ |
| 53 | zoneId, |
| 54 | matchers: [{ type: "all" }], |
| 55 | actions: [{ type: "drop" }], |
| 56 | enabled: false, |
| 57 | name: "", |
| 58 | }), |
| 59 | ), |
| 60 | Effect.retry({ |
| 61 | while: (e) => e._tag === "Forbidden", |
| 62 | schedule: forbiddenRetrySchedule, |
| 63 | times: 8, |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | describe.sequential.skipIf(!emailRoutingScoped)( |
| 68 | "EmailCatchAll", |
| 69 | { |
| 70 | tags: ["provider:cloudflare", "provider:cloudflare:email", "provider:cloudflare:zone", "live"], |
| 71 | }, |
| 72 | () => { |
| 73 | test.provider( |
| 74 | "configures the catch-all rule and restores the baseline on destroy", |
| 75 | (stack) => |
| 76 | Effect.gen(function* () { |
| 77 | const zoneId = yield* resolveZoneId; |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | yield* setBaseline(zoneId); |
| 81 | |
| 82 | const catchAll = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | const routing = yield* Cloudflare.Email.Routing("Routing", { zone: zoneName }); |
| 85 | return yield* Cloudflare.Email.CatchAll("CatchAll", { |
| 86 | zone: { zoneId: routing.zoneId }, |
| 87 | name: testName("alchemy catch-all"), |
| 88 | actions: [{ type: "drop" }], |
| 89 | }); |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(catchAll.zoneId).toEqual(zoneId); |
| 94 | expect(catchAll.ruleId).not.toEqual(""); |
| 95 | expect(catchAll.name).toEqual(testName("alchemy catch-all")); |
| 96 | expect(catchAll.enabled).toEqual(true); |
| 97 | expect(catchAll.actions).toEqual([{ type: "drop" }]); |
| 98 | // The pre-management state was captured for restore-on-destroy. |
| 99 | expect(catchAll.initialEnabled).toEqual(false); |
| 100 | expect(catchAll.initialName).toEqual(""); |
| 101 | expect(catchAll.initialActions).toEqual([{ type: "drop" }]); |
| 102 | |
| 103 | // Out-of-band verification against the live API. |
| 104 | const live = yield* getCatchAll(zoneId); |
| 105 | expect(live.enabled).toEqual(true); |
| 106 | expect(live.name).toEqual(testName("alchemy catch-all")); |
| 107 | expect(live.actions).toEqual([{ type: "drop" }]); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | // Destroy restored the state the catch-all rule had before we |
| 112 | // managed it. (The rule itself always exists — it is a singleton.) |
| 113 | const restored = yield* getCatchAll(zoneId); |
| 114 | expect(restored.enabled).toEqual(false); |
| 115 | expect(restored.name ?? "").toEqual(""); |
| 116 | expect(restored.actions).toEqual([{ type: "drop" }]); |
| 117 | }).pipe(logLevel), |
| 118 | { exclusive: EMAIL_ROUTING_ZONE }, |
| 119 | ); |
| 120 | |
| 121 | test.provider( |
| 122 | "updates the catch-all rule in place and keeps the captured baseline", |
| 123 | (stack) => |
| 124 | Effect.gen(function* () { |
| 125 | const zoneId = yield* resolveZoneId; |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | yield* setBaseline(zoneId); |
| 129 | |
| 130 | const deployCatchAll = (props: { |
| 131 | name: string; |
| 132 | enabled?: boolean; |
| 133 | actions: Cloudflare.Email.Action[]; |
| 134 | }) => |
| 135 | stack.deploy( |
| 136 | Effect.gen(function* () { |
| 137 | const routing = yield* Cloudflare.Email.Routing("Routing", { zone: zoneName }); |
| 138 | return yield* Cloudflare.Email.CatchAll("CatchAll", { |
| 139 | zone: { zoneId: routing.zoneId }, |
| 140 | ...props, |
| 141 | }); |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | const initial = yield* deployCatchAll({ |
| 146 | name: "alchemy update test", |
| 147 | actions: [{ type: "drop" }], |
| 148 | }); |
| 149 | |
| 150 | expect(initial.enabled).toEqual(true); |
| 151 | expect(initial.initialEnabled).toEqual(false); |
| 152 | const ruleId = initial.ruleId; |
| 153 | |
| 154 | const updated = yield* deployCatchAll({ |
| 155 | name: "alchemy update test v2", |
| 156 | enabled: false, |
| 157 | actions: [{ type: "drop" }], |
| 158 | }); |
| 159 | |
| 160 | // Same singleton updated in place; the captured baseline survives |
| 161 | // the update so destroy still restores the pre-management state. |
| 162 | expect(updated.ruleId).toEqual(ruleId); |
| 163 | expect(updated.zoneId).toEqual(zoneId); |
| 164 | expect(updated.name).toEqual("alchemy update test v2"); |
| 165 | expect(updated.enabled).toEqual(false); |
| 166 | expect(updated.initialEnabled).toEqual(false); |
| 167 | expect(updated.initialName).toEqual(""); |
| 168 | |
| 169 | const live = yield* getCatchAll(zoneId); |
| 170 | expect(live.enabled).toEqual(false); |
| 171 | expect(live.name).toEqual("alchemy update test v2"); |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | const restored = yield* getCatchAll(zoneId); |
| 176 | expect(restored.enabled).toEqual(false); |
| 177 | expect(restored.name ?? "").toEqual(""); |
| 178 | expect(restored.actions).toEqual([{ type: "drop" }]); |
| 179 | }).pipe(logLevel), |
| 180 | { exclusive: EMAIL_ROUTING_ZONE }, |
| 181 | ); |
| 182 | |
| 183 | test.provider( |
| 184 | "recovers a half-created catch-all whose creating-state lost Output-valued props (#736)", |
| 185 | (stack) => |
| 186 | Effect.gen(function* () { |
| 187 | const zoneId = yield* resolveZoneId; |
| 188 | |
| 189 | yield* stack.destroy(); |
| 190 | yield* setBaseline(zoneId); |
| 191 | // Safety net: normalize the zone singleton back to the Cloudflare |
| 192 | // default even if the test dies mid-way. |
| 193 | yield* Effect.addFinalizer(() => setBaseline(zoneId).pipe(Effect.ignore)); |
| 194 | |
| 195 | const deployCatchAll = () => |
| 196 | stack.deploy( |
| 197 | Effect.gen(function* () { |
| 198 | const routing = yield* Cloudflare.Email.Routing("Routing", { zone: zoneName }); |
| 199 | return yield* Cloudflare.Email.CatchAll("CatchAll", { |
| 200 | // Output-valued zone reference — the #736 shape. |
| 201 | zone: { zoneId: routing.zoneId }, |
| 202 | name: testName("alchemy recovery test"), |
| 203 | actions: [{ type: "drop" }], |
| 204 | }); |
| 205 | }), |
| 206 | ); |
| 207 | |
| 208 | const created = yield* deployCatchAll(); |
| 209 | expect(created.zoneId).toEqual(zoneId); |
| 210 | expect(created.ruleId).not.toEqual(""); |
| 211 | |
| 212 | // Rewrite the persisted row into the wedged shape an interrupted |
| 213 | // deploy leaves behind: `creating`, no attributes, and the |
| 214 | // Output-valued `zone` prop lost in the round-trip. |
| 215 | const state = yield* yield* State; |
| 216 | const stage = stack.stage; |
| 217 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 218 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 219 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 220 | ); |
| 221 | const wedged = rows.find( |
| 222 | (r): r is { fqn: string; row: ResourceState } => |
| 223 | isResourceState(r.row) && r.row.resourceType === "Cloudflare.Email.CatchAll", |
| 224 | ); |
| 225 | if (!wedged) { |
| 226 | return yield* Effect.die( |
| 227 | new Error("no Cloudflare.Email.CatchAll state row found after deploy"), |
| 228 | ); |
| 229 | } |
| 230 | yield* state.set({ |
| 231 | stack: stack.name, |
| 232 | stage, |
| 233 | fqn: wedged.fqn, |
| 234 | value: { |
| 235 | ...wedged.row, |
| 236 | status: "creating", |
| 237 | attr: undefined, |
| 238 | props: { ...wedged.row.props, zone: undefined }, |
| 239 | }, |
| 240 | }); |
| 241 | |
| 242 | // Before the fix `read` crashed resolving `olds.zone === undefined` |
| 243 | // (TypeError reading `name` of undefined inside `resolve`). After |
| 244 | // the fix it reports "not found" and reconcile converges on the |
| 245 | // same zone singleton — same rule, no duplicate. |
| 246 | const recovered = yield* deployCatchAll(); |
| 247 | expect(recovered.zoneId).toEqual(zoneId); |
| 248 | expect(recovered.ruleId).toEqual(created.ruleId); |
| 249 | expect(recovered.name).toEqual(testName("alchemy recovery test")); |
| 250 | expect(recovered.enabled).toEqual(true); |
| 251 | expect(recovered.actions).toEqual([{ type: "drop" }]); |
| 252 | |
| 253 | // Out-of-band verification against the live API. |
| 254 | const live = yield* getCatchAll(zoneId); |
| 255 | expect(live.enabled).toEqual(true); |
| 256 | expect(live.name).toEqual(testName("alchemy recovery test")); |
| 257 | |
| 258 | yield* stack.destroy(); |
| 259 | // The recovery re-adopted the singleton with the managed state as |
| 260 | // its restore baseline, so destroy restores that state — normalize |
| 261 | // the zone back to the Cloudflare default for subsequent tests. |
| 262 | yield* setBaseline(zoneId); |
| 263 | }).pipe(logLevel), |
| 264 | { timeout: 240_000, exclusive: EMAIL_ROUTING_ZONE }, |
| 265 | ); |
| 266 | |
| 267 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 268 | // API for this per-zone catch-all rule, so `list()` enumerates every zone |
| 269 | // via `listAllZones` and reads the singleton in each (skipping zones without |
| 270 | // Email Routing enabled). Ensure Email Routing is enabled on the standing |
| 271 | // test zone, then assert the result is non-empty, well-typed, and contains |
| 272 | // the test zone. |
| 273 | test.provider( |
| 274 | "list enumerates the catch-all rule across all zones", |
| 275 | (stack) => |
| 276 | Effect.gen(function* () { |
| 277 | const zoneId = yield* resolveZoneId; |
| 278 | |
| 279 | yield* stack.destroy(); |
| 280 | // Email Routing must be enabled for the test zone's catch-all to be |
| 281 | // visible to `list()`; normalize to a known baseline. |
| 282 | yield* setBaseline(zoneId); |
| 283 | |
| 284 | const provider = yield* Provider.findProvider(Cloudflare.Email.CatchAll); |
| 285 | const all = yield* provider.list(); |
| 286 | |
| 287 | expect(all.length).toBeGreaterThan(0); |
| 288 | const row = all.find((r) => r.zoneId === zoneId); |
| 289 | expect(row).toBeDefined(); |
| 290 | expect(row!.ruleId).not.toEqual(""); |
| 291 | expect(typeof row!.enabled).toBe("boolean"); |
| 292 | expect(Array.isArray(row!.actions)).toBe(true); |
| 293 | |
| 294 | yield* stack.destroy(); |
| 295 | }).pipe(logLevel), |
| 296 | { exclusive: EMAIL_ROUTING_ZONE }, |
| 297 | ); |
| 298 | }, |
| 299 | ); |