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

main
ts 406 lines 17.6 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareApiLive } from "@alchemy.run/cloudflare/Providers";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as workers from "@distilled.cloud/cloudflare/workers";
6 import * as Alchemy from "alchemy";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as HttpClientRequest from "effect/http/HttpClientRequest";
12 import * as Schedule from "effect/Schedule";
13 import { requestWorker, waitUntilStable } from "../Utils/WorkerRequest.ts";
14 import type {
15 CursorErrors,
16 CursorMethods,
17 CursorStreams,
18 HistoryRow,
19 } from "./fixtures/sql-migrations/object.ts";
20 import SqlMigrationsWorker from "./fixtures/sql-migrations/worker.ts";
21
22 type MigratedState = {
23 tag: string;
24 table: string;
25 records: { name: string; hash: string; created_at: number | null }[];
26 boots: number;
27 payloadLength: number;
28 history: HistoryRow[];
29 tables: { name: string }[];
30 users: { name: string; posts: string[] }[];
31 };
32
33 type CustomState = {
34 tag: string;
35 table: string;
36 names: string[];
37 history: HistoryRow[];
38 tables: { name: string }[];
39 values: string[];
40 };
41
42 type RollbackState = {
43 error: string | null;
44 history: HistoryRow[];
45 tables: { name: string }[];
46 values: string[];
47 names: string[];
48 };
49
50 type AdoptionState = {
51 before: HistoryRow[];
52 after: HistoryRow[];
53 adopted: HistoryRow[];
54 repeated: HistoryRow[];
55 names: string[];
56 users: { name: string; posts: string[] }[];
57 };
58
59 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
60 status: number;
61 message: string;
62 }> {}
63
64 const migrationNames = ["20240101000000_init", "20240102000000_posts"];
65 const seedUsers = [{ name: "seed", posts: ["pending migration"] }];
66
67 for (const dev of [true, false]) {
68 const state = dev ? Alchemy.inMemoryState() : Cloudflare.state();
69 describe(
70 `SqlMigrations (${dev ? "local" : "live"})`,
71 { tags: ["provider:cloudflare", "provider:cloudflare:worker"] },
72 () => {
73 const options = {
74 providers: Cloudflare.providers(),
75 state,
76 dev,
77 stage: dev ? Test.defaultStage() : `${Test.defaultStage()}_sql_migrations_live`,
78 };
79 // Leading destroy closes its harness scope; deployment needs a fresh one.
80 const cleanup = Test.make(options);
81 const { test, beforeAll, afterAll, deploy, destroy } = Test.make(options);
82 const Stack = Alchemy.Stack(
83 dev ? "SqlMigrationsLocalStack" : "SqlMigrationsLiveStack",
84 { providers: Cloudflare.providers(), state },
85 Effect.gen(function* () {
86 const worker = yield* SqlMigrationsWorker;
87 return {
88 url: worker.url.as<string>(),
89 workerName: worker.workerName,
90 accountId: worker.accountId,
91 };
92 }),
93 );
94 cleanup.beforeAll(cleanup.destroy(Stack), { timeout: 120_000 });
95 const stack = beforeAll(
96 Effect.gen(function* () {
97 const deployed = yield* deploy(Stack);
98 expect(deployed.url).toMatch(dev ? /^http:\/\/localhost:\d+/ : /^https:\/\//);
99 const client = yield* HttpClient.HttpClient;
100 // Readiness never invokes a migration or repeats an application
101 // write. A new version reaches Cloudflare's hosts one at a time, so
102 // `/health` must answer ready several times in a row.
103 yield* waitUntilStable(
104 `SQL Worker at ${deployed.url}`,
105 Effect.gen(function* () {
106 const response = yield* client.get(`${deployed.url}/health`);
107 const body = yield* response.text;
108 if (response.status !== 200 || body !== "sql-migrations:ready") {
109 return yield* Effect.fail(
110 new WorkerNotReady({
111 status: response.status,
112 message: `GET ${deployed.url}/health: ${response.status}: ${body}`,
113 }),
114 );
115 }
116 return true;
117 }).pipe(
118 Effect.timeout("2 seconds"),
119 Effect.tapError((error) => Effect.logWarning("SQL Worker readiness", error)),
120 Effect.orElseSucceed(() => false),
121 ),
122 { spacing: "2 seconds", timeout: "60 seconds" },
123 );
124 // `/health` never reaches the Durable Object namespace, which a fresh
125 // deploy also rolls out host by host (first calls fail with an opaque
126 // "internal error"). Probe it through a dedicated object name: it
127 // migrates its own isolated database, so the objects the tests
128 // inspect still activate exactly once.
129 yield* waitUntilStable(
130 `SQL Durable Object namespace at ${deployed.url}`,
131 requestWorker(HttpClientRequest.get(`${deployed.url}/state?name=readiness-probe`)).pipe(
132 Effect.map((response) => response.status === 200),
133 Effect.catchTag("WorkerNotPropagated", () => Effect.succeed(false)),
134 ),
135 { timeout: "60 seconds" },
136 );
137 // New actor names can still hit namespace propagation independently.
138 // Warm the cursor actors with reads; never retry cursor operations.
139 yield* Effect.forEach(
140 ["cursor-methods", "cursor-streams", "cursor-errors"],
141 (name) =>
142 waitUntilStable(
143 `SQL cursor object ${name}`,
144 requestWorker(HttpClientRequest.get(`${deployed.url}/state?name=${name}`)).pipe(
145 Effect.map((response) => response.status === 200),
146 Effect.catchTag("WorkerNotPropagated", () => Effect.succeed(false)),
147 ),
148 { timeout: "30 seconds" },
149 ),
150 { concurrency: 3 },
151 );
152 return deployed;
153 }),
154 { timeout: 120_000 },
155 );
156 afterAll(
157 Effect.gen(function* () {
158 const deployed = yield* Effect.exit(stack);
159 yield* destroy(Stack);
160 if (dev || Exit.isFailure(deployed)) return;
161 const deleted = yield* workers
162 .getScript({
163 accountId: deployed.value.accountId,
164 scriptName: deployed.value.workerName,
165 })
166 .pipe(
167 Effect.as(false),
168 Effect.catchTag(["WorkerNotFound", "WorkerHasNoVersions"], () =>
169 Effect.succeed(true),
170 ),
171 Effect.repeat({
172 while: (deleted) => !deleted,
173 schedule: Schedule.spaced("1 second"),
174 times: 8,
175 }),
176 Effect.timeout("15 seconds"),
177 Effect.provide(CloudflareApiLive()),
178 Effect.scoped,
179 );
180 expect(deleted).toBe(true);
181 }),
182 { timeout: 120_000 },
183 );
184
185 const json = <A>(method: "GET" | "POST", path: string) =>
186 Effect.gen(function* () {
187 const { url } = yield* stack;
188 // A fresh workers.dev route reaches edge nodes one at a time; retry
189 // only Cloudflare's not-yet-routed page, never application errors.
190 const response = yield* requestWorker(
191 method === "GET"
192 ? HttpClientRequest.get(`${url}${path}`)
193 : HttpClientRequest.post(`${url}${path}`),
194 );
195 if (response.status !== 200) {
196 return yield* Effect.fail(
197 new Error(`${method} ${url}${path}: ${response.status}: ${yield* response.text}`),
198 );
199 }
200 return (yield* response.json) as A;
201 });
202 const read = (name: string) => json<MigratedState>("GET", `/state?name=${name}`);
203
204 test(
205 "SQL cursor methods remain bound and report live SQLite metadata",
206 Effect.gen(function* () {
207 const result = yield* json<CursorMethods>("POST", "/cursor/methods?name=cursor-methods");
208 expect(result.beforeInsert).toBe(0);
209 expect(result.afterInsert).toBe(3);
210 expect(result.stableMembers).toBe(true);
211 expect(result.columns).toEqual(["value"]);
212 expect(result.sameColumnsAfterConsumption).toBe(true);
213 expect(result.first).toEqual({ done: false, value: { value: 1 } });
214 expect(result.second).toEqual({ done: false, value: { value: 2 } });
215 expect(result.last).toEqual({ value: 3 });
216 expect(result.exhausted.done).toBe(true);
217 expect(result.remaining).toEqual([]);
218 expect(result.rawRemaining).toEqual([]);
219 // SQLite may prefetch a row, but re-running the same statistic Effect
220 // must observe the later reads rather than its first result.
221 expect(result.reads[1]).toBeGreaterThanOrEqual(result.reads[0]!);
222 expect(result.reads[2]).toBeGreaterThan(result.reads[0]!);
223 expect(result.reads[2]).toBeGreaterThanOrEqual(3);
224 expect(result.writes).toBe(0);
225 }),
226 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
227 );
228
229 test(
230 "SQL cursor Stream, Channel, and raw views lazily consume the same SQLite cursor",
231 Effect.gen(function* () {
232 const result = yield* json<CursorStreams>("GET", "/cursor/streams?name=cursor-streams");
233 expect(result.first).toEqual({ done: false, value: { value: 0 } });
234 expect(result.values).toEqual([1000, 2000, 3000, 4000]);
235 expect(result.repeated).toEqual([]);
236 expect(result.sameChannel).toBe(true);
237 expect(result.channelFirst).toEqual({ done: false, value: { value: 1 } });
238 expect(result.channelValues).toEqual([{ value: 2 }, { value: 3 }]);
239 expect(result.channelRepeated).toEqual([]);
240 expect(result.rawFirst).toBe(1);
241 expect(result.rawValues).toEqual([[2, null, [0, 1, 2, 255]]]);
242 expect(result.rawRemaining).toEqual([]);
243 }),
244 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
245 );
246
247 test(
248 "SQL cursor one and exec preserve native SQLite defects",
249 Effect.gen(function* () {
250 const result = yield* json<CursorErrors>("GET", "/cursor/errors?name=cursor-errors");
251 expect(result.failures).toHaveLength(3);
252 for (const failure of result.failures) {
253 expect(failure.wrapped).toEqual(failure.native);
254 expect(failure.wrapped).toHaveLength(1);
255 expect(failure.wrapped[0]!.tag).toBe("Die");
256 expect(failure.wrapped[0]!.name).toBe("Error");
257 expect(failure.wrapped[0]!.message).toBeTruthy();
258 }
259 expect(result.recovered).toEqual({ value: 7 });
260 }),
261 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
262 );
263
264 test(
265 "activation applies modern migrations, relations, and a snapshot larger than 5 KB",
266 Effect.gen(function* () {
267 const state = yield* read("activation");
268 expect(state.tag).toBe("Cloudflare.SqlMigrations");
269 expect(state.table).toBe("__alchemy_migrations");
270 expect(state.boots).toBe(1);
271 expect(state.payloadLength).toBe(6144);
272 expect(state.users).toEqual(seedUsers);
273 expect(state.records.map((record) => record.name)).toEqual(migrationNames);
274 expect(state.history).toHaveLength(2);
275 expect(
276 state.history.map(({ name, hash, created_at }) => ({ name, hash, created_at })),
277 ).toEqual(state.records);
278 for (const row of state.history) {
279 expect(row.hash).toMatch(/^[a-f0-9]{64}$/);
280 expect(row.applied_at).not.toBeNull();
281 }
282 expect(state.tables.map((table) => table.name)).toContain("__alchemy_migrations");
283 expect(state.tables.map((table) => table.name)).not.toContain("__drizzle_migrations");
284 }),
285 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
286 );
287
288 test(
289 "fresh object names each migrate their own isolated database",
290 Effect.gen(function* () {
291 const first = yield* read("isolation-first");
292 const second = yield* read("isolation-second");
293 expect(first.users).toEqual(seedUsers);
294 expect(second.users).toEqual(seedUsers);
295 expect(first.history.map((row) => row.name)).toEqual(migrationNames);
296 expect(second.history.map((row) => row.name)).toEqual(migrationNames);
297 yield* json("POST", "/users?name=isolation-first&user=only-first");
298 expect((yield* read("isolation-first")).users).toEqual([
299 { name: "only-first", posts: [] },
300 ...seedUsers,
301 ]);
302 expect((yield* read("isolation-second")).users).toEqual(seedUsers);
303 }),
304 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
305 );
306
307 test(
308 "repeat application and real object reactivation preserve data and history",
309 Effect.gen(function* () {
310 const before = yield* read("reactivation");
311 yield* json("POST", "/users?name=reactivation&user=preserved");
312 const repeated = yield* json<MigratedState>("POST", "/repeat?name=reactivation");
313 expect(repeated.history).toEqual(before.history);
314 expect(repeated.users).toEqual([{ name: "preserved", posts: [] }, ...seedUsers]);
315 const reset = yield* json<{ reset: boolean }>("POST", "/reset?name=reactivation");
316 expect(reset.reset).toBe(true);
317 const reactivated = yield* read("reactivation");
318 expect(reactivated.boots).toBe(before.boots + 1);
319 expect(reactivated.history).toEqual(before.history);
320 expect(reactivated.users).toEqual(repeated.users);
321 expect(reactivated.payloadLength).toBe(6144);
322 }),
323 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
324 );
325
326 test(
327 "direct activation reads flat SQL files and uses only the custom history table",
328 Effect.gen(function* () {
329 const state = yield* json<CustomState>("GET", "/custom?name=custom");
330 expect(state.tag).toBe("Cloudflare.SqlMigrations");
331 expect(state.table).toBe("fixture_migrations");
332 expect(state.names).toEqual(["0001_values.sql", "0002_values.sql"]);
333 expect(state.history.map((row) => row.name)).toEqual(state.names);
334 expect(state.values).toEqual(["first", "second"]);
335 const tableNames = state.tables.map((table) => table.name);
336 expect(tableNames).toContain("fixture_migrations");
337 expect(tableNames).not.toContain("__alchemy_migrations");
338 expect(tableNames).not.toContain("__drizzle_migrations");
339 const repeated = yield* json<CustomState>("POST", "/custom/repeat?name=custom");
340 expect(repeated).toEqual(state);
341 }),
342 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
343 );
344
345 test(
346 "failed SQL rolls back DML and DDL without recording the failed migration",
347 Effect.gen(function* () {
348 const failed = yield* json<RollbackState>("POST", "/rollback?name=rollback");
349 expect(failed.error).toBe("MigrationError");
350 expect(failed.names).toEqual(["0001_stable.sql", "0002_broken.sql"]);
351 expect(failed.history.map((row) => row.name)).toEqual(["0001_stable.sql"]);
352 expect(failed.values).toEqual(["committed"]);
353 expect(failed.tables.map((table) => table.name)).not.toContain("must_roll_back");
354 // A second explicit attempt must not duplicate the committed predecessor.
355 const repeated = yield* json<RollbackState>("POST", "/rollback?name=rollback");
356 expect(repeated).toEqual(failed);
357 }),
358 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
359 );
360
361 test(
362 "adopts modern Drizzle history once, freezes its table, and applies only pending SQL",
363 Effect.gen(function* () {
364 const state = yield* json<AdoptionState>("POST", "/adopt?name=adoption");
365 expect(state.names).toEqual(migrationNames);
366 expect(state.before).toHaveLength(1);
367 expect(state.before[0]!.name).toBe(migrationNames[0]);
368 expect(state.before[0]!.hash).toBe("");
369 expect(state.before[0]!.applied_at).toMatch(
370 /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/,
371 );
372 expect(state.after).toEqual(state.before);
373 expect(state.adopted).toHaveLength(2);
374 expect(state.adopted[0]).toEqual(state.before[0]);
375 expect(state.adopted.map((row) => row.name)).toEqual(migrationNames);
376 expect(state.repeated).toEqual(state.adopted);
377 expect(state.users).toEqual(seedUsers);
378 }),
379 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
380 );
381
382 for (const empty of [false, true]) {
383 test(
384 `${empty ? "empty migration directory" : "unmatched Drizzle history"} rejects adoption without changing schema or history`,
385 Effect.gen(function* () {
386 const state = yield* json<{
387 error: string | null;
388 before: HistoryRow[];
389 after: HistoryRow[];
390 tables: { name: string }[];
391 users: string[];
392 }>("POST", `/conflict?name=conflict-${empty}${empty ? "&empty" : ""}`);
393 expect(state.error).toBe("MigrationHistoryConflictError");
394 expect(state.before).toHaveLength(1);
395 expect(state.before[0]!.name).toBe(migrationNames[0]);
396 expect(state.after).toEqual(state.before);
397 expect(state.users).toEqual(["seed"]);
398 expect(state.tables.map((table) => table.name)).not.toContain("__alchemy_migrations");
399 expect(state.tables.map((table) => table.name)).not.toContain("posts");
400 }),
401 { tags: [...(dev ? ["local"] : ["live"])], timeout: 90_000 },
402 );
403 }
404 },
405 );
406 }