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 aiSecurity from "@distilled.cloud/cloudflare/ai-security"; |
| 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 | |
| 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 | // AI Security for Apps (Firewall for AI) is entitlement-gated — on the |
| 19 | // standard testing account every call fails with "not entitled to access |
| 20 | // this resource" (code 13101), surfaced as the typed `AiSecurityNotEntitled` |
| 21 | // error. The full lifecycle test below is gated behind an entitled zone id |
| 22 | // supplied via env. |
| 23 | const entitledZoneId = process.env.CLOUDFLARE_TEST_AI_SECURITY_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 | // A freshly minted scoped token propagates eventually-consistently across |
| 35 | // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls. |
| 36 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 37 | |
| 38 | const getTopics = (zoneId: string) => |
| 39 | aiSecurity.getCustomTopic({ zoneId }).pipe( |
| 40 | Effect.retry({ |
| 41 | while: (e) => e._tag === "Forbidden", |
| 42 | schedule: forbiddenRetrySchedule, |
| 43 | times: 8, |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | // Normalize the list to a known baseline so each run starts from the same |
| 48 | // cloud state regardless of what a previous run left behind. |
| 49 | const setBaseline = (zoneId: string, topics: { label: string; topic: string }[]) => |
| 50 | aiSecurity.putCustomTopic({ zoneId, topics }).pipe( |
| 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 AiSecurityNotEntitled error on unentitled accounts", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | const zoneId = yield* resolveZoneId; |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | // The testing account lacks the AI Security entitlement — both the |
| 67 | // read and the write must fail with the typed entitlement tag. |
| 68 | const readError = yield* getTopics(zoneId).pipe(Effect.flip); |
| 69 | expect(readError._tag).toEqual("AiSecurityNotEntitled"); |
| 70 | |
| 71 | const writeError = yield* setBaseline(zoneId, []).pipe(Effect.flip); |
| 72 | expect(writeError._tag).toEqual("AiSecurityNotEntitled"); |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | }).pipe(logLevel), |
| 76 | { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] }, |
| 77 | ); |
| 78 | |
| 79 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 80 | // API for this per-zone list, so `list()` enumerates every zone via |
| 81 | // `listAllZones` and reads the singleton in each, skipping zones that reject |
| 82 | // with the typed `AiSecurityNotEntitled` / `ZoneNotAuthorized` / `Forbidden` |
| 83 | // tags. On the unentitled testing account every zone is skipped, so the |
| 84 | // result is an empty array — the assertion is that `list()` resolves to an |
| 85 | // array (proving the typed skip path) rather than throwing. Presence of the |
| 86 | // standing test zone is asserted only on an entitled account (env-gated). |
| 87 | test.provider( |
| 88 | "list enumerates the custom topics across all zones", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | const provider = yield* Provider.findProvider(Cloudflare.AI.CustomTopics); |
| 92 | const all = yield* provider.list(); |
| 93 | |
| 94 | expect(Array.isArray(all)).toBe(true); |
| 95 | |
| 96 | if (entitledZoneId) { |
| 97 | expect(all.some((t) => t.zoneId === entitledZoneId)).toBe(true); |
| 98 | } |
| 99 | |
| 100 | // `stack` is unused (the singleton always exists on every entitled zone), |
| 101 | // but keep the destroy bookends so the harness state stays clean. |
| 102 | yield* stack.destroy(); |
| 103 | }).pipe(logLevel), |
| 104 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] }, |
| 105 | ); |
| 106 | |
| 107 | test.provider.skipIf(!entitledZoneId)( |
| 108 | "sets topics, updates the list in place, and restores on destroy", |
| 109 | (stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const zoneId = entitledZoneId!; |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | // Known baseline: no custom topics. |
| 115 | yield* setBaseline(zoneId, []); |
| 116 | |
| 117 | const created = yield* stack.deploy( |
| 118 | Effect.gen(function* () { |
| 119 | return yield* Cloudflare.AI.CustomTopics("Topics", { |
| 120 | zoneId, |
| 121 | topics: [ |
| 122 | { label: "billing", topic: "Questions about invoices and payments" }, |
| 123 | { label: "abuse", topic: "Harassment or abusive language" }, |
| 124 | ], |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(created.zoneId).toEqual(zoneId); |
| 130 | expect(created.topics).toHaveLength(2); |
| 131 | // The pre-management list was captured for restore-on-destroy. |
| 132 | expect(created.initialTopics).toEqual([]); |
| 133 | |
| 134 | // Out-of-band verification via the distilled API. |
| 135 | const live = yield* getTopics(zoneId); |
| 136 | expect(live.topics ?? []).toHaveLength(2); |
| 137 | |
| 138 | // Update in place — the PUT replaces the whole list. |
| 139 | const updated = yield* stack.deploy( |
| 140 | Effect.gen(function* () { |
| 141 | return yield* Cloudflare.AI.CustomTopics("Topics", { |
| 142 | zoneId, |
| 143 | topics: [{ label: "support", topic: "Questions about product support" }], |
| 144 | }); |
| 145 | }), |
| 146 | ); |
| 147 | expect(updated.topics).toHaveLength(1); |
| 148 | expect(updated.topics[0]?.label).toEqual("support"); |
| 149 | expect(updated.initialTopics).toEqual([]); |
| 150 | |
| 151 | const replacedList = yield* getTopics(zoneId); |
| 152 | expect(replacedList.topics ?? []).toHaveLength(1); |
| 153 | |
| 154 | yield* stack.destroy(); |
| 155 | |
| 156 | // Destroy restored the (empty) list the zone had before we managed it. |
| 157 | const restored = yield* getTopics(zoneId); |
| 158 | expect(restored.topics ?? []).toEqual([]); |
| 159 | }).pipe(logLevel), |
| 160 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], timeout: 120_000 }, |
| 161 | ); |