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 { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Output from "alchemy/Output"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import { JWKS_KEY_1 } from "./fixtures/jwks.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 17 | |
| 18 | // JWT validation (API Shield) is entitlement-gated. On the standard testing |
| 19 | // account every token_validation call fails with the typed |
| 20 | // `TokenValidationNotEntitled` error (Cloudflare code 10403), so the deploy |
| 21 | // portion of the list test is gated behind an entitled zone id supplied via |
| 22 | // env. The read-only list assertion below always runs: `list()` fans out over |
| 23 | // every zone and skips unentitled/forbidden zones, returning a well-typed `[]`. |
| 24 | const entitledZoneId = process.env.CLOUDFLARE_TEST_TOKEN_VALIDATION_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 | // Read-only: always safe. On an unentitled account every per-zone listRules |
| 36 | // rejects with the typed `TokenValidationNotEntitled` (or `Forbidden`) tag, |
| 37 | // which `list()` catches per zone -> the aggregate result is a well-typed `[]`. |
| 38 | test.provider( |
| 39 | "list returns a typed array of token validation rules", |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const provider = yield* Provider.findProvider(Cloudflare.TokenValidation.Rule); |
| 45 | const all = yield* provider.list(); |
| 46 | |
| 47 | expect(Array.isArray(all)).toBe(true); |
| 48 | // Each element is the full `read` Attributes shape. |
| 49 | for (const rule of all) { |
| 50 | expect(typeof rule.ruleId).toBe("string"); |
| 51 | expect(typeof rule.zoneId).toBe("string"); |
| 52 | expect(typeof rule.action).toBe("string"); |
| 53 | } |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | }).pipe(logLevel), |
| 57 | { |
| 58 | tags: ["provider:cloudflare", "provider:cloudflare:tokenvalidation", "live"], |
| 59 | timeout: 120_000, |
| 60 | }, |
| 61 | ); |
| 62 | |
| 63 | // Full lifecycle: gated on an entitled zone. Deploy a rule, then assert it is |
| 64 | // present in the exhaustively-paginated `list()` output. |
| 65 | test.provider.skipIf(!entitledZoneId)( |
| 66 | "list enumerates the deployed token validation rule", |
| 67 | (stack) => |
| 68 | Effect.gen(function* () { |
| 69 | const zoneId = entitledZoneId!; |
| 70 | |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const deployed = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const config = yield* Cloudflare.TokenValidation.TokenConfiguration("JwtConfig", { |
| 76 | zoneId, |
| 77 | description: "list-test", |
| 78 | tokenSources: ['http.request.headers["authorization"][0]'], |
| 79 | keys: [JWKS_KEY_1], |
| 80 | }); |
| 81 | return yield* Cloudflare.TokenValidation.Rule("JwtRule", { |
| 82 | zoneId, |
| 83 | action: "log", |
| 84 | expression: Output.interpolate`is_jwt_valid("${config.configId}")`, |
| 85 | selector: { include: [{ host: [zoneName] }] }, |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | const provider = yield* Provider.findProvider(Cloudflare.TokenValidation.Rule); |
| 91 | const all = yield* provider.list(); |
| 92 | |
| 93 | const found = all.find((r) => r.zoneId === zoneId && r.ruleId === deployed.ruleId); |
| 94 | expect(found).toBeDefined(); |
| 95 | expect(found?.action).toEqual("log"); |
| 96 | |
| 97 | yield* stack.destroy(); |
| 98 | }).pipe(logLevel), |
| 99 | { |
| 100 | tags: ["provider:cloudflare", "provider:cloudflare:tokenvalidation", "live"], |
| 101 | timeout: 120_000, |
| 102 | }, |
| 103 | ); |