@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 265 lines 10.6 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, 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 * as Data from "effect/Data";
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 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const GATEWAY_ID = testName("alchemy-test-aigw-dataset");
17 const GATEWAY_ID_B = testName("alchemy-test-aigw-dataset-b");
18
19 class DatasetStillExists extends Data.TaggedError("DatasetStillExists") {}
20
21 // A deleted dataset surfaces as `DatasetNotFound` (Cloudflare error code
22 // 7002, which also covers a deleted parent gateway) — that's the success
23 // condition here.
24 const expectGone = (accountId: string, gatewayId: string, datasetId: string) =>
25 aiGateway.getDataset({ accountId, gatewayId, id: datasetId }).pipe(
26 Effect.flatMap(() => Effect.fail(new DatasetStillExists())),
27 Effect.retry({
28 while: (e): e is DatasetStillExists => e instanceof DatasetStillExists,
29 schedule: Schedule.max([Schedule.exponential("250 millis"), Schedule.recurs(10)]),
30 }),
31 Effect.catchTag("DatasetNotFound", () => Effect.void),
32 );
33
34 // Every case provisions a gateway under the same fixed `GATEWAY_ID`, so under
35 // the suite's default concurrent execution one case's `stack.destroy()` would
36 // delete the gateway out from under another (yielding "Gateway ... not
37 // found"). Run them one at a time so each owns its gateway lifecycle.
38 describe.sequential(
39 "AiGatewayDataset",
40 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] },
41 () => {
42 test.provider("create, update, delete a dataset on a gateway", (stack) =>
43 Effect.gen(function* () {
44 const { accountId } = yield* yield* CloudflareEnvironment;
45
46 yield* stack.destroy();
47
48 const initial = yield* stack.deploy(
49 Effect.gen(function* () {
50 const gateway = yield* Cloudflare.AI.Gateway("DatasetGateway", { id: GATEWAY_ID });
51 const dataset = yield* Cloudflare.AI.Dataset("Dataset", {
52 gatewayId: gateway.gatewayId,
53 name: testName("alchemy-test-dataset"),
54 filters: [{ key: "success", operator: "eq", value: [true] }],
55 });
56 return { gateway, dataset };
57 }),
58 );
59
60 expect(initial.dataset.datasetId).toBeDefined();
61 expect(initial.dataset.accountId).toEqual(accountId);
62 expect(initial.dataset.gatewayId).toEqual(GATEWAY_ID);
63 expect(initial.dataset.name).toEqual(testName("alchemy-test-dataset"));
64 expect(initial.dataset.enable).toBe(true);
65 expect(initial.dataset.filters).toEqual([
66 { key: "success", operator: "eq", value: [true] },
67 ]);
68
69 // Verify out-of-band via the API.
70 const live = yield* aiGateway.getDataset({
71 accountId,
72 gatewayId: GATEWAY_ID,
73 id: initial.dataset.datasetId,
74 });
75 expect(live.name).toEqual(testName("alchemy-test-dataset"));
76 expect(live.enable).toBe(true);
77 expect(live.filters).toEqual([{ key: "success", operator: "eq", value: [true] }]);
78
79 // Update mutable props in place — same dataset id.
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 const gateway = yield* Cloudflare.AI.Gateway("DatasetGateway", { id: GATEWAY_ID });
83 const dataset = yield* Cloudflare.AI.Dataset("Dataset", {
84 gatewayId: gateway.gatewayId,
85 name: testName("alchemy-test-dataset-v2"),
86 enable: false,
87 filters: [
88 { key: "provider", operator: "eq", value: ["workers-ai"] },
89 { key: "cached", operator: "eq", value: [false] },
90 ],
91 });
92 return { gateway, dataset };
93 }),
94 );
95
96 expect(updated.dataset.datasetId).toEqual(initial.dataset.datasetId);
97 expect(updated.dataset.name).toEqual(testName("alchemy-test-dataset-v2"));
98 expect(updated.dataset.enable).toBe(false);
99 expect(updated.dataset.filters).toHaveLength(2);
100
101 const liveUpdated = yield* aiGateway.getDataset({
102 accountId,
103 gatewayId: GATEWAY_ID,
104 id: initial.dataset.datasetId,
105 });
106 expect(liveUpdated.name).toEqual(testName("alchemy-test-dataset-v2"));
107 expect(liveUpdated.enable).toBe(false);
108 expect(liveUpdated.filters).toHaveLength(2);
109
110 // Redeploying identical props is a no-op (still the same dataset).
111 const noop = yield* stack.deploy(
112 Effect.gen(function* () {
113 const gateway = yield* Cloudflare.AI.Gateway("DatasetGateway", { id: GATEWAY_ID });
114 const dataset = yield* Cloudflare.AI.Dataset("Dataset", {
115 gatewayId: gateway.gatewayId,
116 name: testName("alchemy-test-dataset-v2"),
117 enable: false,
118 filters: [
119 { key: "provider", operator: "eq", value: ["workers-ai"] },
120 { key: "cached", operator: "eq", value: [false] },
121 ],
122 });
123 return { gateway, dataset };
124 }),
125 );
126 expect(noop.dataset.datasetId).toEqual(initial.dataset.datasetId);
127
128 yield* stack.destroy();
129
130 yield* expectGone(accountId, GATEWAY_ID, initial.dataset.datasetId);
131 }).pipe(logLevel),
132 );
133
134 test.provider("replaces dataset when the gateway changes", (stack) =>
135 Effect.gen(function* () {
136 const { accountId } = yield* yield* CloudflareEnvironment;
137
138 yield* stack.destroy();
139
140 const initial = yield* stack.deploy(
141 Effect.gen(function* () {
142 const gatewayA = yield* Cloudflare.AI.Gateway("ReplaceGatewayA", { id: GATEWAY_ID });
143 yield* Cloudflare.AI.Gateway("ReplaceGatewayB", { id: GATEWAY_ID_B });
144 const dataset = yield* Cloudflare.AI.Dataset("ReplaceDataset", {
145 gatewayId: gatewayA.gatewayId,
146 name: testName("alchemy-test-dataset-replace"),
147 filters: [],
148 });
149 return { dataset };
150 }),
151 );
152 expect(initial.dataset.gatewayId).toEqual(GATEWAY_ID);
153
154 // Moving the dataset to another gateway is a replacement: new id,
155 // new parent, and the old dataset is removed from gateway A.
156 const moved = yield* stack.deploy(
157 Effect.gen(function* () {
158 yield* Cloudflare.AI.Gateway("ReplaceGatewayA", { id: GATEWAY_ID });
159 const gatewayB = yield* Cloudflare.AI.Gateway("ReplaceGatewayB", { id: GATEWAY_ID_B });
160 const dataset = yield* Cloudflare.AI.Dataset("ReplaceDataset", {
161 gatewayId: gatewayB.gatewayId,
162 name: testName("alchemy-test-dataset-replace"),
163 filters: [],
164 });
165 return { dataset };
166 }),
167 );
168
169 expect(moved.dataset.gatewayId).toEqual(GATEWAY_ID_B);
170 expect(moved.dataset.datasetId).not.toEqual(initial.dataset.datasetId);
171
172 // The replaced dataset is gone from gateway A.
173 yield* expectGone(accountId, GATEWAY_ID, initial.dataset.datasetId);
174
175 yield* stack.destroy();
176
177 yield* expectGone(accountId, GATEWAY_ID_B, moved.dataset.datasetId);
178 }).pipe(logLevel),
179 );
180
181 test.provider("recreates a dataset after out-of-band delete", (stack) =>
182 Effect.gen(function* () {
183 const { accountId } = yield* yield* CloudflareEnvironment;
184
185 yield* stack.destroy();
186
187 const initial = yield* stack.deploy(
188 Effect.gen(function* () {
189 const gateway = yield* Cloudflare.AI.Gateway("HealGateway", { id: GATEWAY_ID });
190 const dataset = yield* Cloudflare.AI.Dataset("HealDataset", {
191 gatewayId: gateway.gatewayId,
192 name: testName("alchemy-test-dataset-heal"),
193 filters: [{ key: "success", operator: "eq", value: [true] }],
194 });
195 return { dataset };
196 }),
197 );
198
199 // Delete the dataset out-of-band. A redeploy with identical props is a
200 // planner no-op, so change a prop to force reconcile — it must observe
201 // the dataset as missing and recreate it instead of failing on a 404.
202 yield* aiGateway.deleteDataset({
203 accountId,
204 gatewayId: GATEWAY_ID,
205 id: initial.dataset.datasetId,
206 });
207
208 const healed = yield* stack.deploy(
209 Effect.gen(function* () {
210 const gateway = yield* Cloudflare.AI.Gateway("HealGateway", { id: GATEWAY_ID });
211 const dataset = yield* Cloudflare.AI.Dataset("HealDataset", {
212 gatewayId: gateway.gatewayId,
213 name: testName("alchemy-test-dataset-heal"),
214 enable: false,
215 filters: [{ key: "success", operator: "eq", value: [true] }],
216 });
217 return { dataset };
218 }),
219 );
220
221 expect(healed.dataset.datasetId).not.toEqual(initial.dataset.datasetId);
222 expect(healed.dataset.enable).toBe(false);
223
224 const live = yield* aiGateway.getDataset({
225 accountId,
226 gatewayId: GATEWAY_ID,
227 id: healed.dataset.datasetId,
228 });
229 expect(live.name).toEqual(testName("alchemy-test-dataset-heal"));
230
231 yield* stack.destroy();
232
233 yield* expectGone(accountId, GATEWAY_ID, healed.dataset.datasetId);
234 }).pipe(logLevel),
235 );
236
237 test.provider("list enumerates datasets across gateways", (stack) =>
238 Effect.gen(function* () {
239 const { accountId } = yield* yield* CloudflareEnvironment;
240
241 yield* stack.destroy();
242
243 const deployed = yield* stack.deploy(
244 Effect.gen(function* () {
245 const gateway = yield* Cloudflare.AI.Gateway("ListGateway", { id: GATEWAY_ID });
246 const dataset = yield* Cloudflare.AI.Dataset("ListDataset", {
247 gatewayId: gateway.gatewayId,
248 filters: [{ key: "success", operator: "eq", value: [true] }],
249 });
250 return { dataset };
251 }),
252 );
253
254 const provider = yield* Provider.findProvider(Cloudflare.AI.Dataset);
255 const all = yield* provider.list();
256
257 expect(all.some((d) => d.datasetId === deployed.dataset.datasetId)).toBe(true);
258
259 yield* stack.destroy();
260
261 yield* expectGone(accountId, GATEWAY_ID, deployed.dataset.datasetId);
262 }).pipe(logLevel),
263 );
264 },
265 );