The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | makeStatementExecutor, |
| 3 | MigrationError, |
| 4 | migrationError, |
| 5 | rowsFromCells, |
| 6 | withPgClient, |
| 7 | } from "@alchemy.run/sql/Migrations"; |
| 8 | import { describe, expect, it } from "@alchemy.run/test"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | |
| 11 | const tags = { tags: ["unit", "local"] }; |
| 12 | |
| 13 | /** A statement executor that records each request it would send. */ |
| 14 | const recording = (dialect: "postgres" | "mysql", batch: "transaction" | "atomic") => { |
| 15 | const sent: string[] = []; |
| 16 | const executor = makeStatementExecutor({ |
| 17 | dialect, |
| 18 | batch, |
| 19 | execute: (sql) => Effect.sync(() => (sent.push(sql), [{ ok: "1" }])), |
| 20 | }); |
| 21 | return { executor, sent }; |
| 22 | }; |
| 23 | |
| 24 | describe("makeStatementExecutor", () => { |
| 25 | it.effect( |
| 26 | "inlines query params for the dialect", |
| 27 | () => |
| 28 | Effect.gen(function* () { |
| 29 | const pg = recording("postgres", "atomic"); |
| 30 | expect(yield* pg.executor.query("SELECT $1, $2", ["it's", 3])).toEqual([{ ok: "1" }]); |
| 31 | const mysql = recording("mysql", "atomic"); |
| 32 | yield* mysql.executor.query("SELECT ?", ["x"]); |
| 33 | expect([...pg.sent, ...mysql.sent]).toEqual(["SELECT 'it''s', 3", "SELECT 'x'"]); |
| 34 | }), |
| 35 | tags, |
| 36 | ); |
| 37 | |
| 38 | it.effect( |
| 39 | "sends a batch as one request, in a transaction when asked", |
| 40 | () => |
| 41 | Effect.gen(function* () { |
| 42 | const pg = recording("postgres", "transaction"); |
| 43 | yield* pg.executor.batch(["CREATE TABLE a (id int)", "INSERT INTO a VALUES (1);"]); |
| 44 | const mysql = recording("mysql", "transaction"); |
| 45 | yield* mysql.executor.batch(["CREATE TABLE a (id int)"]); |
| 46 | const atomic = recording("postgres", "atomic"); |
| 47 | yield* atomic.executor.batch(["SELECT 1", "SELECT 2"]); |
| 48 | expect(pg.sent).toEqual([ |
| 49 | "BEGIN;\nCREATE TABLE a (id int);\nINSERT INTO a VALUES (1);\nCOMMIT;", |
| 50 | ]); |
| 51 | expect(mysql.sent).toEqual(["START TRANSACTION;\nCREATE TABLE a (id int);\nCOMMIT;"]); |
| 52 | expect(atomic.sent).toEqual(["SELECT 1;\nSELECT 2;"]); |
| 53 | }), |
| 54 | tags, |
| 55 | ); |
| 56 | }); |
| 57 | |
| 58 | describe("rowsFromCells", () => { |
| 59 | it.effect( |
| 60 | "keys cells by column and maps nullValue to null", |
| 61 | () => |
| 62 | Effect.sync(() => { |
| 63 | expect( |
| 64 | rowsFromCells( |
| 65 | [{ name: "name" }, { name: "hash" }], |
| 66 | [{ values: [{ value: "0001.sql" }, { nullValue: true }] }], |
| 67 | ), |
| 68 | ).toEqual([{ name: "0001.sql", hash: null }]); |
| 69 | expect(rowsFromCells(undefined, undefined)).toEqual([]); |
| 70 | }), |
| 71 | tags, |
| 72 | ); |
| 73 | }); |
| 74 | |
| 75 | describe("migrationError", () => { |
| 76 | it.effect( |
| 77 | "names the failing step and keeps the cause", |
| 78 | () => |
| 79 | Effect.sync(() => { |
| 80 | const tagged = { _tag: "Forbidden", message: "permission denied" }; |
| 81 | const error = migrationError("executeSql on app")(tagged); |
| 82 | expect(error._tag).toEqual("MigrationError"); |
| 83 | expect(error.message).toEqual("executeSql on app failed: permission denied"); |
| 84 | expect(error.cause).toBe(tagged); |
| 85 | expect(migrationError("x")({ _tag: "Timeout" }).message).toEqual("x failed: Timeout"); |
| 86 | }), |
| 87 | tags, |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | describe("withPgClient", () => { |
| 92 | // Real sockets and real retry spacing: the live clock. |
| 93 | it.live( |
| 94 | "retries only the connect while asked, then rewrites the final failure", |
| 95 | () => |
| 96 | Effect.gen(function* () { |
| 97 | let attempts = 0; |
| 98 | let used = false; |
| 99 | const error = yield* withPgClient( |
| 100 | // Nothing listens on port 1: the connect is refused at once. |
| 101 | { host: "127.0.0.1", port: 1, user: "u", database: "d", connectionTimeoutMillis: 2_000 }, |
| 102 | () => Effect.sync(() => void (used = true)), |
| 103 | { |
| 104 | retryConnect: { |
| 105 | while: (cause) => { |
| 106 | attempts++; |
| 107 | return (cause as { code?: unknown }).code === "ECONNREFUSED"; |
| 108 | }, |
| 109 | times: 2, |
| 110 | interval: "10 millis", |
| 111 | }, |
| 112 | onConnectFailure: (failure) => |
| 113 | new MigrationError({ message: `hinted: ${failure.message}`, cause: failure.cause }), |
| 114 | }, |
| 115 | ).pipe(Effect.flip); |
| 116 | // The first attempt plus `times: 2` retries. |
| 117 | expect(attempts).toEqual(3); |
| 118 | expect(used).toEqual(false); |
| 119 | expect(error.message).toContain("hinted: Connecting to postgres at 127.0.0.1 failed"); |
| 120 | }), |
| 121 | tags, |
| 122 | ); |
| 123 | }); |