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 contentScanning from "@distilled.cloud/cloudflare/content-scanning"; |
| 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 | // WAF Content Scanning is an Enterprise paid add-on. On the testing |
| 18 | // account's zone, reading the status works (it reports "disabled"), but |
| 19 | // enabling fails with "not entitled to use the phase |
| 20 | // http_request_firewall_scan_file", surfaced as the typed |
| 21 | // `ContentScanningNotEntitled` error. The enable lifecycle test below is |
| 22 | // gated behind an entitled zone id supplied via env. |
| 23 | const entitledZoneId = process.env.CLOUDFLARE_TEST_CONTENT_SCANNING_ZONE_ID; |
| 24 | |
| 25 | const resolveZoneId = Effect.gen(function* () { |
| 26 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 27 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 28 | if (!zone) { |
| 29 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 30 | } |
| 31 | return zone.id; |
| 32 | }); |
| 33 | |
| 34 | // The scoped API token the test harness mints propagates eventually- |
| 35 | // consistently across Cloudflare's edge — ride out 403 blips on the test's |
| 36 | // own out-of-band calls by retrying the typed `Forbidden` error. |
| 37 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 38 | |
| 39 | const getStatus = (zoneId: string) => |
| 40 | contentScanning.getContentScanning({ zoneId }).pipe( |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "Forbidden", |
| 43 | schedule: forbiddenRetrySchedule, |
| 44 | times: 8, |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | const setBaseline = (zoneId: string, value: "enabled" | "disabled") => |
| 49 | contentScanning.putContentScanning({ zoneId, value }).pipe( |
| 50 | Effect.retry({ |
| 51 | while: (e) => e._tag === "Forbidden", |
| 52 | schedule: forbiddenRetrySchedule, |
| 53 | times: 8, |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | // Both cases mutate the same zone-level content-scanning enablement singleton; run them serially so they don't corrupt each other's captured `initialValue` under the global concurrent test config. |
| 58 | describe.sequential( |
| 59 | "ContentScanning", |
| 60 | { tags: ["provider:cloudflare", "provider:cloudflare:contentscanning", "live"] }, |
| 61 | () => { |
| 62 | test.provider( |
| 63 | "surfaces the typed ContentScanningNotEntitled error on unentitled zones", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const zoneId = yield* resolveZoneId; |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | // The standard testing zone lacks the Content Scanning add-on — the |
| 71 | // distilled enable call must fail with the typed entitlement tag. |
| 72 | const error = yield* contentScanning |
| 73 | .putContentScanning({ zoneId, value: "enabled" }) |
| 74 | .pipe( |
| 75 | Effect.retry({ |
| 76 | while: (e) => e._tag === "Forbidden", |
| 77 | schedule: forbiddenRetrySchedule, |
| 78 | times: 8, |
| 79 | }), |
| 80 | Effect.flip, |
| 81 | ); |
| 82 | expect(error._tag).toEqual("ContentScanningNotEntitled"); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }).pipe(logLevel), |
| 86 | { tags: ["provider:cloudflare:zone"] }, |
| 87 | ); |
| 88 | |
| 89 | test.provider( |
| 90 | "pins Content Scanning off on an unentitled zone and destroys cleanly", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | const zoneId = yield* resolveZoneId; |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | // Known baseline: an unentitled zone reports "disabled" (the PUT to |
| 97 | // "disabled" succeeds even without the add-on). |
| 98 | yield* setBaseline(zoneId, "disabled"); |
| 99 | |
| 100 | const scanning = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", { |
| 103 | zoneId, |
| 104 | enabled: false, |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(scanning.zoneId).toEqual(zoneId); |
| 110 | expect(scanning.enabled).toEqual(false); |
| 111 | // The pre-management status was captured for restore-on-destroy. |
| 112 | expect(scanning.initialValue).toEqual("disabled"); |
| 113 | |
| 114 | // Out-of-band verification via the distilled API. |
| 115 | const live = yield* getStatus(zoneId); |
| 116 | expect(live.value).toEqual("disabled"); |
| 117 | |
| 118 | // Re-deploying the same desired state is a pure no-op (no PUT) — |
| 119 | // on an unentitled zone any write of "enabled" would fail, so this |
| 120 | // also proves reconcile only calls the API on a delta. |
| 121 | const again = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", { |
| 124 | zoneId, |
| 125 | enabled: false, |
| 126 | }); |
| 127 | }), |
| 128 | ); |
| 129 | expect(again.enabled).toEqual(false); |
| 130 | expect(again.initialValue).toEqual("disabled"); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | |
| 134 | // Destroy restored (kept) the pre-management status. |
| 135 | const restored = yield* getStatus(zoneId); |
| 136 | expect(restored.value).toEqual("disabled"); |
| 137 | }).pipe(logLevel), |
| 138 | { tags: ["provider:cloudflare:zone"] }, |
| 139 | ); |
| 140 | |
| 141 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 142 | // API for this per-zone setting, so `list()` enumerates every zone via |
| 143 | // `listAllZones` and reads the singleton in each. Reading the status works |
| 144 | // on every plan (only enabling is entitlement-gated), so this stays an |
| 145 | // ungated read-only assertion. Assert the result is non-empty and contains |
| 146 | // the standing test zone. |
| 147 | test.provider( |
| 148 | "list enumerates the status across all zones", |
| 149 | (stack) => |
| 150 | Effect.gen(function* () { |
| 151 | const zoneId = yield* resolveZoneId; |
| 152 | |
| 153 | const provider = yield* Provider.findProvider(Cloudflare.ContentScanning.ContentScanning); |
| 154 | // The freshly-minted scoped token propagates eventually-consistently, so |
| 155 | // the account-wide enumeration intermittently 403s (`Forbidden`) or 401s |
| 156 | // (`Unauthorized`). Both are transient here — ride out the blip like |
| 157 | // every other out-of-band call in this suite. |
| 158 | const all = yield* provider.list().pipe( |
| 159 | Effect.retry({ |
| 160 | while: (e) => e._tag === "Forbidden" || e._tag === "Unauthorized", |
| 161 | schedule: forbiddenRetrySchedule, |
| 162 | times: 8, |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | expect(all.length).toBeGreaterThan(0); |
| 167 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 168 | |
| 169 | // `stack` is unused here (the singleton always exists on every zone), |
| 170 | // but keep the destroy bookend so the harness state stays clean. |
| 171 | yield* stack.destroy(); |
| 172 | }).pipe(logLevel), |
| 173 | { tags: ["provider:cloudflare:zone"] }, |
| 174 | ); |
| 175 | |
| 176 | test.provider.skipIf(!entitledZoneId)( |
| 177 | "enables Content Scanning and restores the original status on destroy", |
| 178 | (stack) => |
| 179 | Effect.gen(function* () { |
| 180 | const zoneId = entitledZoneId!; |
| 181 | |
| 182 | yield* stack.destroy(); |
| 183 | // Known baseline: Content Scanning defaults to "disabled". |
| 184 | yield* setBaseline(zoneId, "disabled"); |
| 185 | |
| 186 | const scanning = yield* stack.deploy( |
| 187 | Effect.gen(function* () { |
| 188 | return yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", { |
| 189 | zoneId, |
| 190 | }); |
| 191 | }), |
| 192 | ); |
| 193 | |
| 194 | expect(scanning.zoneId).toEqual(zoneId); |
| 195 | expect(scanning.enabled).toEqual(true); |
| 196 | expect(scanning.initialValue).toEqual("disabled"); |
| 197 | |
| 198 | const live = yield* getStatus(zoneId); |
| 199 | expect(live.value).toEqual("enabled"); |
| 200 | |
| 201 | // Update in place — same singleton, initialValue survives. |
| 202 | const updated = yield* stack.deploy( |
| 203 | Effect.gen(function* () { |
| 204 | return yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", { |
| 205 | zoneId, |
| 206 | enabled: false, |
| 207 | }); |
| 208 | }), |
| 209 | ); |
| 210 | expect(updated.enabled).toEqual(false); |
| 211 | expect(updated.initialValue).toEqual("disabled"); |
| 212 | |
| 213 | yield* stack.destroy(); |
| 214 | |
| 215 | // Destroy restored the status the zone had before we managed it. |
| 216 | const restored = yield* getStatus(zoneId); |
| 217 | expect(restored.value).toEqual("disabled"); |
| 218 | }).pipe(logLevel), |
| 219 | { timeout: 120_000 }, |
| 220 | ); |
| 221 | }, |
| 222 | ); |