@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 416 lines 14.8 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 aisearch from "@distilled.cloud/cloudflare/aisearch";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import AiSearchCrawlTargetWorker from "./fixtures/crawl-target-worker.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // The scoped API token the test harness mints propagates eventually-
18 // consistently across Cloudflare's edge — ride out 403 blips
19 // (`Forbidden`, declared in the distilled error union) on the test's
20 // own out-of-band verification calls. Instances are namespace-scoped, so
21 // verify through the namespace-scoped read (defaulting to `default`).
22 const getInstance = (accountId: string, id: string, namespace = "default") =>
23 aisearch.readNamespaceInstance({ accountId, namespace, id }).pipe(
24 Effect.retry({
25 while: (e) => e._tag === "Forbidden",
26 schedule: Schedule.exponential("500 millis"),
27 times: 8,
28 }),
29 );
30
31 const expectGone = (accountId: string, id: string, namespace = "default") =>
32 getInstance(accountId, id, namespace).pipe(
33 Effect.flatMap(() => Effect.fail({ _tag: "InstanceNotDeleted" } as const)),
34 // A missing instance (`AiSearchInstanceNotFound`, code 7002) or a
35 // missing enclosing namespace (`NamespaceNotFound`, code 7063) is the
36 // success condition here.
37 Effect.catchTag(["AiSearchInstanceNotFound", "NamespaceNotFound"], () => Effect.void),
38 Effect.retry({
39 while: (e) => e._tag === "InstanceNotDeleted",
40 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
41 }),
42 );
43
44 // Keep each stack's credential explicit so Cloudflare cannot reuse a token
45 // owned by another test stack.
46 const scopedToken = Effect.gen(function* () {
47 const { accountId } = yield* yield* CloudflareEnvironment;
48 const apiToken = yield* Cloudflare.ApiToken.AccountApiToken("TokenSource", {
49 policies: [
50 {
51 effect: "allow",
52 permissionGroups: ["AI Search Index Engine"],
53 resources: { [`com.cloudflare.api.account.${accountId}`]: "*" },
54 },
55 ],
56 });
57 return yield* Cloudflare.AI.SearchToken("Token", {
58 cfApiId: apiToken.tokenId,
59 cfApiKey: apiToken.value,
60 });
61 });
62
63 // One program deploying both the R2 source bucket and the AI Search
64 // instance indexing it. The instance's `source` references the bucket
65 // name so the engine orders instance-after-bucket on deploy (and the
66 // reverse on destroy).
67 const program = (props?: Partial<Cloudflare.AI.SearchInstanceProps>) =>
68 Effect.gen(function* () {
69 const bucket = yield* Cloudflare.R2.Bucket("AiSearchSource", {
70 forceDestroy: true,
71 });
72 const token = yield* scopedToken;
73 const instance = yield* Cloudflare.AI.SearchInstance("Search", {
74 tokenId: token.id,
75 source: bucket.bucketName,
76 ...props,
77 });
78 return { bucket, instance };
79 });
80
81 test.provider(
82 "create, update mutable props, and delete an r2-backed instance",
83 (stack) =>
84 Effect.gen(function* () {
85 const { accountId } = yield* yield* CloudflareEnvironment;
86
87 yield* stack.destroy();
88
89 // Create — engine-generated instance id, default settings.
90 const initial = yield* stack.deploy(program());
91
92 expect(initial.instance.instanceId).toBeTruthy();
93 expect(initial.instance.accountId).toEqual(accountId);
94 expect(initial.instance.type).toEqual("r2");
95 expect(initial.instance.source).toEqual(initial.bucket.bucketName);
96
97 const live = yield* getInstance(accountId, initial.instance.instanceId);
98 expect(live.id).toEqual(initial.instance.instanceId);
99 expect(live.source).toEqual(initial.bucket.bucketName);
100 expect(live.type).toEqual("r2");
101
102 // Update mutable props in place — same instance id.
103 const updated = yield* stack.deploy(
104 program({
105 aiSearchModel: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
106 maxNumResults: 20,
107 chunkSize: 512,
108 chunkOverlap: 15,
109 }),
110 );
111
112 expect(updated.instance.instanceId).toEqual(initial.instance.instanceId);
113 expect(updated.instance.aiSearchModel).toEqual("@cf/meta/llama-3.3-70b-instruct-fp8-fast");
114
115 // The update PUT returns the new settings immediately, but the
116 // out-of-band read endpoint reflects them eventually-consistently (it
117 // briefly serves `aiSearchModel: ""` right after the write). Poll the
118 // readback until the mutated props land before asserting, bounded.
119 const liveUpdated = yield* getInstance(accountId, updated.instance.instanceId).pipe(
120 Effect.flatMap((live) =>
121 live.aiSearchModel === "@cf/meta/llama-3.3-70b-instruct-fp8-fast"
122 ? Effect.succeed(live)
123 : Effect.fail({ _tag: "InstanceUpdateNotApplied" } as const),
124 ),
125 Effect.retry({
126 while: (e) => e._tag === "InstanceUpdateNotApplied",
127 schedule: Schedule.spaced("3 seconds"),
128 times: 20,
129 }),
130 );
131 expect(liveUpdated.aiSearchModel).toEqual("@cf/meta/llama-3.3-70b-instruct-fp8-fast");
132 expect(liveUpdated.maxNumResults).toEqual(20);
133 expect(liveUpdated.chunkSize).toEqual(512);
134 expect(liveUpdated.chunkOverlap).toEqual(15);
135
136 // Redeploying identical props is a no-op (still the same instance).
137 const noop = yield* stack.deploy(
138 program({
139 aiSearchModel: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
140 maxNumResults: 20,
141 chunkSize: 512,
142 chunkOverlap: 15,
143 }),
144 );
145 expect(noop.instance.instanceId).toEqual(initial.instance.instanceId);
146
147 yield* stack.destroy();
148
149 yield* expectGone(accountId, initial.instance.instanceId);
150
151 // Destroy again — delete must be idempotent (already gone).
152 yield* stack.destroy();
153 }).pipe(logLevel),
154 {
155 tags: [
156 "provider:cloudflare",
157 "provider:cloudflare:ai",
158 "provider:cloudflare:apitoken",
159 "provider:cloudflare:r2",
160 "live",
161 ],
162 timeout: 240_000,
163 },
164 );
165
166 test.provider(
167 "changing the embedding model triggers a replacement",
168 (stack) =>
169 Effect.gen(function* () {
170 const { accountId } = yield* yield* CloudflareEnvironment;
171
172 yield* stack.destroy();
173
174 const initial = yield* stack.deploy(program({ embeddingModel: "@cf/baai/bge-m3" }));
175 expect(initial.instance.embeddingModel).toEqual("@cf/baai/bge-m3");
176
177 const replaced = yield* stack.deploy(
178 program({ embeddingModel: "@cf/baai/bge-large-en-v1.5" }),
179 );
180
181 // The embedding model defines the vector space and is fixed at
182 // creation — a new physical instance exists.
183 expect(replaced.instance.instanceId).not.toEqual(initial.instance.instanceId);
184 expect(replaced.instance.embeddingModel).toEqual("@cf/baai/bge-large-en-v1.5");
185
186 const live = yield* getInstance(accountId, replaced.instance.instanceId);
187 expect(live.embeddingModel).toEqual("@cf/baai/bge-large-en-v1.5");
188
189 // The old instance was deleted as part of the replacement.
190 yield* expectGone(accountId, initial.instance.instanceId);
191
192 yield* stack.destroy();
193
194 yield* expectGone(accountId, replaced.instance.instanceId);
195 }).pipe(logLevel),
196 {
197 tags: [
198 "provider:cloudflare",
199 "provider:cloudflare:ai",
200 "provider:cloudflare:apitoken",
201 "provider:cloudflare:r2",
202 "live",
203 ],
204 timeout: 240_000,
205 },
206 );
207
208 test.provider(
209 "recreates after out-of-band delete",
210 (stack) =>
211 Effect.gen(function* () {
212 const { accountId } = yield* yield* CloudflareEnvironment;
213
214 yield* stack.destroy();
215
216 const initial = yield* stack.deploy(program());
217
218 // Delete the instance out-of-band. A redeploy with identical props is
219 // a planner no-op, so change a mutable prop to force reconcile — it
220 // must observe the instance as missing and recreate it instead of
221 // failing on a 404.
222 yield* aisearch
223 .deleteNamespaceInstance({
224 accountId,
225 namespace: "default",
226 id: initial.instance.instanceId,
227 })
228 .pipe(
229 Effect.retry({
230 while: (e) => e._tag === "Forbidden",
231 schedule: Schedule.exponential("500 millis"),
232 times: 8,
233 }),
234 );
235 yield* expectGone(accountId, initial.instance.instanceId);
236
237 const healed = yield* stack.deploy(program({ maxNumResults: 10 }));
238
239 expect(healed.instance.instanceId).toEqual(initial.instance.instanceId);
240 const live = yield* getInstance(accountId, healed.instance.instanceId);
241 expect(live.id).toEqual(initial.instance.instanceId);
242
243 yield* stack.destroy();
244
245 yield* expectGone(accountId, healed.instance.instanceId);
246 }).pipe(logLevel),
247 {
248 tags: [
249 "provider:cloudflare",
250 "provider:cloudflare:ai",
251 "provider:cloudflare:apitoken",
252 "provider:cloudflare:r2",
253 "live",
254 ],
255 timeout: 240_000,
256 },
257 );
258
259 // Canonical `list()` test: instances are namespace-scoped, so `list()`
260 // enumerates every namespace (including the account-provided `default`) and
261 // fans out a paginated instance list per namespace, hydrating each into the
262 // `read` Attributes shape. Deploy an instance and assert its id appears.
263 test.provider(
264 "list enumerates the deployed instance",
265 (stack) =>
266 Effect.gen(function* () {
267 yield* stack.destroy();
268
269 const deployed = yield* stack.deploy(program());
270
271 const provider = yield* Provider.findProvider(Cloudflare.AI.SearchInstance);
272 const all = yield* provider.list();
273
274 expect(all.some((x) => x.instanceId === deployed.instance.instanceId)).toBe(true);
275
276 yield* stack.destroy();
277
278 yield* expectGone(deployed.instance.accountId, deployed.instance.instanceId);
279 }).pipe(logLevel),
280 {
281 tags: [
282 "provider:cloudflare",
283 "provider:cloudflare:ai",
284 "provider:cloudflare:apitoken",
285 "provider:cloudflare:r2",
286 "live",
287 ],
288 timeout: 240_000,
289 },
290 );
291
292 // A web-crawler source crawls a seed URL and needs no service token (unlike
293 // an R2 source). Cloudflare only crawls a domain the account owns, so the
294 // crawl is seeded at a Worker we deploy (its `workers.dev` URL is owned by the
295 // account); the fixture serves a sitemap so discovery always finds content.
296 test.provider(
297 "creates a web-crawler instance (no service token)",
298 (stack) =>
299 Effect.gen(function* () {
300 const { accountId } = yield* yield* CloudflareEnvironment;
301
302 yield* stack.destroy();
303
304 // Deploy the crawl target first and wait until its `workers.dev` URL is
305 // actually serving. AI Search fetches the seed synchronously when it
306 // validates a web-crawler at create time; against a URL that isn't live
307 // yet it finds no content and rejects with `missing_sitemap`.
308 const warmed = yield* stack.deploy(
309 Effect.gen(function* () {
310 return yield* AiSearchCrawlTargetWorker;
311 }),
312 );
313 const client = yield* HttpClient.HttpClient;
314 const targetUrl = warmed.url;
315 expect(targetUrl).toBeTypeOf("string");
316 yield* client.get(targetUrl as string).pipe(
317 Effect.flatMap((res) =>
318 res.status === 200
319 ? Effect.succeed(res)
320 : Effect.fail(new Error(`crawl target not ready: ${res.status}`)),
321 ),
322 Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 }),
323 );
324
325 const initial = yield* stack.deploy(
326 Effect.gen(function* () {
327 const target = yield* AiSearchCrawlTargetWorker;
328 const instance = yield* Cloudflare.AI.SearchInstance("Search", {
329 type: "web-crawler",
330 source: target.url.as<string>(),
331 sourceParams: {
332 webCrawler: {
333 // Cloudflare renamed `crawl` → `discover` and removed
334 // `crawlOptions` from the API. The fixture serves a
335 // sitemap, so discovery always finds content.
336 parseType: "discover",
337 },
338 },
339 });
340 return { target, instance };
341 }),
342 );
343
344 // Creating without any tokenId proves a web-crawler needs no token.
345 expect(initial.instance.type).toEqual("web-crawler");
346 expect(initial.instance.source).toEqual(initial.target.url);
347
348 const live = yield* getInstance(accountId, initial.instance.instanceId);
349 expect(live.type).toEqual("web-crawler");
350
351 yield* stack.destroy();
352
353 yield* expectGone(accountId, initial.instance.instanceId);
354 }).pipe(logLevel),
355 {
356 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
357 timeout: 300_000,
358 },
359 );
360
361 // A program that places the instance in a custom namespace. The instance's
362 // `namespace` references the namespace's `name` output, so the engine orders
363 // instance-after-namespace on deploy (and namespace-after-instance on
364 // destroy, so the namespace's instances are torn down before it).
365 const nsProgram = (props?: Partial<Cloudflare.AI.SearchInstanceProps>) =>
366 Effect.gen(function* () {
367 const namespace = yield* Cloudflare.AI.SearchNamespace("AiSearchNs", {});
368 const bucket = yield* Cloudflare.R2.Bucket("AiSearchSource", {
369 forceDestroy: true,
370 });
371 const token = yield* scopedToken;
372 const instance = yield* Cloudflare.AI.SearchInstance("Search", {
373 tokenId: token.id,
374 source: bucket.bucketName,
375 namespace: namespace.name,
376 ...props,
377 });
378 return { namespace, bucket, instance };
379 });
380
381 test.provider(
382 "creates an instance in a custom namespace and moving namespaces replaces",
383 (stack) =>
384 Effect.gen(function* () {
385 const { accountId } = yield* yield* CloudflareEnvironment;
386
387 yield* stack.destroy();
388
389 const initial = yield* stack.deploy(nsProgram());
390 expect(initial.instance.namespace).toEqual(initial.namespace.name);
391 expect(initial.namespace.name).not.toEqual("default");
392
393 // The instance is readable in its namespace, but NOT in `default`.
394 const live = yield* getInstance(
395 accountId,
396 initial.instance.instanceId,
397 initial.namespace.name,
398 );
399 expect(live.id).toEqual(initial.instance.instanceId);
400 yield* expectGone(accountId, initial.instance.instanceId, "default");
401
402 yield* stack.destroy();
403
404 yield* expectGone(accountId, initial.instance.instanceId, initial.namespace.name);
405 }).pipe(logLevel),
406 {
407 tags: [
408 "provider:cloudflare",
409 "provider:cloudflare:ai",
410 "provider:cloudflare:apitoken",
411 "provider:cloudflare:r2",
412 "live",
413 ],
414 timeout: 240_000,
415 },
416 );