The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 botManagement from "@distilled.cloud/cloudflare/bot-management"; |
| 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 | const resolveZoneId = Effect.gen(function* () { |
| 18 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 19 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 20 | if (!zone) { |
| 21 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 22 | } |
| 23 | return zone.id; |
| 24 | }); |
| 25 | |
| 26 | // The scoped API token the test harness mints propagates eventually- |
| 27 | // consistently across Cloudflare's edge — a fresh token intermittently 403s. |
| 28 | // Ride out the blips on the test's own out-of-band verification calls by |
| 29 | // retrying the typed `Forbidden` error (patched into the bot-management |
| 30 | // operations' error unions). |
| 31 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 32 | |
| 33 | /** |
| 34 | * The distilled response is a 4-way union of plan shapes; widen to a flat |
| 35 | * bag of optional fields for assertions. |
| 36 | */ |
| 37 | interface ObservedConfig { |
| 38 | readonly enableJs?: boolean | null; |
| 39 | readonly fightMode?: boolean | null; |
| 40 | readonly aiBotsProtection?: string | null; |
| 41 | readonly aiSearch?: string | null; |
| 42 | readonly aiUser?: string | null; |
| 43 | readonly aiTraining?: string | null; |
| 44 | readonly botPreferenceSyncEnabled?: boolean | null; |
| 45 | readonly sbfmDefinitelyAutomated?: string | null; |
| 46 | readonly sbfmVerifiedBots?: string | null; |
| 47 | readonly sbfmStaticResourceProtection?: boolean | null; |
| 48 | readonly optimizeWordpress?: boolean | null; |
| 49 | } |
| 50 | |
| 51 | const getConfig = (zoneId: string) => |
| 52 | botManagement.getBotManagement({ zoneId }).pipe( |
| 53 | Effect.map((config): ObservedConfig => config as ObservedConfig), |
| 54 | Effect.retry({ |
| 55 | while: (e) => e._tag === "Forbidden", |
| 56 | schedule: forbiddenRetrySchedule, |
| 57 | times: 8, |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | const restoreSbfm = (zoneId: string, original: ObservedConfig) => |
| 62 | Effect.gen(function* () { |
| 63 | const body: Record<string, unknown> = {}; |
| 64 | if (original.sbfmDefinitelyAutomated != null) { |
| 65 | body.sbfmDefinitelyAutomated = original.sbfmDefinitelyAutomated; |
| 66 | } |
| 67 | if (original.sbfmVerifiedBots != null) { |
| 68 | body.sbfmVerifiedBots = original.sbfmVerifiedBots; |
| 69 | } |
| 70 | if (original.sbfmStaticResourceProtection != null) { |
| 71 | body.sbfmStaticResourceProtection = original.sbfmStaticResourceProtection; |
| 72 | } |
| 73 | if (Object.keys(body).length === 0) return; |
| 74 | yield* botManagement.putBotManagement({ zoneId, ...body }); |
| 75 | }).pipe(Effect.ignore); |
| 76 | |
| 77 | const AI_CRAWLER_KEYS = ["aiSearch", "aiUser", "aiTraining", "botPreferenceSyncEnabled"] as const; |
| 78 | |
| 79 | /** The AI crawler policy fields of a config, attributes, or snapshot. */ |
| 80 | const pickAiCrawlers = (source: Pick<ObservedConfig, (typeof AI_CRAWLER_KEYS)[number]>) => |
| 81 | Object.fromEntries(AI_CRAWLER_KEYS.map((key) => [key, source[key] ?? null])); |
| 82 | |
| 83 | const restoreAiCrawlers = (zoneId: string, original: ObservedConfig) => |
| 84 | Effect.gen(function* () { |
| 85 | const body: Record<string, unknown> = {}; |
| 86 | for (const key of AI_CRAWLER_KEYS) { |
| 87 | if (original[key] != null) body[key] = original[key]; |
| 88 | } |
| 89 | if (Object.keys(body).length === 0) return; |
| 90 | yield* botManagement.putBotManagement({ zoneId, ...body }); |
| 91 | }).pipe(Effect.ignore); |
| 92 | |
| 93 | // Setting `sbfm_definitely_automated` to anything but "allow" makes the |
| 94 | // standing zone answer every automated request (curl, fetch, vitest HTTP |
| 95 | // assertions) with a managed challenge — breaking every live suite that |
| 96 | // drives the zone over HTTP, and an interrupted run leaves the setting |
| 97 | // behind. The mutating lifecycle tests are therefore opt-in; the adopt and |
| 98 | // list tests below always run. |
| 99 | const destructive = !!process.env.CLOUDFLARE_TEST_BOT_MANAGEMENT; |
| 100 | |
| 101 | describe.sequential( |
| 102 | "BotManagement", |
| 103 | { |
| 104 | tags: [ |
| 105 | "provider:cloudflare", |
| 106 | "provider:cloudflare:botmanagement", |
| 107 | "provider:cloudflare:zone", |
| 108 | "live", |
| 109 | ], |
| 110 | }, |
| 111 | () => { |
| 112 | test.provider.skipIf(!destructive)( |
| 113 | "manages SBFM settings on the zone singleton and restores them on destroy", |
| 114 | (stack) => |
| 115 | Effect.gen(function* () { |
| 116 | const zoneId = yield* resolveZoneId; |
| 117 | |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const original = yield* getConfig(zoneId); |
| 121 | // Pick deterministic targets that differ from the live config so the |
| 122 | // first reconcile demonstrably writes. |
| 123 | const target = |
| 124 | original.sbfmDefinitelyAutomated === "managed_challenge" |
| 125 | ? "block" |
| 126 | : ("managed_challenge" as const); |
| 127 | const target2 = target === "block" ? "managed_challenge" : "block"; |
| 128 | |
| 129 | yield* Effect.gen(function* () { |
| 130 | // 1. Create (adopt the singleton) with one SBFM field set. |
| 131 | const created = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | return yield* Cloudflare.BotManagement.BotManagement("Bots", { |
| 134 | zoneId, |
| 135 | sbfmDefinitelyAutomated: target, |
| 136 | }); |
| 137 | }), |
| 138 | ); |
| 139 | expect(created.zoneId).toEqual(zoneId); |
| 140 | expect(created.sbfmDefinitelyAutomated).toEqual(target); |
| 141 | // Snapshot captured the pre-management value. |
| 142 | expect(created.initialSettings.sbfmDefinitelyAutomated ?? null).toEqual( |
| 143 | original.sbfmDefinitelyAutomated ?? null, |
| 144 | ); |
| 145 | |
| 146 | const live1 = yield* getConfig(zoneId); |
| 147 | expect(live1.sbfmDefinitelyAutomated).toEqual(target); |
| 148 | |
| 149 | // 2. Update the mutable field — same singleton (same zoneId), and |
| 150 | // the initial snapshot must remain sticky across updates. |
| 151 | const updated = yield* stack.deploy( |
| 152 | Effect.gen(function* () { |
| 153 | return yield* Cloudflare.BotManagement.BotManagement("Bots", { |
| 154 | zoneId, |
| 155 | sbfmDefinitelyAutomated: target2, |
| 156 | }); |
| 157 | }), |
| 158 | ); |
| 159 | expect(updated.zoneId).toEqual(zoneId); |
| 160 | expect(updated.sbfmDefinitelyAutomated).toEqual(target2); |
| 161 | expect(updated.initialSettings.sbfmDefinitelyAutomated ?? null).toEqual( |
| 162 | original.sbfmDefinitelyAutomated ?? null, |
| 163 | ); |
| 164 | |
| 165 | const live2 = yield* getConfig(zoneId); |
| 166 | expect(live2.sbfmDefinitelyAutomated).toEqual(target2); |
| 167 | |
| 168 | // 3. Destroy — the managed field is restored to the snapshot value |
| 169 | // (when the snapshot had one; the test zone always does once the |
| 170 | // field has ever been set, which step 1 guarantees for reruns). |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const after = yield* getConfig(zoneId); |
| 174 | if (original.sbfmDefinitelyAutomated != null) { |
| 175 | expect(after.sbfmDefinitelyAutomated).toEqual(original.sbfmDefinitelyAutomated); |
| 176 | } |
| 177 | }).pipe(Effect.ensuring(restoreSbfm(zoneId, original))); |
| 178 | |
| 179 | yield* stack.destroy(); |
| 180 | }).pipe(logLevel), |
| 181 | { timeout: 240_000 }, |
| 182 | ); |
| 183 | |
| 184 | test.provider( |
| 185 | "deploy with no settings set adopts the singleton without writing", |
| 186 | (stack) => |
| 187 | Effect.gen(function* () { |
| 188 | const zoneId = yield* resolveZoneId; |
| 189 | |
| 190 | yield* stack.destroy(); |
| 191 | |
| 192 | const before = yield* getConfig(zoneId); |
| 193 | |
| 194 | const adopted = yield* stack.deploy( |
| 195 | Effect.gen(function* () { |
| 196 | return yield* Cloudflare.BotManagement.BotManagement("Bots", { zoneId }); |
| 197 | }), |
| 198 | ); |
| 199 | expect(adopted.zoneId).toEqual(zoneId); |
| 200 | |
| 201 | // Nothing was set => no PUT => live config unchanged. |
| 202 | const afterDeploy = yield* getConfig(zoneId); |
| 203 | expect(afterDeploy.sbfmDefinitelyAutomated ?? null).toEqual( |
| 204 | before.sbfmDefinitelyAutomated ?? null, |
| 205 | ); |
| 206 | expect(afterDeploy.sbfmVerifiedBots ?? null).toEqual(before.sbfmVerifiedBots ?? null); |
| 207 | expect(afterDeploy.sbfmStaticResourceProtection ?? null).toEqual( |
| 208 | before.sbfmStaticResourceProtection ?? null, |
| 209 | ); |
| 210 | expect(afterDeploy.enableJs ?? null).toEqual(before.enableJs ?? null); |
| 211 | |
| 212 | // Destroy of an untouched singleton restores nothing and never errors. |
| 213 | yield* stack.destroy(); |
| 214 | |
| 215 | const afterDestroy = yield* getConfig(zoneId); |
| 216 | expect(afterDestroy.sbfmDefinitelyAutomated ?? null).toEqual( |
| 217 | before.sbfmDefinitelyAutomated ?? null, |
| 218 | ); |
| 219 | expect(afterDestroy.enableJs ?? null).toEqual(before.enableJs ?? null); |
| 220 | }).pipe(logLevel), |
| 221 | { timeout: 240_000 }, |
| 222 | ); |
| 223 | |
| 224 | test.provider.skipIf(!destructive)( |
| 225 | "toggles a boolean SBFM field and restores it on destroy", |
| 226 | (stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const zoneId = yield* resolveZoneId; |
| 229 | |
| 230 | yield* stack.destroy(); |
| 231 | |
| 232 | const original = yield* getConfig(zoneId); |
| 233 | const toggled = !(original.sbfmStaticResourceProtection ?? false); |
| 234 | |
| 235 | yield* Effect.gen(function* () { |
| 236 | const created = yield* stack.deploy( |
| 237 | Effect.gen(function* () { |
| 238 | return yield* Cloudflare.BotManagement.BotManagement("Bots", { |
| 239 | zoneId, |
| 240 | sbfmStaticResourceProtection: toggled, |
| 241 | }); |
| 242 | }), |
| 243 | ); |
| 244 | expect(created.sbfmStaticResourceProtection).toEqual(toggled); |
| 245 | |
| 246 | const live = yield* getConfig(zoneId); |
| 247 | expect(live.sbfmStaticResourceProtection).toEqual(toggled); |
| 248 | |
| 249 | yield* stack.destroy(); |
| 250 | |
| 251 | const after = yield* getConfig(zoneId); |
| 252 | if (original.sbfmStaticResourceProtection != null) { |
| 253 | expect(after.sbfmStaticResourceProtection).toEqual( |
| 254 | original.sbfmStaticResourceProtection, |
| 255 | ); |
| 256 | } |
| 257 | }).pipe(Effect.ensuring(restoreSbfm(zoneId, original))); |
| 258 | |
| 259 | yield* stack.destroy(); |
| 260 | }).pipe(logLevel), |
| 261 | { timeout: 240_000 }, |
| 262 | ); |
| 263 | |
| 264 | // AI crawler policies only affect AI crawlers and the managed robots.txt, |
| 265 | // not the plain HTTP clients other live suites use against the zone, so |
| 266 | // this lifecycle runs ungated. |
| 267 | test.provider( |
| 268 | "manages the AI crawler policies and restores them on destroy", |
| 269 | (stack) => |
| 270 | Effect.gen(function* () { |
| 271 | const zoneId = yield* resolveZoneId; |
| 272 | |
| 273 | yield* stack.destroy(); |
| 274 | |
| 275 | const original = yield* getConfig(zoneId); |
| 276 | // Targets that differ from the live config so reconcile must write. |
| 277 | const target = { |
| 278 | aiSearch: original.aiSearch === "block" ? "disabled" : "block", |
| 279 | aiUser: original.aiUser === "block" ? "disabled" : "block", |
| 280 | aiTraining: original.aiTraining === "disallow" ? "disabled" : "disallow", |
| 281 | botPreferenceSyncEnabled: !(original.botPreferenceSyncEnabled ?? false), |
| 282 | } as const; |
| 283 | |
| 284 | yield* Effect.gen(function* () { |
| 285 | const created = yield* stack.deploy( |
| 286 | Effect.gen(function* () { |
| 287 | return yield* Cloudflare.BotManagement.BotManagement("Bots", { |
| 288 | zoneId, |
| 289 | ...target, |
| 290 | }); |
| 291 | }), |
| 292 | ); |
| 293 | expect(pickAiCrawlers(created)).toEqual(target); |
| 294 | // The snapshot captured the pre-management values. |
| 295 | expect(pickAiCrawlers(created.initialSettings)).toEqual(pickAiCrawlers(original)); |
| 296 | |
| 297 | expect(pickAiCrawlers(yield* getConfig(zoneId))).toEqual(target); |
| 298 | |
| 299 | yield* stack.destroy(); |
| 300 | |
| 301 | expect(pickAiCrawlers(yield* getConfig(zoneId))).toEqual(pickAiCrawlers(original)); |
| 302 | }).pipe(Effect.ensuring(restoreAiCrawlers(zoneId, original))); |
| 303 | |
| 304 | yield* stack.destroy(); |
| 305 | }).pipe(logLevel), |
| 306 | { timeout: 240_000 }, |
| 307 | ); |
| 308 | |
| 309 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 310 | // API for this per-zone config, so `list()` enumerates every zone via |
| 311 | // `listAllZones` and reads the bot-management singleton in each. Assert the |
| 312 | // result is non-empty and contains the standing test zone. This is a |
| 313 | // read-only assertion (no mutation), so it runs regardless of whether the |
| 314 | // zone has the paid Bot Management add-on. |
| 315 | test.provider( |
| 316 | "list enumerates bot management across all zones", |
| 317 | (stack) => |
| 318 | Effect.gen(function* () { |
| 319 | const zoneId = yield* resolveZoneId; |
| 320 | |
| 321 | const provider = yield* Provider.findProvider(Cloudflare.BotManagement.BotManagement); |
| 322 | const all = yield* provider.list(); |
| 323 | |
| 324 | expect(all.length).toBeGreaterThan(0); |
| 325 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 326 | |
| 327 | // `stack` is unused (the singleton always exists on every zone), but |
| 328 | // keep the destroy bookend so the harness state stays clean. |
| 329 | yield* stack.destroy(); |
| 330 | }).pipe(logLevel), |
| 331 | { timeout: 240_000 }, |
| 332 | ); |
| 333 | }, |
| 334 | ); |