@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, 06:00 UTC

main
ts 234 lines 9.49 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe, 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 Fiber from "effect/Fiber";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Schedule from "effect/Schedule";
9 import { requestWorker } from "../Utils/WorkerRequest.ts";
10 import WorkerV1 from "./fixtures/catch-up/v1.ts";
11 import WorkerV2 from "./fixtures/catch-up/v2.ts";
12 import WorkerV3 from "./fixtures/catch-up/v3.ts";
13 import WorkerV3b from "./fixtures/catch-up/v3b.ts";
14
15 /** A route answered with something other than what the test waits for. */
16 class NotYet extends Data.TaggedError("NotYet")<{
17 readonly status: number;
18 readonly body: string;
19 }> {}
20
21 /** Call a route on a fresh connection, so the request reaches the latest deployment. */
22 const call = (url: string, method: "GET" | "POST", path: string) =>
23 requestWorker(
24 (method === "GET" ? HttpClientRequest.get : HttpClientRequest.post)(`${url}${path}`).pipe(
25 HttpClientRequest.setHeaders({ connection: "close", "cache-control": "no-cache" }),
26 ),
27 ).pipe(
28 // A dropped connection is the network's, not the Worker's answer: ask again.
29 Effect.retry({
30 while: (error) => error._tag === "HttpClientError",
31 schedule: Schedule.spaced("1 second"),
32 times: 5,
33 }),
34 Effect.flatMap((res) =>
35 res.text.pipe(
36 Effect.flatMap((body) =>
37 res.status === 200
38 ? Effect.succeed(body)
39 : Effect.fail(new NotYet({ status: res.status, body })),
40 ),
41 ),
42 ),
43 );
44
45 /** Wait until the Worker serves `version`: a redeploy takes a few seconds to roll out. */
46 const serving = (url: string, version: "v1" | "v2" | "v3" | "v3b") =>
47 call(url, "GET", "/version").pipe(
48 Effect.filterOrFail(
49 (body) => body === version,
50 (body) => new NotYet({ status: 200, body }),
51 ),
52 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 90 }),
53 );
54
55 const deposit = (url: string, account: string, amount: number) =>
56 call(url, "POST", `/deposit/${account}/${amount}`).pipe(
57 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }),
58 );
59
60 /** Poll the view until an account's deposits are `expected`. */
61 const deposits = (url: string, account: string, expected: ReadonlyArray<number>) =>
62 call(url, "GET", `/deposits/${account}`).pipe(
63 Effect.map((body) => (JSON.parse(body) as { amounts: ReadonlyArray<number> } | null)?.amounts),
64 // Cloudflare rolls a deploy out host by host: a host still on v1 has no such route.
65 Effect.catchIf(
66 (e) => e._tag === "NotYet" && e.status === 404 && e.body === "Not Found",
67 () => Effect.succeed(undefined),
68 ),
69 Effect.repeat({
70 schedule: Schedule.spaced("2 seconds"),
71 until: (amounts) => JSON.stringify(amounts) === JSON.stringify(expected),
72 // Instances still on the code from before the deploy hold the backfill back
73 // until they have been idle, in 30 second steps: allow a few minutes.
74 times: 150,
75 }),
76 );
77
78 /** Poll the timeline (one key fed by every account) until it holds `count` entries. */
79 const timeline = (url: string, count: number) =>
80 call(url, "GET", "/timeline").pipe(
81 Effect.map((body) => (JSON.parse(body) as { entries: ReadonlyArray<string> } | null)?.entries),
82 Effect.catchIf(
83 (e) => e._tag === "NotYet" && e.status === 404 && e.body === "Not Found",
84 () => Effect.succeed(undefined),
85 ),
86 Effect.repeat({
87 schedule: Schedule.spaced("2 seconds"),
88 until: (entries) => (entries?.length ?? 0) >= count,
89 // Instances still on the code from before the deploy hold the backfill back
90 // until they have been idle, in 30 second steps: allow a few minutes.
91 times: 150,
92 }),
93 );
94
95 /**
96 * Poll the view's history until it reports every earlier event has reached it.
97 * The directory answers it, not the view's objects: a Durable Object keeps its
98 * old code while calls keep it busy, so polling the view itself would hold it on
99 * the code from before the deploy.
100 */
101 const historyComplete = (url: string) =>
102 call(url, "GET", "/history").pipe(
103 Effect.map((body) => (JSON.parse(body) as { complete: boolean }).complete),
104 Effect.catchIf(
105 (e) => e._tag === "NotYet" && e.status === 404 && e.body === "Not Found",
106 () => Effect.succeed(false),
107 ),
108 Effect.repeat({
109 schedule: Schedule.spaced("5 seconds"),
110 until: (complete) => complete,
111 // Instances still on the code from before the deploy hold the backfill back
112 // until they have been idle, in 30 second steps: allow a few minutes.
113 times: 150,
114 }),
115 );
116
117 /**
118 * Read the view's history once. A deploy waits for the backfill it starts, so
119 * right after one it is already complete; only a host still on the code from
120 * before the deploy (which has no such route) is asked again.
121 */
122 const historyNow = (url: string) =>
123 call(url, "GET", "/history").pipe(
124 Effect.map((body) => (JSON.parse(body) as { complete: boolean }).complete),
125 Effect.retry({
126 while: (e) => e._tag === "NotYet" && e.status === 404 && e.body === "Not Found",
127 schedule: Schedule.spaced("2 seconds"),
128 times: 30,
129 }),
130 );
131
132 // Locally a redeploy restarts every object at once; live, an object keeps its old
133 // code until it has been idle, and the catch-up must wait it out.
134 for (const dev of [true, false]) {
135 describe(dev ? "local catch-up" : "live catch-up", () => {
136 const { test } = Test.make({ providers: Cloudflare.providers(), dev });
137
138 test.provider(
139 // The mode is in the name: the stack is named after the test, and the local and
140 // live runs must not share one.
141 `${dev ? "local" : "live"}: a view added in a redeploy catches up, and a version bump rebuilds it`,
142 (stack) =>
143 Effect.gen(function* () {
144 yield* stack.destroy();
145
146 const { worker: v1 } = yield* stack.deploy(
147 Effect.gen(function* () {
148 return { worker: yield* WorkerV1 };
149 }),
150 );
151 const url = v1.url!.replace(/\/+$/, "");
152 yield* serving(url, "v1");
153 const history: Record<string, ReadonlyArray<number>> = {
154 "a-1": [10, 20],
155 "a-2": [5],
156 "a-3": [7, 8, 9],
157 };
158 for (const [account, amounts] of Object.entries(history)) {
159 for (const amount of amounts) yield* deposit(url, account, amount);
160 }
161
162 // Redeploy with the view: the accounts' history reaches it without another command.
163 yield* stack.deploy(
164 Effect.gen(function* () {
165 return { worker: yield* WorkerV2 };
166 }),
167 );
168 yield* serving(url, "v2");
169 // The deploy returned once the backfill was done.
170 if (!dev) expect(yield* historyNow(url)).toBe(true);
171 for (const [account, amounts] of Object.entries(history)) {
172 expect(yield* deposits(url, account, amounts)).toEqual(amounts);
173 }
174
175 // One key fed by every account merges their history in the order it was committed.
176 const committed = Object.entries(history).flatMap(([account, amounts]) =>
177 amounts.map((amount) => `${account}:${amount}`),
178 );
179 expect(yield* timeline(url, committed.length)).toEqual(committed);
180
181 // Caught up, it receives new deposits live, after the history.
182 yield* deposit(url, "a-1", 30);
183 expect(yield* deposits(url, "a-1", [10, 20, 30])).toEqual([10, 20, 30]);
184 expect(yield* historyComplete(url)).toBe(true);
185
186 // Redefine the view (it now doubles each deposit) and bump its version: it is
187 // rebuilt. Written while the deploy waits on the rebuild: a new account, and an
188 // account that was inactive.
189 const writes = yield* Effect.forkChild(
190 Effect.gen(function* () {
191 yield* serving(url, "v3");
192 yield* deposit(url, "a-4", 4);
193 yield* deposit(url, "a-2", 6);
194 }),
195 );
196 yield* stack.deploy(
197 Effect.gen(function* () {
198 return { worker: yield* WorkerV3 };
199 }),
200 );
201 yield* Fiber.join(writes);
202 // New code with the same model: no second run, and the history stays complete.
203 yield* stack.deploy(
204 Effect.gen(function* () {
205 return { worker: yield* WorkerV3b };
206 }),
207 );
208 yield* serving(url, "v3b");
209 if (!dev) expect(yield* historyNow(url)).toBe(true);
210 const doubled: Record<string, ReadonlyArray<number>> = {
211 "a-1": [20, 40, 60],
212 "a-2": [10, 12],
213 "a-3": [14, 16, 18],
214 "a-4": [8],
215 };
216 expect(yield* historyComplete(url)).toBe(true);
217 for (const [account, amounts] of Object.entries(doubled)) {
218 expect(yield* deposits(url, account, amounts)).toEqual(amounts);
219 }
220 // The timeline was not rebuilt: it kept its history in order, and received the
221 // new deposits live (live deliveries from different accounts arrive in any order).
222 const after = yield* timeline(url, committed.length + 3);
223 expect(after?.slice(0, committed.length + 1)).toEqual([...committed, "a-1:30"]);
224 expect([...(after?.slice(committed.length + 1) ?? [])].sort()).toEqual([
225 "a-2:6",
226 "a-4:4",
227 ]);
228
229 yield* stack.destroy();
230 }),
231 { timeout: 600_000 },
232 );
233 });
234 }