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 { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as firewall from "@distilled.cloud/cloudflare/firewall"; |
| 7 | import { adopt } from "alchemy/AdoptPolicy"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 19 | |
| 20 | // Deterministic per-test configuration values (TEST-NET-2). A rule's |
| 21 | // configuration is its identity within a scope, so each test owns a disjoint |
| 22 | // IP and the same value is reused on every run (never Date.now()/random). |
| 23 | const IP_DEFAULT = "198.51.100.101"; |
| 24 | const IP_UPDATE = "198.51.100.102"; |
| 25 | const IP_REPLACE_OLD = "198.51.100.103"; |
| 26 | const IP_REPLACE_NEW = "198.51.100.104"; |
| 27 | const IP_ACCOUNT = "198.51.100.105"; |
| 28 | const IP_LIST = "198.51.100.106"; |
| 29 | |
| 30 | const resolveZoneId = Effect.gen(function* () { |
| 31 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 32 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 33 | if (!zone) { |
| 34 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 35 | } |
| 36 | return zone.id; |
| 37 | }); |
| 38 | |
| 39 | // The scoped API token the test harness mints propagates eventually- |
| 40 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 41 | // declared in the distilled error union via patches) on the test's own |
| 42 | // out-of-band verification calls. |
| 43 | const forbiddenRetry = { |
| 44 | while: (e: { _tag: string }) => e._tag === "Forbidden", |
| 45 | schedule: Schedule.exponential("500 millis"), |
| 46 | times: 8, |
| 47 | } as const; |
| 48 | |
| 49 | const getZoneRule = (zoneId: string, ruleId: string) => |
| 50 | firewall.getAccessRuleForZone({ zoneId, ruleId }).pipe(Effect.retry(forbiddenRetry)); |
| 51 | |
| 52 | const getAccountRule = (accountId: string, ruleId: string) => |
| 53 | firewall.getAccessRuleForAccount({ accountId, ruleId }).pipe(Effect.retry(forbiddenRetry)); |
| 54 | |
| 55 | const listByIp = (scope: { zoneId: string } | { accountId: string }, ip: string) => |
| 56 | ("zoneId" in scope |
| 57 | ? firewall.listAccessRulesForZone.items({ zoneId: scope.zoneId }) |
| 58 | : firewall.listAccessRulesForAccount.items({ accountId: scope.accountId }) |
| 59 | ).pipe( |
| 60 | Stream.filter((r) => r.configuration.target === "ip" && r.configuration.value === ip), |
| 61 | Stream.runCollect, |
| 62 | Effect.map((chunk) => Array.from(chunk)), |
| 63 | Effect.retry(forbiddenRetry), |
| 64 | ); |
| 65 | |
| 66 | // Delete every rule matching the ip — used to purge leftovers from |
| 67 | // interrupted runs so each test starts from a clean slate (a leaked rule |
| 68 | // would surface as Unowned/duplicate because configuration is identity). |
| 69 | const purgeRules = (scope: { zoneId: string } | { accountId: string }, ip: string) => |
| 70 | listByIp(scope, ip).pipe( |
| 71 | Effect.flatMap( |
| 72 | Effect.forEach((r) => |
| 73 | ("zoneId" in scope |
| 74 | ? firewall.deleteAccessRuleForZone({ zoneId: scope.zoneId, ruleId: r.id }) |
| 75 | : firewall.deleteAccessRuleForAccount({ accountId: scope.accountId, ruleId: r.id }) |
| 76 | ).pipe( |
| 77 | Effect.retry(forbiddenRetry), |
| 78 | Effect.catchTag("AccessRuleNotFound", () => Effect.void), |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ); |
| 83 | |
| 84 | // Poll until the rule is gone — a missing rule surfaces as the typed |
| 85 | // `AccessRuleNotFound` (Cloudflare code 10001, firewallaccessrules.api.not_found). |
| 86 | const expectZoneRuleGone = (zoneId: string, ruleId: string) => |
| 87 | getZoneRule(zoneId, ruleId).pipe( |
| 88 | Effect.flatMap(() => Effect.fail({ _tag: "RuleNotDeleted" } as const)), |
| 89 | Effect.catchTag("AccessRuleNotFound", () => Effect.void), |
| 90 | Effect.retry({ |
| 91 | while: (e) => e._tag === "RuleNotDeleted", |
| 92 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | const expectAccountRuleGone = (accountId: string, ruleId: string) => |
| 97 | getAccountRule(accountId, ruleId).pipe( |
| 98 | Effect.flatMap(() => Effect.fail({ _tag: "RuleNotDeleted" } as const)), |
| 99 | Effect.catchTag("AccessRuleNotFound", () => Effect.void), |
| 100 | Effect.retry({ |
| 101 | while: (e) => e._tag === "RuleNotDeleted", |
| 102 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | test.provider( |
| 107 | "create and delete a zone-scoped ip rule", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 111 | const zoneId = yield* resolveZoneId; |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | yield* purgeRules({ zoneId }, IP_DEFAULT); |
| 115 | |
| 116 | const rule = yield* stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | return yield* Cloudflare.Firewall.AccessRule("DefaultRule", { |
| 119 | zoneId, |
| 120 | configuration: { target: "ip", value: IP_DEFAULT }, |
| 121 | mode: "challenge", |
| 122 | notes: "alchemy firewall test (default)", |
| 123 | }).pipe(adopt(true)); |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | expect(rule.ruleId).toBeDefined(); |
| 128 | expect(rule.zoneId).toEqual(zoneId); |
| 129 | expect(rule.accountId).toEqual(accountId); |
| 130 | expect(rule.configuration).toEqual({ target: "ip", value: IP_DEFAULT }); |
| 131 | expect(rule.mode).toEqual("challenge"); |
| 132 | expect(rule.notes).toEqual("alchemy firewall test (default)"); |
| 133 | expect(rule.allowedModes).toContain("block"); |
| 134 | |
| 135 | const live = yield* getZoneRule(zoneId, rule.ruleId); |
| 136 | expect(live.id).toEqual(rule.ruleId); |
| 137 | expect(live.configuration.value).toEqual(IP_DEFAULT); |
| 138 | expect(live.mode).toEqual("challenge"); |
| 139 | |
| 140 | yield* stack.destroy(); |
| 141 | |
| 142 | yield* expectZoneRuleGone(zoneId, rule.ruleId); |
| 143 | }).pipe(logLevel), |
| 144 | { |
| 145 | tags: [ |
| 146 | "provider:cloudflare", |
| 147 | "provider:cloudflare:firewall", |
| 148 | "provider:cloudflare:zone", |
| 149 | "live", |
| 150 | ], |
| 151 | }, |
| 152 | ); |
| 153 | |
| 154 | test.provider( |
| 155 | "update mode and notes in place (same ruleId)", |
| 156 | (stack) => |
| 157 | Effect.gen(function* () { |
| 158 | const zoneId = yield* resolveZoneId; |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | yield* purgeRules({ zoneId }, IP_UPDATE); |
| 162 | |
| 163 | const initial = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | return yield* Cloudflare.Firewall.AccessRule("UpdateRule", { |
| 166 | zoneId, |
| 167 | configuration: { target: "ip", value: IP_UPDATE }, |
| 168 | mode: "challenge", |
| 169 | notes: "v1", |
| 170 | }).pipe(adopt(true)); |
| 171 | }), |
| 172 | ); |
| 173 | |
| 174 | expect(initial.mode).toEqual("challenge"); |
| 175 | expect(initial.notes).toEqual("v1"); |
| 176 | |
| 177 | const updated = yield* stack.deploy( |
| 178 | Effect.gen(function* () { |
| 179 | return yield* Cloudflare.Firewall.AccessRule("UpdateRule", { |
| 180 | zoneId, |
| 181 | configuration: { target: "ip", value: IP_UPDATE }, |
| 182 | mode: "managed_challenge", |
| 183 | notes: "v2", |
| 184 | }).pipe(adopt(true)); |
| 185 | }), |
| 186 | ); |
| 187 | |
| 188 | // Same rule patched in place — not a replacement. |
| 189 | expect(updated.ruleId).toEqual(initial.ruleId); |
| 190 | expect(updated.mode).toEqual("managed_challenge"); |
| 191 | expect(updated.notes).toEqual("v2"); |
| 192 | |
| 193 | const live = yield* getZoneRule(zoneId, updated.ruleId); |
| 194 | expect(live.mode).toEqual("managed_challenge"); |
| 195 | expect(live.notes).toEqual("v2"); |
| 196 | |
| 197 | // Redeploying identical props is a no-op (still the same rule). |
| 198 | const noop = yield* stack.deploy( |
| 199 | Effect.gen(function* () { |
| 200 | return yield* Cloudflare.Firewall.AccessRule("UpdateRule", { |
| 201 | zoneId, |
| 202 | configuration: { target: "ip", value: IP_UPDATE }, |
| 203 | mode: "managed_challenge", |
| 204 | notes: "v2", |
| 205 | }).pipe(adopt(true)); |
| 206 | }), |
| 207 | ); |
| 208 | expect(noop.ruleId).toEqual(initial.ruleId); |
| 209 | |
| 210 | yield* stack.destroy(); |
| 211 | |
| 212 | yield* expectZoneRuleGone(zoneId, initial.ruleId); |
| 213 | }).pipe(logLevel), |
| 214 | { |
| 215 | tags: [ |
| 216 | "provider:cloudflare", |
| 217 | "provider:cloudflare:firewall", |
| 218 | "provider:cloudflare:zone", |
| 219 | "live", |
| 220 | ], |
| 221 | }, |
| 222 | ); |
| 223 | |
| 224 | test.provider( |
| 225 | "changing the configuration triggers replacement", |
| 226 | (stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const zoneId = yield* resolveZoneId; |
| 229 | |
| 230 | yield* stack.destroy(); |
| 231 | yield* purgeRules({ zoneId }, IP_REPLACE_OLD); |
| 232 | yield* purgeRules({ zoneId }, IP_REPLACE_NEW); |
| 233 | |
| 234 | const initial = yield* stack.deploy( |
| 235 | Effect.gen(function* () { |
| 236 | return yield* Cloudflare.Firewall.AccessRule("ReplaceRule", { |
| 237 | zoneId, |
| 238 | configuration: { target: "ip", value: IP_REPLACE_OLD }, |
| 239 | mode: "challenge", |
| 240 | }).pipe(adopt(true)); |
| 241 | }), |
| 242 | ); |
| 243 | |
| 244 | expect(initial.configuration.value).toEqual(IP_REPLACE_OLD); |
| 245 | |
| 246 | const replaced = yield* stack.deploy( |
| 247 | Effect.gen(function* () { |
| 248 | return yield* Cloudflare.Firewall.AccessRule("ReplaceRule", { |
| 249 | zoneId, |
| 250 | configuration: { target: "ip", value: IP_REPLACE_NEW }, |
| 251 | mode: "challenge", |
| 252 | }).pipe(adopt(true)); |
| 253 | }), |
| 254 | ); |
| 255 | |
| 256 | // The configuration is the rule's identity — a new physical rule exists. |
| 257 | expect(replaced.ruleId).not.toEqual(initial.ruleId); |
| 258 | expect(replaced.configuration.value).toEqual(IP_REPLACE_NEW); |
| 259 | |
| 260 | // The old rule was deleted as part of the replacement. |
| 261 | yield* expectZoneRuleGone(zoneId, initial.ruleId); |
| 262 | |
| 263 | const live = yield* getZoneRule(zoneId, replaced.ruleId); |
| 264 | expect(live.configuration.value).toEqual(IP_REPLACE_NEW); |
| 265 | |
| 266 | yield* stack.destroy(); |
| 267 | |
| 268 | yield* expectZoneRuleGone(zoneId, replaced.ruleId); |
| 269 | }).pipe(logLevel), |
| 270 | { |
| 271 | tags: [ |
| 272 | "provider:cloudflare", |
| 273 | "provider:cloudflare:firewall", |
| 274 | "provider:cloudflare:zone", |
| 275 | "live", |
| 276 | ], |
| 277 | }, |
| 278 | ); |
| 279 | |
| 280 | test.provider( |
| 281 | "list enumerates the deployed zone-scoped rule", |
| 282 | (stack) => |
| 283 | Effect.gen(function* () { |
| 284 | const zoneId = yield* resolveZoneId; |
| 285 | |
| 286 | yield* stack.destroy(); |
| 287 | yield* purgeRules({ zoneId }, IP_LIST); |
| 288 | |
| 289 | const rule = yield* stack.deploy( |
| 290 | Effect.gen(function* () { |
| 291 | return yield* Cloudflare.Firewall.AccessRule("ListRule", { |
| 292 | zoneId, |
| 293 | configuration: { target: "ip", value: IP_LIST }, |
| 294 | mode: "challenge", |
| 295 | notes: "alchemy firewall test (list)", |
| 296 | }).pipe(adopt(true)); |
| 297 | }), |
| 298 | ); |
| 299 | |
| 300 | const provider = yield* Provider.findProvider(Cloudflare.Firewall.AccessRule); |
| 301 | const all = yield* provider.list(); |
| 302 | |
| 303 | const found = all.find((r) => r.ruleId === rule.ruleId); |
| 304 | expect(found).toBeDefined(); |
| 305 | expect(found?.zoneId).toEqual(zoneId); |
| 306 | expect(found?.configuration).toEqual({ target: "ip", value: IP_LIST }); |
| 307 | expect(found?.mode).toEqual("challenge"); |
| 308 | |
| 309 | yield* stack.destroy(); |
| 310 | |
| 311 | yield* expectZoneRuleGone(zoneId, rule.ruleId); |
| 312 | }).pipe(logLevel), |
| 313 | { |
| 314 | tags: [ |
| 315 | "provider:cloudflare", |
| 316 | "provider:cloudflare:firewall", |
| 317 | "provider:cloudflare:zone", |
| 318 | "live", |
| 319 | ], |
| 320 | }, |
| 321 | ); |
| 322 | |
| 323 | test.provider( |
| 324 | "account-scoped rule (no zoneId) create, update, delete", |
| 325 | (stack) => |
| 326 | Effect.gen(function* () { |
| 327 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 328 | |
| 329 | yield* stack.destroy(); |
| 330 | yield* purgeRules({ accountId }, IP_ACCOUNT); |
| 331 | |
| 332 | const rule = yield* stack.deploy( |
| 333 | Effect.gen(function* () { |
| 334 | return yield* Cloudflare.Firewall.AccessRule("AccountRule", { |
| 335 | configuration: { target: "ip", value: IP_ACCOUNT }, |
| 336 | mode: "challenge", |
| 337 | notes: "alchemy firewall test (account)", |
| 338 | }).pipe(adopt(true)); |
| 339 | }), |
| 340 | ); |
| 341 | |
| 342 | expect(rule.ruleId).toBeDefined(); |
| 343 | expect(rule.zoneId).toBeUndefined(); |
| 344 | expect(rule.accountId).toEqual(accountId); |
| 345 | expect(rule.configuration).toEqual({ target: "ip", value: IP_ACCOUNT }); |
| 346 | expect(rule.mode).toEqual("challenge"); |
| 347 | |
| 348 | const live = yield* getAccountRule(accountId, rule.ruleId); |
| 349 | expect(live.id).toEqual(rule.ruleId); |
| 350 | expect(live.configuration.value).toEqual(IP_ACCOUNT); |
| 351 | |
| 352 | const updated = yield* stack.deploy( |
| 353 | Effect.gen(function* () { |
| 354 | return yield* Cloudflare.Firewall.AccessRule("AccountRule", { |
| 355 | configuration: { target: "ip", value: IP_ACCOUNT }, |
| 356 | mode: "managed_challenge", |
| 357 | notes: "alchemy firewall test (account, v2)", |
| 358 | }).pipe(adopt(true)); |
| 359 | }), |
| 360 | ); |
| 361 | |
| 362 | expect(updated.ruleId).toEqual(rule.ruleId); |
| 363 | expect(updated.mode).toEqual("managed_challenge"); |
| 364 | |
| 365 | const liveUpdated = yield* getAccountRule(accountId, rule.ruleId); |
| 366 | expect(liveUpdated.mode).toEqual("managed_challenge"); |
| 367 | |
| 368 | yield* stack.destroy(); |
| 369 | |
| 370 | yield* expectAccountRuleGone(accountId, rule.ruleId); |
| 371 | }).pipe(logLevel), |
| 372 | { tags: ["provider:cloudflare", "provider:cloudflare:firewall", "live"] }, |
| 373 | ); |