@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 505 lines 21.5 KB
Raw
1 import type { DurableObjectState } from "@alchemy.run/cloudflare/Workers/DurableObjectState";
2 import { makeEffectVirtualEntry } from "@alchemy.run/cloudflare/Workers/Sources/Rolldown";
3 import {
4 SqlMigrations,
5 type SqlMigrationsInput,
6 } from "@alchemy.run/cloudflare/Workers/SqlMigrations";
7 import {
8 SqlMigrationsRuntime,
9 type SqlMigrationSnapshot,
10 } from "@alchemy.run/cloudflare/Workers/SqlMigrationsRuntime";
11 import { Worker } from "@alchemy.run/cloudflare/Workers/Worker";
12 import {
13 makeWorkerRuntimeContext,
14 type WorkerExport,
15 type WorkerRuntimeContext,
16 } from "@alchemy.run/cloudflare/Workers/WorkerRuntimeContext";
17 import {
18 DrizzleV0LayoutError,
19 MigrationError,
20 type MigrationHistoryConflictError,
21 } from "@alchemy.run/sql/Migrations/Format";
22 import { readMigrationRecords } from "@alchemy.run/sql/Migrations/Records";
23 import { expect, layer } from "@alchemy.run/test";
24 import * as NodeServices from "@effect/platform-node/NodeServices";
25 import { purePlugin } from "alchemy/Bundle/PurePlugin";
26 import type { RuntimeContext } from "alchemy/RuntimeContext";
27 import { sha256 } from "alchemy/Util/sha256";
28 import * as Cause from "effect/Cause";
29 import * as Context from "effect/Context";
30 import * as Effect from "effect/Effect";
31 import * as Exit from "effect/Exit";
32 import * as FileSystem from "effect/FileSystem";
33 import * as Path from "effect/Path";
34 import * as ChildProcess from "effect/process/ChildProcess";
35 import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
36 import * as Stream from "effect/Stream";
37 import { nodePath, nodeSupportsDevMode } from "../../nodeProbe.ts";
38
39 type Assert<T extends true> = T;
40 type Application = ReturnType<Effect.Success<ReturnType<typeof SqlMigrations>>["apply"]>;
41 type _RequiresRuntimeContext = Assert<
42 RuntimeContext extends Effect.Services<Application> ? true : false
43 >;
44 type _RequiresDurableObjectState = Assert<
45 DurableObjectState extends Effect.Services<Application> ? true : false
46 >;
47 type _NotConstructionOnly = Assert<
48 Application extends Effect.Effect<
49 void,
50 MigrationError | MigrationHistoryConflictError,
51 Effect.Services<ReturnType<typeof SqlMigrations>>
52 >
53 ? false
54 : true
55 >;
56 type _RuntimeRequiredEvenWithState = Assert<
57 Application extends Effect.Effect<
58 void,
59 MigrationError | MigrationHistoryConflictError,
60 DurableObjectState
61 >
62 ? false
63 : true
64 >;
65 type _SnapshotHasNoMethod = Assert<"apply" extends keyof SqlMigrationSnapshot ? false : true>;
66
67 // Worker resolves its host through the resource's Self key.
68 const WorkerHost = Context.Service<Worker, WorkerRuntimeContext>(Worker.Self.key);
69
70 const writeMigrations = Effect.fn(function* (files: Record<string, string>) {
71 const fs = yield* FileSystem.FileSystem;
72 const path = yield* Path.Path;
73 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-sql-migrations-unit-" });
74 for (const [name, contents] of Object.entries(files)) {
75 const file = path.join(dir, name);
76 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
77 yield* fs.writeFileString(file, contents);
78 }
79 return dir;
80 });
81
82 const capture = Effect.fn(function* (input: SqlMigrationsInput) {
83 const worker = makeWorkerRuntimeContext("sql-migrations-unit");
84 const snapshot = yield* SqlMigrations(input).pipe(Effect.provideService(WorkerHost, worker));
85 const { default: _default, ...exports } = yield* worker.exports;
86 const migrationExports: Record<string, WorkerExport> = exports;
87 return { worker, snapshot, exports: migrationExports };
88 });
89
90 const generatedEntry = (exports: Record<string, WorkerExport>) =>
91 makeEffectVirtualEntry(exports, { name: "sql-migrations-unit", stage: "test" })("./worker.ts");
92
93 layer(NodeServices.layer)("Cloudflare.SqlMigrations construction", (it) => {
94 it.effect(
95 "captures ordered flat files and their raw-content hashes",
96 () =>
97 Effect.gen(function* () {
98 const first = "CREATE TABLE users (id INTEGER PRIMARY KEY);\n";
99 const second = "CREATE TABLE posts (id INTEGER PRIMARY KEY);\n";
100 const dir = yield* writeMigrations({
101 "10_posts.sql": second,
102 "2_users.sql": first,
103 "ignored.txt": "not a migration",
104 });
105 const { worker, snapshot, exports } = yield* capture(dir);
106
107 expect(snapshot._tag).toBe("Cloudflare.SqlMigrations");
108 expect(snapshot.table).toBe("__alchemy_migrations");
109 expect(snapshot.records).toEqual(yield* readMigrationRecords(dir));
110 expect(snapshot.records.map((record) => record.name)).toEqual([
111 "2_users.sql",
112 "10_posts.sql",
113 ]);
114 expect(snapshot.records.map((record) => record.hash)).toEqual([
115 yield* sha256(first),
116 yield* sha256(second),
117 ]);
118 expect(snapshot.records.map((record) => record.sql)).toEqual([first, second]);
119 expect(snapshot.records.map((record) => record.createdAtMillis)).toEqual([
120 undefined,
121 undefined,
122 ]);
123 expect(typeof snapshot.apply).toBe("function");
124 expect(Effect.isEffect(snapshot.apply())).toBe(true);
125 expect(Object.values(exports)).toEqual([
126 {
127 kind: "sqlMigrations",
128 snapshot: { _tag: snapshot._tag, table: snapshot.table, records: snapshot.records },
129 },
130 ]);
131 for (const exported of Object.values(exports)) {
132 if (exported.kind === "sqlMigrations") {
133 expect(exported.snapshot).not.toHaveProperty("apply");
134 expect(exported.snapshot).not.toBe(snapshot);
135 }
136 }
137 expect(worker.env).toEqual({});
138 }).pipe(Effect.scoped),
139 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
140 );
141
142 it.effect(
143 "captures modern directories with timestamps and a custom table",
144 () =>
145 Effect.gen(function* () {
146 const first =
147 "CREATE TABLE users (id INTEGER PRIMARY KEY);\n--> statement-breakpoint\nINSERT INTO users VALUES (1);\n";
148 const second = "CREATE TABLE posts (id INTEGER PRIMARY KEY);\n";
149 const dir = yield* writeMigrations({
150 "20240102000000_posts/migration.sql": second,
151 "20240101000000_users/migration.sql": first,
152 "20240101000000_users/snapshot.json": "{}",
153 });
154 const { snapshot, exports, worker } = yield* capture({ dir, table: "application_history" });
155
156 expect(snapshot.table).toBe("application_history");
157 expect(snapshot.records).toEqual(yield* readMigrationRecords(dir));
158 expect(snapshot.records.map((record) => record.name)).toEqual([
159 "20240101000000_users",
160 "20240102000000_posts",
161 ]);
162 expect(snapshot.records.map((record) => record.createdAtMillis)).toEqual([
163 1704067200000, 1704153600000,
164 ]);
165 expect(snapshot.records.map((record) => record.hash)).toEqual([
166 yield* sha256(first),
167 yield* sha256(second),
168 ]);
169 expect(snapshot.records[0]!.statements).toEqual([
170 "CREATE TABLE users (id INTEGER PRIMARY KEY);",
171 "INSERT INTO users VALUES (1);",
172 ]);
173 expect(Object.values(exports)).toEqual([
174 {
175 kind: "sqlMigrations",
176 snapshot: { _tag: snapshot._tag, table: snapshot.table, records: snapshot.records },
177 },
178 ]);
179 expect(worker.env).toEqual({});
180 }).pipe(Effect.scoped),
181 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
182 );
183
184 it.effect(
185 "reconstructs apply from the plain runtime snapshot without reading the directory",
186 () =>
187 Effect.gen(function* () {
188 const fs = yield* FileSystem.FileSystem;
189 const path = yield* Path.Path;
190 const root = yield* writeMigrations({
191 "migrations/0001_users.sql": "CREATE TABLE users (id INTEGER PRIMARY KEY);",
192 });
193 const dir = path.join(root, "migrations");
194 const input = { dir, table: "runtime_history" };
195 const captured = yield* capture(input);
196 const bundles: Record<string, SqlMigrationSnapshot> = {};
197 for (const [key, exported] of Object.entries(captured.exports)) {
198 if (exported.kind === "sqlMigrations") {
199 expect(exported.snapshot).not.toHaveProperty("apply");
200 bundles[key] = exported.snapshot;
201 }
202 }
203 expect(Object.keys(bundles)).toHaveLength(1);
204 yield* fs.remove(dir, { recursive: true });
205
206 const reconstructed = yield* Effect.acquireUseRelease(
207 Effect.sync(() => {
208 const previous = globalThis.__ALCHEMY_RUNTIME__;
209 globalThis.__ALCHEMY_RUNTIME__ = true;
210 return previous;
211 }),
212 () =>
213 SqlMigrations(input).pipe(
214 Effect.provideService(SqlMigrationsRuntime, bundles),
215 Effect.provideService(WorkerHost, captured.worker),
216 ),
217 (previous) =>
218 Effect.sync(() => {
219 globalThis.__ALCHEMY_RUNTIME__ = previous;
220 }),
221 );
222
223 expect(reconstructed._tag).toBe(captured.snapshot._tag);
224 expect(reconstructed.table).toBe(captured.snapshot.table);
225 expect(reconstructed.records).toEqual(captured.snapshot.records);
226 expect(typeof reconstructed.apply).toBe("function");
227 expect(Effect.isEffect(reconstructed.apply())).toBe(true);
228 expect(reconstructed.apply).not.toBe(captured.snapshot.apply);
229 for (const snapshot of Object.values(bundles)) {
230 expect(snapshot).not.toHaveProperty("apply");
231 }
232 }).pipe(Effect.scoped),
233 {
234 tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"],
235 exclusive: true,
236 },
237 );
238
239 it.effect(
240 "fails construction for a missing directory before registering exports",
241 () =>
242 Effect.gen(function* () {
243 const path = yield* Path.Path;
244 const root = yield* writeMigrations({});
245 const dir = path.join(root, "missing");
246 const worker = makeWorkerRuntimeContext("missing-sql-migrations");
247 const exit = yield* SqlMigrations(dir).pipe(
248 Effect.provideService(WorkerHost, worker),
249 Effect.exit,
250 );
251
252 expect(Exit.isFailure(exit)).toBe(true);
253 if (Exit.isFailure(exit)) {
254 const error = Cause.squash(exit.cause);
255 expect(error).toBeInstanceOf(MigrationError);
256 expect(error).toMatchObject({ message: expect.stringContaining(dir) });
257 }
258 expect(Object.keys(yield* worker.exports)).toEqual(["default"]);
259 expect(worker.env).toEqual({});
260 }).pipe(Effect.scoped),
261 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
262 );
263
264 it.effect(
265 "rejects legacy journals even when modern migration files exist",
266 () =>
267 Effect.gen(function* () {
268 const dir = yield* writeMigrations({
269 "meta/_journal.json": '{"version":"7","entries":[]}',
270 "20240101000000_users/migration.sql": "CREATE TABLE users (id INTEGER);",
271 });
272 const worker = makeWorkerRuntimeContext("legacy-sql-migrations");
273 const exit = yield* SqlMigrations(dir).pipe(
274 Effect.provideService(WorkerHost, worker),
275 Effect.exit,
276 );
277
278 expect(Exit.isFailure(exit)).toBe(true);
279 if (Exit.isFailure(exit)) {
280 const error = Cause.squash(exit.cause);
281 expect(error).toBeInstanceOf(DrizzleV0LayoutError);
282 expect(error).toMatchObject({ dir, message: expect.stringContaining("drizzle-kit up") });
283 }
284 expect(Object.keys(yield* worker.exports)).toEqual(["default"]);
285 }).pipe(Effect.scoped),
286 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
287 );
288
289 it.effect(
290 "embeds SQL larger than 5 KB without file imports or environment bindings",
291 () =>
292 Effect.gen(function* () {
293 const sql = `CREATE TABLE messages (body TEXT);\n--> statement-breakpoint\nINSERT INTO messages VALUES ('${"large-payload-".repeat(600)}');\n`;
294 expect(sql.length).toBeGreaterThan(5 * 1024);
295 const dir = yield* writeMigrations({ "0001_messages.sql": sql });
296 const { snapshot, exports, worker } = yield* capture({ dir, table: "large_history" });
297 const entry = generatedEntry(exports);
298 const snapshots = Object.fromEntries(
299 Object.entries(exports).map(([key]) => [key, snapshot]),
300 );
301
302 expect(snapshot.records[0]!.sql).toBe(sql);
303 expect(entry).toContain(`withSqlMigrations(entrypoint, ${JSON.stringify(snapshots)})`);
304 expect(entry).toContain(JSON.stringify(sql));
305 expect(entry).toContain(snapshot.records[0]!.hash);
306 expect(entry).toContain('"table":"large_history"');
307 expect(entry).not.toMatch(
308 /\b(?:from\s*|import\s*(?:\(\s*)?)(["'])[^"']*\.sql(?:\?[^"']*)?\1/,
309 );
310 expect(entry).not.toContain("migrations.js");
311 expect(entry).not.toMatch(/\benv\s*(?:\.|\[)/);
312 expect(entry).not.toContain("process.env");
313 expect(entry).not.toContain("Records.ts");
314 expect(worker.env).toEqual({});
315 }).pipe(Effect.scoped),
316 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
317 );
318
319 it.effect(
320 "changes the generated entry when SQL changes under the same capture key",
321 () =>
322 Effect.gen(function* () {
323 const fs = yield* FileSystem.FileSystem;
324 const path = yield* Path.Path;
325 const firstSql = "CREATE TABLE first_version (id INTEGER);\n";
326 const secondSql = "CREATE TABLE second_version (id INTEGER);\n";
327 const dir = yield* writeMigrations({ "0001_schema.sql": firstSql });
328 const input = { dir, table: "stable_history" };
329 const first = yield* capture(input);
330 const firstEntry = generatedEntry(first.exports);
331 yield* fs.writeFileString(path.join(dir, "0001_schema.sql"), secondSql);
332 const second = yield* capture(input);
333 const secondEntry = generatedEntry(second.exports);
334
335 expect(Object.keys(second.exports)).toEqual(Object.keys(first.exports));
336 expect(second.snapshot.records[0]!.name).toBe(first.snapshot.records[0]!.name);
337 expect(second.snapshot.records[0]!.hash).toBe(yield* sha256(secondSql));
338 expect(second.snapshot.records[0]!.hash).not.toBe(first.snapshot.records[0]!.hash);
339 expect(secondEntry).not.toBe(firstEntry);
340 expect(firstEntry).toContain(JSON.stringify(firstSql));
341 expect(secondEntry).toContain(JSON.stringify(secondSql));
342 expect(secondEntry).not.toContain(JSON.stringify(firstSql));
343 expect(first.snapshot.records[0]!.sql).toBe(firstSql);
344 }).pipe(Effect.scoped),
345 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
346 );
347
348 it.effect(
349 "keeps deploy-time readers out of the emitted Durable Object migrator graph",
350 () =>
351 Effect.gen(function* () {
352 const path = yield* Path.Path;
353 const input = yield* path.fromFileUrl(
354 new URL("../../../../cloudflare/src/Workers/SqlMigrationsApply.ts", import.meta.url),
355 );
356 const { rolldown } = yield* Effect.promise(() => import("rolldown"));
357 const graph = new Set<string>();
358 const output = yield* Effect.acquireUseRelease(
359 Effect.promise(() =>
360 rolldown({
361 input,
362 platform: "neutral",
363 external: [/^effect\//, /^node:/],
364 plugins: [
365 {
366 name: "sql-migrations-runtime-boundary",
367 moduleParsed(info) {
368 graph.add(info.id);
369 for (const id of [...info.importedIds, ...info.dynamicallyImportedIds]) {
370 graph.add(id);
371 }
372 },
373 },
374 ],
375 }),
376 ),
377 (bundle) => Effect.promise(() => bundle.generate({ format: "esm" })),
378 (bundle) => Effect.promise(() => bundle.close()),
379 );
380 const chunks = output.output.filter((item) => item.type === "chunk");
381 const modules = [...graph, ...chunks.flatMap((chunk) => Object.keys(chunk.modules))];
382 const code = chunks.map((chunk) => chunk.code).join("\n");
383 const imports = chunks.flatMap((chunk) => [...chunk.imports, ...chunk.dynamicImports]);
384
385 expect(chunks.flatMap((chunk) => chunk.exports)).toContain("applySqlMigrations");
386 expect(modules.some((id) => id.endsWith("/sql/src/Migrations/AlchemyFormat.ts"))).toBe(
387 true,
388 );
389 expect(modules.some((id) => id.endsWith("/sql/src/Migrations/Convert.ts"))).toBe(true);
390 expect(modules.some((id) => id.endsWith("/sql/src/Migrations/Utils.ts"))).toBe(true);
391 const forbidden =
392 /(?:sql(?:\/src)?\/(?:Migrations\/(?:Records|Registry|Detect)|SqlFile)(?:\.ts)?$|node:(?:crypto|fs)|effect\/(?:FileSystem|Path))/;
393 expect(modules.filter((id) => forbidden.test(id))).toEqual([]);
394 expect(imports.filter((id) => forbidden.test(id))).toEqual([]);
395 expect(code).not.toContain("node:crypto");
396 expect(code).not.toContain("readMigrationRecords");
397 }),
398 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
399 );
400
401 it.effect(
402 "emits the SqlMigrations factory and apply method without filesystem imports",
403 () =>
404 Effect.gen(function* () {
405 const path = yield* Path.Path;
406 const input = yield* path.fromFileUrl(
407 new URL("../../../../cloudflare/src/Workers/SqlMigrations.ts", import.meta.url),
408 );
409 const { rolldown } = yield* Effect.promise(() => import("rolldown"));
410 const output = yield* Effect.acquireUseRelease(
411 Effect.promise(() =>
412 rolldown({
413 input,
414 platform: "neutral",
415 external: (id) => !id.startsWith(".") && !path.isAbsolute(id),
416 transform: { define: { "globalThis.__ALCHEMY_RUNTIME__": "true" } },
417 plugins: [purePlugin()],
418 }),
419 ),
420 (bundle) => Effect.promise(() => bundle.generate({ format: "esm", minify: "dce-only" })),
421 (bundle) => Effect.promise(() => bundle.close()),
422 );
423 const chunks = output.output.filter((item) => item.type === "chunk");
424 const modules = chunks.flatMap((chunk) =>
425 Object.entries(chunk.modules)
426 .filter(([, module]) => module.renderedLength > 0)
427 .map(([id]) => id),
428 );
429 const imports = chunks.flatMap((chunk) => [...chunk.imports, ...chunk.dynamicImports]);
430 const code = chunks.map((chunk) => chunk.code).join("\n");
431
432 expect(chunks.flatMap((chunk) => chunk.exports)).toContain("SqlMigrations");
433 expect(modules.some((id) => id.endsWith("/Workers/SqlMigrations.ts"))).toBe(true);
434 expect(modules.some((id) => id.endsWith("/Workers/SqlMigrationsApply.ts"))).toBe(true);
435 const forbidden =
436 /(?:sql(?:\/src)?\/(?:Migrations\/(?:Records|Registry|Detect)|SqlFile)(?:\.ts)?$|node:(?:crypto|fs)|effect\/(?:FileSystem|Path)|@effect\/platform-(?:node|bun))/;
437 expect(modules.filter((id) => forbidden.test(id))).toEqual([]);
438 expect(imports.filter((id) => forbidden.test(id))).toEqual([]);
439 expect(code).not.toContain("readMigrationRecords");
440 expect(code).not.toContain("node:fs");
441 expect(code).not.toContain("node:crypto");
442 }),
443 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
444 );
445
446 it.effect.skipIf(!nodeSupportsDevMode)(
447 "imports a SqlMigrations Durable Object through Node's Oxc loader and captures SQL",
448 () =>
449 Effect.gen(function* () {
450 const path = yield* Path.Path;
451 const spawner = yield* ChildProcessSpawner;
452 const sql = "CREATE TABLE node_loader_probe (id INTEGER PRIMARY KEY);\n";
453 const root = yield* writeMigrations({ "migrations/0001_probe.sql": sql });
454 const loader = yield* path.fromFileUrl(
455 new URL("../../../../alchemy/bin/register-dev-mode.js", import.meta.url),
456 );
457 const fixture = yield* path.fromFileUrl(
458 new URL("./fixtures/sql-migrations-unit/node-import.ts", import.meta.url),
459 );
460 const handle = yield* spawner.spawn(
461 ChildProcess.make(nodePath!, ["--import", loader, fixture], {
462 cwd: root,
463 env: { NO_COLOR: "1", npm_execpath: "", npm_config_user_agent: "" },
464 extendEnv: true,
465 stdin: "ignore",
466 stdout: "pipe",
467 stderr: "pipe",
468 killSignal: "SIGKILL",
469 }),
470 );
471 const [stdout, stderr, exitCode] = yield* Effect.all(
472 [
473 handle.stdout.pipe(
474 Stream.decodeText,
475 Stream.runCollect,
476 Effect.map((chunks) => chunks.join("")),
477 ),
478 handle.stderr.pipe(
479 Stream.decodeText,
480 Stream.runCollect,
481 Effect.map((chunks) => chunks.join("")),
482 ),
483 handle.exitCode,
484 ],
485 { concurrency: 3 },
486 );
487
488 expect(stderr).toBe("");
489 expect(exitCode).toBe(0);
490 expect(stdout).toContain('"runtime":"node"');
491 expect(stdout).toContain('"object":"SqlMigrationsUnitObject"');
492 expect(stdout).toContain('"table":"node_loader_history"');
493 expect(stdout).toContain(JSON.stringify(sql));
494 expect(stdout).toContain(yield* sha256(sql));
495 expect(stdout).toContain('"capturedHasApply":true');
496 expect(stdout).toContain('"exportedHasApply":false');
497 expect(stdout).toContain('"exports":1');
498 expect(stdout).toContain('"env":{}');
499 }).pipe(Effect.scoped),
500 {
501 tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"],
502 timeout: 60_000,
503 },
504 );
505 });