The best way to control Claude Code, Codex and more from any computer or phone

main
ts 287 lines 12 KB
Raw
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 Provider from "alchemy/Provider";
7 import { poll } from "alchemy/Util/poll";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12 import { testName } from "../../names.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const GATEWAY_ID = testName("alchemy-test-aigw-routing");
19 const GATEWAY_ID_B = testName("alchemy-test-aigw-routing-b");
20
21 const graph = (model: string, retries: number): Cloudflare.AI.RouteElement[] => [
22 { id: "start", type: "start", outputs: { next: { elementId: "model" } } },
23 {
24 id: "model",
25 type: "model",
26 properties: { provider: "workers-ai", model, retries, timeout: 30_000 },
27 outputs: { success: { elementId: "end" }, fallback: { elementId: "end" } },
28 },
29 { id: "end", type: "end", outputs: {} },
30 ];
31
32 class RouteStillExists extends Data.TaggedError("RouteStillExists") {}
33
34 // A deleted route surfaces as `RouteNotFound` (Cloudflare error code 7005);
35 // once the parent gateway is destroyed the same probe fails with
36 // `GatewayNotFound` (code 7002). Either way the route is gone.
37 const expectGone = (accountId: string, gatewayId: string, routeId: string) =>
38 aiGateway.getDynamicRouting({ accountId, gatewayId, id: routeId }).pipe(
39 Effect.flatMap(() => Effect.fail(new RouteStillExists())),
40 Effect.retry({
41 while: (e): e is RouteStillExists => e instanceof RouteStillExists,
42 schedule: Schedule.max([Schedule.exponential("250 millis"), Schedule.recurs(10)]),
43 }),
44 Effect.catchTag("RouteNotFound", () => Effect.void),
45 Effect.catchTag("GatewayNotFound", () => Effect.void),
46 );
47
48 test.provider(
49 "create, update elements (new deployed version), rename, delete",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53
54 yield* stack.destroy();
55
56 const initial = yield* stack.deploy(
57 Effect.gen(function* () {
58 const gateway = yield* Cloudflare.AI.Gateway("RoutingGateway", { id: GATEWAY_ID });
59 const route = yield* Cloudflare.AI.GatewayDynamicRouting("Route", {
60 gatewayId: gateway.gatewayId,
61 name: testName("alchemy-test-route"),
62 elements: graph("@cf/meta/llama-3.1-8b-instruct", 1),
63 });
64 return { gateway, route };
65 }),
66 );
67
68 expect(initial.route.routeId).toBeDefined();
69 expect(initial.route.accountId).toEqual(accountId);
70 expect(initial.route.gatewayId).toEqual(GATEWAY_ID);
71 expect(initial.route.name).toEqual(testName("alchemy-test-route"));
72 // Creation auto-deploys version 1.
73 expect(initial.route.versionId).toBeDefined();
74 expect(initial.route.deploymentId).toBeDefined();
75 expect(initial.route.elements).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 1));
76
77 // Verify out-of-band via the API: deployed version matches.
78 const live = yield* aiGateway.getDynamicRouting({
79 accountId,
80 gatewayId: GATEWAY_ID,
81 id: initial.route.routeId,
82 });
83 expect(live.name).toEqual(testName("alchemy-test-route"));
84 expect(live.deployment.versionId).toEqual(initial.route.versionId);
85 expect(live.version.data).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 1));
86
87 // Update the element graph and rename — same route id, but a new
88 // version must be created AND deployed.
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 const gateway = yield* Cloudflare.AI.Gateway("RoutingGateway", { id: GATEWAY_ID });
92 const route = yield* Cloudflare.AI.GatewayDynamicRouting("Route", {
93 gatewayId: gateway.gatewayId,
94 name: testName("alchemy-test-route-v2"),
95 elements: graph("@cf/meta/llama-3.1-8b-instruct", 2),
96 });
97 return { gateway, route };
98 }),
99 );
100
101 expect(updated.route.routeId).toEqual(initial.route.routeId);
102 expect(updated.route.name).toEqual(testName("alchemy-test-route-v2"));
103 expect(updated.route.versionId).not.toEqual(initial.route.versionId);
104 expect(updated.route.elements).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 2));
105
106 const liveUpdated = yield* aiGateway.getDynamicRouting({
107 accountId,
108 gatewayId: GATEWAY_ID,
109 id: initial.route.routeId,
110 });
111 expect(liveUpdated.name).toEqual(testName("alchemy-test-route-v2"));
112 expect(liveUpdated.version.active).toBe(true);
113 expect(liveUpdated.deployment.versionId).toEqual(updated.route.versionId);
114 expect(liveUpdated.version.data).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 2));
115
116 // Redeploying identical props is a no-op (same deployed version).
117 const noop = yield* stack.deploy(
118 Effect.gen(function* () {
119 const gateway = yield* Cloudflare.AI.Gateway("RoutingGateway", { id: GATEWAY_ID });
120 const route = yield* Cloudflare.AI.GatewayDynamicRouting("Route", {
121 gatewayId: gateway.gatewayId,
122 name: testName("alchemy-test-route-v2"),
123 elements: graph("@cf/meta/llama-3.1-8b-instruct", 2),
124 });
125 return { gateway, route };
126 }),
127 );
128 expect(noop.route.routeId).toEqual(initial.route.routeId);
129 expect(noop.route.versionId).toEqual(updated.route.versionId);
130
131 yield* stack.destroy();
132
133 yield* expectGone(accountId, GATEWAY_ID, initial.route.routeId);
134 }).pipe(logLevel),
135 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] },
136 );
137
138 test.provider(
139 "replaces route when the gateway changes",
140 (stack) =>
141 Effect.gen(function* () {
142 const { accountId } = yield* yield* CloudflareEnvironment;
143
144 yield* stack.destroy();
145
146 const initial = yield* stack.deploy(
147 Effect.gen(function* () {
148 const gatewayA = yield* Cloudflare.AI.Gateway("RouteGatewayA", { id: GATEWAY_ID });
149 yield* Cloudflare.AI.Gateway("RouteGatewayB", { id: GATEWAY_ID_B });
150 const route = yield* Cloudflare.AI.GatewayDynamicRouting("ReplaceRoute", {
151 gatewayId: gatewayA.gatewayId,
152 name: testName("alchemy-test-route-replace"),
153 elements: graph("@cf/meta/llama-3.1-8b-instruct", 1),
154 });
155 return { route };
156 }),
157 );
158 expect(initial.route.gatewayId).toEqual(GATEWAY_ID);
159
160 // Moving the route to another gateway is a replacement: new id, new
161 // parent, and the old route is removed from gateway A.
162 const moved = yield* stack.deploy(
163 Effect.gen(function* () {
164 yield* Cloudflare.AI.Gateway("RouteGatewayA", { id: GATEWAY_ID });
165 const gatewayB = yield* Cloudflare.AI.Gateway("RouteGatewayB", { id: GATEWAY_ID_B });
166 const route = yield* Cloudflare.AI.GatewayDynamicRouting("ReplaceRoute", {
167 gatewayId: gatewayB.gatewayId,
168 name: testName("alchemy-test-route-replace"),
169 elements: graph("@cf/meta/llama-3.1-8b-instruct", 1),
170 });
171 return { route };
172 }),
173 );
174
175 expect(moved.route.gatewayId).toEqual(GATEWAY_ID_B);
176 expect(moved.route.routeId).not.toEqual(initial.route.routeId);
177
178 // The replaced route is gone from gateway A.
179 yield* expectGone(accountId, GATEWAY_ID, initial.route.routeId);
180
181 yield* stack.destroy();
182
183 yield* expectGone(accountId, GATEWAY_ID_B, moved.route.routeId);
184 }).pipe(logLevel),
185 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] },
186 );
187
188 test.provider(
189 "recreates a route after out-of-band delete",
190 (stack) =>
191 Effect.gen(function* () {
192 const { accountId } = yield* yield* CloudflareEnvironment;
193
194 yield* stack.destroy();
195
196 const initial = yield* stack.deploy(
197 Effect.gen(function* () {
198 const gateway = yield* Cloudflare.AI.Gateway("HealRouteGateway", { id: GATEWAY_ID });
199 const route = yield* Cloudflare.AI.GatewayDynamicRouting("HealRoute", {
200 gatewayId: gateway.gatewayId,
201 name: testName("alchemy-test-route-heal"),
202 elements: graph("@cf/meta/llama-3.1-8b-instruct", 1),
203 });
204 return { route };
205 }),
206 );
207
208 // Delete the route out-of-band. A redeploy with identical props is a
209 // planner no-op, so change a prop to force reconcile — it must observe
210 // the route as missing and recreate it instead of failing on a 404.
211 yield* aiGateway.deleteDynamicRouting({
212 accountId,
213 gatewayId: GATEWAY_ID,
214 id: initial.route.routeId,
215 });
216
217 const healed = yield* stack.deploy(
218 Effect.gen(function* () {
219 const gateway = yield* Cloudflare.AI.Gateway("HealRouteGateway", { id: GATEWAY_ID });
220 const route = yield* Cloudflare.AI.GatewayDynamicRouting("HealRoute", {
221 gatewayId: gateway.gatewayId,
222 name: testName("alchemy-test-route-heal"),
223 elements: graph("@cf/meta/llama-3.1-8b-instruct", 3),
224 });
225 return { route };
226 }),
227 );
228
229 expect(healed.route.routeId).not.toEqual(initial.route.routeId);
230 expect(healed.route.elements).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 3));
231
232 yield* stack.destroy();
233
234 yield* expectGone(accountId, GATEWAY_ID, healed.route.routeId);
235 }).pipe(logLevel),
236 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] },
237 );
238
239 // Canonical `list()` test (parent fan-out): routes are scoped under a gateway
240 // with no account-wide route API, so `list()` enumerates every account gateway
241 // and exhaustively lists each gateway's routes. Deploy a gateway + route, then
242 // assert the deployed route appears in the exhaustively-paginated result.
243 test.provider(
244 "list enumerates routes across all gateways",
245 (stack) =>
246 Effect.gen(function* () {
247 yield* stack.destroy();
248
249 const deployed = yield* stack.deploy(
250 Effect.gen(function* () {
251 const gateway = yield* Cloudflare.AI.Gateway("ListRouteGateway", { id: GATEWAY_ID });
252 const route = yield* Cloudflare.AI.GatewayDynamicRouting("ListRoute", {
253 gatewayId: gateway.gatewayId,
254 name: testName("alchemy-test-route-list"),
255 elements: graph("@cf/meta/llama-3.1-8b-instruct", 1),
256 });
257 return { route };
258 }),
259 );
260
261 const provider = yield* Provider.findProvider(Cloudflare.AI.GatewayDynamicRouting);
262
263 // The route appears in list() shortly after deploy, but its element graph
264 // is materialized from a separate deployed-version lookup that propagates
265 // with its own eventual-consistency lag. Poll until the route is present
266 // AND its graph has propagated before asserting.
267 const all = yield* poll({
268 description: "list() includes the deployed route with its element graph",
269 effect: provider.list(),
270 predicate: (all) =>
271 (all.find((r) => r.routeId === deployed.route.routeId)?.elements?.length ?? 0) > 0,
272 // Bound the poll so it converges (or fails with a clear PredicateFailed)
273 // well within the test timeout below — the default schedule (50 × 5s)
274 // outruns the timeout and surfaces as an opaque "Test timed out".
275 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(40)]),
276 });
277
278 const found = all.find((r) => r.routeId === deployed.route.routeId);
279 expect(found).toBeDefined();
280 expect(found?.gatewayId).toEqual(GATEWAY_ID);
281 expect(found?.name).toEqual(testName("alchemy-test-route-list"));
282 expect(found?.elements).toEqual(graph("@cf/meta/llama-3.1-8b-instruct", 1));
283
284 yield* stack.destroy();
285 }).pipe(logLevel),
286 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], timeout: 180_000 },
287 );