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

main
ts 106 lines 3.87 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import WriteBindingWorker from "./fixtures/write-binding.ts";
11 import WriteHttpWorker from "./fixtures/write-http.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
18 status: number;
19 body: string;
20 }> {}
21
22 const ready = Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]);
23
24 /** POST and retry until the producer route accepts the message (202). */
25 const post = (base: string, path: string, body?: string) => {
26 const base$ = HttpClientRequest.post(`${base}${path}`);
27 const req = body !== undefined ? HttpClientRequest.bodyText(base$, body) : base$;
28 return HttpClient.execute(req).pipe(
29 Effect.flatMap((res) =>
30 res.status === 202
31 ? Effect.succeed(res)
32 : res.text.pipe(
33 Effect.flatMap((b) => Effect.fail(new WorkerNotReady({ status: res.status, body: b }))),
34 ),
35 ),
36 Effect.retry({
37 // Ride out cold-start propagation — a fresh workers.dev URL
38 // serves 404 ("nothing here yet") or 500 (code 1104 "Script not
39 // found") for a few seconds before the script goes live. The
40 // bounded spaced schedule caps total wait so a genuine failure
41 // (worker returns its own JSON error body) still surfaces.
42 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
43 schedule: ready,
44 }),
45 );
46 };
47
48 /**
49 * Cloudflare Queue is producer-only at the binding layer, so there is
50 * no Read/ReadWrite split — only a Write producer. This deploys two
51 * Workers that both bind one shared queue (native Worker binding and
52 * scoped HTTP API token), then drives every {@link WriteQueueClient}
53 * method over `fetch` — `send` and `sendBatch`, each in its JSON and
54 * `text` content-type form — and asserts the producer accepts the
55 * messages (202), proving the binding/token are wired and reach the
56 * real queue.
57 */
58 test.provider(
59 "Queue write producer over binding + http",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const out = yield* stack.deploy(
65 Effect.gen(function* () {
66 const writeBinding = yield* WriteBindingWorker;
67 const writeHttp = yield* WriteHttpWorker;
68 return {
69 writeBinding: writeBinding.url,
70 writeHttp: writeHttp.url,
71 };
72 }),
73 );
74
75 const url = (u: unknown) => {
76 expect(u).toBeTypeOf("string");
77 return u as string;
78 };
79
80 // Drive the full producer surface (send + sendBatch, json + text)
81 // against one base url.
82 const exercise = (base: string, label: string) =>
83 Effect.gen(function* () {
84 expect((yield* post(base, "/send", `${label}-json`)).status).toBe(202);
85 expect((yield* post(base, "/send-text", `${label}-text`)).status).toBe(202);
86 expect((yield* post(base, "/sendBatch")).status).toBe(202);
87 expect((yield* post(base, "/sendBatch-text")).status).toBe(202);
88 });
89
90 // ── Native binding producer ──
91 yield* exercise(url(out.writeBinding), "binding");
92 // ── HTTP token producer ──
93 yield* exercise(url(out.writeHttp), "http");
94
95 yield* stack.destroy();
96 }).pipe(logLevel),
97 {
98 tags: [
99 "provider:cloudflare",
100 "provider:cloudflare:queue",
101 "provider:cloudflare:worker",
102 "live",
103 ],
104 timeout: 240_000,
105 },
106 );