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 acm from "@distilled.cloud/cloudflare/acm"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as pathe from "pathe"; |
| 13 | |
| 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 | // The custom origin trust store requires the Advanced Certificate Manager |
| 21 | // add-on (~$10/mo per zone). On the standard testing zone every call — |
| 22 | // including list/get — fails with the typed |
| 23 | // `AdvancedCertificateManagerRequired` (code 1450) error. The full CRUD |
| 24 | // lifecycle is gated behind an entitled zone id supplied via env. |
| 25 | const entitledZoneId = process.env.CLOUDFLARE_TEST_ACM_ZONE_ID; |
| 26 | |
| 27 | // Checked-in, deterministic self-signed root CA fixtures (no key material). |
| 28 | const ROOT_CA_PEM = pathe.resolve(import.meta.dirname, "fixtures/root-ca.pem"); |
| 29 | const ROOT_CA_2_PEM = pathe.resolve(import.meta.dirname, "fixtures/root-ca-2.pem"); |
| 30 | |
| 31 | const resolveZoneId = Effect.gen(function* () { |
| 32 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 33 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 34 | if (!zone) { |
| 35 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 36 | } |
| 37 | return zone.id; |
| 38 | }); |
| 39 | |
| 40 | // Freshly-minted scoped API tokens propagate eventually-consistently across |
| 41 | // Cloudflare's edge — ride out intermittent 403s via the typed `Forbidden` |
| 42 | // tag in each acm operation's error union. |
| 43 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 44 | |
| 45 | /** Out-of-band read; "gone" (typed) maps to undefined. */ |
| 46 | const getTrustStore = (zoneId: string, id: string) => |
| 47 | acm.getCustomTrustStore({ zoneId, customOriginTrustStoreId: id }).pipe( |
| 48 | Effect.map((cert): acm.GetCustomTrustStoreResponse | undefined => cert), |
| 49 | Effect.catchTag(["CustomTrustStoreNotFound", "InvalidObjectIdentifier"], () => |
| 50 | Effect.succeed(undefined), |
| 51 | ), |
| 52 | Effect.retry({ |
| 53 | while: (e) => e._tag === "Forbidden", |
| 54 | schedule: forbiddenRetrySchedule, |
| 55 | times: 8, |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | test.provider( |
| 60 | "surfaces the typed AdvancedCertificateManagerRequired error on unentitled zones", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | const zoneId = yield* resolveZoneId; |
| 64 | const fs = yield* FileSystem.FileSystem; |
| 65 | const certificate = yield* fs.readFileString(ROOT_CA_PEM); |
| 66 | |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | // The standard testing zone lacks the ACM add-on — uploading a trust |
| 70 | // store certificate must fail with the typed entitlement tag (1450). |
| 71 | const error = yield* acm.createCustomTrustStore({ zoneId, certificate }).pipe( |
| 72 | Effect.retry({ |
| 73 | while: (e) => e._tag === "Forbidden", |
| 74 | schedule: forbiddenRetrySchedule, |
| 75 | times: 8, |
| 76 | }), |
| 77 | Effect.flip, |
| 78 | ); |
| 79 | expect(error._tag).toEqual("AdvancedCertificateManagerRequired"); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | }).pipe(logLevel), |
| 83 | { tags: ["provider:cloudflare", "provider:cloudflare:acm", "provider:cloudflare:zone", "live"] }, |
| 84 | ); |
| 85 | |
| 86 | // Canonical `list()` test (zone-scoped collection): `list()` enumerates |
| 87 | // every zone via `listAllZones` and lists the trust store certificates in |
| 88 | // each, paginating exhaustively. Zones lacking the ACM add-on reject the |
| 89 | // route with the typed `AdvancedCertificateManagerRequired` tag, which |
| 90 | // `list()` skips. On a standard testing account (no entitled zones) the |
| 91 | // result is empty — assert the enumeration itself succeeds (an array) and |
| 92 | // surfaces no untyped errors. |
| 93 | test.provider( |
| 94 | "list enumerates trust store certificates across zones", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | yield* stack.destroy(); |
| 98 | |
| 99 | const provider = yield* Provider.findProvider(Cloudflare.Acm.CustomTrustStore); |
| 100 | const all = yield* provider.list(); |
| 101 | |
| 102 | expect(Array.isArray(all)).toBe(true); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | }).pipe(logLevel), |
| 106 | { tags: ["provider:cloudflare", "provider:cloudflare:acm", "live"] }, |
| 107 | ); |
| 108 | |
| 109 | // On an entitled zone, `list()` must contain the deployed certificate. |
| 110 | test.provider.skipIf(!entitledZoneId)( |
| 111 | "list includes the deployed trust store certificate on an entitled zone", |
| 112 | (stack) => |
| 113 | Effect.gen(function* () { |
| 114 | const zoneId = entitledZoneId!; |
| 115 | const fs = yield* FileSystem.FileSystem; |
| 116 | const certificate = yield* fs.readFileString(ROOT_CA_PEM); |
| 117 | |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const created = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Cloudflare.Acm.CustomTrustStore("RootCa", { zoneId, certificate }); |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | const provider = yield* Provider.findProvider(Cloudflare.Acm.CustomTrustStore); |
| 127 | const all = yield* provider.list(); |
| 128 | |
| 129 | expect(all.some((c) => c.id === created.id && c.zoneId === zoneId)).toBe(true); |
| 130 | |
| 131 | yield* stack.destroy(); |
| 132 | }).pipe(logLevel), |
| 133 | { tags: ["provider:cloudflare", "provider:cloudflare:acm", "live"], timeout: 120_000 }, |
| 134 | ); |
| 135 | |
| 136 | test.provider.skipIf(!entitledZoneId)( |
| 137 | "uploads a root CA, replaces it on certificate change, and deletes it", |
| 138 | (stack) => |
| 139 | Effect.gen(function* () { |
| 140 | const zoneId = entitledZoneId!; |
| 141 | const fs = yield* FileSystem.FileSystem; |
| 142 | const certificate = yield* fs.readFileString(ROOT_CA_PEM); |
| 143 | const certificate2 = yield* fs.readFileString(ROOT_CA_2_PEM); |
| 144 | |
| 145 | yield* stack.destroy(); |
| 146 | |
| 147 | // Create. |
| 148 | const created = yield* stack.deploy( |
| 149 | Effect.gen(function* () { |
| 150 | return yield* Cloudflare.Acm.CustomTrustStore("RootCa", { zoneId, certificate }); |
| 151 | }), |
| 152 | ); |
| 153 | expect(created.zoneId).toEqual(zoneId); |
| 154 | expect(created.id).toBeTruthy(); |
| 155 | expect(created.issuer).toContain("Alchemy ACM Test Root CA 1"); |
| 156 | |
| 157 | // Out-of-band verification. |
| 158 | const live = yield* getTrustStore(zoneId, created.id); |
| 159 | expect(live).toBeDefined(); |
| 160 | |
| 161 | // Changing the certificate replaces the resource — there is no |
| 162 | // update API, so a new certificate id must be issued. |
| 163 | const replaced = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | return yield* Cloudflare.Acm.CustomTrustStore("RootCa", { |
| 166 | zoneId, |
| 167 | certificate: certificate2, |
| 168 | }); |
| 169 | }), |
| 170 | ); |
| 171 | expect(replaced.id).not.toEqual(created.id); |
| 172 | expect(replaced.issuer).toContain("Alchemy ACM Test Root CA 2"); |
| 173 | |
| 174 | // Destroy, then wait until the certificate is actually gone |
| 175 | // (deletion is async: pending_deletion -> deleted). |
| 176 | yield* stack.destroy(); |
| 177 | const gone = yield* getTrustStore(zoneId, replaced.id).pipe( |
| 178 | Effect.repeat({ |
| 179 | schedule: Schedule.spaced("2 seconds"), |
| 180 | until: (cert) => |
| 181 | cert === undefined || cert.status === "pending_deletion" || cert.status === "deleted", |
| 182 | times: 30, |
| 183 | }), |
| 184 | ); |
| 185 | expect( |
| 186 | gone === undefined || gone.status === "pending_deletion" || gone.status === "deleted", |
| 187 | ).toBe(true); |
| 188 | }).pipe(logLevel), |
| 189 | { tags: ["provider:cloudflare", "provider:cloudflare:acm", "live"], timeout: 120_000 }, |
| 190 | ); |