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

main
ts 168 lines 5.82 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 Layer from "effect/Layer";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { A as AsyncCycleA, B as AsyncCycleB } from "./fixtures/async-circular-bindings/stack.ts";
11 import CycleWorkerALive, { WorkerA as CycleWorkerA } from "./fixtures/circular-bindings/workerA.ts";
12 import CycleWorkerBLive, { WorkerB as CycleWorkerB } from "./fixtures/circular-bindings/workerB.ts";
13 import BindingSelfWorkerLive, {
14 BindingSelfWorker,
15 } from "./fixtures/worker-worker-binding/binding-self-worker.ts";
16
17 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
18 // matching the process topology of the real `alchemy dev` command.
19 const { test } = Test.make({
20 providers: Cloudflare.providers(),
21 dev: true,
22 });
23
24 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
25
26 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
27 status: number;
28 body: string;
29 }> {}
30
31 /**
32 * GET a route, retrying until the freshly started workerd serves a 200 —
33 * including 500s while a peer's dev-registry entry is still propagating.
34 */
35 const getTextReady = (url: string) =>
36 Effect.gen(function* () {
37 const client = yield* HttpClient.HttpClient;
38 const res = yield* client.get(url).pipe(
39 Effect.flatMap((res) =>
40 res.status === 200
41 ? Effect.succeed(res)
42 : res.text.pipe(
43 Effect.flatMap((body) =>
44 Effect.fail(new WorkerNotReady({ status: res.status, body })),
45 ),
46 ),
47 ),
48 Effect.retry({
49 while: (e) => e._tag === "WorkerNotReady",
50 schedule: Schedule.max([
51 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
52 Schedule.recurs(10),
53 ]),
54 }),
55 );
56 return yield* res.text;
57 }).pipe(Effect.orDie);
58
59 /**
60 * Under `alchemy dev` an Effect Worker's `bindWorker(Self)` resolves through
61 * local workerd's `ctx.exports.default` loopback, the same path it takes
62 * when deployed.
63 */
64 test.provider(
65 "local effect worker calls its own RPC method via bindWorker(Self)",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 const deployed = yield* stack.deploy(
71 BindingSelfWorker.pipe(Effect.provide(BindingSelfWorkerLive)),
72 );
73
74 // Dev marker: served from the local dev proxy, no cloud deploy ran.
75 expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/);
76
77 expect(yield* getTextReady(`${deployed.url}/?name=erin`)).toBe("hello erin from self");
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 {
82 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
83 timeout: 180_000,
84 },
85 );
86
87 /**
88 * WorkerA binds the `Counter` hosted by WorkerB (`Counter.from(WorkerB)`)
89 * while WorkerB binds WorkerA (`bindWorker(WorkerA)`). The two Workers are
90 * cycle peers, so both are pre-created before either's outputs resolve —
91 * WorkerA's pre-creation must not read WorkerB's still-unresolved
92 * `workerName` from the cross-script Durable Object binding (#2146).
93 */
94 test.provider(
95 "local workers in a cycle bind a cross-script Durable Object",
96 (stack) =>
97 Effect.gen(function* () {
98 yield* stack.destroy();
99
100 const deployed = yield* stack.deploy(
101 Effect.gen(function* () {
102 const workerA = yield* CycleWorkerA;
103 const workerB = yield* CycleWorkerB;
104 return {
105 urlA: workerA.url.as<string>(),
106 urlB: workerB.url.as<string>(),
107 };
108 }).pipe(Effect.provide(Layer.mergeAll(CycleWorkerALive, CycleWorkerBLive))),
109 );
110
111 // Dev markers: both Workers serve from the local dev proxy, no cloud
112 // deploy ran.
113 expect(deployed.urlA).toMatch(/^http:\/\/localhost:\d+$/);
114 expect(deployed.urlB).toMatch(/^http:\/\/localhost:\d+$/);
115
116 // A request through WorkerA increments WorkerB's Counter, and WorkerB
117 // reads the same instance from its own namespace.
118 const key = crypto.randomUUID();
119 expect(JSON.parse(yield* getTextReady(`${deployed.urlA}/?key=${key}`))).toEqual({
120 value: 1,
121 });
122 expect(JSON.parse(yield* getTextReady(`${deployed.urlB}/?key=${key}`))).toEqual({
123 value: 1,
124 });
125
126 // The other edge of the cycle: WorkerB calls WorkerA.
127 expect(yield* getTextReady(`${deployed.urlB}/worker-a`)).toBe("WorkerA");
128
129 yield* stack.destroy();
130 }).pipe(logLevel),
131 {
132 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
133 timeout: 180_000,
134 },
135 );
136
137 /**
138 * Two plain async Workers bind each other: A binds B's default entrypoint
139 * and B binds A's named `Callbacks` entrypoint. Declaring the stack used to
140 * recurse between the two `env` declarations until the heap ran out (#2672).
141 */
142 test.provider(
143 "local async workers in a cycle call each other",
144 (stack) =>
145 Effect.gen(function* () {
146 yield* stack.destroy();
147
148 const deployed = yield* stack.deploy(
149 Effect.gen(function* () {
150 const a = yield* AsyncCycleA;
151 const b = yield* AsyncCycleB;
152 return { urlA: a.url.as<string>(), urlB: b.url.as<string>() };
153 }),
154 );
155
156 expect(deployed.urlA).toMatch(/^http:\/\/localhost:\d+$/);
157 expect(deployed.urlB).toMatch(/^http:\/\/localhost:\d+$/);
158
159 // A → B.start → A's Callbacks.done: both edges of the cycle.
160 expect(yield* getTextReady(`${deployed.urlA}/?id=7`)).toBe("a got 7");
161
162 yield* stack.destroy();
163 }).pipe(logLevel),
164 {
165 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
166 timeout: 180_000,
167 },
168 );