@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 264 lines 9.68 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { progressOf } from "@alchemy.run/cloudflare/Domain/Backfill";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as workflows from "@distilled.cloud/cloudflare/workflows";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Fiber from "effect/Fiber";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import { requestWorker } from "../Utils/WorkerRequest.ts";
14 import BenchV1 from "./fixtures/bench/v1.ts";
15 import BenchV2Short from "./fixtures/bench/v2-short.ts";
16 import BenchV2 from "./fixtures/bench/v2.ts";
17 import BenchV3 from "./fixtures/bench/v3.ts";
18 import LedgerV1 from "./fixtures/catch-up/v1.ts";
19 import LedgerV2WideBumped from "./fixtures/catch-up/v2-wide-bumped.ts";
20 import LedgerV2Wide from "./fixtures/catch-up/v2-wide.ts";
21 import LedgerV2 from "./fixtures/catch-up/v2.ts";
22
23 /**
24 * How a deploy and a backfill behave together on Cloudflare: a deploy waits
25 * for the backfill and reports its progress; it is safe to repeat, to time
26 * out and to interrupt (the backfill runs on, and the next deploy waits on it);
27 * a version bump rebuilds a view while its backfill runs; and removing a view
28 * stops its backfill. Each case seeds accounts, then deploys views over them.
29 */
30
31 const ACCOUNTS = 1000;
32 const EVENTS = 5;
33 // Ten accounts per request, many requests at once.
34 const SEED_BATCH = 10;
35
36 class NotYet extends Data.TaggedError("NotYet")<{
37 readonly status: number;
38 readonly body: string;
39 }> {}
40
41 const call = (url: string, method: "GET" | "POST", path: string) =>
42 requestWorker(
43 (method === "GET" ? HttpClientRequest.get : HttpClientRequest.post)(`${url}${path}`).pipe(
44 HttpClientRequest.setHeaders({ connection: "close", "cache-control": "no-cache" }),
45 ),
46 ).pipe(
47 Effect.flatMap((res) =>
48 res.text.pipe(
49 Effect.flatMap((body) =>
50 res.status === 200
51 ? Effect.succeed(body)
52 : Effect.fail(new NotYet({ status: res.status, body })),
53 ),
54 ),
55 ),
56 Effect.timeout("2 minutes"),
57 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }),
58 );
59
60 const serving = (url: string, version: string) =>
61 call(url, "GET", "/version").pipe(
62 Effect.filterOrFail(
63 (body) => body === version,
64 (body) => new NotYet({ status: 200, body }),
65 ),
66 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 90 }),
67 );
68
69 /** Deploy accounts only, and give each of them its deposits. */
70 const seeded = (stack: Test.ScratchStack) =>
71 Effect.gen(function* () {
72 yield* stack.destroy();
73 const { worker } = yield* stack.deploy(
74 Effect.gen(function* () {
75 return { worker: yield* BenchV1 };
76 }),
77 );
78 const url = worker.url!.replace(/\/+$/, "");
79 yield* serving(url, "v1");
80 yield* Effect.forEach(
81 Array.from({ length: ACCOUNTS / SEED_BATCH }, (_, i) => i * SEED_BATCH),
82 (from) => call(url, "POST", `/seed/${from}/${SEED_BATCH}/${EVENTS}`),
83 { concurrency: 32, discard: true },
84 );
85 return { url, workerName: worker.workerName };
86 });
87
88 const deployOf = (stack: Test.ScratchStack, worker: typeof BenchV1) =>
89 stack.deploy(
90 Effect.gen(function* () {
91 return { worker: yield* worker };
92 }),
93 );
94
95 /** The view's state for a few accounts: each has every deposit, once. */
96 const checkViews = (url: string) =>
97 Effect.gen(function* () {
98 const expected = Array.from({ length: EVENTS }, (_, n) => n + 1);
99 for (const i of [0, ACCOUNTS / 2, ACCOUNTS - 1]) {
100 const view = JSON.parse(yield* call(url, "GET", `/view/b${i}`)) as {
101 amounts: Array<number>;
102 } | null;
103 expect(view?.amounts).toEqual(expected);
104 }
105 const history = JSON.parse(yield* call(url, "GET", "/history")) as { complete: boolean };
106 expect(history.complete).toBe(true);
107 });
108
109 /** Whether a deploy failed because the backfill outlasted its wait. */
110 const stillRunning = (deploy: Effect.Effect<unknown, unknown, never>) =>
111 deploy.pipe(
112 Effect.as(""),
113 Effect.catchCause((cause) => Effect.succeed(String(cause))),
114 Effect.map((failure) => /BackfillStillRunning|still running/.test(failure)),
115 );
116
117 /** The latest completed instance of the Worker's backfill Workflow. */
118 const lastWait = (workerName: string) =>
119 Effect.gen(function* () {
120 const { accountId } = yield* yield* CloudflareEnvironment;
121 const all = yield* workflows.listWorkflows.items({ accountId }).pipe(Stream.runCollect);
122 const workflow = [...all].find((w) => w.scriptName === workerName);
123 expect(workflow).toBeDefined();
124 const instances = yield* workflows.listInstances
125 .items({ accountId, workflowName: workflow!.name, status: "complete" })
126 .pipe(Stream.runCollect);
127 const latest = [...instances].sort((a, b) => b.createdOn.localeCompare(a.createdOn))[0];
128 expect(latest).toBeDefined();
129 return yield* workflows.getInstance({
130 accountId,
131 workflowName: workflow!.name,
132 instanceId: latest!.id,
133 });
134 });
135
136 const { test } = Test.make({ providers: Cloudflare.providers() });
137
138 describe.sequential("a deploy and its backfill", () => {
139 test.provider(
140 "a deploy waits for the backfill and reports its progress, and the same deploy again changes nothing",
141 (stack) =>
142 Effect.gen(function* () {
143 const { url, workerName } = yield* seeded(stack);
144 yield* deployOf(stack, BenchV2);
145 yield* serving(url, "v2");
146 yield* checkViews(url);
147
148 // What the deploy reported as it waited: the run, done, over every account.
149 const wait = yield* lastWait(workerName);
150 const [run] = progressOf(wait.steps ?? []) ?? [];
151 expect(run?.target).toBe("view:Deposits");
152 expect(run?.phase).toBe("complete");
153 expect(run?.done).toBe(run?.subtrees);
154 expect(run?.instances).toBeGreaterThanOrEqual(ACCOUNTS);
155
156 // The same model again: nothing to backfill, nothing to wait for.
157 const plan = yield* stack.plan(
158 Effect.gen(function* () {
159 return { worker: yield* BenchV2 };
160 }),
161 );
162 expect(plan.resources["Ledger.DomainBackfillRun"]?.action).toBe("noop");
163
164 yield* stack.destroy();
165 }),
166 { timeout: 900_000 },
167 );
168
169 test.provider(
170 "a deploy that times out leaves the backfill running, and the next deploy waits for it",
171 (stack) =>
172 Effect.gen(function* () {
173 const { url } = yield* seeded(stack);
174 expect(yield* stillRunning(deployOf(stack, BenchV2Short))).toBe(true);
175 yield* deployOf(stack, BenchV2);
176 yield* serving(url, "v2");
177 yield* checkViews(url);
178 yield* stack.destroy();
179 }),
180 { timeout: 900_000 },
181 );
182
183 test.provider(
184 "an interrupted deploy leaves the backfill running, and the next deploy waits for it",
185 (stack) =>
186 Effect.gen(function* () {
187 const { url } = yield* seeded(stack);
188 // Interrupt the deploy once the view's code is up and its backfill has begun.
189 const deploy = yield* Effect.forkChild(deployOf(stack, BenchV2));
190 yield* serving(url, "v2");
191 yield* Fiber.interrupt(deploy);
192 yield* deployOf(stack, BenchV2);
193 yield* checkViews(url);
194 yield* stack.destroy();
195 }),
196 { timeout: 900_000 },
197 );
198
199 test.provider(
200 "a version bump while a backfill runs rebuilds the view from the history",
201 (stack) =>
202 Effect.gen(function* () {
203 const { url } = yield* seeded(stack);
204 expect(yield* stillRunning(deployOf(stack, BenchV2Short))).toBe(true);
205 yield* deployOf(stack, BenchV3);
206 yield* serving(url, "v3");
207 yield* checkViews(url);
208 yield* stack.destroy();
209 }),
210 { timeout: 900_000 },
211 );
212
213 test.provider(
214 "removing a view while it backfills stops it, and adding it back rebuilds it",
215 (stack) =>
216 Effect.gen(function* () {
217 const { url } = yield* seeded(stack);
218 expect(yield* stillRunning(deployOf(stack, BenchV2Short))).toBe(true);
219 yield* deployOf(stack, BenchV1);
220 yield* serving(url, "v1");
221 yield* deployOf(stack, BenchV3);
222 yield* serving(url, "v3");
223 yield* checkViews(url);
224 yield* stack.destroy();
225 }),
226 { timeout: 900_000 },
227 );
228
229 test.provider(
230 "a view that reads more sources without a version bump fails the deploy, and a bump rebuilds it",
231 (stack) =>
232 Effect.gen(function* () {
233 yield* stack.destroy();
234 const ledger = (worker: typeof LedgerV1) =>
235 stack.deploy(
236 Effect.gen(function* () {
237 return { worker: yield* worker };
238 }),
239 );
240 const { worker } = yield* ledger(LedgerV1);
241 const url = worker.url!.replace(/\/+$/, "");
242 yield* serving(url, "v1");
243 yield* call(url, "POST", "/deposit/a-1/10");
244 yield* call(url, "POST", "/deposit/a-2/5");
245 yield* ledger(LedgerV2);
246
247 // The timeline's backfill read accounts only: the deploy refuses to wait on it.
248 const refused = yield* ledger(LedgerV2Wide).pipe(
249 Effect.as(""),
250 Effect.catchCause((cause) => Effect.succeed(String(cause))),
251 );
252 expect(refused).toContain("Bump its version");
253
254 yield* ledger(LedgerV2WideBumped);
255 yield* serving(url, "v2-wide-bumped");
256 const timeline = JSON.parse(yield* call(url, "GET", "/timeline")) as {
257 entries: Array<string>;
258 } | null;
259 expect([...(timeline?.entries ?? [])].sort()).toEqual(["a-1:10", "a-2:5"]);
260 yield* stack.destroy();
261 }),
262 { timeout: 900_000 },
263 );
264 });