The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | // Ride out fresh-token 403 blips on out-of-band calls. |
| 15 | const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) => |
| 16 | eff.pipe( |
| 17 | Effect.retry({ |
| 18 | while: (e) => e._tag === "Forbidden", |
| 19 | schedule: Schedule.exponential("500 millis"), |
| 20 | times: 8, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | const getSettings = (accountId: string) => retryForbidden(dns.getSettingAccount({ accountId })); |
| 25 | |
| 26 | // Baselines for the (entitlement-free) zone-default fields these tests |
| 27 | // manage. NOTE: `zoneDefaults.nsTtl`, custom SOA values, and custom zone |
| 28 | // modes are entitlement-gated on the testing account (code 1003 — the |
| 29 | // typed `DnsSettingNotAvailable`), and `zoneDefaults.flattenAllCnames` |
| 30 | // is rejected outright ("Setting an account-level default for CNAME |
| 31 | // flattening is not supported"), so the tests exercise `multiProvider` |
| 32 | // and `secondaryOverrides` instead. These only affect defaults for |
| 33 | // FUTURE zones, so mutating them is safe on a shared test account. |
| 34 | const BASELINE_MULTI_PROVIDER = false; |
| 35 | const BASELINE_SECONDARY_OVERRIDES = false; |
| 36 | |
| 37 | // Normalize the singleton to a known baseline so each run starts from |
| 38 | // the same cloud state regardless of what a previous (possibly |
| 39 | // interrupted) run left behind. |
| 40 | const normalizeBaseline = (accountId: string) => |
| 41 | Effect.gen(function* () { |
| 42 | const observed = yield* getSettings(accountId); |
| 43 | if ( |
| 44 | observed.zoneDefaults.multiProvider === BASELINE_MULTI_PROVIDER && |
| 45 | observed.zoneDefaults.secondaryOverrides === BASELINE_SECONDARY_OVERRIDES |
| 46 | ) { |
| 47 | return; |
| 48 | } |
| 49 | yield* retryForbidden( |
| 50 | dns.patchSettingAccount({ |
| 51 | accountId, |
| 52 | zoneDefaults: { |
| 53 | multiProvider: BASELINE_MULTI_PROVIDER, |
| 54 | secondaryOverrides: BASELINE_SECONDARY_OVERRIDES, |
| 55 | }, |
| 56 | }), |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | describe.sequential( |
| 61 | "AccountSettings", |
| 62 | { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"] }, |
| 63 | () => { |
| 64 | test.provider( |
| 65 | "list returns the account's DNS settings singleton", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 69 | |
| 70 | // Account singleton — read-only enumeration, no mutation. |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const provider = yield* Provider.findProvider(Cloudflare.DNS.AccountDnsSettings); |
| 74 | const all = yield* provider.list(); |
| 75 | |
| 76 | // Exactly the one account-wide settings object, fully typed. |
| 77 | expect(all.length).toEqual(1); |
| 78 | const [settings] = all; |
| 79 | expect(settings.accountId).toEqual(accountId); |
| 80 | expect(typeof settings.enforceDnsOnly).toEqual("boolean"); |
| 81 | expect(typeof settings.zoneDefaults.multiProvider).toEqual("boolean"); |
| 82 | // `read` mirror: nothing managed yet, snapshot is its own baseline. |
| 83 | expect(settings.managedKeys).toEqual([]); |
| 84 | expect(settings.initialSettings.zoneDefaults.multiProvider).toEqual( |
| 85 | settings.zoneDefaults.multiProvider, |
| 86 | ); |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | }).pipe(logLevel), |
| 90 | { timeout: 120_000 }, |
| 91 | ); |
| 92 | |
| 93 | test.provider( |
| 94 | "pins a zone default and restores the pre-management value on destroy", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | yield* normalizeBaseline(accountId); |
| 101 | |
| 102 | const settings = yield* stack.deploy( |
| 103 | Cloudflare.DNS.AccountDnsSettings("AccountDns", { |
| 104 | zoneDefaults: { multiProvider: true }, |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(settings.accountId).toEqual(accountId); |
| 109 | expect(settings.zoneDefaults.multiProvider).toEqual(true); |
| 110 | // The pre-management snapshot was captured for restore-on-destroy. |
| 111 | expect(settings.initialSettings.zoneDefaults.multiProvider).toEqual( |
| 112 | BASELINE_MULTI_PROVIDER, |
| 113 | ); |
| 114 | expect(settings.managedKeys).toContain("zoneDefaults.multiProvider"); |
| 115 | |
| 116 | // Out-of-band verify via the SDK. |
| 117 | const live = yield* getSettings(accountId); |
| 118 | expect(live.zoneDefaults.multiProvider).toEqual(true); |
| 119 | |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | // Destroy restored the managed field to its pre-management value. |
| 123 | const restored = yield* getSettings(accountId); |
| 124 | expect(restored.zoneDefaults.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 125 | |
| 126 | // Re-running destroy is idempotent (nothing left to restore). |
| 127 | yield* stack.destroy(); |
| 128 | const still = yield* getSettings(accountId); |
| 129 | expect(still.zoneDefaults.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 130 | }).pipe(logLevel), |
| 131 | { timeout: 300_000 }, |
| 132 | ); |
| 133 | |
| 134 | test.provider( |
| 135 | "updates in place, unions managedKeys, restores all managed fields", |
| 136 | (stack) => |
| 137 | Effect.gen(function* () { |
| 138 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 139 | |
| 140 | yield* stack.destroy(); |
| 141 | yield* normalizeBaseline(accountId); |
| 142 | |
| 143 | const initial = yield* stack.deploy( |
| 144 | Cloudflare.DNS.AccountDnsSettings("AccountDns", { |
| 145 | zoneDefaults: { multiProvider: true }, |
| 146 | }), |
| 147 | ); |
| 148 | expect(initial.zoneDefaults.multiProvider).toEqual(true); |
| 149 | expect(initial.managedKeys).toContain("zoneDefaults.multiProvider"); |
| 150 | |
| 151 | // Same singleton patched in place — a second managed field joins; |
| 152 | // the original snapshot survives the update. |
| 153 | const updated = yield* stack.deploy( |
| 154 | Cloudflare.DNS.AccountDnsSettings("AccountDns", { |
| 155 | zoneDefaults: { multiProvider: true, secondaryOverrides: true }, |
| 156 | }), |
| 157 | ); |
| 158 | expect(updated.zoneDefaults.multiProvider).toEqual(true); |
| 159 | expect(updated.zoneDefaults.secondaryOverrides).toEqual(true); |
| 160 | expect(updated.initialSettings.zoneDefaults.multiProvider).toEqual( |
| 161 | BASELINE_MULTI_PROVIDER, |
| 162 | ); |
| 163 | expect(updated.initialSettings.zoneDefaults.secondaryOverrides).toEqual( |
| 164 | BASELINE_SECONDARY_OVERRIDES, |
| 165 | ); |
| 166 | expect(updated.managedKeys).toContain("zoneDefaults.multiProvider"); |
| 167 | expect(updated.managedKeys).toContain("zoneDefaults.secondaryOverrides"); |
| 168 | |
| 169 | const live = yield* getSettings(accountId); |
| 170 | expect(live.zoneDefaults.multiProvider).toEqual(true); |
| 171 | expect(live.zoneDefaults.secondaryOverrides).toEqual(true); |
| 172 | |
| 173 | // Drop `multiProvider` from props — the key stays managed (union |
| 174 | // across all reconciles) so destroy still restores it. |
| 175 | const dropped = yield* stack.deploy( |
| 176 | Cloudflare.DNS.AccountDnsSettings("AccountDns", { |
| 177 | zoneDefaults: { secondaryOverrides: true }, |
| 178 | }), |
| 179 | ); |
| 180 | expect(dropped.managedKeys).toContain("zoneDefaults.multiProvider"); |
| 181 | expect(dropped.managedKeys).toContain("zoneDefaults.secondaryOverrides"); |
| 182 | |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | // Both managed fields were restored to their pre-management values. |
| 186 | const restored = yield* getSettings(accountId); |
| 187 | expect(restored.zoneDefaults.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 188 | expect(restored.zoneDefaults.secondaryOverrides).toEqual(BASELINE_SECONDARY_OVERRIDES); |
| 189 | }).pipe(logLevel), |
| 190 | { timeout: 300_000 }, |
| 191 | ); |
| 192 | |
| 193 | // Regression for #1982: Cloudflare stops accepting/returning |
| 194 | // `foundation_dns` on 2026-11-23. Default nameservers (including |
| 195 | // Advanced Nameservers, `cloudflare.advanced`) are set through |
| 196 | // `zoneDefaults.nameservers.type`; the resource never sends or snapshots |
| 197 | // the removed boolean. Every non-default nameserver type is |
| 198 | // entitlement-gated on the testing account (`DnsSettingNotAvailable`), |
| 199 | // so the type is pinned to its observed value next to a field that |
| 200 | // forces a real PATCH. |
| 201 | test.provider( |
| 202 | "manages zoneDefaults.nameservers.type without foundationDns (#1982)", |
| 203 | (stack) => |
| 204 | Effect.gen(function* () { |
| 205 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 206 | |
| 207 | yield* stack.destroy(); |
| 208 | yield* normalizeBaseline(accountId); |
| 209 | const baseline = (yield* getSettings(accountId)).zoneDefaults.nameservers.type; |
| 210 | |
| 211 | const settings = yield* stack.deploy( |
| 212 | Cloudflare.DNS.AccountDnsSettings("AccountDns", { |
| 213 | zoneDefaults: { nameservers: { type: baseline }, multiProvider: true }, |
| 214 | }), |
| 215 | ); |
| 216 | expect(settings.zoneDefaults.nameservers.type).toEqual(baseline); |
| 217 | expect(settings.zoneDefaults.multiProvider).toEqual(true); |
| 218 | expect(settings.managedKeys).toContain("zoneDefaults.nameservers"); |
| 219 | expect(settings.managedKeys).not.toContain("zoneDefaults.foundationDns"); |
| 220 | expect("foundationDns" in settings.zoneDefaults).toBe(false); |
| 221 | expect("foundationDns" in settings.initialSettings.zoneDefaults).toBe(false); |
| 222 | |
| 223 | const live = yield* getSettings(accountId); |
| 224 | expect(live.zoneDefaults.nameservers.type).toEqual(baseline); |
| 225 | expect(live.zoneDefaults.multiProvider).toEqual(true); |
| 226 | |
| 227 | yield* stack.destroy(); |
| 228 | |
| 229 | const restored = yield* getSettings(accountId); |
| 230 | expect(restored.zoneDefaults.nameservers.type).toEqual(baseline); |
| 231 | expect(restored.zoneDefaults.multiProvider).toEqual(BASELINE_MULTI_PROVIDER); |
| 232 | }).pipe(logLevel), |
| 233 | { timeout: 300_000 }, |
| 234 | ); |
| 235 | }, |
| 236 | ); |