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 { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as schemaValidation from "@distilled.cloud/cloudflare/schema-validation"; |
| 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 | const resolveZoneId = Effect.gen(function* () { |
| 19 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 20 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 21 | if (!zone) { |
| 22 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 23 | } |
| 24 | return zone.id; |
| 25 | }); |
| 26 | |
| 27 | // Retry 403 blips while the harness-minted scoped token propagates. |
| 28 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 29 | |
| 30 | const getSettingOob = (zoneId: string) => |
| 31 | schemaValidation.getSetting({ zoneId }).pipe( |
| 32 | Effect.retry({ |
| 33 | while: (e) => e._tag === "Forbidden", |
| 34 | schedule: forbiddenRetrySchedule, |
| 35 | times: 8, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | // Normalize the zone to the Cloudflare default so each run starts from the |
| 40 | // same cloud state regardless of what a previous run left behind. |
| 41 | const setBaseline = (zoneId: string) => |
| 42 | schemaValidation |
| 43 | .putSetting({ |
| 44 | zoneId, |
| 45 | validationDefaultMitigationAction: "none", |
| 46 | validationOverrideMitigationAction: null, |
| 47 | }) |
| 48 | .pipe( |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "Forbidden", |
| 51 | schedule: forbiddenRetrySchedule, |
| 52 | times: 8, |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | test.provider( |
| 57 | "pins zone settings, updates in place, and restores the baseline on destroy", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | const zoneId = yield* resolveZoneId; |
| 61 | |
| 62 | yield* stack.destroy(); |
| 63 | yield* setBaseline(zoneId); |
| 64 | |
| 65 | const created = yield* stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | return yield* Cloudflare.SchemaValidation.Settings("Settings", { |
| 68 | zoneId, |
| 69 | validationDefaultMitigationAction: "block", |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(created.zoneId).toEqual(zoneId); |
| 75 | expect(created.validationDefaultMitigationAction).toEqual("block"); |
| 76 | expect(created.validationOverrideMitigationAction).toEqual(null); |
| 77 | // The pre-management state was captured for restore-on-destroy. |
| 78 | expect(created.initialDefaultMitigationAction).toEqual("none"); |
| 79 | expect(created.initialOverrideMitigationAction).toEqual(null); |
| 80 | |
| 81 | const live = yield* getSettingOob(zoneId); |
| 82 | expect(live.validationDefaultMitigationAction).toEqual("block"); |
| 83 | |
| 84 | // Update in place — flip on the zone-wide kill switch. The captured |
| 85 | // initial state survives the update. |
| 86 | const updated = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* Cloudflare.SchemaValidation.Settings("Settings", { |
| 89 | zoneId, |
| 90 | validationDefaultMitigationAction: "block", |
| 91 | validationOverrideMitigationAction: "none", |
| 92 | }); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(updated.validationDefaultMitigationAction).toEqual("block"); |
| 97 | expect(updated.validationOverrideMitigationAction).toEqual("none"); |
| 98 | expect(updated.initialDefaultMitigationAction).toEqual("none"); |
| 99 | expect(updated.initialOverrideMitigationAction).toEqual(null); |
| 100 | |
| 101 | const liveUpdated = yield* getSettingOob(zoneId); |
| 102 | expect(liveUpdated.validationOverrideMitigationAction).toEqual("none"); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | // Destroy restored the values the zone had before we managed them. |
| 107 | const restored = yield* getSettingOob(zoneId); |
| 108 | expect(restored.validationDefaultMitigationAction).toEqual("none"); |
| 109 | expect(restored.validationOverrideMitigationAction ?? null).toEqual(null); |
| 110 | }).pipe(logLevel), |
| 111 | { |
| 112 | tags: [ |
| 113 | "provider:cloudflare", |
| 114 | "provider:cloudflare:schemavalidation", |
| 115 | "provider:cloudflare:zone", |
| 116 | "live", |
| 117 | ], |
| 118 | timeout: 120_000, |
| 119 | }, |
| 120 | ); |
| 121 | |
| 122 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 123 | // API for this per-zone setting, so `list()` enumerates every zone via |
| 124 | // `listAllZones` and reads the singleton in each. Assert the result is |
| 125 | // non-empty and contains the standing test zone. |
| 126 | test.provider( |
| 127 | "list enumerates the schema validation settings across all zones", |
| 128 | (stack) => |
| 129 | Effect.gen(function* () { |
| 130 | const zoneId = yield* resolveZoneId; |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | |
| 134 | const provider = yield* Provider.findProvider(Cloudflare.SchemaValidation.Settings); |
| 135 | const all = yield* provider.list(); |
| 136 | |
| 137 | expect(all.length).toBeGreaterThan(0); |
| 138 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 139 | |
| 140 | // The singleton always exists on every zone, so nothing was deployed; |
| 141 | // keep the destroy bookends so the harness state stays clean. |
| 142 | yield* stack.destroy(); |
| 143 | }).pipe(logLevel), |
| 144 | { |
| 145 | tags: [ |
| 146 | "provider:cloudflare", |
| 147 | "provider:cloudflare:schemavalidation", |
| 148 | "provider:cloudflare:zone", |
| 149 | "live", |
| 150 | ], |
| 151 | timeout: 120_000, |
| 152 | }, |
| 153 | ); |