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 { 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 keylessCertificates from "@distilled.cloud/cloudflare/keyless-certificates"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { testName } from "../../names.ts"; |
| 13 | import { CERT_1, CERT_2 } from "./fixtures/certs.ts"; |
| 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 | // Keyless SSL is Enterprise-only. On the testing account's zone every |
| 22 | // createKeylessCertificate fails with Cloudflare code 1067 ("Keyless SSL is |
| 23 | // not available for this zone. Please contact support."), surfaced as the |
| 24 | // typed `KeylessSslNotAvailable` error. The full lifecycle test below is |
| 25 | // gated behind an Enterprise zone id supplied via env. |
| 26 | const enterpriseZoneId = process.env.CLOUDFLARE_TEST_ENTERPRISE_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 | // The scoped API token the test harness mints propagates eventually- |
| 38 | // consistently across Cloudflare's edge — ride out 403 blips on the test's |
| 39 | // own out-of-band calls by retrying the typed `Forbidden` error. |
| 40 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 41 | |
| 42 | const getKeyless = (zoneId: string, keylessCertificateId: string) => |
| 43 | keylessCertificates.getKeylessCertificate({ zoneId, keylessCertificateId }).pipe( |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "Forbidden", |
| 46 | schedule: forbiddenRetrySchedule, |
| 47 | times: 8, |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | // A destroyed Keyless SSL configuration either disappears (typed |
| 52 | // `KeylessCertificateNotFound`, Cloudflare code 1005) or briefly lingers as |
| 53 | // a "deleted" tombstone — both count as gone. |
| 54 | const expectGone = (zoneId: string, keylessCertificateId: string) => |
| 55 | getKeyless(zoneId, keylessCertificateId).pipe( |
| 56 | Effect.flatMap((observed) => |
| 57 | observed.status === "deleted" |
| 58 | ? Effect.void |
| 59 | : Effect.fail({ _tag: "KeylessNotDeleted" } as const), |
| 60 | ), |
| 61 | Effect.catchTag("KeylessCertificateNotFound", () => Effect.void), |
| 62 | Effect.retry({ |
| 63 | while: (e) => e._tag === "KeylessNotDeleted", |
| 64 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | test.provider( |
| 69 | "surfaces the typed KeylessSslNotAvailable error on non-Enterprise zones", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | const zoneId = yield* resolveZoneId; |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | // Listing works on every plan — verifies token scope and the typed |
| 77 | // pagination surface out of band. |
| 78 | const existing = yield* keylessCertificates.listKeylessCertificates.items({ zoneId }).pipe( |
| 79 | Stream.runCollect, |
| 80 | Effect.retry({ |
| 81 | while: (e) => e._tag === "Forbidden", |
| 82 | schedule: forbiddenRetrySchedule, |
| 83 | times: 8, |
| 84 | }), |
| 85 | ); |
| 86 | expect(Array.from(existing).filter((k) => k.status !== "deleted")).toEqual([]); |
| 87 | |
| 88 | // Reading a non-existent configuration surfaces the typed |
| 89 | // `KeylessCertificateNotFound` (Cloudflare code 1005). |
| 90 | const readError = yield* getKeyless(zoneId, "00000000000000000000000000000000").pipe( |
| 91 | Effect.flip, |
| 92 | ); |
| 93 | expect(readError._tag).toEqual("KeylessCertificateNotFound"); |
| 94 | |
| 95 | // Deleting a non-existent configuration converges idempotently once |
| 96 | // the typed not-found tag is absorbed — exactly what the provider's |
| 97 | // delete does. |
| 98 | yield* keylessCertificates |
| 99 | .deleteKeylessCertificate({ |
| 100 | zoneId, |
| 101 | keylessCertificateId: "00000000000000000000000000000000", |
| 102 | }) |
| 103 | .pipe( |
| 104 | Effect.catchTag("KeylessCertificateNotFound", () => Effect.void), |
| 105 | Effect.retry({ |
| 106 | while: (e) => e._tag === "Forbidden", |
| 107 | schedule: forbiddenRetrySchedule, |
| 108 | times: 8, |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | // Creating on a non-Enterprise zone fails with the typed entitlement |
| 113 | // tag (Cloudflare code 1067). |
| 114 | const createError = yield* keylessCertificates |
| 115 | .createKeylessCertificate({ |
| 116 | zoneId, |
| 117 | certificate: CERT_1, |
| 118 | host: `${testName("keyless")}.${zoneName}`, |
| 119 | port: 24008, |
| 120 | name: "alchemy-keyless-entitlement-probe", |
| 121 | }) |
| 122 | .pipe( |
| 123 | Effect.retry({ |
| 124 | while: (e) => e._tag === "Forbidden", |
| 125 | schedule: forbiddenRetrySchedule, |
| 126 | times: 8, |
| 127 | }), |
| 128 | Effect.flip, |
| 129 | ); |
| 130 | expect(createError._tag).toEqual("KeylessSslNotAvailable"); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | }).pipe(logLevel), |
| 134 | { |
| 135 | tags: [ |
| 136 | "provider:cloudflare", |
| 137 | "provider:cloudflare:keylesscertificate", |
| 138 | "provider:cloudflare:zone", |
| 139 | "live", |
| 140 | ], |
| 141 | }, |
| 142 | ); |
| 143 | |
| 144 | // `list()` enumerates every zone in the account and lists its Keyless SSL |
| 145 | // configurations (zone-scoped collection, pattern (c)). This read-only |
| 146 | // assertion runs on every plan: the testing account has no Keyless SSL |
| 147 | // configurations (Enterprise-only), so the exhaustively-paginated result is a |
| 148 | // well-typed (possibly empty) array whose elements match `read`'s Attributes. |
| 149 | test.provider( |
| 150 | "list enumerates keyless certificates across zones", |
| 151 | (stack) => |
| 152 | Effect.gen(function* () { |
| 153 | yield* stack.destroy(); |
| 154 | |
| 155 | const provider = yield* Provider.findProvider( |
| 156 | Cloudflare.KeylessCertificate.KeylessCertificate, |
| 157 | ); |
| 158 | const all = yield* provider.list().pipe( |
| 159 | Effect.retry({ |
| 160 | while: (e) => e._tag === "Forbidden", |
| 161 | schedule: forbiddenRetrySchedule, |
| 162 | times: 8, |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | expect(Array.isArray(all)).toBe(true); |
| 167 | for (const item of all) { |
| 168 | expect(typeof item.keylessCertificateId).toBe("string"); |
| 169 | expect(typeof item.zoneId).toBe("string"); |
| 170 | expect(item.status).not.toEqual("deleted"); |
| 171 | } |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | }).pipe(logLevel), |
| 175 | { tags: ["provider:cloudflare", "provider:cloudflare:keylesscertificate", "live"] }, |
| 176 | ); |
| 177 | |
| 178 | // On an entitled Enterprise zone, the deployed configuration must appear in the |
| 179 | // exhaustively-paginated `list()` result. Gated behind an Enterprise zone id |
| 180 | // (creation otherwise fails with the typed `KeylessSslNotAvailable`, code 1067). |
| 181 | test.provider.skipIf(!enterpriseZoneId)( |
| 182 | "list includes a deployed keyless certificate", |
| 183 | (stack) => |
| 184 | Effect.gen(function* () { |
| 185 | const zoneId = enterpriseZoneId!; |
| 186 | |
| 187 | yield* stack.destroy(); |
| 188 | |
| 189 | const deployed = yield* stack.deploy( |
| 190 | Cloudflare.KeylessCertificate.KeylessCertificate("ListKeyless", { |
| 191 | zoneId, |
| 192 | certificate: CERT_1, |
| 193 | host: `${testName("keyless")}.${zoneName}`, |
| 194 | port: 24008, |
| 195 | }), |
| 196 | ); |
| 197 | |
| 198 | const provider = yield* Provider.findProvider( |
| 199 | Cloudflare.KeylessCertificate.KeylessCertificate, |
| 200 | ); |
| 201 | const all = yield* provider.list().pipe( |
| 202 | Effect.retry({ |
| 203 | while: (e) => e._tag === "Forbidden", |
| 204 | schedule: forbiddenRetrySchedule, |
| 205 | times: 8, |
| 206 | }), |
| 207 | ); |
| 208 | |
| 209 | expect(all.some((k) => k.keylessCertificateId === deployed.keylessCertificateId)).toBe(true); |
| 210 | |
| 211 | yield* stack.destroy(); |
| 212 | }).pipe(logLevel), |
| 213 | { |
| 214 | tags: ["provider:cloudflare", "provider:cloudflare:keylesscertificate", "live"], |
| 215 | timeout: 120_000, |
| 216 | }, |
| 217 | ); |
| 218 | |
| 219 | test.provider.skipIf(!enterpriseZoneId)( |
| 220 | "creates, updates in place, replaces on certificate change, and destroys", |
| 221 | (stack) => |
| 222 | Effect.gen(function* () { |
| 223 | const zoneId = enterpriseZoneId!; |
| 224 | |
| 225 | yield* stack.destroy(); |
| 226 | |
| 227 | // Create with an explicit name and conventional port. |
| 228 | const created = yield* stack.deploy( |
| 229 | Cloudflare.KeylessCertificate.KeylessCertificate("Keyless", { |
| 230 | zoneId, |
| 231 | certificate: CERT_1, |
| 232 | host: `${testName("keyless")}.${zoneName}`, |
| 233 | port: 24008, |
| 234 | name: testName("alchemy-keyless-lifecycle"), |
| 235 | }), |
| 236 | ); |
| 237 | |
| 238 | expect(created.keylessCertificateId).toBeDefined(); |
| 239 | expect(created.zoneId).toEqual(zoneId); |
| 240 | expect(created.name).toEqual(testName("alchemy-keyless-lifecycle")); |
| 241 | expect(created.host).toEqual(`${testName("keyless")}.${zoneName}`); |
| 242 | expect(created.port).toEqual(24008); |
| 243 | expect(created.status).toEqual("active"); |
| 244 | |
| 245 | // Out-of-band verify against the live API. |
| 246 | const live = yield* getKeyless(zoneId, created.keylessCertificateId); |
| 247 | expect(live.id).toEqual(created.keylessCertificateId); |
| 248 | expect(live.host).toEqual(`${testName("keyless")}.${zoneName}`); |
| 249 | expect(live.port).toEqual(24008); |
| 250 | |
| 251 | // Update mutable props in place — same identifier. |
| 252 | const updated = yield* stack.deploy( |
| 253 | Cloudflare.KeylessCertificate.KeylessCertificate("Keyless", { |
| 254 | zoneId, |
| 255 | certificate: CERT_1, |
| 256 | host: `${testName("keyless2")}.${zoneName}`, |
| 257 | port: 24009, |
| 258 | name: testName("alchemy-keyless-lifecycle-v2"), |
| 259 | enabled: false, |
| 260 | }), |
| 261 | ); |
| 262 | expect(updated.keylessCertificateId).toEqual(created.keylessCertificateId); |
| 263 | expect(updated.host).toEqual(`${testName("keyless2")}.${zoneName}`); |
| 264 | expect(updated.port).toEqual(24009); |
| 265 | expect(updated.name).toEqual(testName("alchemy-keyless-lifecycle-v2")); |
| 266 | expect(updated.enabled).toEqual(false); |
| 267 | |
| 268 | const liveUpdated = yield* getKeyless(zoneId, updated.keylessCertificateId); |
| 269 | expect(liveUpdated.host).toEqual(`${testName("keyless2")}.${zoneName}`); |
| 270 | expect(liveUpdated.port).toEqual(24009); |
| 271 | expect(liveUpdated.enabled).toEqual(false); |
| 272 | |
| 273 | // Changing the certificate replaces the configuration (the PATCH API |
| 274 | // has no certificate field) — a new identifier is assigned. |
| 275 | const replaced = yield* stack.deploy( |
| 276 | Cloudflare.KeylessCertificate.KeylessCertificate("Keyless", { |
| 277 | zoneId, |
| 278 | certificate: CERT_2, |
| 279 | host: `${testName("keyless2")}.${zoneName}`, |
| 280 | port: 24009, |
| 281 | name: testName("alchemy-keyless-lifecycle-v2"), |
| 282 | enabled: false, |
| 283 | }), |
| 284 | ); |
| 285 | expect(replaced.keylessCertificateId).not.toEqual(updated.keylessCertificateId); |
| 286 | yield* expectGone(zoneId, updated.keylessCertificateId); |
| 287 | |
| 288 | yield* stack.destroy(); |
| 289 | |
| 290 | yield* expectGone(zoneId, replaced.keylessCertificateId); |
| 291 | }).pipe(logLevel), |
| 292 | { |
| 293 | tags: ["provider:cloudflare", "provider:cloudflare:keylesscertificate", "live"], |
| 294 | timeout: 120_000, |
| 295 | }, |
| 296 | ); |