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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as aiGateway from "@distilled.cloud/cloudflare/ai-gateway"; |
| 6 | import * as Alchemy from "alchemy"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import { State } from "alchemy/State"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import { testName } from "../../names.ts"; |
| 15 | import { Gateway } from "./fixtures/Gateway.ts"; |
| 16 | import TestWorker from "./fixtures/TestWorker.ts"; |
| 17 | |
| 18 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 19 | providers: Cloudflare.providers(), |
| 20 | }); |
| 21 | |
| 22 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 23 | |
| 24 | test.provider( |
| 25 | "create and delete ai gateway with default props", |
| 26 | (stack) => |
| 27 | Effect.gen(function* () { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const gateway = yield* stack.deploy( |
| 33 | Effect.gen(function* () { |
| 34 | return yield* Cloudflare.AI.Gateway("DefaultGateway", { |
| 35 | id: "alchemy-test-ai-gateway-default", |
| 36 | }); |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | expect(gateway.gatewayId).toEqual("alchemy-test-ai-gateway-default"); |
| 41 | expect(gateway.cacheInvalidateOnUpdate).toEqual(false); |
| 42 | expect(gateway.cacheTtl).toEqual(null); |
| 43 | expect(gateway.collectLogs).toEqual(true); |
| 44 | expect(gateway.rateLimitingInterval).toEqual(null); |
| 45 | expect(gateway.rateLimitingLimit).toEqual(null); |
| 46 | expect(gateway.rateLimitingTechnique).toEqual("fixed"); |
| 47 | |
| 48 | const actualGateway = yield* aiGateway.getAiGateway({ accountId, id: gateway.gatewayId }); |
| 49 | expect(actualGateway.id).toEqual(gateway.gatewayId); |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | yield* waitForGatewayToBeDeleted(gateway.gatewayId, accountId); |
| 54 | }).pipe(logLevel), |
| 55 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"] }, |
| 56 | ); |
| 57 | |
| 58 | test.provider( |
| 59 | "create, update, delete ai gateway", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const gateway = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* Cloudflare.AI.Gateway("TestGateway", { |
| 69 | id: "alchemy-test-ai-gateway", |
| 70 | cacheTtl: 60, |
| 71 | collectLogs: true, |
| 72 | rateLimitingInterval: 60, |
| 73 | rateLimitingLimit: 100, |
| 74 | rateLimitingTechnique: "fixed", |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | const actualGateway = yield* aiGateway.getAiGateway({ accountId, id: gateway.gatewayId }); |
| 80 | expect(actualGateway.id).toEqual(gateway.gatewayId); |
| 81 | expect(actualGateway.cacheTtl).toEqual(60); |
| 82 | expect(actualGateway.rateLimitingLimit).toEqual(100); |
| 83 | |
| 84 | const updatedGateway = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | return yield* Cloudflare.AI.Gateway("TestGateway", { |
| 87 | id: "alchemy-test-ai-gateway", |
| 88 | cacheTtl: 120, |
| 89 | collectLogs: true, |
| 90 | rateLimitingInterval: 120, |
| 91 | rateLimitingLimit: 200, |
| 92 | rateLimitingTechnique: "sliding", |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | const actualUpdatedGateway = yield* aiGateway.getAiGateway({ |
| 98 | accountId, |
| 99 | id: updatedGateway.gatewayId, |
| 100 | }); |
| 101 | expect(actualUpdatedGateway.cacheTtl).toEqual(120); |
| 102 | expect(actualUpdatedGateway.rateLimitingInterval).toEqual(120); |
| 103 | expect(actualUpdatedGateway.rateLimitingLimit).toEqual(200); |
| 104 | expect(actualUpdatedGateway.rateLimitingTechnique).toEqual("sliding"); |
| 105 | |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | yield* waitForGatewayToBeDeleted(gateway.gatewayId, accountId); |
| 109 | }).pipe(logLevel), |
| 110 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"] }, |
| 111 | ); |
| 112 | |
| 113 | // Per-gateway spend limits replace the deprecated account-level spending |
| 114 | // limit. Verify they are applied on create, mutate in place, and that a |
| 115 | // redeploy of identical props is a no-op (exercising the diff normalization |
| 116 | // that drops the server-assigned rule id and per-rule `enabled` default). |
| 117 | test.provider( |
| 118 | "create, update spend limits, delete ai gateway", |
| 119 | (stack) => |
| 120 | Effect.gen(function* () { |
| 121 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const gateway = yield* stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | return yield* Cloudflare.AI.Gateway("SpendGateway", { |
| 128 | id: "alchemy-test-ai-gateway-spend", |
| 129 | spendLimits: { |
| 130 | enabled: true, |
| 131 | // Effect Duration.Input is decoded to whole seconds for the API. |
| 132 | rules: [{ limitType: "cost", limit: 500_00, window: "1 day" }], |
| 133 | }, |
| 134 | }); |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | expect(gateway.spendLimits?.enabled).toEqual(true); |
| 139 | expect(gateway.spendLimits?.rules?.[0]?.limit).toEqual(500_00); |
| 140 | expect(gateway.spendLimits?.rules?.[0]?.window).toEqual(86_400); |
| 141 | |
| 142 | const live = yield* aiGateway.getAiGateway({ accountId, id: gateway.gatewayId }); |
| 143 | expect(live.spendLimits?.enabled).toEqual(true); |
| 144 | expect(live.spendLimits?.rules?.[0]?.limit).toEqual(500_00); |
| 145 | expect(live.spendLimits?.rules?.[0]?.window).toEqual(86_400); |
| 146 | |
| 147 | // Re-deploying identical props must be a no-op (no perpetual drift from |
| 148 | // the server-assigned rule id / enabled default). |
| 149 | const again = yield* stack.deploy( |
| 150 | Effect.gen(function* () { |
| 151 | return yield* Cloudflare.AI.Gateway("SpendGateway", { |
| 152 | id: "alchemy-test-ai-gateway-spend", |
| 153 | spendLimits: { |
| 154 | enabled: true, |
| 155 | rules: [{ limitType: "cost", limit: 500_00, window: "1 day" }], |
| 156 | }, |
| 157 | }); |
| 158 | }), |
| 159 | ); |
| 160 | expect(again.spendLimits?.rules?.[0]?.limit).toEqual(500_00); |
| 161 | |
| 162 | // Update the cap in place. |
| 163 | const updated = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | return yield* Cloudflare.AI.Gateway("SpendGateway", { |
| 166 | id: "alchemy-test-ai-gateway-spend", |
| 167 | spendLimits: { |
| 168 | enabled: true, |
| 169 | rules: [{ limitType: "cost", limit: 1_000_00, window: "1 hour" }], |
| 170 | }, |
| 171 | }); |
| 172 | }), |
| 173 | ); |
| 174 | expect(updated.spendLimits?.rules?.[0]?.limit).toEqual(1_000_00); |
| 175 | |
| 176 | const liveUpdated = yield* aiGateway.getAiGateway({ accountId, id: gateway.gatewayId }); |
| 177 | expect(liveUpdated.spendLimits?.rules?.[0]?.limit).toEqual(1_000_00); |
| 178 | expect(liveUpdated.spendLimits?.rules?.[0]?.window).toEqual(3_600); |
| 179 | |
| 180 | yield* stack.destroy(); |
| 181 | yield* waitForGatewayToBeDeleted(gateway.gatewayId, accountId); |
| 182 | }).pipe(logLevel), |
| 183 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"] }, |
| 184 | ); |
| 185 | |
| 186 | test.provider( |
| 187 | "list enumerates the deployed ai gateway", |
| 188 | (stack) => |
| 189 | Effect.gen(function* () { |
| 190 | yield* stack.destroy(); |
| 191 | |
| 192 | const gatewayId = testName("alchemy-test-ai-gateway-list"); |
| 193 | |
| 194 | const deployed = yield* stack.deploy( |
| 195 | Effect.gen(function* () { |
| 196 | return yield* Cloudflare.AI.Gateway("ListGateway", { id: gatewayId }); |
| 197 | }), |
| 198 | ); |
| 199 | |
| 200 | const provider = yield* Provider.findProvider(Cloudflare.AI.Gateway); |
| 201 | const all = yield* provider.list(); |
| 202 | |
| 203 | expect(all.some((g) => g.gatewayId === deployed.gatewayId)).toBe(true); |
| 204 | |
| 205 | yield* stack.destroy(); |
| 206 | yield* waitForGatewayToBeDeleted(deployed.gatewayId, deployed.accountId); |
| 207 | }).pipe(logLevel), |
| 208 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"] }, |
| 209 | ); |
| 210 | |
| 211 | // Engine-level adoption: AI Gateways have no ownership signal (Cloudflare |
| 212 | // doesn't expose tags on AI Gateways), so a name match in `read` is treated |
| 213 | // as silent adoption. |
| 214 | test.provider( |
| 215 | "existing ai gateway (matching id) is silently adopted without --adopt", |
| 216 | (stack) => |
| 217 | Effect.gen(function* () { |
| 218 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 219 | |
| 220 | yield* stack.destroy(); |
| 221 | |
| 222 | // Phase 1: deploy normally so a real AI Gateway exists. No explicit |
| 223 | // `id` — the engine generates a random-suffixed physical id |
| 224 | // (collision-free across concurrent runs); the deploy output hands |
| 225 | // back the real id, which pins the gateway's identity for the |
| 226 | // adoption phase below. |
| 227 | const initial = yield* stack.deploy( |
| 228 | Effect.gen(function* () { |
| 229 | return yield* Cloudflare.AI.Gateway("AdoptableGateway"); |
| 230 | }), |
| 231 | ); |
| 232 | const gatewayId = initial.gatewayId; |
| 233 | |
| 234 | // Phase 2: wipe local state — the gateway stays on Cloudflare. |
| 235 | yield* Effect.gen(function* () { |
| 236 | const state = yield* yield* State; |
| 237 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "AdoptableGateway" }); |
| 238 | }).pipe(Effect.provide(stack.state)); |
| 239 | |
| 240 | // Phase 3: redeploy without `adopt(true)`. The engine calls |
| 241 | // `provider.read`, which fetches the gateway by id and returns plain |
| 242 | // attrs — silent adoption. |
| 243 | const adopted = yield* stack.deploy( |
| 244 | Effect.gen(function* () { |
| 245 | return yield* Cloudflare.AI.Gateway("AdoptableGateway", { id: gatewayId }); |
| 246 | }), |
| 247 | ); |
| 248 | |
| 249 | expect(adopted.gatewayId).toEqual(gatewayId); |
| 250 | |
| 251 | const persisted = yield* Effect.gen(function* () { |
| 252 | const state = yield* yield* State; |
| 253 | return yield* state.get({ stack: stack.name, stage: stack.stage, fqn: "AdoptableGateway" }); |
| 254 | }).pipe(Effect.provide(stack.state)); |
| 255 | |
| 256 | expect((persisted as any)?.attr).toMatchObject({ gatewayId }); |
| 257 | |
| 258 | yield* stack.destroy(); |
| 259 | yield* waitForGatewayToBeDeleted(gatewayId, accountId); |
| 260 | }).pipe(logLevel), |
| 261 | { tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"] }, |
| 262 | ); |
| 263 | |
| 264 | const waitForGatewayToBeDeleted = Effect.fn(function* (gatewayId: string, accountId: string) { |
| 265 | yield* aiGateway.getAiGateway({ accountId, id: gatewayId }).pipe( |
| 266 | Effect.flatMap(() => Effect.fail(new GatewayStillExists())), |
| 267 | Effect.retry({ |
| 268 | while: (e): e is GatewayStillExists => e instanceof GatewayStillExists, |
| 269 | schedule: Schedule.exponential(100), |
| 270 | }), |
| 271 | Effect.catchTag("GatewayNotFound", () => Effect.void), |
| 272 | ); |
| 273 | }); |
| 274 | |
| 275 | class GatewayStillExists extends Data.TaggedError("GatewayStillExists") {} |
| 276 | |
| 277 | const Stack = Alchemy.Stack( |
| 278 | "AiGatewayBindingStack", |
| 279 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 280 | Effect.gen(function* () { |
| 281 | const gateway = yield* Gateway; |
| 282 | const worker = yield* TestWorker; |
| 283 | return { gatewayId: gateway.gatewayId, url: worker.url.as<string>() }; |
| 284 | }), |
| 285 | ); |
| 286 | |
| 287 | const stack = beforeAll(deploy(Stack)); |
| 288 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 289 | |
| 290 | test( |
| 291 | "deployed worker can call AiGateway binding (effect-native getUrl)", |
| 292 | Effect.gen(function* () { |
| 293 | const out = yield* stack; |
| 294 | const workerUrl = out.url; |
| 295 | expect(workerUrl).toBeTypeOf("string"); |
| 296 | expect(out.gatewayId).toBeTypeOf("string"); |
| 297 | expect(out.gatewayId.length).toBeGreaterThan(0); |
| 298 | |
| 299 | const client = yield* HttpClient.HttpClient; |
| 300 | const res = yield* client.get(`${workerUrl}/url`).pipe( |
| 301 | Effect.flatMap((res) => |
| 302 | res.status === 200 |
| 303 | ? Effect.succeed(res) |
| 304 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 305 | ), |
| 306 | Effect.retry({ schedule: Schedule.exponential("500 millis"), times: 15 }), |
| 307 | ); |
| 308 | expect(res.status).toBe(200); |
| 309 | const body = (yield* res.json) as { url: string }; |
| 310 | // The runtime gateway exposes a stable account-scoped URL like |
| 311 | // https://gateway.ai.cloudflare.com/v1/<accountId>/<gatewayId> |
| 312 | expect(body.url).toContain(out.gatewayId); |
| 313 | expect(body.url).toContain("gateway.ai.cloudflare.com"); |
| 314 | }).pipe(logLevel), |
| 315 | { |
| 316 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 317 | timeout: 180_000, |
| 318 | }, |
| 319 | ); |