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