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 * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { testName } from "../../names.ts"; |
| 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 — the standard testing |
| 18 | // account is not entitled (typed `EmailSecurityNotEntitled`), so the |
| 19 | // lifecycle test is gated behind an entitled account flagged via env. |
| 20 | const entitled = !!process.env.CLOUDFLARE_EMAIL_SECURITY; |
| 21 | |
| 22 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 23 | |
| 24 | const pattern = `${testName("alchemy-trusted")}.alchemy-test-2.us`; |
| 25 | |
| 26 | const findByPattern = (accountId: string) => |
| 27 | emailSecurity.listSettingTrustedDomains.items({ accountId, pattern }).pipe( |
| 28 | Stream.runCollect, |
| 29 | Effect.map((chunk) => Array.from(chunk).find((p) => p.pattern === pattern)), |
| 30 | Effect.retry({ |
| 31 | while: (e) => e._tag === "Forbidden", |
| 32 | schedule: forbiddenRetrySchedule, |
| 33 | times: 8, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | test.provider.skipIf(!entitled)( |
| 38 | "create, update in place, destroy", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | // Trust a lookalike partner domain. |
| 46 | const created = yield* stack.deploy( |
| 47 | Effect.gen(function* () { |
| 48 | return yield* Cloudflare.Email.TrustedDomain("Trusted", { |
| 49 | pattern, |
| 50 | isSimilarity: true, |
| 51 | comments: "v1", |
| 52 | }); |
| 53 | }), |
| 54 | ); |
| 55 | expect(created.trustedDomainId).toBeDefined(); |
| 56 | expect(created.accountId).toEqual(accountId); |
| 57 | expect(created.pattern).toEqual(pattern); |
| 58 | expect(created.isSimilarity).toEqual(true); |
| 59 | expect(created.isRecent).toEqual(false); |
| 60 | expect(created.isRegex).toEqual(false); |
| 61 | expect(created.comments).toEqual("v1"); |
| 62 | |
| 63 | // Out-of-band verification via the distilled API. |
| 64 | const live = yield* findByPattern(accountId); |
| 65 | expect(live?.id).toEqual(created.trustedDomainId); |
| 66 | |
| 67 | // Update mutable fields in place — same physical entry. |
| 68 | const updated = yield* stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | return yield* Cloudflare.Email.TrustedDomain("Trusted", { |
| 71 | pattern, |
| 72 | isSimilarity: true, |
| 73 | isRecent: true, |
| 74 | comments: "v2", |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | expect(updated.trustedDomainId).toEqual(created.trustedDomainId); |
| 79 | expect(updated.isRecent).toEqual(true); |
| 80 | expect(updated.comments).toEqual("v2"); |
| 81 | |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | // The entry is gone after destroy. |
| 85 | const gone = yield* findByPattern(accountId).pipe( |
| 86 | Effect.repeat({ |
| 87 | schedule: Schedule.spaced("2 seconds"), |
| 88 | until: (p) => p === undefined, |
| 89 | times: 10, |
| 90 | }), |
| 91 | ); |
| 92 | expect(gone).toBeUndefined(); |
| 93 | }).pipe(logLevel), |
| 94 | { |
| 95 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 96 | timeout: 120_000, |
| 97 | }, |
| 98 | ); |
| 99 | |
| 100 | // `list()` enumerates the account's trusted domains. On the standard testing |
| 101 | // account (not entitled) the typed `EmailSecurityNotEntitled` error is caught |
| 102 | // and the result is an empty array — so this assertion is meaningful without |
| 103 | // the add-on. On an entitled account it also asserts the deployed entry is |
| 104 | // present. |
| 105 | test.provider( |
| 106 | "list enumerates trusted domains", |
| 107 | (stack) => |
| 108 | Effect.gen(function* () { |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const provider = yield* Provider.findProvider(Cloudflare.Email.TrustedDomain); |
| 112 | |
| 113 | if (!entitled) { |
| 114 | // Unentitled account: typed entitlement error -> []. |
| 115 | const all = yield* provider.list(); |
| 116 | expect(all).toEqual([]); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | const deployed = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Cloudflare.Email.TrustedDomain("ListTrusted", { |
| 123 | pattern, |
| 124 | isSimilarity: true, |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | const all = yield* provider.list(); |
| 130 | expect(all.some((x) => x.trustedDomainId === deployed.trustedDomainId)).toBe(true); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | }).pipe(logLevel), |
| 134 | { |
| 135 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 136 | timeout: 120_000, |
| 137 | }, |
| 138 | ); |