@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, 04:45 UTC

main
ts 281 lines 10.8 KB
Raw
1 import { expect } from "bun:test";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Effect from "effect/Effect";
5 import * as HttpBody from "effect/http/HttpBody";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Redacted from "effect/Redacted";
9 import * as Schedule from "effect/Schedule";
10 import Stack from "../alchemy.run.ts";
11 import { WORKFLOW_SECRET_VALUE } from "../src/NotifyWorkflow.ts";
12
13 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
14 providers: Cloudflare.providers(),
15 state: Cloudflare.state(),
16 // dev: true,
17 });
18
19 // This stack deploys a Container (Sandbox) whose image build + push can take
20 // well over the default 120s hook budget, so give deploy/destroy more room.
21 const stack = beforeAll(deploy(Stack), { timeout: 600_000 });
22
23 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
24 timeout: 600_000,
25 });
26
27 // A fresh `workers.dev` URL transiently 404s/5xxs while the route propagates;
28 // `HttpClient.execute` resolves on those, so retry until the worker answers.
29 const { executeWhenReady } = Test;
30
31 test(
32 "integ",
33 Effect.gen(function* () {
34 const { url } = yield* stack;
35
36 expect(url).toBeString();
37 }),
38 );
39
40 /**
41 * Better Auth on D1 (auto-migrated at deploy): sign-up + sign-in through
42 * the `/auth/*` routes served by `auth.fetch`, asserting a session cookie
43 * comes back. The assets auth panel (`index.html`) drives the same routes
44 * from the browser.
45 */
46 test(
47 "better auth: sign-up and sign-in on D1",
48 Effect.gen(function* () {
49 const { url } = yield* stack;
50 const email = "auth-integ@example.com";
51 const password = "password1234";
52
53 const post = (path: string, body: unknown) =>
54 Effect.tryPromise(async (signal) => {
55 const response = await fetch(`${url}${path}`, {
56 method: "POST",
57 signal,
58 headers: { "content-type": "application/json" },
59 body: JSON.stringify(body),
60 });
61 return {
62 status: response.status,
63 body: await response.text(),
64 cookies: response.headers.getSetCookie(),
65 };
66 });
67
68 // A leftover user from a prior NO_DESTROY run is fine — sign-in is the
69 // real assertion. Retries ride out workers.dev propagation.
70 yield* post("/auth/sign-up/email", { email, password, name: "Integ" }).pipe(
71 Effect.filterOrFail(
72 (r) => r.status === 200 || r.body.includes("USER_ALREADY_EXISTS"),
73 (r) => new Error(`sign-up failed: ${r.status} ${r.body.slice(0, 200)}`),
74 ),
75 Effect.retry({ schedule: Schedule.exponential("1 second"), times: 8 }),
76 );
77
78 const signIn = yield* post("/auth/sign-in/email", { email, password });
79 expect(signIn.status).toBe(200);
80 expect(signIn.cookies.length).toBeGreaterThan(0);
81 }),
82 { timeout: 120_000 },
83 );
84
85 /**
86 * Regression guard for https://github.com/alchemy-run/alchemy/pull/172
87 *
88 * The stack now includes two Workers (`Api` and `SecondaryApi`) that both
89 * bind the same `Agent` Durable Object, which in turn binds the `Sandbox`
90 * Container. Each `yield* Agent` runs the DO's outer init, calling
91 * `Cloudflare.Container(Sandbox)` once per Worker, so the Sandbox
92 * ContainerApplication receives two bindings sharing one `namespaceId`.
93 *
94 * Before the dedupe fix, `getDurableObjects` counted those as two distinct
95 * namespaces and the deploy in `beforeAll` died with:
96 *
97 * "A Container can only be bound to one Durable Object namespace.
98 * Found 2 namespaces in bindings: <id>, <id>"
99 *
100 * If the deploy ever starts failing again, the whole suite stops at
101 * `beforeAll` — that is the regression signal. This case just asserts the
102 * second Worker showed up with a URL so a silent regression that drops the
103 * binding still surfaces here.
104 */
105 test(
106 "two workers binding the same container deploy without dedup error",
107 Effect.gen(function* () {
108 const { secondaryApiUrl } = yield* stack;
109 expect(secondaryApiUrl).toBeString();
110 }),
111 );
112
113 /**
114 * Regression guard for https://github.com/alchemy-run/alchemy/pull/71
115 *
116 * `NotifyWorkflow` accesses `Cloudflare.Workers.WorkerEnvironment` inside its body and
117 * performs a KV roundtrip via `env.KV.put` / `env.KV.get`. If the fix from #71
118 * is ever reverted, the body Effect loses the `WorkerEnvironment` service and
119 * dies with `Service not found: Cloudflare.Workers.WorkerEnvironment` on the
120 * first `yield* Cloudflare.Workers.WorkerEnvironment` — the workflow instance never
121 * reaches `complete`, and this test times out or surfaces the `errored` status.
122 */
123 test(
124 "workflow body can access WorkerEnvironment and exercise env bindings",
125 Effect.gen(function* () {
126 const { url } = yield* stack;
127
128 interface WorkflowStatus {
129 status: string;
130 output?: { secret?: string };
131 error?: unknown;
132 }
133
134 // Start a fresh workflow instance and poll it to a terminal state. A
135 // freshly-deployed worker occasionally errors a step while its bindings
136 // are still propagating, so this effect FAILS on any non-complete
137 // terminal state, letting the outer retry take another swing with a
138 // brand-new instance rather than flaking on a transient `errored`.
139 const runOnce = Effect.gen(function* () {
140 const roomId = `smoke-${Date.now()}`;
141
142 // A freshly-deployed worker transiently 404s (route still propagating)
143 // or 5xxs (bindings still settling) on the workflow routes, so retry
144 // through the cold-start window instead of asserting `200` on the first
145 // hit. Only a non-cold-start status reaches the `expect` below.
146 const startResponse = yield* executeWhenReady(
147 HttpClientRequest.post(`${url}/workflow/start/${roomId}`),
148 );
149 expect(startResponse.status).toBe(200);
150
151 const { instanceId } = (yield* startResponse.json) as {
152 instanceId: string;
153 };
154 expect(instanceId).toBeString();
155
156 const client = yield* HttpClient.HttpClient;
157 const lastStatus = yield* client.get(`${url}/workflow/status/${instanceId}`).pipe(
158 // Only decode JSON on a 200; a transient 5xx (HTML error page) while
159 // the worker settles is treated as non-terminal so the poll keeps
160 // swinging instead of dying on a JSON decode error.
161 Effect.flatMap((res) =>
162 res.status === 200
163 ? (res.json as Effect.Effect<unknown, unknown>).pipe(
164 Effect.map((body) => body as WorkflowStatus),
165 )
166 : Effect.succeed({ status: "pending" } as WorkflowStatus),
167 ),
168 Effect.repeat({
169 schedule: Schedule.spaced("2 seconds"),
170 until: (s) => s.status === "complete" || s.status === "errored",
171 times: 30,
172 }),
173 );
174
175 // Surface a non-complete terminal state as a failure so the outer retry
176 // can restart with a fresh instance.
177 if (lastStatus.status !== "complete") {
178 return yield* Effect.fail(
179 new Error(`workflow ${lastStatus.status}: ${JSON.stringify(lastStatus.error)}`),
180 );
181 }
182 return lastStatus;
183 });
184
185 // Cloudflare can briefly route workflow invocations to a worker version
186 // that predates the final upload (e.g. the pre-create stub), which
187 // errors instances with "The entrypoint name Notifier was not found in
188 // this worker" until the deployed version propagates. Each errored
189 // attempt terminates within a few seconds, so give propagation a
190 // bounded ~45s of fresh instances rather than 3 swings in 16s.
191 const lastStatus = yield* runOnce.pipe(
192 Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 6 }),
193 );
194
195 expect(lastStatus.status).toBe("complete");
196 expect(lastStatus.error).toBeFalsy();
197
198 // Prove the `Alchemy.Secret(...)` bound at plantime made it all the
199 // way through to the workflow body's runtime read. The workflow body
200 // unwraps `Redacted.value(secret)` and embeds it in the returned
201 // `processed` payload.
202 expect(lastStatus.output?.secret).toBe(Redacted.value(WORKFLOW_SECRET_VALUE));
203 }),
204 { timeout: 120_000 },
205 );
206
207 /**
208 * Queue producer→consumer round-trip via the Effect-style
209 * `Cloudflare.Queues.consumeQueueMessages(Queue, handler)` API.
210 *
211 * Producer: `POST /queue/send` returns `{ sent: { id, text, sentAt } }`
212 * after enqueuing a message.
213 *
214 * Consumer: the worker's queue() handler (registered via subscribe in
215 * src/Api.ts) writes the message body to R2 at `/queue/<id>`. The
216 * route `GET /queue/result/<id>` reads it back. Cloudflare's queue
217 * dispatch is async and best-effort, so we poll for up to 60s.
218 */
219 test(
220 "queue producer→consumer round-trip via consumeQueueMessages()",
221 Effect.gen(function* () {
222 const { url } = yield* stack;
223 const text = `hello-${Date.now()}`;
224 type Message = { id: string; text: string; sentAt: number };
225
226 const send = Effect.gen(function* () {
227 const sendResponse = yield* executeWhenReady(
228 HttpClientRequest.post(`${url}/queue/send`).pipe(
229 HttpClientRequest.setBody(HttpBody.text(text)),
230 ),
231 );
232 expect(sendResponse.status).toBe(202);
233 const { sent } = (yield* sendResponse.json) as { sent: Message };
234 expect(sent.id).toBeTypeOf("string");
235 return sent;
236 });
237
238 const sent: Message[] = [yield* send];
239
240 // First delivery on a freshly created queue+consumer can lag well past
241 // the settled steady state (consumer attachment propagates through
242 // Cloudflare's queue subsystem asynchronously). Poll for ANY of the
243 // messages we sent, re-sending every ~20s in case an early message was
244 // published into the propagation window; the consumer persists each
245 // message to R2 keyed by its id, so the first one to land wins.
246 let polls = 0;
247 const findConsumed = Effect.gen(function* () {
248 polls++;
249 if (polls % 10 === 0 && sent.length < 5) {
250 sent.push(yield* send);
251 }
252 for (const message of sent) {
253 const resultResponse = yield* HttpClient.get(`${url}/queue/result/${message.id}`);
254 if (resultResponse.status === 200) {
255 return (yield* resultResponse.json) as Message;
256 }
257 }
258 return undefined;
259 });
260
261 const consumed = yield* findConsumed.pipe(
262 Effect.repeat({
263 schedule: Schedule.spaced("2 seconds"),
264 until: (message) => message !== undefined,
265 times: 75,
266 }),
267 );
268
269 expect(consumed).toBeDefined();
270 expect(sent.map((message) => message.id)).toContain(consumed!.id);
271 expect(consumed!.text).toBe(text);
272
273 // Clean up the consumed R2 entries so afterAll's stack.destroy()
274 // can delete the bucket — otherwise Cloudflare rejects the
275 // bucket delete with "bucket is not empty".
276 yield* Effect.forEach(sent, (message) =>
277 HttpClient.execute(HttpClientRequest.make("DELETE")(`${url}/queue/result/${message.id}`)),
278 );
279 }),
280 { timeout: 180_000 },
281 );