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 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | // Email Security (Area 1) is an enterprise add-on — the standard testing |
| 17 | // account is not entitled (typed `EmailSecurityNotEntitled`), so the |
| 18 | // lifecycle test is gated behind an entitled account flagged via env. |
| 19 | const entitled = !!process.env.CLOUDFLARE_EMAIL_SECURITY; |
| 20 | |
| 21 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 22 | |
| 23 | const pattern = "alchemy-block-sender@alchemy-test-2.us"; |
| 24 | |
| 25 | const findByPattern = (accountId: string) => |
| 26 | emailSecurity.listSettingBlockSenders.items({ accountId, pattern }).pipe( |
| 27 | Stream.runCollect, |
| 28 | Effect.map((chunk) => Array.from(chunk).find((p) => p.pattern === pattern)), |
| 29 | Effect.retry({ |
| 30 | while: (e) => e._tag === "Forbidden", |
| 31 | schedule: forbiddenRetrySchedule, |
| 32 | times: 8, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test.provider.skipIf(!entitled)( |
| 37 | "create, update in place, destroy", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | // Block a sender address. |
| 45 | const created = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | return yield* Cloudflare.Email.BlockSender("Blocked", { |
| 48 | pattern, |
| 49 | patternType: "EMAIL", |
| 50 | comments: "v1", |
| 51 | }); |
| 52 | }), |
| 53 | ); |
| 54 | expect(created.blockSenderId).toBeDefined(); |
| 55 | expect(created.accountId).toEqual(accountId); |
| 56 | expect(created.pattern).toEqual(pattern); |
| 57 | expect(created.patternType).toEqual("EMAIL"); |
| 58 | expect(created.isRegex).toEqual(false); |
| 59 | expect(created.comments).toEqual("v1"); |
| 60 | |
| 61 | // Out-of-band verification via the distilled API. |
| 62 | const live = yield* findByPattern(accountId); |
| 63 | expect(live?.id).toEqual(created.blockSenderId); |
| 64 | |
| 65 | // Update mutable fields in place — same physical entry. |
| 66 | const updated = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* Cloudflare.Email.BlockSender("Blocked", { |
| 69 | pattern, |
| 70 | patternType: "EMAIL", |
| 71 | comments: "v2", |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | expect(updated.blockSenderId).toEqual(created.blockSenderId); |
| 76 | expect(updated.comments).toEqual("v2"); |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | // The entry is gone after destroy. |
| 81 | const gone = yield* findByPattern(accountId).pipe( |
| 82 | Effect.repeat({ |
| 83 | schedule: Schedule.spaced("2 seconds"), |
| 84 | until: (p) => p === undefined, |
| 85 | times: 10, |
| 86 | }), |
| 87 | ); |
| 88 | expect(gone).toBeUndefined(); |
| 89 | }).pipe(logLevel), |
| 90 | { |
| 91 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 92 | timeout: 120_000, |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | // list() enumerates the account's blocked senders. On the entitled account it |
| 97 | // asserts the freshly-deployed entry is present; on the standard (un-entitled) |
| 98 | // account the typed `EmailSecurityNotEntitled` is caught and list() yields the |
| 99 | // well-typed empty array. |
| 100 | test.provider( |
| 101 | "list enumerates the deployed block sender", |
| 102 | (stack) => |
| 103 | Effect.gen(function* () { |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const provider = yield* Provider.findProvider(Cloudflare.Email.BlockSender); |
| 107 | |
| 108 | if (entitled) { |
| 109 | const deployed = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* Cloudflare.Email.BlockSender("ListBlocked", { |
| 112 | pattern, |
| 113 | patternType: "EMAIL", |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | const all = yield* provider.list(); |
| 119 | expect(all.some((x) => x.blockSenderId === deployed.blockSenderId)).toBe(true); |
| 120 | } else { |
| 121 | const all = yield* provider.list(); |
| 122 | expect(all).toEqual([]); |
| 123 | } |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | }).pipe(logLevel), |
| 127 | { |
| 128 | tags: ["provider:cloudflare", "provider:cloudflare:email", "live"], |
| 129 | timeout: 120_000, |
| 130 | }, |
| 131 | ); |