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

main
ts 117 lines 4.44 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 queues from "@distilled.cloud/cloudflare/queues";
6 import { Action } from "alchemy/Action";
7 import { poll } from "alchemy/Util/poll";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Sending to a Queue inside an Action via `WriteQueueLocal` — the local
17 // (current-credentials) implementation of the `WriteQueue` binding. Exercises
18 // both the binding client (send/sendBatch) and the accessor mechanism
19 // (`yield* queue.queueId` resolved at apply time). The producer binding is
20 // send-only, so we verify out-of-band by pulling the messages back off the
21 // queue through an HTTP-pull consumer.
22 test.provider(
23 "WriteQueueLocal: send and sendBatch to a queue from an Action",
24 (stack) =>
25 Effect.gen(function* () {
26 const { accountId } = yield* yield* CloudflareEnvironment;
27
28 yield* stack.destroy();
29
30 const marker = yield* Effect.sync(() => crypto.randomUUID());
31
32 const out = yield* stack.deploy(
33 Effect.gen(function* () {
34 const queue = yield* Cloudflare.Queues.Queue("LocalWriteQueue");
35
36 const Seed = Action(
37 "Seed",
38 Effect.gen(function* () {
39 const q = yield* Cloudflare.Queues.WriteQueue(queue);
40 // Accessor — resolved at apply time against the tracker.
41 const queueId = yield* queue.queueId;
42
43 return Effect.fn(function* () {
44 const id = yield* queueId;
45
46 // Register an HTTP-pull consumer so the test can pull the
47 // messages back out-of-band. A brand-new queue can briefly
48 // 404 the consumer create under load — ride out the lag.
49 yield* queues
50 .createConsumer({
51 accountId,
52 queueId: id,
53 type: "http_pull",
54 })
55 .pipe(
56 Effect.retry({
57 while: (e) => e._tag === "QueueNotFound",
58 schedule: Schedule.exponential("500 millis"),
59 times: 8,
60 }),
61 Effect.catchTag("ConsumerAlreadyExists", () => Effect.void),
62 );
63
64 yield* q.send({ marker, seq: 1 });
65 yield* q.sendBatch([{ body: { marker, seq: 2 } }, { body: { marker, seq: 3 } }]);
66
67 return { queueId: id };
68 });
69 }).pipe(Effect.provide(Cloudflare.Queues.WriteQueueLocal)),
70 );
71
72 return yield* Seed({});
73 }),
74 );
75
76 expect(out.queueId).toBeTruthy();
77
78 // Out-of-band verification: pull the messages back off the queue and
79 // assert all three payloads (carrying our unique marker) arrive. Pulls
80 // lease a subset per call, so accumulate distinct bodies across polls.
81 const collected = new Set<string>();
82 yield* poll({
83 description: "pull sent messages back off the queue",
84 effect: Effect.gen(function* () {
85 const res = yield* queues.pullMessage({
86 accountId,
87 queueId: out.queueId,
88 batchSize: 10,
89 visibilityTimeoutMs: 2_000,
90 });
91 for (const m of res.messages ?? []) {
92 if (m.body) collected.add(m.body);
93 }
94 const acks = (res.messages ?? [])
95 .filter((m): m is typeof m & { leaseId: string } => !!m.leaseId)
96 .map((m) => ({ leaseId: m.leaseId }));
97 if (acks.length > 0) {
98 yield* queues.ackMessage({ accountId, queueId: out.queueId, acks });
99 }
100 return collected.size;
101 }),
102 predicate: (size) => size >= 3,
103 schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(30)]),
104 });
105
106 expect(collected.size).toBeGreaterThanOrEqual(3);
107 for (const body of collected) {
108 expect(body).toContain(marker);
109 }
110
111 yield* stack.destroy();
112 }).pipe(logLevel),
113 {
114 tags: ["provider:cloudflare", "provider:cloudflare:queue", "live"],
115 timeout: 120_000,
116 },
117 );