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

main
ts 65 lines 2.13 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 { createFlueClient } from "@flue/sdk";
5 import * as Console from "effect/Console";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import Stack from "../alchemy.run.ts";
9
10 // Fresh `workers.dev` URLs transiently 404 while the route propagates.
11 // `Test.getWhenReady` retries until the Worker serves a real response.
12 const { getWhenReady } = Test;
13
14 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
15 providers: Cloudflare.providers(),
16 state: Cloudflare.state(),
17 });
18
19 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
20 timeout: 600_000,
21 });
22 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
23
24 const base = Effect.map(stack, ({ url }) => {
25 if (!url) throw new Error("expected the site to expose a workers.dev url");
26 return String(url).replace(/\/+$/, "");
27 });
28
29 test(
30 "serves a Hono route from app.ts",
31 Effect.gen(function* () {
32 const url = yield* base;
33 const res = yield* getWhenReady(`${url}/api/ping`);
34 expect(res.status).toBe(200);
35 expect(yield* res.json).toEqual({ pong: true });
36 }),
37 { timeout: 180_000 },
38 );
39
40 test(
41 "the agent answers through Workers AI",
42 Effect.gen(function* () {
43 const url = yield* base;
44 // Wait for the Worker before talking to the agent.
45 yield* getWhenReady(`${url}/api/ping`);
46 // A conversation lives at the agent's mount plus a caller-chosen id; the
47 // agent's Durable Object runs the model call through `env.AI`.
48 const client = createFlueClient({
49 url: `${url}/agents/assistant/integ-${crypto.randomUUID()}`,
50 });
51 const reply = yield* Effect.tryPromise(async () => {
52 const admission = await client.send({
53 message: { kind: "user", body: "Reply with the single word PONG and nothing else." },
54 });
55 return client.read(admission);
56 }).pipe(
57 Effect.retry({
58 schedule: Schedule.spaced("3 seconds"),
59 times: 5,
60 }),
61 );
62 expect(reply.text.toUpperCase()).toContain("PONG");
63 }),
64 { timeout: 300_000 },
65 );