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 pageShield from "@distilled.cloud/cloudflare/page-shield"; |
| 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 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 18 | |
| 19 | // Page Shield CSP policies are an Enterprise add-on. On the testing |
| 20 | // account's zone every create fails with "exceeded the maximum number of |
| 21 | // rules in the phase http_response_page_shield: 1 out of 0", surfaced as |
| 22 | // the typed `PolicyQuotaExceeded` error. The full lifecycle test below is |
| 23 | // gated behind an entitled zone id supplied via env. |
| 24 | const entitledZoneId = process.env.CLOUDFLARE_TEST_PAGE_SHIELD_POLICY_ZONE_ID; |
| 25 | |
| 26 | const resolveZoneId = Effect.gen(function* () { |
| 27 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 28 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 29 | if (!zone) { |
| 30 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 31 | } |
| 32 | return zone.id; |
| 33 | }); |
| 34 | |
| 35 | // Fresh scoped tokens propagate eventually-consistently across Cloudflare's |
| 36 | // edge — retry the typed `Forbidden` error on out-of-band calls. |
| 37 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 38 | |
| 39 | const getPolicy = (zoneId: string, policyId: string) => |
| 40 | pageShield.getPolicy({ zoneId, policyId }).pipe( |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "Forbidden", |
| 43 | schedule: forbiddenRetrySchedule, |
| 44 | times: 8, |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | const findPolicyByDescription = (zoneId: string, description: string) => |
| 49 | pageShield.listPolicies({ zoneId }).pipe( |
| 50 | Effect.map((list) => list.result.find((p) => p.description === description)), |
| 51 | Effect.retry({ |
| 52 | while: (e) => e._tag === "Forbidden", |
| 53 | schedule: forbiddenRetrySchedule, |
| 54 | times: 8, |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | test.provider( |
| 59 | "surfaces the typed PolicyQuotaExceeded error on unentitled zones", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | const zoneId = yield* resolveZoneId; |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | // The standard testing zone lacks the Enterprise CSP entitlement — |
| 67 | // the distilled call must fail with the typed quota tag. |
| 68 | const error = yield* pageShield |
| 69 | .createPolicy({ |
| 70 | zoneId, |
| 71 | action: "log", |
| 72 | description: "alchemy-pageshield-quota-probe", |
| 73 | enabled: true, |
| 74 | expression: `http.host eq "${zoneName}"`, |
| 75 | value: "script-src 'self'", |
| 76 | }) |
| 77 | .pipe( |
| 78 | Effect.retry({ |
| 79 | while: (e) => e._tag === "Forbidden", |
| 80 | schedule: forbiddenRetrySchedule, |
| 81 | times: 8, |
| 82 | }), |
| 83 | Effect.flip, |
| 84 | ); |
| 85 | expect(error._tag).toEqual("PolicyQuotaExceeded"); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | }).pipe(logLevel), |
| 89 | { |
| 90 | tags: [ |
| 91 | "provider:cloudflare", |
| 92 | "provider:cloudflare:pageshield", |
| 93 | "provider:cloudflare:zone", |
| 94 | "live", |
| 95 | ], |
| 96 | timeout: 120_000, |
| 97 | }, |
| 98 | ); |
| 99 | |
| 100 | test.provider( |
| 101 | "list enumerates Page Shield policies across all zones", |
| 102 | (stack) => |
| 103 | Effect.gen(function* () { |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const provider = yield* Provider.findProvider(Cloudflare.PageShield.Policy); |
| 107 | |
| 108 | // Page Shield CSP policies are an Enterprise add-on; the testing |
| 109 | // account has a zero rule quota, so we cannot deploy a policy to |
| 110 | // observe. Enumeration must still succeed (fanning out across every |
| 111 | // zone, skipping unentitled `Forbidden` zones) and return a |
| 112 | // well-typed array — empty when no policies exist. |
| 113 | if (entitledZoneId) { |
| 114 | const description = testName("alchemy-pageshield-list"); |
| 115 | const deployed = yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | yield* Cloudflare.PageShield.Settings("PageShield", { |
| 118 | zoneId: entitledZoneId, |
| 119 | }); |
| 120 | return yield* Cloudflare.PageShield.Policy("ListPolicy", { |
| 121 | zoneId: entitledZoneId, |
| 122 | description, |
| 123 | action: "log", |
| 124 | expression: `http.host eq "${zoneName}"`, |
| 125 | value: "script-src 'self'", |
| 126 | }); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | const all = yield* provider.list(); |
| 131 | expect(Array.isArray(all)).toBe(true); |
| 132 | expect(all.some((p) => p.policyId === deployed.policyId)).toBe(true); |
| 133 | } else { |
| 134 | const all = yield* provider.list(); |
| 135 | expect(Array.isArray(all)).toBe(true); |
| 136 | } |
| 137 | |
| 138 | yield* stack.destroy(); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: ["provider:cloudflare", "provider:cloudflare:pageshield", "live"], |
| 142 | timeout: 180_000, |
| 143 | }, |
| 144 | ); |
| 145 | |
| 146 | // Requires the Enterprise Page Shield CSP entitlement — unentitled zones have a zero |
| 147 | // rule quota and fail with the typed PolicyQuotaExceeded. Unlock with CLOUDFLARE_TEST_PAGE_SHIELD_POLICY_ZONE_ID=<zone id>. |
| 148 | test.provider.skipIf(!entitledZoneId)( |
| 149 | "creates, updates in place, and deletes a CSP policy", |
| 150 | (stack) => |
| 151 | Effect.gen(function* () { |
| 152 | const zoneId = entitledZoneId!; |
| 153 | const description = testName("alchemy-pageshield-policy-lifecycle"); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | // Create — log-only CSP policy (Page Shield itself is enabled by |
| 158 | // the Settings singleton in the same stack). |
| 159 | const created = yield* stack.deploy( |
| 160 | Effect.gen(function* () { |
| 161 | yield* Cloudflare.PageShield.Settings("PageShield", { |
| 162 | zoneId, |
| 163 | }); |
| 164 | return yield* Cloudflare.PageShield.Policy("CspPolicy", { |
| 165 | zoneId, |
| 166 | description, |
| 167 | action: "log", |
| 168 | expression: `http.host eq "${zoneName}"`, |
| 169 | value: "script-src 'self'", |
| 170 | }); |
| 171 | }), |
| 172 | ); |
| 173 | |
| 174 | expect(created.policyId).toBeDefined(); |
| 175 | expect(created.zoneId).toEqual(zoneId); |
| 176 | expect(created.action).toEqual("log"); |
| 177 | expect(created.description).toEqual(description); |
| 178 | expect(created.enabled).toEqual(true); |
| 179 | |
| 180 | // Out-of-band verification via the distilled API. |
| 181 | const live = yield* getPolicy(zoneId, created.policyId); |
| 182 | expect(live.action).toEqual("log"); |
| 183 | expect(live.value).toEqual("script-src 'self'"); |
| 184 | |
| 185 | // Update in place — action and value are mutable, same policy id. |
| 186 | const updated = yield* stack.deploy( |
| 187 | Effect.gen(function* () { |
| 188 | yield* Cloudflare.PageShield.Settings("PageShield", { |
| 189 | zoneId, |
| 190 | }); |
| 191 | return yield* Cloudflare.PageShield.Policy("CspPolicy", { |
| 192 | zoneId, |
| 193 | description, |
| 194 | action: "allow", |
| 195 | expression: `http.host eq "${zoneName}"`, |
| 196 | value: "script-src 'self' 'unsafe-inline'", |
| 197 | }); |
| 198 | }), |
| 199 | ); |
| 200 | expect(updated.policyId).toEqual(created.policyId); |
| 201 | expect(updated.action).toEqual("allow"); |
| 202 | expect(updated.value).toEqual("script-src 'self' 'unsafe-inline'"); |
| 203 | |
| 204 | const liveUpdated = yield* getPolicy(zoneId, updated.policyId); |
| 205 | expect(liveUpdated.action).toEqual("allow"); |
| 206 | |
| 207 | yield* stack.destroy(); |
| 208 | |
| 209 | // The policy is gone (delete is idempotent — Cloudflare answers a |
| 210 | // 204 even for already-deleted policies). |
| 211 | const gone = yield* findPolicyByDescription(zoneId, description); |
| 212 | expect(gone).toBeUndefined(); |
| 213 | }).pipe(logLevel), |
| 214 | { |
| 215 | tags: ["provider:cloudflare", "provider:cloudflare:pageshield", "live"], |
| 216 | timeout: 120_000, |
| 217 | }, |
| 218 | ); |