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

main
ts 1,050 lines 40.3 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import type { SubscriptionResourceSource } from "@alchemy.run/cloudflare/Queues/Subscription";
4 import { describe, expect, test as unitTest } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as TestCore from "@alchemy.run/test/Core";
7 import * as ai from "@distilled.cloud/cloudflare/ai";
8 import * as images from "@distilled.cloud/cloudflare/images";
9 import * as kv from "@distilled.cloud/cloudflare/kv";
10 import * as queues from "@distilled.cloud/cloudflare/queues";
11 import * as r2 from "@distilled.cloud/cloudflare/r2";
12 import * as vectorize from "@distilled.cloud/cloudflare/vectorize";
13 import * as workers from "@distilled.cloud/cloudflare/workers";
14 import * as Cause from "effect/Cause";
15 import * as Clock from "effect/Clock";
16 import * as Effect from "effect/Effect";
17 import * as Exit from "effect/Exit";
18 import * as Fiber from "effect/Fiber";
19 import * as Layer from "effect/Layer";
20 import * as Schedule from "effect/Schedule";
21 import * as Schema from "effect/Schema";
22 import * as TestClock from "effect/testing/TestClock";
23 import {
24 hasReadySubscriptionEvent,
25 makeSubscriptionCleanup,
26 matchesSubscriptionEvent,
27 SubscriptionEvent,
28 type SubscriptionProbe,
29 } from "./SubscriptionSources.ts";
30
31 const { test } = Test.make({ providers: Cloudflare.providers() });
32
33 type Assert<T extends true> = T;
34 type Accepts<T> = T extends SubscriptionResourceSource ? true : false;
35 type _Variant = Assert<Accepts<Cloudflare.Images.Variant>>;
36 type _Namespace = Assert<Accepts<Cloudflare.KV.Namespace>>;
37 type _Bucket = Assert<Accepts<Cloudflare.R2.Bucket>>;
38 type _Job = Assert<Accepts<Cloudflare.R2.SuperSlurperJob>>;
39 type _Index = Assert<Accepts<Cloudflare.Vectorize.Index>>;
40 type _Model = Assert<Accepts<Cloudflare.AI.Model>>;
41 type _SearchModel = Assert<Cloudflare.AI.SearchModel extends string ? true : false>;
42 type _SearchModelIsNotResource = Assert<
43 Accepts<Cloudflare.AI.SearchModel> extends false ? true : false
44 >;
45 type _Worker = Assert<Accepts<Cloudflare.Worker>>;
46
47 const references = Effect.gen(function* () {
48 const queue = yield* Cloudflare.Queues.Queue("EventsQueue");
49 const sources = [
50 yield* Cloudflare.Images.Variant.ref("Image"),
51 yield* Cloudflare.KV.Namespace.ref("Cache"),
52 yield* Cloudflare.R2.Bucket.ref("Uploads"),
53 yield* Cloudflare.R2.SuperSlurperJob.ref("Migration"),
54 yield* Cloudflare.Vectorize.Index.ref("Search"),
55 yield* Cloudflare.AI.Model.ref("Embeddings"),
56 yield* Cloudflare.Worker.ref("Website"),
57 ];
58 const Subscription = yield* Cloudflare.Queues.Subscription;
59 for (const [index, source] of sources.entries()) {
60 yield* Subscription(
61 `Events${index}`,
62 Effect.succeed({ source, events: ["product.event"], queueId: queue.queueId }),
63 );
64 }
65 yield* Subscription("Invalid", {
66 // @ts-expect-error arbitrary account-bearing objects are not resource sources
67 source: { accountId: "account", modelName: "model" },
68 events: ["batch.queued"],
69 queueId: queue.queueId,
70 });
71 });
72 void references;
73
74 type Kind = "images" | "kv" | "r2" | "vectorize" | "model" | "worker";
75 const createSource = (kind: Kind, id = "Source") =>
76 Effect.gen(function* () {
77 switch (kind) {
78 case "images":
79 return yield* Cloudflare.Images.Variant(id, {
80 name: `alchemySubscription${id}`,
81 fit: "contain",
82 width: 100,
83 height: 100,
84 });
85 case "kv":
86 return yield* Cloudflare.KV.Namespace(id);
87 case "r2":
88 return yield* Cloudflare.R2.Bucket(id, { forceDestroy: true });
89 case "vectorize":
90 return yield* Cloudflare.Vectorize.Index(id, { dimensions: 32, metric: "cosine" });
91 case "model":
92 return yield* Cloudflare.AI.Model(id, { modelName: "@cf/baai/bge-m3" });
93 case "worker":
94 return yield* Cloudflare.Worker(id, {
95 main: `${import.meta.dirname}/fixtures/subscription-source-worker.ts`,
96 });
97 }
98 });
99 const refSource = (kind: Kind, options?: { stack: string; stage: string }) =>
100 Effect.gen(function* () {
101 switch (kind) {
102 case "images":
103 return yield* Cloudflare.Images.Variant.ref("Source", options);
104 case "kv":
105 return yield* Cloudflare.KV.Namespace.ref("Source", options);
106 case "r2":
107 return yield* Cloudflare.R2.Bucket.ref("Source", options);
108 case "vectorize":
109 return yield* Cloudflare.Vectorize.Index.ref("Source", options);
110 case "model":
111 return yield* Cloudflare.AI.Model.ref("Source", options);
112 case "worker":
113 return yield* Cloudflare.Worker.ref("Source", options);
114 }
115 });
116 const eventType = (kind: Kind) =>
117 ({
118 images: process.env.CLOUDFLARE_TEST_IMAGES_EVENT ?? "image.uploaded",
119 kv: "namespace.created",
120 r2: "bucket.created",
121 vectorize: "index.created",
122 model: "batch.queued",
123 worker: "build.started",
124 })[kind];
125
126 const verifySource = (source: {
127 accountId: string;
128 variantName?: string;
129 namespaceId?: string;
130 bucketName?: string;
131 indexName?: string;
132 modelName?: string;
133 workerName?: string;
134 }) =>
135 Effect.gen(function* () {
136 const { accountId } = source;
137 if (source.variantName)
138 yield* images.getV1Variant({ accountId, variantId: source.variantName });
139 else if (source.namespaceId)
140 yield* kv.getNamespace({ accountId, namespaceId: source.namespaceId });
141 else if (source.bucketName) yield* r2.getBucket({ accountId, bucketName: source.bucketName });
142 else if (source.indexName)
143 yield* vectorize.getIndex({ accountId, indexName: source.indexName });
144 else if (source.modelName) yield* ai.getModelSchema({ accountId, model: source.modelName });
145 else if (source.workerName)
146 yield* workers.getScriptScriptAndVersionSetting({ accountId, scriptName: source.workerName });
147 else throw new Error("Unknown source identity");
148 });
149
150 const gone = (accountId: string, subscriptionId: string) =>
151 queues.getSubscription({ accountId, subscriptionId }).pipe(
152 Effect.flatMap(() => Effect.fail(new Error("Subscription still exists"))),
153 Effect.catchTag("SubscriptionNotFound", () => Effect.void),
154 );
155
156 const triggerEvent = (kind: Kind, accountId: string, name: string) =>
157 Effect.gen(function* () {
158 const eventName = `${name.slice(0, 48)}-event`;
159 if (kind === "kv") {
160 const namespace = yield* Effect.acquireRelease(
161 kv.createNamespace({ accountId, title: eventName }),
162 (namespace) =>
163 kv.deleteNamespace({ accountId, namespaceId: namespace.id }).pipe(Effect.orDie),
164 );
165 return namespace.id;
166 }
167 if (kind === "r2") {
168 const bucket = yield* Effect.acquireRelease(
169 r2.createBucket({ accountId, name: eventName }),
170 () => r2.deleteBucket({ accountId, bucketName: eventName }).pipe(Effect.orDie),
171 );
172 return bucket.name!;
173 }
174 if (kind === "vectorize") {
175 const index = yield* Effect.acquireRelease(
176 vectorize.createIndex({
177 accountId,
178 name: eventName,
179 config: { dimensions: 32, metric: "cosine" },
180 }),
181 () => vectorize.deleteIndex({ accountId, indexName: eventName }).pipe(Effect.orDie),
182 );
183 yield* Effect.logInfo("Vectorize index created", {
184 name: index.name,
185 createdOn: index.createdOn,
186 });
187 return index.name!;
188 }
189 return yield* Effect.fail(new Error(`No lifecycle trigger for ${kind}`));
190 });
191
192 const pullEvents = (accountId: string, queueId: string, received: SubscriptionEvent[] = []) =>
193 Effect.gen(function* () {
194 const batch = yield* queues
195 .pullMessage({ accountId, queueId, batchSize: 100, visibilityTimeoutMs: 30_000 })
196 .pipe(
197 Effect.retry({
198 while: (error) => error._tag === "QueueHttpPullNotEnabled",
199 schedule: Schedule.spaced("2 seconds"),
200 times: 8,
201 }),
202 );
203 const events = yield* Effect.forEach(
204 (batch.messages ?? []).flatMap(({ body }) => (body ? [body] : [])),
205 (body) => Schema.decodeUnknownEffect(SubscriptionEvent)(body),
206 );
207 received.push(...events);
208 if (events.length) {
209 yield* Effect.logInfo("Subscription events received", events);
210 }
211 const acks = (batch.messages ?? []).flatMap(({ leaseId }) => (leaseId ? [{ leaseId }] : []));
212 if (acks.length) {
213 const result = yield* queues.ackMessage({ accountId, queueId, acks });
214 expect(result.ackCount).toBe(acks.length);
215 expect(Object.keys(result.warnings ?? {})).toHaveLength(0);
216 }
217 return events;
218 });
219
220 const waitForDelivery = (
221 kind: "kv" | "r2" | "vectorize",
222 accountId: string,
223 queueId: string,
224 subscriptionId: string,
225 readyAfter: number,
226 ) =>
227 Effect.gen(function* () {
228 const probes: SubscriptionProbe[] = [];
229 let ready = false;
230 const createProbe = Effect.gen(function* () {
231 const createdAt = yield* Clock.currentTimeMillis;
232 probes.push({
233 createdAt,
234 identity: yield* triggerEvent(
235 kind,
236 accountId,
237 `${kind}-${subscriptionId}-${probes.length}`,
238 ),
239 });
240 });
241 const observe = Effect.gen(function* () {
242 const events = yield* pullEvents(accountId, queueId);
243 ready = hasReadySubscriptionEvent(events, probes, {
244 source: kind,
245 type: eventType(kind),
246 accountId,
247 subscriptionId,
248 readyAfter,
249 });
250 });
251 if (kind === "vectorize") {
252 const now = yield* Clock.currentTimeMillis;
253 yield* Effect.sleep(Math.max(0, readyAfter - now));
254 yield* createProbe;
255 yield* observe.pipe(
256 Effect.repeat({ schedule: Schedule.spaced("2 seconds"), times: 8, until: () => ready }),
257 );
258 } else {
259 yield* createProbe.pipe(
260 Effect.andThen(observe),
261 Effect.repeat({ schedule: Schedule.spaced("7 seconds"), times: 8, until: () => ready }),
262 );
263 }
264 expect(ready).toBe(true);
265 }).pipe(Effect.timeout("80 seconds"));
266
267 // Account-wide subscriptions are unique per product, regardless of the selected resource.
268 describe.sequential(
269 "resource subscription sources",
270 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:queue", "live"] },
271 () => {
272 for (const kind of ["images", "kv", "r2", "vectorize", "model", "worker"] as const) {
273 test.provider(
274 `${kind} direct source and persisted ref retain identity and ownership`,
275 (stack) =>
276 Effect.gen(function* () {
277 yield* stack.destroy();
278 const { accountId } = yield* yield* CloudflareEnvironment;
279 const program = (reference: boolean) =>
280 Effect.gen(function* () {
281 const resource = yield* createSource(kind);
282 const queue = yield* Cloudflare.Queues.Queue("EventsQueue");
283 const subscription = yield* Cloudflare.Queues.Subscription(
284 "Events",
285 Effect.gen(function* () {
286 return {
287 source: reference ? yield* refSource(kind) : resource,
288 events: [eventType(kind)],
289 queueId: queue.queueId,
290 };
291 }),
292 );
293 return { resource, queue, subscription };
294 });
295 const initial = yield* stack.deploy(program(false));
296 const configuredAt = yield* Clock.currentTimeMillis;
297 expect(initial.subscription.accountId).toBe(accountId);
298 const expected =
299 kind === "model"
300 ? { type: "workersAi.model", modelName: "@cf/baai/bge-m3" }
301 : kind === "worker" && "workerName" in initial.resource
302 ? { type: "workersBuilds.worker", workerName: initial.resource.workerName }
303 : { type: kind };
304 expect(initial.subscription.source).toEqual(expected);
305 const plan = yield* stack.plan(program(false));
306 expect(plan.resources.Source.downstream).toContain("Events");
307 expect(plan.resources.Events.state).toHaveProperty("props.sourceAccountId", accountId);
308 expect(plan.resources.Events.state).toHaveProperty("props.source", expected);
309 const observed = yield* queues.getSubscription({
310 accountId,
311 subscriptionId: initial.subscription.subscriptionId,
312 });
313 expect(observed.source).toEqual(expect.objectContaining(expected));
314 const receivesLifecycle = kind === "kv" || kind === "r2" || kind === "vectorize";
315 if (receivesLifecycle) {
316 yield* queues.createConsumer({
317 accountId,
318 queueId: initial.queue.queueId,
319 type: "http_pull",
320 });
321 yield* pullEvents(accountId, initial.queue.queueId);
322 }
323 const referenced = yield* stack.deploy(program(true));
324 expect(referenced.subscription.subscriptionId).toBe(
325 initial.subscription.subscriptionId,
326 );
327 if (receivesLifecycle) {
328 yield* Effect.gen(function* () {
329 yield* waitForDelivery(
330 kind,
331 accountId,
332 referenced.queue.queueId,
333 referenced.subscription.subscriptionId,
334 // Allow the observed Vectorize propagation interval before probing.
335 kind === "vectorize" ? configuredAt + 60_000 : 0,
336 );
337 const identities = [
338 yield* triggerEvent(kind, accountId, referenced.queue.queueName),
339 ];
340 if (kind === "vectorize") {
341 for (const suffix of ["second", "third"]) {
342 identities.push(
343 yield* triggerEvent(
344 kind,
345 accountId,
346 `vec-${referenced.subscription.subscriptionId}-${suffix}`,
347 ),
348 );
349 }
350 }
351 const events: SubscriptionEvent[] = [];
352 const missing = () =>
353 identities.filter(
354 (identity) =>
355 !events.some((event) =>
356 matchesSubscriptionEvent(event, {
357 source: kind,
358 type: eventType(kind),
359 accountId,
360 subscriptionId: referenced.subscription.subscriptionId,
361 identity,
362 }),
363 ),
364 );
365 yield* pullEvents(accountId, referenced.queue.queueId, events).pipe(
366 Effect.repeat({
367 schedule: Schedule.spaced("5 seconds"),
368 times: 10,
369 until: () => missing().length === 0,
370 }),
371 Effect.timeout("60 seconds"),
372 Effect.onExit(() =>
373 Effect.gen(function* () {
374 if (missing().length === 0) return;
375 yield* Effect.logInfo("Missing subscription delivery", {
376 expected: identities,
377 missing: missing(),
378 received: events,
379 });
380 yield* Effect.all({
381 subscription: queues.getSubscription({
382 accountId,
383 subscriptionId: referenced.subscription.subscriptionId,
384 }),
385 queue: queues.getQueue({ accountId, queueId: referenced.queue.queueId }),
386 metrics: queues.getMetricsQueue({
387 accountId,
388 queueId: referenced.queue.queueId,
389 }),
390 }).pipe(
391 Effect.tap((state) => Effect.logInfo("Subscription delivery state", state)),
392 Effect.timeout("5 seconds"),
393 Effect.catch((error) =>
394 Effect.logWarning("Subscription delivery state unavailable", {
395 error: error._tag,
396 }),
397 ),
398 );
399 }),
400 ),
401 );
402 expect(missing()).toEqual([]);
403 }).pipe(Effect.timeout("90 seconds"));
404 }
405 yield* stack.deploy(createSource(kind));
406 yield* gone(accountId, initial.subscription.subscriptionId);
407 yield* verifySource(initial.resource);
408 yield* stack.destroy();
409 if (kind === "model") yield* ai.getModelSchema({ accountId, model: "@cf/baai/bge-m3" });
410 }).pipe(Effect.scoped, Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
411 {
412 tags: [
413 "provider:cloudflare:ai",
414 "provider:cloudflare:images",
415 "provider:cloudflare:r2",
416 "provider:cloudflare:vectorize",
417 "provider:cloudflare:worker",
418 ],
419 timeout: 120_000,
420 exclusive: "cloudflare:event-subscriptions",
421 },
422 );
423 }
424
425 test.provider(
426 "cross-stack and cross-stage namespace ref does not own the source",
427 (stack) => {
428 const host = TestCore.scratchStack(
429 { providers: Cloudflare.providers(), stage: `${stack.stage}-host` },
430 "SubscriptionRefHost",
431 "test/Cloudflare/Queue/SubscriptionSources.test.ts",
432 );
433 return Effect.gen(function* () {
434 yield* stack.destroy();
435 yield* host.destroy();
436 const source = yield* host.deploy(Cloudflare.KV.Namespace("Source"));
437 const program = Effect.gen(function* () {
438 const queue = yield* Cloudflare.Queues.Queue("Queue");
439 return yield* Cloudflare.Queues.Subscription("Events", {
440 source: yield* refSource("kv", { stack: host.name, stage: host.stage }),
441 events: ["namespace.created"],
442 queueId: queue.queueId,
443 });
444 });
445 const subscription = yield* stack.deploy(program);
446 expect(subscription.source).toEqual({ type: "kv" });
447 const plan = yield* stack.plan(program);
448 expect(
449 Object.values(plan.resources).some(
450 (node) => node.resource.Type === "Cloudflare.KV.Namespace",
451 ),
452 ).toBe(false);
453 yield* stack.destroy();
454 yield* verifySource(source);
455 yield* host.destroy();
456 }).pipe(
457 Effect.ensuring(stack.destroy().pipe(Effect.andThen(host.destroy()), Effect.orDie)),
458 );
459 },
460 {
461 tags: [
462 "provider:cloudflare:ai",
463 "provider:cloudflare:images",
464 "provider:cloudflare:r2",
465 "provider:cloudflare:vectorize",
466 "provider:cloudflare:worker",
467 ],
468 timeout: 120_000,
469 exclusive: "cloudflare:event-subscriptions",
470 },
471 );
472
473 test.provider(
474 "a source reference from another account is rejected before subscription creation",
475 (stack) => {
476 const host = TestCore.scratchStack(
477 { providers: Cloudflare.providers(), stage: `${stack.stage}-host` },
478 "SubscriptionAccountHost",
479 "test/Cloudflare/Queue/SubscriptionSources.test.ts",
480 );
481 return Effect.gen(function* () {
482 yield* stack.destroy();
483 yield* host.destroy();
484 const environment = yield* yield* CloudflareEnvironment;
485 const hosted = yield* host.deploy(
486 Effect.gen(function* () {
487 const source = yield* Cloudflare.KV.Namespace("Source");
488 const queue = yield* Cloudflare.Queues.Queue("Queue");
489 return { source, queue };
490 }),
491 );
492 const rejecting = TestCore.scratchStack(
493 {
494 providers: Layer.mergeAll(
495 Cloudflare.providers(),
496 Layer.succeed(
497 CloudflareEnvironment,
498 Effect.succeed({ ...environment, accountId: "00000000000000000000000000000000" }),
499 ),
500 ),
501 stage: stack.stage,
502 },
503 "SubscriptionAccountMismatch",
504 "test/Cloudflare/Queue/SubscriptionSources.test.ts",
505 );
506 const cleanup = TestCore.scratchStack(
507 { providers: Cloudflare.providers(), stage: stack.stage },
508 "SubscriptionAccountMismatch",
509 "test/Cloudflare/Queue/SubscriptionSources.test.ts",
510 );
511 yield* cleanup.destroy();
512 yield* Effect.addFinalizer(() => cleanup.destroy().pipe(Effect.orDie));
513 const mismatch = yield* rejecting
514 .deploy(
515 Effect.gen(function* () {
516 return yield* Cloudflare.Queues.Subscription("Events", {
517 source: yield* refSource("kv", { stack: host.name, stage: host.stage }),
518 events: ["namespace.created"],
519 queueId: hosted.queue.queueId,
520 });
521 }),
522 )
523 .pipe(Effect.exit);
524 expect(Exit.isFailure(mismatch)).toBe(true);
525 if (Exit.isFailure(mismatch))
526 expect(Cause.pretty(mismatch.cause)).toContain("SubscriptionSourceAccountMismatch");
527 yield* stack.destroy();
528 yield* verifySource(hosted.source);
529 yield* host.destroy();
530 }).pipe(
531 Effect.scoped,
532 Effect.ensuring(stack.destroy().pipe(Effect.andThen(host.destroy()), Effect.orDie)),
533 );
534 },
535 {
536 tags: [
537 "provider:cloudflare:ai",
538 "provider:cloudflare:images",
539 "provider:cloudflare:r2",
540 "provider:cloudflare:vectorize",
541 "provider:cloudflare:worker",
542 ],
543 timeout: 120_000,
544 exclusive: "cloudflare:event-subscriptions",
545 },
546 );
547
548 test.provider(
549 "a foreign subscription collision preserves the original destination",
550 (stack) => {
551 const host = TestCore.scratchStack(
552 { providers: Cloudflare.providers(), stage: `${stack.stage}-owner` },
553 "SubscriptionOwner",
554 "test/Cloudflare/Queue/SubscriptionSources.test.ts",
555 );
556 return Effect.gen(function* () {
557 yield* stack.destroy();
558 yield* host.destroy();
559 const owner = yield* host.deploy(
560 Effect.gen(function* () {
561 const queue = yield* Cloudflare.Queues.Queue("Queue");
562 const subscription = yield* Cloudflare.Queues.Subscription("OwnedEvents", {
563 source: { type: "kv" },
564 events: ["namespace.created"],
565 queueId: queue.queueId,
566 });
567 return { queue, subscription };
568 }),
569 );
570 const result = yield* stack
571 .deploy(
572 Effect.gen(function* () {
573 const queue = yield* Cloudflare.Queues.Queue("Queue");
574 const source = yield* Cloudflare.KV.Namespace("Source");
575 return yield* Cloudflare.Queues.Subscription("ForeignEvents", {
576 source,
577 events: ["namespace.created"],
578 queueId: queue.queueId,
579 });
580 }),
581 )
582 .pipe(Effect.exit);
583 expect(Exit.isFailure(result)).toBe(true);
584 if (Exit.isFailure(result))
585 expect(Cause.pretty(result.cause)).toContain("SubscriptionAlreadyExists");
586 yield* stack.destroy();
587 const observed = yield* queues.getSubscription({
588 accountId: owner.subscription.accountId,
589 subscriptionId: owner.subscription.subscriptionId,
590 });
591 expect(observed.destination.queueId).toBe(owner.queue.queueId);
592 expect(observed.name).toBe(owner.subscription.name);
593 yield* host.destroy();
594 }).pipe(
595 Effect.ensuring(stack.destroy().pipe(Effect.andThen(host.destroy()), Effect.orDie)),
596 );
597 },
598 { timeout: 120_000, exclusive: "cloudflare:event-subscriptions" },
599 );
600 },
601 );
602
603 describe(
604 "subscription cleanup",
605 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:queue", "local"] },
606 () => {
607 unitTest.effect("returns successful cleanup results", () =>
608 Effect.gen(function* () {
609 const cleanup = makeSubscriptionCleanup();
610 expect(yield* cleanup(Effect.succeed(42))).toBe(42);
611 }),
612 );
613
614 unitTest.effect("preserves cleanup failures", () =>
615 Effect.gen(function* () {
616 const cleanup = makeSubscriptionCleanup();
617 const result = yield* cleanup(Effect.fail(new Error("delete failed"))).pipe(Effect.exit);
618 expect(Exit.isFailure(result)).toBe(true);
619 if (Exit.isFailure(result)) expect(Cause.pretty(result.cause)).toContain("delete failed");
620 }),
621 );
622
623 unitTest.effect("bounds a stalled release inside scope finalization", () =>
624 Effect.gen(function* () {
625 const cleanup = makeSubscriptionCleanup();
626 const fiber = yield* Effect.acquireRelease(Effect.void, () => cleanup(Effect.never)).pipe(
627 Effect.scoped,
628 Effect.exit,
629 Effect.forkChild({ startImmediately: true }),
630 );
631 yield* TestClock.adjust("5 seconds");
632 const result = yield* Fiber.join(fiber);
633 expect(Exit.isFailure(result)).toBe(true);
634 if (Exit.isFailure(result)) expect(Cause.pretty(result.cause)).toContain("TimeoutError");
635 }),
636 );
637
638 unitTest.effect(
639 "shares one deadline across releases and stops starting work when exhausted",
640 () =>
641 Effect.gen(function* () {
642 const cleanup = makeSubscriptionCleanup();
643 yield* cleanup(Effect.void);
644 yield* TestClock.adjust("19 seconds");
645 const fiber = yield* cleanup(Effect.never).pipe(
646 Effect.exit,
647 Effect.forkChild({ startImmediately: true }),
648 );
649 yield* TestClock.adjust("1 second");
650 expect(Exit.isFailure(yield* Fiber.join(fiber))).toBe(true);
651 let started = false;
652 const result = yield* cleanup(
653 Effect.sync(() => {
654 started = true;
655 }),
656 ).pipe(Effect.exit);
657 expect(Exit.isFailure(result)).toBe(true);
658 expect(started).toBe(false);
659 if (Exit.isFailure(result))
660 expect(Cause.pretty(result.cause)).toContain("Subscription cleanup deadline exceeded");
661 }),
662 );
663 },
664 );
665
666 const expected = {
667 source: "vectorize" as const,
668 type: "index.created",
669 accountId: "account",
670 subscriptionId: "subscription",
671 identity: "vectorize-subscription-0-event",
672 };
673 const event: SubscriptionEvent = {
674 type: "cf.vectorize.index.created",
675 source: { type: "vectorize" },
676 metadata: {
677 accountId: "account",
678 eventSubscriptionId: "subscription",
679 eventTimestamp: "2026-09-20T00:00:00Z",
680 },
681 payload: { name: expected.identity },
682 };
683
684 describe(
685 "subscription event identity",
686 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:queue", "local"] },
687 () => {
688 unitTest(
689 "an early event from the current subscription does not establish settled routing",
690 () => {
691 expect(
692 hasReadySubscriptionEvent([event], [{ identity: expected.identity, createdAt: 49_000 }], {
693 ...expected,
694 readyAfter: 60_000,
695 }),
696 ).toBe(false);
697 },
698 );
699
700 unitTest("an early probe delivered later cannot substitute for a fresh probe", () => {
701 expect(
702 hasReadySubscriptionEvent(
703 [event],
704 [
705 { identity: expected.identity, createdAt: 49_000 },
706 { identity: "fresh-probe", createdAt: 61_000 },
707 ],
708 { ...expected, readyAfter: 60_000 },
709 ),
710 ).toBe(false);
711 });
712
713 unitTest("a fresh matching probe establishes readiness after the propagation interval", () => {
714 expect(
715 hasReadySubscriptionEvent([event], [{ identity: expected.identity, createdAt: 61_000 }], {
716 ...expected,
717 readyAfter: 60_000,
718 }),
719 ).toBe(true);
720 });
721
722 unitTest("matches the exact Vectorize event envelope", () => {
723 const decoded = Schema.decodeUnknownSync(SubscriptionEvent)(JSON.stringify(event));
724 expect(matchesSubscriptionEvent(decoded, expected)).toBe(true);
725 });
726
727 unitTest(
728 "an index name containing the subscription id does not prove subscription identity",
729 () => {
730 expect(
731 matchesSubscriptionEvent(
732 { ...event, metadata: { ...event.metadata, eventSubscriptionId: "other" } },
733 expected,
734 ),
735 ).toBe(false);
736 },
737 );
738
739 unitTest("a readiness probe cannot satisfy a distinct final index", () => {
740 expect(matchesSubscriptionEvent(event, { ...expected, identity: "final-index" })).toBe(false);
741 });
742
743 unitTest("matches the whole resource name, not a substring", () => {
744 expect(
745 matchesSubscriptionEvent(
746 { ...event, payload: { name: `${expected.identity}-other` } },
747 expected,
748 ),
749 ).toBe(false);
750 });
751
752 unitTest("rejects another account even when its name contains the expected account", () => {
753 expect(
754 matchesSubscriptionEvent(
755 {
756 ...event,
757 metadata: { ...event.metadata, accountId: "other" },
758 payload: { name: expected.identity, id: expected.accountId },
759 },
760 expected,
761 ),
762 ).toBe(false);
763 });
764
765 unitTest("requires the exact event type and product source", () => {
766 expect(matchesSubscriptionEvent({ ...event, type: `${event.type}.other` }, expected)).toBe(
767 false,
768 );
769 expect(matchesSubscriptionEvent({ ...event, source: { type: "r2" } }, expected)).toBe(false);
770 });
771
772 unitTest("matches an Images upload by exact payload id and subscription", () => {
773 const image = {
774 ...event,
775 type: "cf.images.image.uploaded",
776 source: { type: "images" },
777 payload: { id: "subscription-final" },
778 };
779 const target = {
780 ...expected,
781 source: "images" as const,
782 type: "image.uploaded",
783 identity: "subscription-final",
784 };
785 expect(matchesSubscriptionEvent(image, target)).toBe(true);
786 expect(
787 matchesSubscriptionEvent({ ...image, payload: { id: "subscription-probe-0" } }, target),
788 ).toBe(false);
789 expect(
790 matchesSubscriptionEvent(
791 { ...image, metadata: { ...image.metadata, eventSubscriptionId: "old-subscription" } },
792 target,
793 ),
794 ).toBe(false);
795 expect(
796 matchesSubscriptionEvent({ ...image, payload: { name: target.identity } }, target),
797 ).toBe(false);
798 });
799
800 unitTest("matches KV ids and R2 names in their documented payload fields", () => {
801 expect(
802 matchesSubscriptionEvent(
803 {
804 ...event,
805 type: "cf.kv.namespace.created",
806 source: { type: "kv" },
807 payload: { id: "namespace-id", name: "namespace-name" },
808 },
809 { ...expected, source: "kv", type: "namespace.created", identity: "namespace-id" },
810 ),
811 ).toBe(true);
812 expect(
813 matchesSubscriptionEvent(
814 {
815 ...event,
816 type: "cf.r2.bucket.created",
817 source: { type: "r2" },
818 payload: { name: "bucket-name" },
819 },
820 { ...expected, source: "r2", type: "bucket.created", identity: "bucket-name" },
821 ),
822 ).toBe(true);
823 });
824 },
825 );
826
827 // Native routing experiments are opt-in; control-plane reads are not delivery barriers.
828 for (const change of ["replacement", "update", "paused-update"] as const) {
829 const enabled =
830 process.env[
831 {
832 replacement: "CLOUDFLARE_TEST_VECTORIZE_IMMEDIATE_REPLACEMENT",
833 update: "CLOUDFLARE_TEST_VECTORIZE_IMMEDIATE_UPDATE",
834 "paused-update": "CLOUDFLARE_TEST_VECTORIZE_PAUSED_UPDATE",
835 }[change]
836 ] === "1";
837 test.provider.skipIf(!enabled)(
838 `Vectorize immediately routes post-${change} events to the current subscription`,
839 (stack) =>
840 Effect.gen(function* () {
841 yield* stack.destroy();
842 const { accountId } = yield* yield* CloudflareEnvironment;
843 const start = yield* Clock.currentTimeMillis;
844 const cleanup = makeSubscriptionCleanup();
845 const makeQueue = (suffix: string) =>
846 Effect.gen(function* () {
847 const queue = yield* Effect.acquireRelease(
848 queues.createQueue({
849 accountId,
850 queueName: `alchemy-vectorize-replacement-${suffix}`,
851 }),
852 (queue) => queues.deleteQueue({ accountId, queueId: queue.queueId! }).pipe(cleanup),
853 );
854 yield* queues.createConsumer({ accountId, queueId: queue.queueId!, type: "http_pull" });
855 return queue.queueId!;
856 });
857 const a = yield* makeQueue("a");
858 const b = yield* makeQueue("b");
859 const makeSubscription = (queueId: string) =>
860 Effect.acquireRelease(
861 queues.createSubscription({
862 accountId,
863 name: `native-${queueId}`,
864 source: { type: "vectorize" },
865 events: ["index.created"],
866 enabled: true,
867 destination: { type: "queues.queue", queueId },
868 }),
869 (subscription) =>
870 queues.deleteSubscription({ accountId, subscriptionId: subscription.id }).pipe(
871 Effect.catchTag("SubscriptionNotFound", () => Effect.void),
872 cleanup,
873 ),
874 );
875 const create = (name: string) =>
876 Effect.gen(function* () {
877 const index = yield* Effect.acquireRelease(
878 vectorize.createIndex({
879 accountId,
880 name,
881 config: { dimensions: 32, metric: "cosine" },
882 }),
883 () => vectorize.deleteIndex({ accountId, indexName: name }).pipe(cleanup),
884 );
885 yield* Effect.logInfo(`Native ${change} create`, {
886 elapsedMs: (yield* Clock.currentTimeMillis) - start,
887 index,
888 });
889 return name;
890 });
891 const received: { queueId: string; event: SubscriptionEvent }[] = [];
892 const pull = (queueId: string) =>
893 Effect.gen(function* () {
894 const batch = yield* queues
895 .pullMessage({ accountId, queueId, batchSize: 100, visibilityTimeoutMs: 30_000 })
896 .pipe(
897 Effect.retry({
898 while: (error) => error._tag === "QueueHttpPullNotEnabled",
899 schedule: Schedule.spaced("2 seconds"),
900 times: 8,
901 }),
902 );
903 for (const message of batch.messages ?? []) {
904 if (message.body) {
905 const event = yield* Schema.decodeUnknownEffect(SubscriptionEvent)(message.body);
906 expect(event.type).toBe("cf.vectorize.index.created");
907 expect(event.source.type).toBe("vectorize");
908 expect(event.metadata.accountId).toBe(accountId);
909 received.push({ queueId, event });
910 yield* Effect.logInfo(`Native ${change} receipt`, {
911 elapsedMs: (yield* Clock.currentTimeMillis) - start,
912 queueId,
913 messageId: message.id,
914 timestampMs: message.timestampMs,
915 attempts: message.attempts,
916 rawBody: message.body,
917 event,
918 });
919 }
920 }
921 const acks = (batch.messages ?? []).flatMap(({ leaseId }) =>
922 leaseId ? [{ leaseId }] : [],
923 );
924 if (acks.length) {
925 const ack = yield* queues.ackMessage({ accountId, queueId, acks });
926 expect(ack.ackCount).toBe(acks.length);
927 expect(Object.keys(ack.warnings ?? {})).toHaveLength(0);
928 }
929 });
930 const old = yield* makeSubscription(a);
931 yield* pull(b);
932 const initialProbes: string[] = [];
933 const initiallyReady = () =>
934 received.some(
935 ({ queueId, event }) =>
936 queueId === a &&
937 event.metadata.eventSubscriptionId === old.id &&
938 initialProbes.includes(event.payload.name!),
939 );
940 yield* Effect.gen(function* () {
941 initialProbes.push(yield* create(`vec-${a}-${initialProbes.length}`));
942 yield* pull(a);
943 }).pipe(
944 Effect.repeat({
945 schedule: Schedule.spaced("7 seconds"),
946 times: 8,
947 until: initiallyReady,
948 }),
949 );
950 expect(initiallyReady()).toBe(true);
951 const observe = Effect.fn(function* (subscriptionId: string, phase: string) {
952 const observed = yield* queues.getSubscription({ accountId, subscriptionId });
953 yield* Effect.logInfo(`Native ${change} configuration`, {
954 phase,
955 elapsedMs: (yield* Clock.currentTimeMillis) - start,
956 subscription: observed,
957 });
958 return observed;
959 });
960 const current = yield* Effect.gen(function* () {
961 if (change === "paused-update") {
962 yield* queues.patchSubscription({ accountId, subscriptionId: old.id, enabled: false });
963 const paused = yield* observe(old.id, "disabled");
964 expect(paused.enabled).toBe(false);
965 expect(paused.destination.queueId).toBe(a);
966 yield* create(`vec-${a}-disabled`);
967 yield* pull(a);
968 yield* pull(b);
969 yield* queues.patchSubscription({
970 accountId,
971 subscriptionId: old.id,
972 destination: { type: "queues.queue", queueId: b },
973 });
974 const moved = yield* observe(old.id, "destination-updated-while-disabled");
975 expect(moved.enabled).toBe(false);
976 expect(moved.destination.queueId).toBe(b);
977 const resumed = yield* queues.patchSubscription({
978 accountId,
979 subscriptionId: old.id,
980 enabled: true,
981 });
982 expect(resumed.id).toBe(old.id);
983 return resumed;
984 }
985 if (change === "update") {
986 const updated = yield* queues.patchSubscription({
987 accountId,
988 subscriptionId: old.id,
989 destination: { type: "queues.queue", queueId: b },
990 });
991 expect(updated.id).toBe(old.id);
992 return updated;
993 }
994 yield* queues.deleteSubscription({ accountId, subscriptionId: old.id });
995 yield* queues.getSubscription({ accountId, subscriptionId: old.id }).pipe(
996 Effect.flatMap(() => Effect.fail(new Error("Deleted subscription still exists"))),
997 Effect.catchTag("SubscriptionNotFound", () => Effect.void),
998 );
999 return yield* makeSubscription(b);
1000 });
1001 const observed = yield* observe(current.id, "active-at-new-destination");
1002 expect(observed.destination.queueId).toBe(b);
1003 expect(observed.enabled).toBe(true);
1004 yield* Effect.logInfo(`Native ${change} identities`, {
1005 oldSubscription: old.id,
1006 oldQueue: a,
1007 newSubscription: current.id,
1008 newQueue: b,
1009 elapsedMs: (yield* Clock.currentTimeMillis) - start,
1010 });
1011 const identities = yield* Effect.forEach([0, 1, 2], (i) =>
1012 create(`vec-${b}-after-${change}-${i}`),
1013 );
1014 yield* Effect.gen(function* () {
1015 yield* pull(a);
1016 yield* pull(b);
1017 }).pipe(Effect.repeat({ schedule: Schedule.spaced("2 seconds"), times: 5 }));
1018 const deliveries = received.filter(({ event }) => identities.includes(event.payload.name!));
1019 yield* Effect.logInfo(`Native ${change} result`, { identities, deliveries });
1020 expect(deliveries.filter(({ queueId }) => queueId === a)).toEqual([]);
1021 for (const identity of identities) {
1022 expect(
1023 deliveries.some(
1024 ({ queueId, event }) =>
1025 queueId === b &&
1026 event.metadata.eventSubscriptionId === current.id &&
1027 event.payload.name === identity,
1028 ),
1029 ).toBe(true);
1030 }
1031 yield* stack.destroy();
1032 }).pipe(
1033 Effect.timeout("90 seconds"),
1034 Effect.scoped,
1035 Effect.ensuring(
1036 stack.destroy().pipe(Effect.timeout("5 seconds"), Effect.orDie, Effect.interruptible),
1037 ),
1038 ),
1039 {
1040 tags: [
1041 "provider:cloudflare",
1042 "provider:cloudflare:queue",
1043 "provider:cloudflare:vectorize",
1044 "live",
1045 ],
1046 timeout: 120_000,
1047 exclusive: "cloudflare:event-subscriptions",
1048 },
1049 );
1050 }