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 { 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 zaraz from "@distilled.cloud/cloudflare/zaraz"; |
| 7 | import { deepEqual } from "alchemy/Diff"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 10 | import { stripNullFields, stripUndefinedFields } from "alchemy/Util/data"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 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_ZARAZ_ZONE_NAME ?? "alchemy-test-2.us"; |
| 18 | |
| 19 | // Zaraz needs no entitlement (available on all plans), but its config is a |
| 20 | // zone-wide singleton the mutating tests below change in place (and restore). |
| 21 | // They run against a dedicated zone that no other suite drives over HTTP: |
| 22 | // CLOUDFLARE_TEST_ZARAZ_ZONE_ID (+ _ZONE_NAME) when set, otherwise the |
| 23 | // standing secondary test zone `alchemy-test-3.us`. Set |
| 24 | // CLOUDFLARE_TEST_ZARAZ=0 to skip them. |
| 25 | const skipMutations = process.env.CLOUDFLARE_TEST_ZARAZ === "0"; |
| 26 | const mutationZoneName = process.env.CLOUDFLARE_TEST_ZARAZ_ZONE_ID ? zoneName : "alchemy-test-3.us"; |
| 27 | const resolveMutationZoneId = process.env.CLOUDFLARE_TEST_ZARAZ_ZONE_ID |
| 28 | ? Effect.succeed(process.env.CLOUDFLARE_TEST_ZARAZ_ZONE_ID) |
| 29 | : Effect.gen(function* () { |
| 30 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 31 | const zone = yield* findZoneByName({ accountId, name: mutationZoneName }); |
| 32 | if (!zone) { |
| 33 | return yield* Effect.die(new Error(`zone "${mutationZoneName}" not found in account`)); |
| 34 | } |
| 35 | return zone.id; |
| 36 | }); |
| 37 | // All cases mutate the same zone-wide Zaraz config singleton; run them |
| 38 | // serially so they don't corrupt each other under the global concurrent |
| 39 | // test config. |
| 40 | describe.sequential( |
| 41 | "Config", |
| 42 | { tags: ["provider:cloudflare", "provider:cloudflare:zaraz", "live"] }, |
| 43 | () => { |
| 44 | test.provider.skipIf(skipMutations)( |
| 45 | "updates and retains a zone-level Zaraz config", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | const zoneId = yield* resolveMutationZoneId; |
| 50 | |
| 51 | const original = yield* zaraz.getConfig({ zoneId }); |
| 52 | const toggledDataLayer = !original.dataLayer; |
| 53 | |
| 54 | yield* Effect.gen(function* () { |
| 55 | const updated = yield* stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | return yield* Cloudflare.Zaraz.Config("Config", { |
| 58 | zone: { zoneId, name: mutationZoneName }, |
| 59 | dataLayer: toggledDataLayer, |
| 60 | }); |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | expect(updated.zoneId).toEqual(zoneId); |
| 65 | expect(updated.dataLayer).toEqual(toggledDataLayer); |
| 66 | |
| 67 | const liveUpdated = yield* zaraz.getConfig({ zoneId }); |
| 68 | expect(liveUpdated.dataLayer).toEqual(toggledDataLayer); |
| 69 | |
| 70 | const restored = yield* stack.deploy( |
| 71 | Effect.gen(function* () { |
| 72 | return yield* Cloudflare.Zaraz.Config("Config", { |
| 73 | zone: { zoneId, name: mutationZoneName }, |
| 74 | dataLayer: original.dataLayer, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(restored.dataLayer).toEqual(original.dataLayer); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const liveRetained = yield* zaraz.getConfig({ zoneId }); |
| 84 | expect(liveRetained.dataLayer).toEqual(original.dataLayer); |
| 85 | }).pipe( |
| 86 | Effect.ensuring(zaraz.putConfig(toPutConfig(zoneId, original)).pipe(Effect.ignore)), |
| 87 | ); |
| 88 | }).pipe(logLevel), |
| 89 | { timeout: 120_000 }, |
| 90 | ); |
| 91 | |
| 92 | // Same gate as above: mutates the dedicated zone's Zaraz singleton. |
| 93 | test.provider.skipIf(skipMutations)( |
| 94 | "delete true resets Zaraz config to defaults", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | yield* stack.destroy(); |
| 98 | const zoneId = yield* resolveMutationZoneId; |
| 99 | |
| 100 | const original = yield* zaraz.getConfig({ zoneId }); |
| 101 | const originalWorkflow = yield* zaraz.getWorkflow({ zoneId }); |
| 102 | const defaults = yield* zaraz.getDefault({ zoneId }); |
| 103 | |
| 104 | yield* Effect.gen(function* () { |
| 105 | yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* Cloudflare.Zaraz.Config("Config", { |
| 108 | zone: { zoneId, name: mutationZoneName }, |
| 109 | dataLayer: !defaults.dataLayer, |
| 110 | workflow: "preview", |
| 111 | delete: true, |
| 112 | }); |
| 113 | }), |
| 114 | ); |
| 115 | |
| 116 | const liveUpdated = yield* zaraz.getConfig({ zoneId }); |
| 117 | expect(liveUpdated.dataLayer).toEqual(!defaults.dataLayer); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const liveDeleted = yield* zaraz.getConfig({ zoneId }); |
| 122 | expect(liveDeleted.dataLayer).toEqual(defaults.dataLayer); |
| 123 | const liveDeletedWorkflow = yield* zaraz.getWorkflow({ zoneId }); |
| 124 | expect(liveDeletedWorkflow).toEqual("realtime"); |
| 125 | }).pipe( |
| 126 | Effect.ensuring( |
| 127 | Effect.gen(function* () { |
| 128 | yield* zaraz.putConfig(toPutConfig(zoneId, original)).pipe(Effect.ignore); |
| 129 | yield* zaraz.putZaraz({ zoneId, workflow: originalWorkflow }).pipe(Effect.ignore); |
| 130 | }), |
| 131 | ), |
| 132 | ); |
| 133 | }).pipe(logLevel), |
| 134 | { timeout: 120_000 }, |
| 135 | ); |
| 136 | |
| 137 | // Same gate as above: mutates the dedicated zone's Zaraz singleton. |
| 138 | test.provider.skipIf(skipMutations)( |
| 139 | "updates and retains Zaraz workflow mode", |
| 140 | (stack) => |
| 141 | Effect.gen(function* () { |
| 142 | yield* stack.destroy(); |
| 143 | const zoneId = yield* resolveMutationZoneId; |
| 144 | |
| 145 | const original = yield* zaraz.getWorkflow({ zoneId }); |
| 146 | const workflow = original === "realtime" ? "preview" : "realtime"; |
| 147 | |
| 148 | yield* Effect.gen(function* () { |
| 149 | const updated = yield* stack.deploy( |
| 150 | Effect.gen(function* () { |
| 151 | return yield* Cloudflare.Zaraz.Config("Config", { |
| 152 | zone: { zoneId, name: mutationZoneName }, |
| 153 | workflow, |
| 154 | }); |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | expect(updated.workflow).toEqual(workflow); |
| 159 | |
| 160 | const liveUpdated = yield* zaraz.getWorkflow({ zoneId }); |
| 161 | expect(liveUpdated).toEqual(workflow); |
| 162 | |
| 163 | yield* stack.destroy(); |
| 164 | |
| 165 | const liveRetained = yield* zaraz.getWorkflow({ zoneId }); |
| 166 | expect(liveRetained).toEqual(workflow); |
| 167 | }).pipe( |
| 168 | Effect.ensuring(zaraz.putZaraz({ zoneId, workflow: original }).pipe(Effect.ignore)), |
| 169 | ); |
| 170 | }).pipe(logLevel), |
| 171 | { timeout: 120_000 }, |
| 172 | ); |
| 173 | |
| 174 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 175 | // API for the per-zone Zaraz config, so `list()` enumerates every zone via |
| 176 | // `listAllZones` and reads the singleton in each. Read-only — asserts the |
| 177 | // result is well-typed, non-empty, and contains the standing test zone. |
| 178 | test.provider( |
| 179 | "list enumerates Zaraz config across all zones", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 183 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 184 | if (!zone) { |
| 185 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 186 | } |
| 187 | |
| 188 | yield* stack.destroy(); |
| 189 | |
| 190 | const provider = yield* Provider.findProvider(Cloudflare.Zaraz.Config); |
| 191 | const all = yield* provider.list(); |
| 192 | |
| 193 | expect(all.length).toBeGreaterThan(0); |
| 194 | const row = all.find((c) => c.zoneId === zone.id); |
| 195 | expect(row).toBeDefined(); |
| 196 | expect(typeof row!.zoneId).toBe("string"); |
| 197 | expect(typeof row!.dataLayer).toBe("boolean"); |
| 198 | expect(typeof row!.zarazVersion).toBe("number"); |
| 199 | expect(row!.settings).toBeDefined(); |
| 200 | expect(row!.workflow).toBeDefined(); |
| 201 | |
| 202 | yield* stack.destroy(); |
| 203 | }).pipe(logLevel), |
| 204 | { tags: ["provider:cloudflare:zone"], timeout: 120_000 }, |
| 205 | ); |
| 206 | |
| 207 | // Ungated like the `list()` test: the deploy passes NO mutable props, so |
| 208 | // reconcile observes the zone config, computes an identical desired config, |
| 209 | // and skips putConfig/putZaraz entirely — the standing zone's singleton is |
| 210 | // never mutated (capture-and-restore below is just a safety net). |
| 211 | test.provider( |
| 212 | "recovers a half-created config whose creating-state lost Output-valued props (#736)", |
| 213 | (stack) => |
| 214 | Effect.gen(function* () { |
| 215 | yield* stack.destroy(); |
| 216 | |
| 217 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 218 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 219 | if (!zone) { |
| 220 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 221 | } |
| 222 | |
| 223 | const original = yield* zaraz.getConfig({ zoneId: zone.id }); |
| 224 | |
| 225 | yield* Effect.gen(function* () { |
| 226 | const deployConfig = () => |
| 227 | stack.deploy( |
| 228 | Effect.gen(function* () { |
| 229 | // No mutable props: reconcile is a pure observe (no put). |
| 230 | return yield* Cloudflare.Zaraz.Config("Config", { |
| 231 | zone: { zoneId: zone.id, name: zoneName }, |
| 232 | }); |
| 233 | }), |
| 234 | ); |
| 235 | |
| 236 | const created = yield* deployConfig(); |
| 237 | expect(created.zoneId).toEqual(zone.id); |
| 238 | |
| 239 | // Rewrite the persisted row into the wedged shape an interrupted |
| 240 | // deploy leaves behind: `creating`, no attributes, and the |
| 241 | // Output-valued `zone` prop lost in the round-trip (#736). |
| 242 | const state = yield* yield* State; |
| 243 | const stage = stack.stage; |
| 244 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 245 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 246 | state |
| 247 | .get({ stack: stack.name, stage, fqn }) |
| 248 | .pipe(Effect.map((row) => ({ fqn, row }))), |
| 249 | ); |
| 250 | const wedged = rows.find( |
| 251 | (r): r is { fqn: string; row: ResourceState } => |
| 252 | isResourceState(r.row) && r.row.resourceType === "Cloudflare.Zaraz.Config", |
| 253 | ); |
| 254 | if (!wedged) { |
| 255 | return yield* Effect.die( |
| 256 | new Error("no Cloudflare.Zaraz.Config state row found after deploy"), |
| 257 | ); |
| 258 | } |
| 259 | yield* state.set({ |
| 260 | stack: stack.name, |
| 261 | stage, |
| 262 | fqn: wedged.fqn, |
| 263 | value: { |
| 264 | ...wedged.row, |
| 265 | status: "creating", |
| 266 | attr: undefined, |
| 267 | props: { ...wedged.row.props, zone: undefined }, |
| 268 | }, |
| 269 | }); |
| 270 | |
| 271 | // Before the fix this crashed in read's recovery path with |
| 272 | // `TypeError: undefined is not an object (evaluating 'zone.name')`. |
| 273 | const recovered = yield* deployConfig(); |
| 274 | expect(recovered.zoneId).toEqual(created.zoneId); |
| 275 | expect(recovered.dataLayer).toEqual(created.dataLayer); |
| 276 | expect(recovered.workflow).toEqual(created.workflow); |
| 277 | |
| 278 | yield* stack.destroy(); |
| 279 | }).pipe( |
| 280 | Effect.ensuring( |
| 281 | // Safety net: restore only if something actually changed, so the |
| 282 | // happy path leaves the standing zone's singleton untouched. |
| 283 | Effect.gen(function* () { |
| 284 | const current = yield* zaraz.getConfig({ zoneId: zone.id }); |
| 285 | if (!deepEqual(current, original)) { |
| 286 | yield* zaraz.putConfig(toPutConfig(zone.id, original)); |
| 287 | } |
| 288 | }).pipe(Effect.ignore), |
| 289 | ), |
| 290 | ); |
| 291 | }).pipe(logLevel), |
| 292 | { tags: ["provider:cloudflare:zone"], timeout: 240_000 }, |
| 293 | ); |
| 294 | }, |
| 295 | ); |
| 296 | |
| 297 | type ConfigResponse = zaraz.GetConfigResponse | zaraz.PutConfigResponse; |
| 298 | |
| 299 | const toPutConfig = (zoneId: string, config: ConfigResponse): zaraz.PutConfigRequest => |
| 300 | stripUndefinedFields({ |
| 301 | zoneId, |
| 302 | dataLayer: config.dataLayer, |
| 303 | debugKey: config.debugKey, |
| 304 | settings: stripNullFields(config.settings), |
| 305 | tools: config.tools, |
| 306 | triggers: config.triggers, |
| 307 | variables: config.variables, |
| 308 | zarazVersion: config.zarazVersion, |
| 309 | analytics: config.analytics ? stripNullFields(config.analytics) : undefined, |
| 310 | consent: config.consent ? stripNullFields(config.consent) : undefined, |
| 311 | historyChange: config.historyChange ?? undefined, |
| 312 | }) as zaraz.PutConfigRequest; |