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 zones from "@distilled.cloud/cloudflare/zones"; |
| 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 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 17 | |
| 18 | // Enabling account-level custom nameservers (ACNS) on a zone requires an |
| 19 | // account nameserver set to exist first — a Business/Enterprise feature the |
| 20 | // testing account does not have. The enable/disable lifecycle below is |
| 21 | // gated behind a zone id (in an ACNS-entitled account) supplied via env. |
| 22 | const acnsZoneId = process.env.CLOUDFLARE_TEST_ACNS_ZONE_ID; |
| 23 | |
| 24 | const resolveZoneId = Effect.gen(function* () { |
| 25 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 26 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 27 | if (!zone) { |
| 28 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 29 | } |
| 30 | return zone.id; |
| 31 | }); |
| 32 | |
| 33 | // The scoped API token the test harness mints propagates eventually- |
| 34 | // consistently across Cloudflare's edge — a fresh token intermittently 403s. |
| 35 | // Ride out the blips on the test's own out-of-band calls by retrying the |
| 36 | // typed `Forbidden` error. |
| 37 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 38 | |
| 39 | const getCustomNs = (zoneId: string) => |
| 40 | zones.getCustomNameserver({ zoneId }).pipe( |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "Forbidden", |
| 43 | schedule: forbiddenRetrySchedule, |
| 44 | times: 8, |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "pins the custom nameserver toggle and leaves the zone at its baseline on destroy", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | const zoneId = yield* resolveZoneId; |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | // Baseline: the testing zone never has ACNS enabled (the account has |
| 57 | // no custom nameserver set), so `enabled: false` is the observed |
| 58 | // pre-management state the resource must capture and converge on. |
| 59 | const baseline = yield* getCustomNs(zoneId); |
| 60 | expect(baseline.enabled ?? false).toEqual(false); |
| 61 | |
| 62 | const customNs = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* Cloudflare.Zone.CustomNameservers("CustomNs", { zoneId, enabled: false }); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(customNs.zoneId).toEqual(zoneId); |
| 69 | expect(customNs.enabled).toEqual(false); |
| 70 | // The pre-management state was captured for restore-on-destroy. |
| 71 | expect(customNs.initialEnabled).toEqual(false); |
| 72 | expect(customNs.initialNsSet).toBeUndefined(); |
| 73 | |
| 74 | const live = yield* getCustomNs(zoneId); |
| 75 | expect(live.enabled ?? false).toEqual(false); |
| 76 | |
| 77 | // Re-deploy with no changes — reconcile observes the in-sync state |
| 78 | // and applies nothing. |
| 79 | const steady = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* Cloudflare.Zone.CustomNameservers("CustomNs", { zoneId, enabled: false }); |
| 82 | }), |
| 83 | ); |
| 84 | expect(steady.enabled).toEqual(false); |
| 85 | expect(steady.initialEnabled).toEqual(false); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | |
| 89 | // Destroy restored (kept) the zone's pre-management state. |
| 90 | const restored = yield* getCustomNs(zoneId); |
| 91 | expect(restored.enabled ?? false).toEqual(false); |
| 92 | }).pipe(logLevel), |
| 93 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 94 | ); |
| 95 | |
| 96 | test.provider( |
| 97 | "surfaces the typed CustomNameserverSetNotFound error when enabling without an account nameserver set", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | const zoneId = yield* resolveZoneId; |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | // The testing account has no account-level custom nameserver set, so |
| 105 | // enabling ACNS on the zone must fail with the typed gate error. |
| 106 | const error = yield* zones.putCustomNameserver({ zoneId, enabled: true }).pipe( |
| 107 | Effect.retry({ |
| 108 | while: (e) => e._tag === "Forbidden", |
| 109 | schedule: forbiddenRetrySchedule, |
| 110 | times: 8, |
| 111 | }), |
| 112 | Effect.flip, |
| 113 | ); |
| 114 | expect(error._tag).toEqual("CustomNameserverSetNotFound"); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | }).pipe(logLevel), |
| 118 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 119 | ); |
| 120 | |
| 121 | // Canonical `list()` test (zone-scoped singleton). There is no account-wide |
| 122 | // API for the per-zone ACNS toggle, so `list()` enumerates every zone via |
| 123 | // `listAllZones` and reads the singleton in each — emitting one Attributes |
| 124 | // only for zones that actively use account custom nameservers (skipping the |
| 125 | // default-disabled ones, mirroring `read`). The read-only shape assertion |
| 126 | // always runs; the testing account has no ACNS entitlement, so the array is |
| 127 | // typically empty. |
| 128 | test.provider( |
| 129 | "list enumerates zones using account custom nameservers", |
| 130 | (stack) => |
| 131 | Effect.gen(function* () { |
| 132 | yield* stack.destroy(); |
| 133 | |
| 134 | const provider = yield* Provider.findProvider(Cloudflare.Zone.CustomNameservers); |
| 135 | const all = yield* provider.list(); |
| 136 | |
| 137 | expect(Array.isArray(all)).toBe(true); |
| 138 | for (const item of all) { |
| 139 | expect(typeof item.zoneId).toBe("string"); |
| 140 | // `list()` only emits zones with ACNS actively enabled. |
| 141 | expect(item.enabled).toBe(true); |
| 142 | expect(typeof item.initialEnabled).toBe("boolean"); |
| 143 | expect(item.nsSet === undefined || typeof item.nsSet === "number").toBe(true); |
| 144 | expect(item.initialNsSet === undefined || typeof item.initialNsSet === "number").toBe(true); |
| 145 | } |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | }).pipe(logLevel), |
| 149 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 150 | ); |
| 151 | |
| 152 | // In an ACNS-entitled account, `list()` must surface the enabled zone. |
| 153 | test.provider.skipIf(!acnsZoneId)( |
| 154 | "list includes a zone with account custom nameservers enabled", |
| 155 | (stack) => |
| 156 | Effect.gen(function* () { |
| 157 | const zoneId = acnsZoneId!; |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | |
| 161 | const enabled = yield* stack.deploy( |
| 162 | Effect.gen(function* () { |
| 163 | return yield* Cloudflare.Zone.CustomNameservers("AcnsList", { zoneId, enabled: true }); |
| 164 | }), |
| 165 | ); |
| 166 | expect(enabled.enabled).toEqual(true); |
| 167 | |
| 168 | const provider = yield* Provider.findProvider(Cloudflare.Zone.CustomNameservers); |
| 169 | const all = yield* provider.list(); |
| 170 | expect(all.some((x) => x.zoneId === zoneId && x.enabled)).toBe(true); |
| 171 | |
| 172 | yield* stack.destroy(); |
| 173 | }).pipe(logLevel), |
| 174 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 175 | ); |
| 176 | |
| 177 | test.provider.skipIf(!acnsZoneId)( |
| 178 | "enables account custom nameservers and restores the baseline on destroy", |
| 179 | (stack) => |
| 180 | Effect.gen(function* () { |
| 181 | const zoneId = acnsZoneId!; |
| 182 | |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | const enabled = yield* stack.deploy( |
| 186 | Effect.gen(function* () { |
| 187 | return yield* Cloudflare.Zone.CustomNameservers("AcnsToggle", { zoneId, enabled: true }); |
| 188 | }), |
| 189 | ); |
| 190 | expect(enabled.enabled).toEqual(true); |
| 191 | expect(enabled.initialEnabled).toEqual(false); |
| 192 | |
| 193 | const live = yield* getCustomNs(zoneId); |
| 194 | expect(live.enabled).toEqual(true); |
| 195 | |
| 196 | yield* stack.destroy(); |
| 197 | |
| 198 | // Destroy restored the zone to Cloudflare-assigned nameservers. |
| 199 | const restored = yield* getCustomNs(zoneId); |
| 200 | expect(restored.enabled ?? false).toEqual(false); |
| 201 | }).pipe(logLevel), |
| 202 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 203 | ); |