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 ssl from "@distilled.cloud/cloudflare/ssl"; |
| 7 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Result from "effect/Result"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import * as Stream from "effect/Stream"; |
| 15 | import { testName } from "../../names.ts"; |
| 16 | |
| 17 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 22 | |
| 23 | // Ordering an advanced certificate pack requires the Advanced Certificate |
| 24 | // Manager subscription ($10/mo add-on per zone). The standard testing zone |
| 25 | // does not carry it, so the full lifecycle test below is gated behind a |
| 26 | // zone name supplied via env whose zone has ACM purchased. |
| 27 | const acmZoneName = process.env.CLOUDFLARE_TEST_ACM_ZONE_NAME; |
| 28 | |
| 29 | const resolveZoneId = (name: string) => |
| 30 | Effect.gen(function* () { |
| 31 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 32 | const zone = yield* findZoneByName({ accountId, name }); |
| 33 | if (!zone) { |
| 34 | return yield* Effect.die(new Error(`zone "${name}" not found`)); |
| 35 | } |
| 36 | return zone.id; |
| 37 | }); |
| 38 | |
| 39 | // A freshly minted scoped API token propagates eventually-consistently |
| 40 | // across Cloudflare's edge — retry the typed `Forbidden` blips on the |
| 41 | // test's own out-of-band calls. |
| 42 | const transientRetrySchedule = Schedule.exponential("500 millis"); |
| 43 | |
| 44 | const getPack = (zoneId: string, certificatePackId: string) => |
| 45 | ssl.getCertificatePack({ zoneId, certificatePackId }).pipe( |
| 46 | Effect.map((pack): ssl.GetCertificatePackResponse | undefined => pack), |
| 47 | Effect.catchTag("CertificatePackNotFound", () => Effect.succeed(undefined)), |
| 48 | Effect.retry({ |
| 49 | while: (e) => e._tag === "Forbidden" || e._tag === "TooManyRequests", |
| 50 | schedule: transientRetrySchedule, |
| 51 | times: 8, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "surfaces the typed AdvancedCertificateManagerRequired error on unentitled zones", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const zoneId = yield* resolveZoneId(zoneName); |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | // The standard testing zone lacks the ACM subscription — ordering |
| 64 | // must fail with the typed entitlement tag (Cloudflare code 1450). |
| 65 | const result = yield* ssl |
| 66 | .createCertificatePack({ |
| 67 | zoneId, |
| 68 | type: "advanced", |
| 69 | certificateAuthority: "google", |
| 70 | hosts: [zoneName, `${testName("acm-probe")}.${zoneName}`], |
| 71 | validationMethod: "txt", |
| 72 | validityDays: 90, |
| 73 | }) |
| 74 | .pipe( |
| 75 | Effect.retry({ |
| 76 | while: (e) => e._tag === "Forbidden" || e._tag === "TooManyRequests", |
| 77 | schedule: transientRetrySchedule, |
| 78 | times: 8, |
| 79 | }), |
| 80 | Effect.result, |
| 81 | ); |
| 82 | |
| 83 | if (Result.isSuccess(result)) { |
| 84 | // Safety net: should the account ever become entitled, clean up |
| 85 | // the accidental order so the probe stays side-effect free. |
| 86 | yield* ssl |
| 87 | .deleteCertificatePack({ |
| 88 | zoneId, |
| 89 | certificatePackId: result.success.id, |
| 90 | }) |
| 91 | .pipe(Effect.catchTag("CertificatePackNotFound", () => Effect.void)); |
| 92 | } |
| 93 | expect(Result.isFailure(result)).toBe(true); |
| 94 | if (Result.isFailure(result)) { |
| 95 | expect(result.failure._tag).toEqual("AdvancedCertificateManagerRequired"); |
| 96 | } |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | }).pipe(logLevel), |
| 100 | { |
| 101 | tags: ["provider:cloudflare", "provider:cloudflare:ssl", "provider:cloudflare:zone", "live"], |
| 102 | }, |
| 103 | ); |
| 104 | |
| 105 | test.provider( |
| 106 | "list enumerates advanced certificate packs across zones", |
| 107 | (stack) => |
| 108 | Effect.gen(function* () { |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | // list() fans out over every zone in the account and returns the |
| 112 | // exact `read` Attributes shape for each advanced pack. The standard |
| 113 | // testing zone has no ACM subscription (so no advanced packs), but |
| 114 | // list() must still return a well-typed, exhaustively-paginated array. |
| 115 | const provider = yield* Provider.findProvider(Cloudflare.Ssl.CertificatePack); |
| 116 | const all = yield* provider.list(); |
| 117 | |
| 118 | expect(Array.isArray(all)).toBe(true); |
| 119 | for (const pack of all) { |
| 120 | expect(typeof pack.certificatePackId).toBe("string"); |
| 121 | expect(typeof pack.zoneId).toBe("string"); |
| 122 | expect(Array.isArray(pack.hosts)).toBe(true); |
| 123 | } |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | }).pipe(logLevel), |
| 127 | { |
| 128 | tags: ["provider:cloudflare", "provider:cloudflare:ssl", "live"], |
| 129 | timeout: 120_000, |
| 130 | }, |
| 131 | ); |
| 132 | |
| 133 | test.provider( |
| 134 | "list excludes Worker custom-domain packs and their deleted tombstones", |
| 135 | (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 138 | const zoneId = yield* resolveZoneId(zoneName); |
| 139 | // Deterministic per-stage hostname on the standing test zone. |
| 140 | const hostname = `certpack-${stack.stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}.${zoneName}`; |
| 141 | const provider = yield* Provider.findProvider(Cloudflare.Ssl.CertificatePack); |
| 142 | |
| 143 | yield* stack.destroy(); |
| 144 | |
| 145 | // A Worker custom domain makes Cloudflare auto-order an `advanced` |
| 146 | // pack it manages itself (the domain's `certId`). |
| 147 | yield* stack.deploy( |
| 148 | Cloudflare.Worker("CertPackDomainWorker", { |
| 149 | script: `export default { fetch() { return new Response("ok"); } };`, |
| 150 | workersDev: false, |
| 151 | domain: hostname, |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | const domain = yield* workers.listDomains |
| 156 | .items({ accountId, hostname }) |
| 157 | .pipe(Stream.runHead, Effect.map(Option.getOrUndefined)); |
| 158 | expect(domain?.certId).toBeTruthy(); |
| 159 | const certId = domain!.certId; |
| 160 | |
| 161 | const managed = yield* getPack(zoneId, certId); |
| 162 | expect(managed?.type).toEqual("advanced"); |
| 163 | expect(managed?.hosts).toContain(hostname); |
| 164 | |
| 165 | // ...but it is not ours: list() must not surface it (nuke would |
| 166 | // otherwise strip TLS from a live custom domain). |
| 167 | const whileAttached = yield* provider.list(); |
| 168 | expect(whileAttached.some((p) => p.certificatePackId === certId)).toBe(false); |
| 169 | |
| 170 | // Detaching the domain leaves a `status: "deleted"` tombstone for a |
| 171 | // minute or two. It is still returned by get / list?status=all, and |
| 172 | // deleting it fails with the typed CertificatePackNotDeletable |
| 173 | // ("Bad response certificate service") — it must not be listed. |
| 174 | yield* stack.destroy(); |
| 175 | |
| 176 | const after = yield* getPack(zoneId, certId).pipe( |
| 177 | Effect.repeat({ |
| 178 | schedule: Schedule.spaced("2 seconds"), |
| 179 | until: (p) => p === undefined || p.status === "deleted", |
| 180 | times: 15, |
| 181 | }), |
| 182 | ); |
| 183 | if (after?.status === "deleted") { |
| 184 | const tombstoneListed = yield* provider.list(); |
| 185 | expect(tombstoneListed.some((p) => p.certificatePackId === certId)).toBe(false); |
| 186 | |
| 187 | const error = yield* ssl |
| 188 | .deleteCertificatePack({ zoneId, certificatePackId: certId }) |
| 189 | .pipe(Effect.flip); |
| 190 | // A purge racing this call answers 404 instead. |
| 191 | expect(["CertificatePackNotDeletable", "CertificatePackNotFound"]).toContain(error._tag); |
| 192 | } else { |
| 193 | expect(after).toBeUndefined(); |
| 194 | } |
| 195 | |
| 196 | yield* stack.destroy(); |
| 197 | }).pipe(logLevel), |
| 198 | { |
| 199 | tags: [ |
| 200 | "provider:cloudflare", |
| 201 | "provider:cloudflare:ssl", |
| 202 | "provider:cloudflare:worker", |
| 203 | "provider:cloudflare:zone", |
| 204 | "live", |
| 205 | ], |
| 206 | timeout: 120_000, |
| 207 | }, |
| 208 | ); |
| 209 | |
| 210 | test.provider.skipIf(!acmZoneName)( |
| 211 | "list includes a deployed advanced certificate pack", |
| 212 | (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | const name = acmZoneName!; |
| 215 | const zoneId = yield* resolveZoneId(name); |
| 216 | const hosts = [name, `acmlist.${name}`]; |
| 217 | |
| 218 | yield* stack.destroy(); |
| 219 | |
| 220 | const pack = yield* stack.deploy( |
| 221 | Effect.gen(function* () { |
| 222 | return yield* Cloudflare.Ssl.CertificatePack("ListPack", { |
| 223 | zoneId, |
| 224 | certificateAuthority: "google", |
| 225 | hosts, |
| 226 | validationMethod: "txt", |
| 227 | validityDays: 90, |
| 228 | }); |
| 229 | }), |
| 230 | ); |
| 231 | |
| 232 | const provider = yield* Provider.findProvider(Cloudflare.Ssl.CertificatePack); |
| 233 | const all = yield* provider.list(); |
| 234 | |
| 235 | expect(all.some((p) => p.certificatePackId === pack.certificatePackId)).toBe(true); |
| 236 | |
| 237 | yield* stack.destroy(); |
| 238 | }).pipe(logLevel), |
| 239 | { |
| 240 | tags: ["provider:cloudflare", "provider:cloudflare:ssl", "provider:cloudflare:zone", "live"], |
| 241 | timeout: 120_000, |
| 242 | }, |
| 243 | ); |
| 244 | |
| 245 | test.provider.skipIf(!acmZoneName)( |
| 246 | "orders a pack, updates validation method in place, and deletes it", |
| 247 | (stack) => |
| 248 | Effect.gen(function* () { |
| 249 | const name = acmZoneName!; |
| 250 | const zoneId = yield* resolveZoneId(name); |
| 251 | const hosts = [name, `acmtest.${name}`]; |
| 252 | |
| 253 | yield* stack.destroy(); |
| 254 | |
| 255 | // Create — order the advanced pack. Issuance is async: the |
| 256 | // resource returns as soon as the order is placed. |
| 257 | const pack = yield* stack.deploy( |
| 258 | Effect.gen(function* () { |
| 259 | return yield* Cloudflare.Ssl.CertificatePack("Pack", { |
| 260 | zoneId, |
| 261 | certificateAuthority: "google", |
| 262 | hosts, |
| 263 | validationMethod: "txt", |
| 264 | validityDays: 90, |
| 265 | }); |
| 266 | }), |
| 267 | ); |
| 268 | |
| 269 | expect(pack.zoneId).toEqual(zoneId); |
| 270 | expect(pack.certificatePackId).toBeTruthy(); |
| 271 | expect([...pack.hosts].sort()).toEqual([...hosts].sort()); |
| 272 | expect(pack.certificateAuthority).toEqual("google"); |
| 273 | |
| 274 | // Out-of-band verify via the distilled API. |
| 275 | const live = yield* getPack(zoneId, pack.certificatePackId); |
| 276 | expect(live?.id).toEqual(pack.certificatePackId); |
| 277 | expect(live?.type).toEqual("advanced"); |
| 278 | |
| 279 | // Update in place — validationMethod is mutable via the SSL |
| 280 | // verification API; the pack id must not change. |
| 281 | const updated = yield* stack.deploy( |
| 282 | Effect.gen(function* () { |
| 283 | return yield* Cloudflare.Ssl.CertificatePack("Pack", { |
| 284 | zoneId, |
| 285 | certificateAuthority: "google", |
| 286 | hosts, |
| 287 | validationMethod: "http", |
| 288 | validityDays: 90, |
| 289 | }); |
| 290 | }), |
| 291 | ); |
| 292 | expect(updated.certificatePackId).toEqual(pack.certificatePackId); |
| 293 | |
| 294 | yield* stack.destroy(); |
| 295 | |
| 296 | // Wait until the pack is actually gone (deletion is async — the |
| 297 | // pack may linger briefly in `pending_deletion`). |
| 298 | const gone = yield* getPack(zoneId, pack.certificatePackId).pipe( |
| 299 | Effect.repeat({ |
| 300 | schedule: Schedule.spaced("3 seconds"), |
| 301 | until: (p) => p === undefined || p.status === "deleted", |
| 302 | times: 20, |
| 303 | }), |
| 304 | ); |
| 305 | expect(gone === undefined || gone.status === "deleted").toBe(true); |
| 306 | }).pipe(logLevel), |
| 307 | { |
| 308 | tags: ["provider:cloudflare", "provider:cloudflare:ssl", "provider:cloudflare:zone", "live"], |
| 309 | timeout: 120_000, |
| 310 | }, |
| 311 | ); |