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 emailSecurity from "@distilled.cloud/cloudflare/email-security"; |
| 6 | import { adopt } from "alchemy/AdoptPolicy"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 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 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // Email Security (Area 1) is an enterprise add-on AND domains can only be |
| 18 | // onboarded in the dashboard. The lifecycle test needs an entitled account |
| 19 | // plus an explicitly sacrificial onboarded domain named via env — |
| 20 | // **destroying the resource offboards the domain from Email Security**, so |
| 21 | // never point this at a production domain. |
| 22 | const sacrificialDomain = process.env.CLOUDFLARE_EMAIL_SECURITY_DOMAIN; |
| 23 | |
| 24 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 25 | |
| 26 | const findDomain = (accountId: string, domain: string) => |
| 27 | emailSecurity.listSettingDomains.items({ accountId, domain: [domain] }).pipe( |
| 28 | Stream.runCollect, |
| 29 | Effect.map((chunk) => Array.from(chunk).find((d) => d.domain === domain)), |
| 30 | Effect.retry({ |
| 31 | while: (e) => e._tag === "Forbidden", |
| 32 | schedule: forbiddenRetrySchedule, |
| 33 | times: 8, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | test.provider.skipIf(!sacrificialDomain)( |
| 38 | "adopts an onboarded domain, patches settings in place, offboards on destroy", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 42 | const domainName = sacrificialDomain!; |
| 43 | |
| 44 | const baseline = yield* findDomain(accountId, domainName); |
| 45 | expect(baseline).toBeDefined(); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | // Adopt the onboarded domain — `read` reports it Unowned, so taking |
| 50 | // it under management requires `adopt(true)`. |
| 51 | const created = yield* stack.deploy( |
| 52 | Effect.gen(function* () { |
| 53 | return yield* Cloudflare.Email.Domain("MailDomain", { |
| 54 | domain: domainName, |
| 55 | lookbackHops: 5, |
| 56 | }).pipe(adopt(true)); |
| 57 | }), |
| 58 | ); |
| 59 | expect(created.domainId).toEqual(baseline?.id); |
| 60 | expect(created.domain).toEqual(domainName); |
| 61 | expect(created.lookbackHops).toEqual(5); |
| 62 | |
| 63 | // Update settings in place — same physical domain. |
| 64 | const updated = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* Cloudflare.Email.Domain("MailDomain", { |
| 67 | domain: domainName, |
| 68 | lookbackHops: 10, |
| 69 | dropDispositions: ["MALICIOUS"], |
| 70 | }).pipe(adopt(true)); |
| 71 | }), |
| 72 | ); |
| 73 | expect(updated.domainId).toEqual(created.domainId); |
| 74 | expect(updated.lookbackHops).toEqual(10); |
| 75 | expect(updated.dropDispositions).toEqual(["MALICIOUS"]); |
| 76 | |
| 77 | // Out-of-band verification via the distilled API. |
| 78 | const live = yield* findDomain(accountId, domainName); |
| 79 | expect(live?.lookbackHops).toEqual(10); |
| 80 | |
| 81 | // DESTRUCTIVE: destroy offboards the domain from Email Security. |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | const gone = yield* findDomain(accountId, domainName).pipe( |
| 85 | Effect.repeat({ |
| 86 | schedule: Schedule.spaced("2 seconds"), |
| 87 | until: (d) => d === undefined, |
| 88 | times: 10, |
| 89 | }), |
| 90 | ); |
| 91 | expect(gone).toBeUndefined(); |
| 92 | }).pipe(logLevel), |
| 93 | { |
| 94 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 95 | timeout: 120_000, |
| 96 | }, |
| 97 | ); |
| 98 | |
| 99 | // Read-only list assertion — safe to run on any account. Email Security is |
| 100 | // a paid add-on; on accounts without the entitlement the account-scoped |
| 101 | // list op rejects with EmailSecurityNotEntitled / Forbidden, which the |
| 102 | // provider's `list()` maps to a well-typed []. On entitled accounts every |
| 103 | // onboarded domain is returned in the exact `read` Attributes shape. |
| 104 | test.provider( |
| 105 | "list enumerates Email Security domains (or [] when unentitled)", |
| 106 | (stack) => |
| 107 | Effect.gen(function* () { |
| 108 | yield* stack.destroy(); |
| 109 | |
| 110 | const provider = yield* Provider.findProvider(Cloudflare.Email.Domain); |
| 111 | const all = yield* provider.list(); |
| 112 | expect(Array.isArray(all)).toBe(true); |
| 113 | |
| 114 | // When an entitled sacrificial domain is configured, it must appear in |
| 115 | // the exhaustively-paginated result in the read Attributes shape. |
| 116 | if (sacrificialDomain) { |
| 117 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 118 | const baseline = yield* findDomain(accountId, sacrificialDomain); |
| 119 | if (baseline) { |
| 120 | expect(all.some((d) => d.domainId === baseline.id)).toBe(true); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | }).pipe(logLevel), |
| 126 | { |
| 127 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 128 | timeout: 120_000, |
| 129 | }, |
| 130 | ); |