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 hostnames from "@distilled.cloud/cloudflare/hostnames"; |
| 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 { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 18 | |
| 19 | // Per-hostname TLS settings are gated behind Advanced Certificate Manager |
| 20 | // (or Cloudflare for SaaS) — on the standard testing zone every PUT/DELETE |
| 21 | // fails with Cloudflare code 1450, surfaced as the typed |
| 22 | // `AdvancedCertificateManagerRequired` error. The full lifecycle test below |
| 23 | // is gated behind an entitled zone + hostname supplied via env. |
| 24 | const acmZoneId = process.env.CLOUDFLARE_TEST_ACM_ZONE_ID; |
| 25 | const acmHostname = process.env.CLOUDFLARE_TEST_ACM_HOSTNAME; |
| 26 | |
| 27 | const resolveZoneId = Effect.gen(function* () { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 30 | if (!zone) { |
| 31 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 32 | } |
| 33 | return zone.id; |
| 34 | }); |
| 35 | |
| 36 | // Freshly-minted scoped tokens propagate eventually-consistently across |
| 37 | // Cloudflare's edge — ride out intermittent 403 blips on the test's |
| 38 | // out-of-band calls by retrying the typed `Forbidden` error. |
| 39 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 40 | |
| 41 | test.provider( |
| 42 | "lists overrides and surfaces the typed AdvancedCertificateManagerRequired error on unentitled zones", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const zoneId = yield* resolveZoneId; |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | // Reading a hostname's override works on any zone; the standing test |
| 50 | // zone has no overrides, so the probe resolves to "none". |
| 51 | const observed = yield* hostnames |
| 52 | .listSettingsTls({ |
| 53 | zoneId, |
| 54 | settingId: "min_tls_version", |
| 55 | }) |
| 56 | .pipe( |
| 57 | Effect.map((settings) => |
| 58 | settings.find( |
| 59 | (setting) => setting.hostname === `${testName("alchemy-htls-gate")}.${zoneName}`, |
| 60 | ), |
| 61 | ), |
| 62 | ); |
| 63 | expect(observed).toBeUndefined(); |
| 64 | |
| 65 | // The standard testing zone lacks the ACM entitlement — a write must |
| 66 | // fail with the typed entitlement tag (Cloudflare code 1450). |
| 67 | const error = yield* hostnames |
| 68 | .putSettingTls({ |
| 69 | zoneId, |
| 70 | settingId: "min_tls_version", |
| 71 | hostname: `${testName("alchemy-htls-gate")}.${zoneName}`, |
| 72 | value: "1.2", |
| 73 | }) |
| 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("AdvancedCertificateManagerRequired"); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: [ |
| 88 | "provider:cloudflare", |
| 89 | "provider:cloudflare:hostnametlssetting", |
| 90 | "provider:cloudflare:zone", |
| 91 | "live", |
| 92 | ], |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | // Canonical `list()` test (zone-scoped collection): there is no account-wide |
| 97 | // API for per-hostname overrides, so `list()` enumerates every zone via |
| 98 | // `listAllZones` and lists the overrides for each of the three TLS settings. |
| 99 | // The standing test zone has no ACM entitlement and therefore |
| 100 | // no overrides, so the well-typed result is normally empty; when an entitled |
| 101 | // zone + hostname is supplied via env we deploy one and assert its presence. |
| 102 | test.provider( |
| 103 | "list enumerates per-hostname TLS overrides", |
| 104 | (stack) => |
| 105 | Effect.gen(function* () { |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | if (acmZoneId && acmHostname) { |
| 109 | yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* Cloudflare.HostnameTlsSetting.HostnameTlsSetting("ListMinTls", { |
| 112 | zoneId: acmZoneId, |
| 113 | settingId: "min_tls_version", |
| 114 | hostname: acmHostname, |
| 115 | value: "1.2", |
| 116 | }); |
| 117 | }), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | const provider = yield* Provider.findProvider( |
| 122 | Cloudflare.HostnameTlsSetting.HostnameTlsSetting, |
| 123 | ); |
| 124 | const all = yield* provider.list(); |
| 125 | |
| 126 | // Always a well-typed array (possibly empty on an unentitled account). |
| 127 | expect(Array.isArray(all)).toBe(true); |
| 128 | |
| 129 | if (acmZoneId && acmHostname) { |
| 130 | expect( |
| 131 | all.some( |
| 132 | (s) => |
| 133 | s.zoneId === acmZoneId && |
| 134 | s.settingId === "min_tls_version" && |
| 135 | s.hostname === acmHostname, |
| 136 | ), |
| 137 | ).toBe(true); |
| 138 | } |
| 139 | |
| 140 | yield* stack.destroy(); |
| 141 | }).pipe(logLevel), |
| 142 | { |
| 143 | tags: ["provider:cloudflare", "provider:cloudflare:hostnametlssetting", "live"], |
| 144 | timeout: 120_000, |
| 145 | }, |
| 146 | ); |
| 147 | |
| 148 | test.provider.skipIf(!acmZoneId || !acmHostname)( |
| 149 | "create, update in place, and destroy a min_tls_version override", |
| 150 | (stack) => |
| 151 | Effect.gen(function* () { |
| 152 | const zoneId = acmZoneId!; |
| 153 | const hostname = acmHostname!; |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | const created = yield* stack.deploy( |
| 158 | Effect.gen(function* () { |
| 159 | return yield* Cloudflare.HostnameTlsSetting.HostnameTlsSetting("MinTls", { |
| 160 | zoneId, |
| 161 | settingId: "min_tls_version", |
| 162 | hostname, |
| 163 | value: "1.2", |
| 164 | }); |
| 165 | }), |
| 166 | ); |
| 167 | |
| 168 | expect(created.zoneId).toEqual(zoneId); |
| 169 | expect(created.settingId).toEqual("min_tls_version"); |
| 170 | expect(created.hostname).toEqual(hostname); |
| 171 | expect(created.value).toEqual("1.2"); |
| 172 | |
| 173 | // Out-of-band verification via the distilled API. |
| 174 | const live = yield* hostnames |
| 175 | .listSettingsTls({ |
| 176 | zoneId, |
| 177 | settingId: "min_tls_version", |
| 178 | }) |
| 179 | .pipe(Effect.map((settings) => settings.find((setting) => setting.hostname === hostname))); |
| 180 | expect(live).toBeDefined(); |
| 181 | expect(live!.value).toEqual("1.2"); |
| 182 | |
| 183 | // Update in place — PUT upserts the same (settingId, hostname) pair. |
| 184 | const updated = yield* stack.deploy( |
| 185 | Effect.gen(function* () { |
| 186 | return yield* Cloudflare.HostnameTlsSetting.HostnameTlsSetting("MinTls", { |
| 187 | zoneId, |
| 188 | settingId: "min_tls_version", |
| 189 | hostname, |
| 190 | value: "1.3", |
| 191 | }); |
| 192 | }), |
| 193 | ); |
| 194 | expect(updated.hostname).toEqual(hostname); |
| 195 | expect(updated.value).toEqual("1.3"); |
| 196 | |
| 197 | const liveUpdated = yield* hostnames |
| 198 | .listSettingsTls({ |
| 199 | zoneId, |
| 200 | settingId: "min_tls_version", |
| 201 | }) |
| 202 | .pipe(Effect.map((settings) => settings.find((setting) => setting.hostname === hostname))); |
| 203 | expect(liveUpdated!.value).toEqual("1.3"); |
| 204 | |
| 205 | yield* stack.destroy(); |
| 206 | |
| 207 | // Removal is eventually consistent — poll the GET (bounded) until |
| 208 | // the override disappears and the hostname reverts to zone defaults. |
| 209 | const gone = yield* hostnames.listSettingsTls({ zoneId, settingId: "min_tls_version" }).pipe( |
| 210 | Effect.map((settings) => settings.find((setting) => setting.hostname === hostname)), |
| 211 | Effect.repeat({ |
| 212 | schedule: Schedule.spaced("3 seconds"), |
| 213 | until: (entry) => entry === undefined, |
| 214 | times: 10, |
| 215 | }), |
| 216 | ); |
| 217 | expect(gone).toBeUndefined(); |
| 218 | }).pipe(logLevel), |
| 219 | { |
| 220 | tags: ["provider:cloudflare", "provider:cloudflare:hostnametlssetting", "live"], |
| 221 | timeout: 120_000, |
| 222 | }, |
| 223 | ); |