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 { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 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 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 16 | |
| 17 | const resolveZoneId = Effect.gen(function* () { |
| 18 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 19 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 20 | if (!zone) { |
| 21 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 22 | } |
| 23 | return zone.id; |
| 24 | }); |
| 25 | |
| 26 | // Ride out fresh-token 403 blips on out-of-band calls by retrying the typed |
| 27 | // `Forbidden` error (added to the dns ops' unions via distilled patches). |
| 28 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 29 | |
| 30 | const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) => |
| 31 | eff.pipe( |
| 32 | Effect.retry({ |
| 33 | while: (e) => e._tag === "Forbidden", |
| 34 | schedule: forbiddenRetrySchedule, |
| 35 | times: 8, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | const getSettings = (zoneId: string) => retryForbidden(dns.getSettingZone({ zoneId })); |
| 40 | |
| 41 | // Baselines for the (entitlement-free) fields these tests manage. |
| 42 | // NOTE: `nsTtl` and custom SOA records are entitlement-gated on the testing |
| 43 | // account ("Custom nameserver TTLs / Custom SOA records are not available to |
| 44 | // this account or zone"), so the tests exercise `flattenAllCnames` and |
| 45 | // `multiProvider` instead. |
| 46 | const BASELINE_FLATTEN_ALL_CNAMES = false; |
| 47 | const BASELINE_MULTI_PROVIDER = false; |
| 48 | |
| 49 | // Normalize the singleton to a known baseline so each run starts from the |
| 50 | // same cloud state regardless of what a previous (possibly interrupted) run |
| 51 | // left behind. Patches only when something actually drifted. |
| 52 | const normalizeBaseline = (zoneId: string) => |
| 53 | Effect.gen(function* () { |
| 54 | const observed = yield* getSettings(zoneId); |
| 55 | if ( |
| 56 | observed.flattenAllCnames === BASELINE_FLATTEN_ALL_CNAMES && |
| 57 | observed.multiProvider === BASELINE_MULTI_PROVIDER |
| 58 | ) { |
| 59 | return; |
| 60 | } |
| 61 | yield* retryForbidden( |
| 62 | dns.patchSettingZone({ |
| 63 | zoneId, |
| 64 | flattenAllCnames: BASELINE_FLATTEN_ALL_CNAMES, |
| 65 | multiProvider: BASELINE_MULTI_PROVIDER, |
| 66 | }), |
| 67 | ); |
| 68 | }); |
| 69 | |
| 70 | describe.sequential( |
| 71 | "ZoneSettings", |
| 72 | { tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"] }, |
| 73 | () => { |
| 74 | test.provider( |
| 75 | "pins flattenAllCnames and restores the pre-management value on destroy", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | const zoneId = yield* resolveZoneId; |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | yield* normalizeBaseline(zoneId); |
| 82 | |
| 83 | const settings = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 86 | zoneId, |
| 87 | flattenAllCnames: true, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(settings.zoneId).toEqual(zoneId); |
| 93 | expect(settings.flattenAllCnames).toEqual(true); |
| 94 | // The pre-management snapshot was captured for restore-on-destroy. |
| 95 | expect(settings.initialSettings.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 96 | expect(settings.managedKeys).toContain("flattenAllCnames"); |
| 97 | |
| 98 | // Out-of-band verify via the SDK. |
| 99 | const live = yield* getSettings(zoneId); |
| 100 | expect(live.flattenAllCnames).toEqual(true); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | // Destroy restored the managed field to its pre-management value. |
| 105 | const restored = yield* getSettings(zoneId); |
| 106 | expect(restored.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 107 | |
| 108 | // Re-running destroy is idempotent (nothing left to restore). |
| 109 | yield* stack.destroy(); |
| 110 | const still = yield* getSettings(zoneId); |
| 111 | expect(still.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 112 | }).pipe(logLevel), |
| 113 | { timeout: 300_000 }, |
| 114 | ); |
| 115 | |
| 116 | test.provider( |
| 117 | "updates in place, unions managedKeys, restores all managed fields", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | const zoneId = yield* resolveZoneId; |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | yield* normalizeBaseline(zoneId); |
| 124 | |
| 125 | const initial = yield* stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 128 | zoneId, |
| 129 | multiProvider: true, |
| 130 | }); |
| 131 | }), |
| 132 | ); |
| 133 | expect(initial.multiProvider).toEqual(true); |
| 134 | expect(initial.initialSettings.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 135 | expect(initial.managedKeys).toContain("multiProvider"); |
| 136 | |
| 137 | // Same singleton patched in place — new value plus a second managed |
| 138 | // field; the original snapshot survives the update. |
| 139 | const updated = yield* stack.deploy( |
| 140 | Effect.gen(function* () { |
| 141 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 142 | zoneId, |
| 143 | multiProvider: true, |
| 144 | flattenAllCnames: true, |
| 145 | }); |
| 146 | }), |
| 147 | ); |
| 148 | expect(updated.zoneId).toEqual(zoneId); |
| 149 | expect(updated.multiProvider).toEqual(true); |
| 150 | expect(updated.flattenAllCnames).toEqual(true); |
| 151 | expect(updated.initialSettings.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 152 | expect(updated.initialSettings.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 153 | expect(updated.managedKeys).toContain("multiProvider"); |
| 154 | expect(updated.managedKeys).toContain("flattenAllCnames"); |
| 155 | |
| 156 | const live = yield* getSettings(zoneId); |
| 157 | expect(live.multiProvider).toEqual(true); |
| 158 | expect(live.flattenAllCnames).toEqual(true); |
| 159 | |
| 160 | // Drop `multiProvider` from props — the key stays managed (union |
| 161 | // across all reconciles) so destroy still restores it. |
| 162 | const dropped = yield* stack.deploy( |
| 163 | Effect.gen(function* () { |
| 164 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 165 | zoneId, |
| 166 | flattenAllCnames: true, |
| 167 | }); |
| 168 | }), |
| 169 | ); |
| 170 | expect(dropped.managedKeys).toContain("multiProvider"); |
| 171 | expect(dropped.managedKeys).toContain("flattenAllCnames"); |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | // Both managed fields were restored to their pre-management values. |
| 176 | const restored = yield* getSettings(zoneId); |
| 177 | expect(restored.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 178 | expect(restored.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 179 | }).pipe(logLevel), |
| 180 | { timeout: 300_000 }, |
| 181 | ); |
| 182 | |
| 183 | // Regression for #1982: Cloudflare stops accepting/returning |
| 184 | // `foundation_dns` on 2026-11-23. Advanced Nameservers are expressed via |
| 185 | // `nameservers.type`, and the resource never sends or snapshots the |
| 186 | // removed boolean. The nameserver type is pinned to its observed value — |
| 187 | // flipping the standing test zone's nameservers would break delegation. |
| 188 | test.provider( |
| 189 | "manages nameservers.type without foundationDns (#1982)", |
| 190 | (stack) => |
| 191 | Effect.gen(function* () { |
| 192 | const zoneId = yield* resolveZoneId; |
| 193 | |
| 194 | yield* stack.destroy(); |
| 195 | yield* normalizeBaseline(zoneId); |
| 196 | const baseline = yield* getSettings(zoneId); |
| 197 | |
| 198 | const settings = yield* stack.deploy( |
| 199 | Effect.gen(function* () { |
| 200 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 201 | zoneId, |
| 202 | nameservers: { type: baseline.nameservers.type }, |
| 203 | flattenAllCnames: true, |
| 204 | }); |
| 205 | }), |
| 206 | ); |
| 207 | |
| 208 | expect(settings.nameservers.type).toEqual(baseline.nameservers.type); |
| 209 | expect(settings.flattenAllCnames).toEqual(true); |
| 210 | expect(settings.managedKeys).toContain("nameservers"); |
| 211 | expect(settings.managedKeys).not.toContain("foundationDns"); |
| 212 | expect("foundationDns" in settings).toBe(false); |
| 213 | expect("foundationDns" in settings.initialSettings).toBe(false); |
| 214 | |
| 215 | const live = yield* getSettings(zoneId); |
| 216 | expect(live.nameservers.type).toEqual(baseline.nameservers.type); |
| 217 | expect(live.flattenAllCnames).toEqual(true); |
| 218 | |
| 219 | // A redeploy with nothing changed converges without a PATCH. |
| 220 | const redeployed = yield* stack.deploy( |
| 221 | Effect.gen(function* () { |
| 222 | return yield* Cloudflare.DNS.ZoneDnsSettings("DnsSettings", { |
| 223 | zoneId, |
| 224 | nameservers: { type: baseline.nameservers.type }, |
| 225 | flattenAllCnames: true, |
| 226 | }); |
| 227 | }), |
| 228 | ); |
| 229 | expect(redeployed.flattenAllCnames).toEqual(true); |
| 230 | |
| 231 | yield* stack.destroy(); |
| 232 | |
| 233 | const restored = yield* getSettings(zoneId); |
| 234 | expect(restored.flattenAllCnames).toEqual(BASELINE_FLATTEN_ALL_CNAMES); |
| 235 | expect(restored.nameservers.type).toEqual(baseline.nameservers.type); |
| 236 | }).pipe(logLevel), |
| 237 | { timeout: 300_000 }, |
| 238 | ); |
| 239 | |
| 240 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 241 | // API for this per-zone settings object, so `list()` enumerates every zone |
| 242 | // via `listAllZones` and reads the singleton in each. Assert the result is |
| 243 | // non-empty and contains the standing test zone. |
| 244 | test.provider("list enumerates DNS settings across all zones", (stack) => |
| 245 | Effect.gen(function* () { |
| 246 | const zoneId = yield* resolveZoneId; |
| 247 | |
| 248 | const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneDnsSettings); |
| 249 | const all = yield* provider.list(); |
| 250 | |
| 251 | expect(all.length).toBeGreaterThan(0); |
| 252 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 253 | |
| 254 | // `stack` is unused here (the singleton always exists on every zone), |
| 255 | // but keep the destroy bookend so the harness state stays clean. |
| 256 | yield* stack.destroy(); |
| 257 | }).pipe(logLevel), |
| 258 | ); |
| 259 | }, |
| 260 | ); |