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 clientCertificates from "@distilled.cloud/cloudflare/client-certificates"; |
| 7 | import { adopt } from "alchemy/AdoptPolicy"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { CSR_A, CSR_B } from "./fixtures/csr.ts"; |
| 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 | const resolveZoneId = Effect.gen(function* () { |
| 21 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 22 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 23 | if (!zone) { |
| 24 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 25 | } |
| 26 | return zone.id; |
| 27 | }); |
| 28 | |
| 29 | // The scoped API token the test harness mints propagates eventually- |
| 30 | // consistently across Cloudflare's edge — a fresh token intermittently 403s. |
| 31 | // Ride out the blips on the test's own out-of-band calls by retrying the |
| 32 | // typed `Forbidden` error (part of every client-certificates operation's |
| 33 | // error union via distilled patches). |
| 34 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 35 | |
| 36 | const getCertificate = (zoneId: string, clientCertificateId: string) => |
| 37 | clientCertificates.getClientCertificate({ zoneId, clientCertificateId }).pipe( |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "Forbidden", |
| 40 | schedule: forbiddenRetrySchedule, |
| 41 | times: 8, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // DELETE revokes the certificate; revoked certificates stay listed on the |
| 46 | // zone forever, so "gone" means the certificate is revoking, not a 404. |
| 47 | // Revocation transits `pending_revocation` asynchronously and Cloudflare can |
| 48 | // linger there well past the test's budget before flipping to `revoked` — |
| 49 | // both states are the delete semantic (the certificate can no longer serve |
| 50 | // mTLS), so treat either as "gone" and poll the typed GET until it lands, |
| 51 | // bounded so the test fails fast instead of riding the vitest timeout. |
| 52 | const isRevoking = (status: string | null | undefined): boolean => |
| 53 | status === "revoked" || status === "pending_revocation"; |
| 54 | |
| 55 | const waitUntilRevoked = (zoneId: string, clientCertificateId: string) => |
| 56 | getCertificate(zoneId, clientCertificateId).pipe( |
| 57 | // Frequent, bounded spaced poll (~90s): revocation settles through |
| 58 | // `pending_revocation` asynchronously, so check every 3s rather than |
| 59 | // backing off exponentially (whose late gaps would overshoot the |
| 60 | // timeout). Bounded so a stuck revoke fails fast instead of riding the |
| 61 | // vitest timeout. |
| 62 | Effect.repeat({ |
| 63 | schedule: Schedule.spaced("3 seconds"), |
| 64 | until: (cert) => isRevoking(cert.status), |
| 65 | times: 30, |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | test.provider( |
| 70 | "create signs the CSR, destroy revokes the certificate", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | const zoneId = yield* resolveZoneId; |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | // adopt(true): revoked certificates stay listed forever and the |
| 78 | // cold-read identity is (csr, validityDays), so an interrupted prior |
| 79 | // run can leave a live certificate this deploy should converge onto. |
| 80 | const cert = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* Cloudflare.ClientCertificate.ClientCertificate("CreateCert", { |
| 83 | zoneId, |
| 84 | csr: CSR_A, |
| 85 | validityDays: 90, |
| 86 | }).pipe(adopt(true)); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(cert.clientCertificateId).toBeDefined(); |
| 91 | expect(cert.zoneId).toEqual(zoneId); |
| 92 | expect(cert.status).toEqual("active"); |
| 93 | expect(cert.validityDays).toEqual(90); |
| 94 | expect(cert.certificate).toContain("-----BEGIN CERTIFICATE-----"); |
| 95 | expect(cert.commonName).toEqual("alchemy-client-cert-a.alchemy-test-2.us"); |
| 96 | expect(cert.organization).toEqual("Alchemy"); |
| 97 | expect(cert.country).toEqual("US"); |
| 98 | expect(cert.issuedOn).toBeDefined(); |
| 99 | expect(cert.expiresOn).toBeDefined(); |
| 100 | expect(cert.serialNumber).toBeDefined(); |
| 101 | expect(cert.fingerprintSha256).toBeDefined(); |
| 102 | |
| 103 | // Out-of-band verification through the raw API. |
| 104 | const live = yield* getCertificate(zoneId, cert.clientCertificateId); |
| 105 | expect(live.id).toEqual(cert.clientCertificateId); |
| 106 | expect(live.status).toEqual("active"); |
| 107 | expect(live.validityDays).toEqual(90); |
| 108 | |
| 109 | // Re-deploying the same props is a no-op — same physical certificate. |
| 110 | const again = yield* stack.deploy( |
| 111 | Effect.gen(function* () { |
| 112 | return yield* Cloudflare.ClientCertificate.ClientCertificate("CreateCert", { |
| 113 | zoneId, |
| 114 | csr: CSR_A, |
| 115 | validityDays: 90, |
| 116 | }).pipe(adopt(true)); |
| 117 | }), |
| 118 | ); |
| 119 | expect(again.clientCertificateId).toEqual(cert.clientCertificateId); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | // Destroy revokes — the certificate remains listed but transitions to |
| 124 | // `revoked`, which this resource treats as deleted. |
| 125 | const revoked = yield* waitUntilRevoked(zoneId, cert.clientCertificateId); |
| 126 | expect(isRevoking(revoked.status)).toBe(true); |
| 127 | }).pipe(logLevel), |
| 128 | // Two deploys (issue + no-op re-deploy) on Cloudflare's per-zone-serialized |
| 129 | // client-cert API plus a ~90s spaced revoke poll — under a full concurrent |
| 130 | // `./test/Cloudflare` run this contends with sibling cert suites, so give |
| 131 | // headroom while staying bounded. |
| 132 | { |
| 133 | tags: [ |
| 134 | "provider:cloudflare", |
| 135 | "provider:cloudflare:clientcertificate", |
| 136 | "provider:cloudflare:zone", |
| 137 | "live", |
| 138 | ], |
| 139 | timeout: 240_000, |
| 140 | }, |
| 141 | ); |
| 142 | |
| 143 | test.provider( |
| 144 | "changing validityDays replaces — new id issued, old certificate revoked", |
| 145 | (stack) => |
| 146 | Effect.gen(function* () { |
| 147 | const zoneId = yield* resolveZoneId; |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | const initial = yield* stack.deploy( |
| 152 | Effect.gen(function* () { |
| 153 | return yield* Cloudflare.ClientCertificate.ClientCertificate("ReplaceCert", { |
| 154 | zoneId, |
| 155 | csr: CSR_B, |
| 156 | validityDays: 30, |
| 157 | }).pipe(adopt(true)); |
| 158 | }), |
| 159 | ); |
| 160 | expect(initial.status).toEqual("active"); |
| 161 | expect(initial.validityDays).toEqual(30); |
| 162 | |
| 163 | // validityDays is immutable — the API cannot re-sign, so the engine |
| 164 | // replaces: a new certificate is issued and the old one is revoked. |
| 165 | const replaced = yield* stack.deploy( |
| 166 | Effect.gen(function* () { |
| 167 | return yield* Cloudflare.ClientCertificate.ClientCertificate("ReplaceCert", { |
| 168 | zoneId, |
| 169 | csr: CSR_B, |
| 170 | validityDays: 60, |
| 171 | }).pipe(adopt(true)); |
| 172 | }), |
| 173 | ); |
| 174 | |
| 175 | expect(replaced.clientCertificateId).not.toEqual(initial.clientCertificateId); |
| 176 | expect(replaced.status).toEqual("active"); |
| 177 | expect(replaced.validityDays).toEqual(60); |
| 178 | |
| 179 | // The outgoing certificate was revoked as part of the replacement. |
| 180 | const oldCert = yield* waitUntilRevoked(zoneId, initial.clientCertificateId); |
| 181 | expect(isRevoking(oldCert.status)).toBe(true); |
| 182 | |
| 183 | // The replacement is live and untouched by the old one's revocation. |
| 184 | const live = yield* getCertificate(zoneId, replaced.clientCertificateId); |
| 185 | expect(live.status).toEqual("active"); |
| 186 | |
| 187 | yield* stack.destroy(); |
| 188 | |
| 189 | const revoked = yield* waitUntilRevoked(zoneId, replaced.clientCertificateId); |
| 190 | expect(isRevoking(revoked.status)).toBe(true); |
| 191 | }).pipe(logLevel), |
| 192 | // This is a REPLACEMENT: the second deploy issues a brand-new certificate |
| 193 | // and revokes the old one, then the test runs TWO ~90s spaced revoke polls |
| 194 | // (the outgoing cert after replace, the replacement after destroy) on top |
| 195 | // of three serialized client-cert mutations. Under a full concurrent |
| 196 | // `./test/Cloudflare` run this far exceeds the default 120s, so give real |
| 197 | // headroom while every poll stays bounded. |
| 198 | { |
| 199 | tags: [ |
| 200 | "provider:cloudflare", |
| 201 | "provider:cloudflare:clientcertificate", |
| 202 | "provider:cloudflare:zone", |
| 203 | "live", |
| 204 | ], |
| 205 | timeout: 300_000, |
| 206 | }, |
| 207 | ); |
| 208 | |
| 209 | test.provider( |
| 210 | "list enumerates client certificates across zones", |
| 211 | (stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const zoneId = yield* resolveZoneId; |
| 214 | |
| 215 | yield* stack.destroy(); |
| 216 | |
| 217 | const cert = yield* stack.deploy( |
| 218 | Effect.gen(function* () { |
| 219 | return yield* Cloudflare.ClientCertificate.ClientCertificate("ListCert", { |
| 220 | zoneId, |
| 221 | csr: CSR_A, |
| 222 | validityDays: 90, |
| 223 | }).pipe(adopt(true)); |
| 224 | }), |
| 225 | ); |
| 226 | |
| 227 | const provider = yield* Provider.findProvider(Cloudflare.ClientCertificate.ClientCertificate); |
| 228 | |
| 229 | // A freshly-issued client certificate is eventually consistent in the |
| 230 | // account-wide list fan-out — poll (bounded) until it shows up rather |
| 231 | // than asserting on a single immediate snapshot. |
| 232 | const found = yield* Effect.gen(function* () { |
| 233 | const all = yield* provider.list(); |
| 234 | return all.find((c) => c.clientCertificateId === cert.clientCertificateId); |
| 235 | }).pipe( |
| 236 | Effect.flatMap((f) => |
| 237 | f === undefined ? Effect.fail("not-yet-listed" as const) : Effect.succeed(f), |
| 238 | ), |
| 239 | Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 }), |
| 240 | ); |
| 241 | |
| 242 | expect(found).toBeDefined(); |
| 243 | expect(found?.zoneId).toEqual(zoneId); |
| 244 | expect(found?.status).not.toEqual("revoked"); |
| 245 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie).pipe(Effect.ignore)), logLevel), |
| 246 | // `list()` fans out over every zone in the account and exhaustively |
| 247 | // paginates each, plus a deploy on the per-zone-serialized client-cert API |
| 248 | // — give headroom under a full concurrent `./test/Cloudflare` run. |
| 249 | { |
| 250 | tags: [ |
| 251 | "provider:cloudflare", |
| 252 | "provider:cloudflare:clientcertificate", |
| 253 | "provider:cloudflare:zone", |
| 254 | "live", |
| 255 | ], |
| 256 | timeout: 180_000, |
| 257 | }, |
| 258 | ); |