The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as originCa from "@distilled.cloud/cloudflare/origin-ca-certificates"; |
| 5 | import { adopt } from "alchemy/AdoptPolicy"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | import { TEST_CSR } 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 = "alchemy-test-2.us"; |
| 19 | // Each test owns a DISTINCT hostname. Adoption keys purely off the hostname |
| 20 | // set (the engine probes `read` with `olds: news` even on a fresh deploy, so |
| 21 | // `findByHostnames` runs every time), and Origin CA certs carry no other |
| 22 | // identity. A shared hostname therefore couples the tests: a leftover cert |
| 23 | // from another test or a prior crashed run can be adopted, and a sibling's |
| 24 | // destroy can revoke a cert this test is mid-verifying. Per-test hostnames |
| 25 | // make each test fully self-contained. |
| 26 | |
| 27 | // The scoped API token the test harness mints propagates eventually- |
| 28 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 29 | // declared in the distilled error union) on out-of-band verification calls. |
| 30 | const getCertificate = (certificateId: string) => |
| 31 | originCa.getOriginCaCertificate({ certificateId }).pipe( |
| 32 | Effect.retry({ |
| 33 | while: (e) => e._tag === "Forbidden", |
| 34 | schedule: Schedule.exponential("500 millis"), |
| 35 | times: 8, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | // After destroy, the certificate is revoked. GET keeps returning a revoked |
| 40 | // certificate (with `revokedAt` set) for a while, and may eventually flip to |
| 41 | // `CertificateNotFound` — either state counts as gone. |
| 42 | const expectRevoked = (certificateId: string) => |
| 43 | getCertificate(certificateId).pipe( |
| 44 | Effect.flatMap((cert) => |
| 45 | cert.revokedAt ? Effect.void : Effect.fail({ _tag: "CertificateNotRevoked" } as const), |
| 46 | ), |
| 47 | Effect.catchTag("CertificateNotFound", () => Effect.void), |
| 48 | Effect.retry({ |
| 49 | while: (e) => e._tag === "CertificateNotRevoked", |
| 50 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | test.provider( |
| 55 | "issue, verify, and revoke a certificate", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const hostname = `${testName("originissue")}.${zoneName}`; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const cert = yield* stack.deploy( |
| 62 | Cloudflare.OriginCaCertificate.OriginCaCertificate("Cert", { |
| 63 | csr: TEST_CSR, |
| 64 | hostnames: [hostname], |
| 65 | requestType: "origin-rsa", |
| 66 | requestedValidity: 90, |
| 67 | }).pipe(adopt(true)), |
| 68 | ); |
| 69 | |
| 70 | // Issuance is synchronous — the signed PEM comes back on create. |
| 71 | expect(cert.certificateId).toBeTruthy(); |
| 72 | expect(cert.certificate).toContain("-----BEGIN CERTIFICATE-----"); |
| 73 | expect(cert.csr).toContain("-----BEGIN CERTIFICATE REQUEST-----"); |
| 74 | expect(cert.hostnames).toEqual([hostname]); |
| 75 | expect(cert.requestType).toEqual("origin-rsa"); |
| 76 | expect(cert.requestedValidity).toEqual(90); |
| 77 | expect(cert.expiresOn).toBeTruthy(); |
| 78 | |
| 79 | // Out-of-band verification: the certificate is live and not revoked. |
| 80 | const live = yield* getCertificate(cert.certificateId); |
| 81 | expect(live.id).toEqual(cert.certificateId); |
| 82 | expect(live.hostnames).toEqual([hostname]); |
| 83 | expect(live.revokedAt ?? null).toBeNull(); |
| 84 | |
| 85 | // Redeploying identical props is a no-op (same certificate). |
| 86 | const noop = yield* stack.deploy( |
| 87 | Cloudflare.OriginCaCertificate.OriginCaCertificate("Cert", { |
| 88 | csr: TEST_CSR, |
| 89 | hostnames: [hostname], |
| 90 | requestType: "origin-rsa", |
| 91 | requestedValidity: 90, |
| 92 | }).pipe(adopt(true)), |
| 93 | ); |
| 94 | expect(noop.certificateId).toEqual(cert.certificateId); |
| 95 | |
| 96 | // Destroy revokes the certificate; a second destroy is idempotent. |
| 97 | yield* stack.destroy(); |
| 98 | yield* expectRevoked(cert.certificateId); |
| 99 | yield* stack.destroy(); |
| 100 | }).pipe(logLevel), |
| 101 | { tags: ["provider:cloudflare", "provider:cloudflare:origincacertificate", "live"] }, |
| 102 | ); |
| 103 | |
| 104 | test.provider( |
| 105 | "list enumerates issued certificates", |
| 106 | (stack) => |
| 107 | Effect.gen(function* () { |
| 108 | const hostname = `${testName("originlist")}.${zoneName}`; |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const cert = yield* stack.deploy( |
| 112 | Cloudflare.OriginCaCertificate.OriginCaCertificate("ListCert", { |
| 113 | csr: TEST_CSR, |
| 114 | hostnames: [hostname], |
| 115 | requestType: "origin-rsa", |
| 116 | requestedValidity: 90, |
| 117 | }).pipe(adopt(true)), |
| 118 | ); |
| 119 | |
| 120 | const provider = yield* Provider.findProvider( |
| 121 | Cloudflare.OriginCaCertificate.OriginCaCertificate, |
| 122 | ); |
| 123 | const all = yield* provider.list(); |
| 124 | |
| 125 | // `list()` is account-wide but enumerated per zone with the `zone_id` |
| 126 | // query param; a zone the scoped token can't read for Origin CA rejects |
| 127 | // with the typed `Forbidden` tag, which is swallowed so that zone simply |
| 128 | // contributes []. The standing token can list the test zone, so the |
| 129 | // freshly issued certificate must appear in the exhaustively-paginated |
| 130 | // result in the `read` Attributes shape. |
| 131 | expect(Array.isArray(all)).toBe(true); |
| 132 | const match = all.find((c) => c.certificateId === cert.certificateId); |
| 133 | expect(match).toBeDefined(); |
| 134 | expect(match!.certificateId).toEqual(cert.certificateId); |
| 135 | expect(match!.hostnames).toEqual([hostname]); |
| 136 | expect(match!.requestType).toEqual("origin-rsa"); |
| 137 | |
| 138 | yield* stack.destroy(); |
| 139 | yield* expectRevoked(cert.certificateId); |
| 140 | }).pipe(logLevel), |
| 141 | { tags: ["provider:cloudflare", "provider:cloudflare:origincacertificate", "live"] }, |
| 142 | ); |
| 143 | |
| 144 | // Explicit revoke for certificates the wedged-state recovery orphans out of |
| 145 | // engine state (their rows lose `attr`, so `stack.destroy()` can't reclaim |
| 146 | // them). Revoked/absent both count as cleaned up. |
| 147 | const revokeQuietly = (certificateId: string) => |
| 148 | originCa.deleteOriginCaCertificate({ certificateId }).pipe( |
| 149 | Effect.retry({ |
| 150 | while: (e) => e._tag === "Forbidden", |
| 151 | schedule: Schedule.exponential("500 millis"), |
| 152 | times: 8, |
| 153 | }), |
| 154 | Effect.catchTag("CertificateNotFound", () => Effect.void), |
| 155 | Effect.catchTag("CertificateAlreadyRevoked", () => Effect.void), |
| 156 | ); |
| 157 | |
| 158 | test.provider( |
| 159 | "recovers a creating-state row whose Output-valued hostnames were lost (#736)", |
| 160 | (stack) => |
| 161 | Effect.gen(function* () { |
| 162 | const hostname = `${testName("originwedged")}.${zoneName}`; |
| 163 | yield* stack.destroy(); |
| 164 | |
| 165 | const deployCert = () => |
| 166 | stack.deploy( |
| 167 | Cloudflare.OriginCaCertificate.OriginCaCertificate("WedgedCert", { |
| 168 | csr: TEST_CSR, |
| 169 | hostnames: [hostname], |
| 170 | requestType: "origin-rsa", |
| 171 | requestedValidity: 90, |
| 172 | }).pipe(adopt(true)), |
| 173 | ); |
| 174 | |
| 175 | const created = yield* deployCert(); |
| 176 | expect(created.certificateId).toBeTruthy(); |
| 177 | |
| 178 | // Rewrite the certificate's persisted row into the wedged shape an |
| 179 | // interrupted deploy leaves behind: `creating`, no attributes, and the |
| 180 | // Output-valued props lost in the state round-trip (#736). |
| 181 | const state = yield* yield* State; |
| 182 | const stage = stack.stage; |
| 183 | const wedgeRow = (junk: Record<string, unknown>) => |
| 184 | Effect.gen(function* () { |
| 185 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 186 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 187 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 188 | ); |
| 189 | const wedged = rows.find( |
| 190 | (r): r is { fqn: string; row: ResourceState } => |
| 191 | isResourceState(r.row) && |
| 192 | r.row.resourceType === "Cloudflare.OriginCaCertificate.OriginCaCertificate", |
| 193 | ); |
| 194 | if (!wedged) { |
| 195 | return yield* Effect.die( |
| 196 | new Error("no OriginCaCertificate state row found after deploy"), |
| 197 | ); |
| 198 | } |
| 199 | yield* state.set({ |
| 200 | stack: stack.name, |
| 201 | stage, |
| 202 | fqn: wedged.fqn, |
| 203 | value: { |
| 204 | ...wedged.row, |
| 205 | status: "creating", |
| 206 | attr: undefined, |
| 207 | props: { ...wedged.row.props, ...junk }, |
| 208 | }, |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | // The wedge orphans the previous certificate out of engine state, so |
| 213 | // destroy can never reclaim it — revoke it explicitly on scope close |
| 214 | // even if the body fails mid-way. |
| 215 | yield* Effect.addFinalizer(() => revokeQuietly(created.certificateId).pipe(Effect.ignore)); |
| 216 | |
| 217 | // Wedge 1 — the #736 shape: the hostnames array survives serialization |
| 218 | // but its Output-valued ELEMENT deserializes as undefined. Before the |
| 219 | // fix, read's cold path only checked `hostnames?.length`, passed the |
| 220 | // truthy-length junk array into `findByHostnames`, and crashed on |
| 221 | // `hostnames[0].replace(...)`. After the fix, read reports "missing" |
| 222 | // and the engine re-creates. |
| 223 | yield* wedgeRow({ hostnames: [undefined] }); |
| 224 | const recovered = yield* deployCert(); |
| 225 | expect(recovered.certificateId).toBeTruthy(); |
| 226 | expect(recovered.hostnames).toEqual([hostname]); |
| 227 | const live = yield* getCertificate(recovered.certificateId); |
| 228 | expect(live.hostnames).toEqual([hostname]); |
| 229 | expect(live.revokedAt ?? null).toBeNull(); |
| 230 | |
| 231 | yield* Effect.addFinalizer(() => revokeQuietly(recovered.certificateId).pipe(Effect.ignore)); |
| 232 | |
| 233 | // Wedge 2 — every Output-valued prop lost wholesale (`undefined`, not |
| 234 | // `[undefined]`): the whole hostnames array AND the csr. Guarded both |
| 235 | // before and after the fix in `read`; csr must be wedged too because |
| 236 | // `diff` keys its "no prior props" guard off `olds.csr`. Recovery is |
| 237 | // the same: read reports missing, the engine re-creates. |
| 238 | yield* wedgeRow({ hostnames: undefined, csr: undefined }); |
| 239 | const recovered2 = yield* deployCert(); |
| 240 | expect(recovered2.certificateId).toBeTruthy(); |
| 241 | expect(recovered2.hostnames).toEqual([hostname]); |
| 242 | const live2 = yield* getCertificate(recovered2.certificateId); |
| 243 | expect(live2.hostnames).toEqual([hostname]); |
| 244 | expect(live2.revokedAt ?? null).toBeNull(); |
| 245 | |
| 246 | yield* stack.destroy(); |
| 247 | yield* expectRevoked(recovered2.certificateId); |
| 248 | |
| 249 | // Explicitly reclaim the certificates the wedges orphaned. |
| 250 | yield* revokeQuietly(created.certificateId); |
| 251 | yield* revokeQuietly(recovered.certificateId); |
| 252 | yield* expectRevoked(created.certificateId); |
| 253 | yield* expectRevoked(recovered.certificateId); |
| 254 | }).pipe(logLevel), |
| 255 | { |
| 256 | tags: ["provider:cloudflare", "provider:cloudflare:origincacertificate", "live"], |
| 257 | timeout: 240_000, |
| 258 | }, |
| 259 | ); |
| 260 | |
| 261 | test.provider( |
| 262 | "replacement on requestedValidity change", |
| 263 | (stack) => |
| 264 | Effect.gen(function* () { |
| 265 | const hostname = `${testName("originvalidity")}.${zoneName}`; |
| 266 | yield* stack.destroy(); |
| 267 | |
| 268 | const initial = yield* stack.deploy( |
| 269 | Cloudflare.OriginCaCertificate.OriginCaCertificate("ValidityCert", { |
| 270 | csr: TEST_CSR, |
| 271 | hostnames: [hostname], |
| 272 | requestType: "origin-rsa", |
| 273 | requestedValidity: 90, |
| 274 | }).pipe(adopt(true)), |
| 275 | ); |
| 276 | expect(initial.requestedValidity).toEqual(90); |
| 277 | |
| 278 | // There is no update API — changing the validity issues a new |
| 279 | // certificate and revokes the old one. |
| 280 | const replaced = yield* stack.deploy( |
| 281 | Cloudflare.OriginCaCertificate.OriginCaCertificate("ValidityCert", { |
| 282 | csr: TEST_CSR, |
| 283 | hostnames: [hostname], |
| 284 | requestType: "origin-rsa", |
| 285 | requestedValidity: 30, |
| 286 | }).pipe(adopt(true)), |
| 287 | ); |
| 288 | |
| 289 | expect(replaced.certificateId).not.toEqual(initial.certificateId); |
| 290 | expect(replaced.requestedValidity).toEqual(30); |
| 291 | yield* expectRevoked(initial.certificateId); |
| 292 | |
| 293 | const live = yield* getCertificate(replaced.certificateId); |
| 294 | expect(live.revokedAt ?? null).toBeNull(); |
| 295 | |
| 296 | yield* stack.destroy(); |
| 297 | yield* expectRevoked(replaced.certificateId); |
| 298 | }).pipe(logLevel), |
| 299 | { tags: ["provider:cloudflare", "provider:cloudflare:origincacertificate", "live"] }, |
| 300 | ); |
| 301 | |
| 302 | test.provider( |
| 303 | "replacement on hostnames change", |
| 304 | (stack) => |
| 305 | Effect.gen(function* () { |
| 306 | const hostname = `${testName("originhostsa")}.${zoneName}`; |
| 307 | const altHostname = `${testName("originhostsb")}.${zoneName}`; |
| 308 | yield* stack.destroy(); |
| 309 | |
| 310 | const initial = yield* stack.deploy( |
| 311 | Cloudflare.OriginCaCertificate.OriginCaCertificate("HostnamesCert", { |
| 312 | csr: TEST_CSR, |
| 313 | hostnames: [hostname], |
| 314 | requestType: "origin-rsa", |
| 315 | requestedValidity: 90, |
| 316 | }).pipe(adopt(true)), |
| 317 | ); |
| 318 | expect(initial.hostnames).toEqual([hostname]); |
| 319 | |
| 320 | // Hostnames are immutable — changing the set issues a new certificate |
| 321 | // and revokes the old one. |
| 322 | const replaced = yield* stack.deploy( |
| 323 | Cloudflare.OriginCaCertificate.OriginCaCertificate("HostnamesCert", { |
| 324 | csr: TEST_CSR, |
| 325 | hostnames: [hostname, altHostname], |
| 326 | requestType: "origin-rsa", |
| 327 | requestedValidity: 90, |
| 328 | }).pipe(adopt(true)), |
| 329 | ); |
| 330 | |
| 331 | expect(replaced.certificateId).not.toEqual(initial.certificateId); |
| 332 | expect([...replaced.hostnames].sort()).toEqual([hostname, altHostname].sort()); |
| 333 | yield* expectRevoked(initial.certificateId); |
| 334 | |
| 335 | yield* stack.destroy(); |
| 336 | yield* expectRevoked(replaced.certificateId); |
| 337 | }).pipe(logLevel), |
| 338 | { tags: ["provider:cloudflare", "provider:cloudflare:origincacertificate", "live"] }, |
| 339 | ); |