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

main
ts 151 lines 5.53 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 Alchemy from "alchemy";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import ChatPersistenceTestWorker from "./fixtures/ChatPersistenceWorker.ts";
10 import { Gateway } from "./fixtures/Gateway.ts";
11
12 // Fresh `workers.dev` URLs return non-200 (404 / 500 "Script not
13 // found") for a few seconds while the edge propagates. Each test uses
14 // `HttpClient.filterStatusOk(yield* HttpClient.HttpClient)` so the
15 // existing `Effect.retry` rides through these by converting the
16 // bad-status response into a retryable Effect failure.
17
18 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
19 providers: Cloudflare.providers(),
20 });
21
22 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
23
24 const Stack = Alchemy.Stack(
25 "AiGatewayChatPersistenceStack",
26 {
27 providers: Cloudflare.providers(),
28 state: Cloudflare.state(),
29 },
30 Effect.gen(function* () {
31 const worker = yield* ChatPersistenceTestWorker;
32 const gateway = yield* Gateway;
33 return {
34 gatewayId: gateway.gatewayId,
35 url: worker.url.as<string>(),
36 };
37 }),
38 );
39
40 const stack = beforeAll(deploy(Stack));
41 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
42
43 // Cap exponential backoff at 3s so retries stay bounded when the CF edge is
44 // slow (otherwise the geometric blow-up dominates wall time).
45 const readinessSchedule = Schedule.min([
46 Schedule.exponential("500 millis"),
47 Schedule.spaced("3 seconds"),
48 ]);
49
50 // `filterStatusOk` turns a cold-start non-200 (e.g. a 500 HTML error page)
51 // into a retryable failure, and `catchDefect` promotes any cold-start defect
52 // (`Handler does not export a fetch() function.`) into a failure too — so the
53 // readiness retry absorbs both while the worker propagates to the edge.
54 const retryReady = <A, E, R>(eff: Effect.Effect<A, E, R>) =>
55 eff.pipe(
56 Effect.catchDefect((defect) => Effect.fail(defect)),
57 Effect.retry({ schedule: readinessSchedule, times: 15 }),
58 );
59
60 test(
61 "first turn against a fresh thread persists user + assistant messages",
62 Effect.gen(function* () {
63 const out = yield* stack;
64 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
65 const id = `fresh-${Date.now()}`;
66
67 const res = yield* client
68 .get(`${out.url}/chat?id=${id}&prompt=${encodeURIComponent("Say the single word 'pong'.")}`)
69 .pipe(retryReady);
70 expect(res.status).toBe(200);
71
72 const body = (yield* res.json) as { text: string; turns: number };
73 expect(typeof body.text).toBe("string");
74 expect(body.text.length).toBeGreaterThan(0);
75 // The empty chat starts with no messages; one turn appends the user
76 // prompt and the assistant reply.
77 expect(body.turns).toBe(2);
78 }).pipe(logLevel),
79 {
80 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
81 timeout: 180_000,
82 },
83 );
84
85 test(
86 "persisted chat survives across DO invocations",
87 Effect.gen(function* () {
88 const out = yield* stack;
89 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
90 const id = `memory-${Date.now()}`;
91
92 const r1 = yield* client
93 .get(`${out.url}/chat?id=${id}&prompt=${encodeURIComponent("My name is Sam. Remember it.")}`)
94 .pipe(retryReady);
95 expect(r1.status).toBe(200);
96 const b1 = (yield* r1.json) as { text: string; turns: number };
97 expect(b1.turns).toBe(2);
98
99 // Second request hits the same DO instance (same `id`). The chat
100 // history is reloaded from `state.storage`, so the model can recall
101 // the name from the first turn.
102 const r2 = yield* client
103 .get(
104 `${out.url}/chat?id=${id}&prompt=${encodeURIComponent("What is my name? Answer with just the name.")}`,
105 )
106 .pipe(retryReady);
107 expect(r2.status).toBe(200);
108 const b2 = (yield* r2.json) as { text: string; turns: number };
109
110 expect(b2.text.toLowerCase()).toContain("sam");
111 // The second turn appends another user + assistant pair on top of the
112 // restored history.
113 expect(b2.turns).toBe(4);
114 }).pipe(logLevel),
115 {
116 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
117 timeout: 240_000,
118 },
119 );
120
121 test(
122 "distinct thread ids map to isolated histories",
123 Effect.gen(function* () {
124 const out = yield* stack;
125 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
126 const stamp = Date.now();
127 const idA = `iso-a-${stamp}`;
128 const idB = `iso-b-${stamp}`;
129
130 const ra = yield* client
131 .get(
132 `${out.url}/chat?id=${idA}&prompt=${encodeURIComponent("My favorite color is teal. Remember it.")}`,
133 )
134 .pipe(retryReady);
135 expect(ra.status).toBe(200);
136 expect(((yield* ra.json) as { turns: number }).turns).toBe(2);
137
138 // A different id is a different DO instance with its own storage, so
139 // its first turn also starts from an empty history (turns === 2),
140 // proving the two threads don't share state.
141 const rb = yield* client
142 .get(`${out.url}/chat?id=${idB}&prompt=${encodeURIComponent("Say the single word 'pong'.")}`)
143 .pipe(retryReady);
144 expect(rb.status).toBe(200);
145 expect(((yield* rb.json) as { turns: number }).turns).toBe(2);
146 }).pipe(logLevel),
147 {
148 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
149 timeout: 180_000,
150 },
151 );