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 customCertificates from "@distilled.cloud/cloudflare/custom-certificates"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 20 | |
| 21 | // Custom (BYO) edge certificates are a Business/Enterprise feature. On the |
| 22 | // testing account's zone every custom_certificates call fails with |
| 23 | // "Plan level does not allow custom certificates ..." (Cloudflare error |
| 24 | // code 1011), surfaced as the typed `PlanLevelNotAllowed` error. The full |
| 25 | // lifecycle test below is gated behind an entitled zone id supplied via env. |
| 26 | const entitledZoneId = process.env.CLOUDFLARE_TEST_CUSTOM_CERT_ZONE_ID; |
| 27 | |
| 28 | const resolveZoneId = Effect.gen(function* () { |
| 29 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 30 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 31 | if (!zone) { |
| 32 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 33 | } |
| 34 | return zone.id; |
| 35 | }); |
| 36 | |
| 37 | // Checked-in self-signed PEM fixtures (CN=alchemy-test-2.us, 10y validity). |
| 38 | // The upload API accepts self-signed certificates — validation only requires |
| 39 | // that the key matches the certificate — keeping the test hermetic. |
| 40 | const readFixture = (name: string) => |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const path = yield* Path.Path; |
| 44 | return yield* fs.readFileString(path.join(import.meta.dirname, "fixtures", name)); |
| 45 | }); |
| 46 | |
| 47 | // A freshly minted scoped token propagates eventually-consistently across |
| 48 | // Cloudflare's edge; ride out 403 blips on out-of-band verification calls. |
| 49 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 50 | |
| 51 | const getCertificate = (zoneId: string, customCertificateId: string) => |
| 52 | customCertificates.getCustomCertificate({ zoneId, customCertificateId }).pipe( |
| 53 | Effect.retry({ |
| 54 | while: (e) => e._tag === "Forbidden", |
| 55 | schedule: forbiddenRetrySchedule, |
| 56 | times: 8, |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | test.provider( |
| 61 | "surfaces the typed PlanLevelNotAllowed error on unentitled zones", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | const zoneId = yield* resolveZoneId; |
| 65 | const certificate = yield* readFixture("cert1.pem"); |
| 66 | const privateKey = yield* readFixture("key1.pem"); |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | // The standard testing zone is not on a Business/Enterprise plan — |
| 71 | // the distilled call must fail with the typed entitlement tag. |
| 72 | const error = yield* customCertificates |
| 73 | .createCustomCertificate({ zoneId, certificate, privateKey, type: "sni_custom" }) |
| 74 | .pipe( |
| 75 | Effect.retry({ |
| 76 | while: (e) => e._tag === "Forbidden", |
| 77 | schedule: forbiddenRetrySchedule, |
| 78 | times: 8, |
| 79 | }), |
| 80 | Effect.flip, |
| 81 | ); |
| 82 | expect(error._tag).toEqual("PlanLevelNotAllowed"); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: [ |
| 88 | "provider:cloudflare", |
| 89 | "provider:cloudflare:customcertificate", |
| 90 | "provider:cloudflare:zone", |
| 91 | "live", |
| 92 | ], |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | // `list()` fans out over every zone in the account, paginates each zone's |
| 97 | // custom certificates, and skips plan-gated zones via the typed |
| 98 | // `PlanLevelNotAllowed`/`Forbidden` tags. On the testing account (no |
| 99 | // Business/Enterprise zones) every zone is skipped, so the call must still |
| 100 | // succeed and return a well-typed array (empty here) rather than throwing. |
| 101 | test.provider( |
| 102 | "list enumerates custom certificates across zones", |
| 103 | (stack) => |
| 104 | Effect.gen(function* () { |
| 105 | yield* stack.destroy(); |
| 106 | |
| 107 | const provider = yield* Provider.findProvider(Cloudflare.CustomCertificate.CustomCertificate); |
| 108 | const all = yield* provider.list(); |
| 109 | |
| 110 | expect(Array.isArray(all)).toBe(true); |
| 111 | // Every item carries the full read Attributes shape. |
| 112 | for (const cert of all) { |
| 113 | expect(typeof cert.certificateId).toBe("string"); |
| 114 | expect(typeof cert.zoneId).toBe("string"); |
| 115 | } |
| 116 | |
| 117 | // On an entitled zone, the certificate we deploy must appear in the |
| 118 | // exhaustively-paginated result. |
| 119 | if (entitledZoneId) { |
| 120 | const cert1 = yield* readFixture("cert1.pem"); |
| 121 | const key1 = yield* readFixture("key1.pem"); |
| 122 | const deployed = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* Cloudflare.CustomCertificate.CustomCertificate("ListEdgeCert", { |
| 125 | zoneId: entitledZoneId, |
| 126 | certificate: cert1, |
| 127 | privateKey: Redacted.make(key1), |
| 128 | type: "sni_custom", |
| 129 | bundleMethod: "force", |
| 130 | }); |
| 131 | }), |
| 132 | ); |
| 133 | const after = yield* provider.list(); |
| 134 | expect(after.some((c) => c.certificateId === deployed.certificateId)).toBe(true); |
| 135 | } |
| 136 | |
| 137 | yield* stack.destroy(); |
| 138 | }).pipe(logLevel), |
| 139 | { |
| 140 | tags: ["provider:cloudflare", "provider:cloudflare:customcertificate", "live"], |
| 141 | timeout: 120_000, |
| 142 | }, |
| 143 | ); |
| 144 | |
| 145 | test.provider.skipIf(!entitledZoneId)( |
| 146 | "uploads, rotates in place, replaces on type change, and deletes", |
| 147 | (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | const zoneId = entitledZoneId!; |
| 150 | const cert1 = yield* readFixture("cert1.pem"); |
| 151 | const key1 = yield* readFixture("key1.pem"); |
| 152 | const cert2 = yield* readFixture("cert2.pem"); |
| 153 | const key2 = yield* readFixture("key2.pem"); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | // Create — upload the first certificate/key pair. |
| 158 | const created = yield* stack.deploy( |
| 159 | Effect.gen(function* () { |
| 160 | return yield* Cloudflare.CustomCertificate.CustomCertificate("EdgeCert", { |
| 161 | zoneId, |
| 162 | certificate: cert1, |
| 163 | privateKey: Redacted.make(key1), |
| 164 | type: "sni_custom", |
| 165 | bundleMethod: "force", |
| 166 | }); |
| 167 | }), |
| 168 | ); |
| 169 | |
| 170 | expect(created.certificateId).toBeDefined(); |
| 171 | expect(created.zoneId).toEqual(zoneId); |
| 172 | expect(created.type).toEqual("sni_custom"); |
| 173 | expect(created.hosts).toContain("alchemy-test-2.us"); |
| 174 | expect(created.contentHash).toHaveLength(64); |
| 175 | |
| 176 | // Out-of-band verification via the distilled API. |
| 177 | const live = yield* getCertificate(zoneId, created.certificateId); |
| 178 | expect(live.id).toEqual(created.certificateId); |
| 179 | |
| 180 | // Rotate in place — a new cert/key pair PATCHes the same id. |
| 181 | const rotated = yield* stack.deploy( |
| 182 | Effect.gen(function* () { |
| 183 | return yield* Cloudflare.CustomCertificate.CustomCertificate("EdgeCert", { |
| 184 | zoneId, |
| 185 | certificate: cert2, |
| 186 | privateKey: Redacted.make(key2), |
| 187 | type: "sni_custom", |
| 188 | bundleMethod: "force", |
| 189 | }); |
| 190 | }), |
| 191 | ); |
| 192 | expect(rotated.certificateId).toEqual(created.certificateId); |
| 193 | expect(rotated.contentHash).not.toEqual(created.contentHash); |
| 194 | // cert2 has a different validity window than cert1. |
| 195 | expect(rotated.expiresOn).not.toEqual(created.expiresOn); |
| 196 | |
| 197 | // Changing the immutable `type` triggers a replacement. |
| 198 | const replaced = yield* stack.deploy( |
| 199 | Effect.gen(function* () { |
| 200 | return yield* Cloudflare.CustomCertificate.CustomCertificate("EdgeCert", { |
| 201 | zoneId, |
| 202 | certificate: cert2, |
| 203 | privateKey: Redacted.make(key2), |
| 204 | type: "legacy_custom", |
| 205 | bundleMethod: "force", |
| 206 | }); |
| 207 | }), |
| 208 | ); |
| 209 | expect(replaced.certificateId).not.toEqual(rotated.certificateId); |
| 210 | expect(replaced.type).toEqual("legacy_custom"); |
| 211 | |
| 212 | yield* stack.destroy(); |
| 213 | |
| 214 | // Destroy deleted the certificate — the typed NotFound tag confirms. |
| 215 | const gone = yield* customCertificates |
| 216 | .getCustomCertificate({ zoneId, customCertificateId: replaced.certificateId }) |
| 217 | .pipe( |
| 218 | Effect.retry({ |
| 219 | while: (e) => e._tag === "Forbidden", |
| 220 | schedule: forbiddenRetrySchedule, |
| 221 | times: 8, |
| 222 | }), |
| 223 | Effect.flip, |
| 224 | ); |
| 225 | expect(gone._tag).toEqual("CustomCertificateNotFound"); |
| 226 | }).pipe(logLevel), |
| 227 | { |
| 228 | tags: ["provider:cloudflare", "provider:cloudflare:customcertificate", "live"], |
| 229 | timeout: 120_000, |
| 230 | }, |
| 231 | ); |