/** * create-db pg-mode test — covers the `pg` Pool branch (lines 29-53). * * Mocks the `pg` module so we don't need a real Postgres. Verifies that * createDb(mode='pg') returns a client that delegates to Pool.connect(). */ import { describe, it, expect, vi, beforeEach } from "vitest"; describe("createDb (pg mode)", () => { beforeEach(() => { vi.resetModules(); }); it("throws if DATABASE_URL is missing in pg mode", async () => { const { createDb } = await import("../src/create-db.js"); delete process.env.DATABASE_URL; await expect(createDb({ mode: "pg" })).rejects.toThrow("DATABASE_URL"); }); it("returns a client backed by pg.Pool", async () => { const fakeQuery = vi.fn().mockResolvedValue({ rows: [{ x: 1 }], rowCount: 1 }); const fakeRelease = vi.fn(); const fakeConnect = vi.fn().mockResolvedValue({ query: fakeQuery, release: fakeRelease }); const fakePool = { connect: fakeConnect }; vi.doMock("pg", () => ({ Pool: vi.fn(() => fakePool) })); const { createDb } = await import("../src/create-db.js"); const db = await createDb({ mode: "pg", databaseUrl: "postgres://localhost/test" }); const res = await db.query("SELECT $1::int AS x", [1]); expect(res.rows).toHaveLength(1); expect(res.rows[0]).toEqual({ x: 1 }); expect(fakeQuery).toHaveBeenCalledWith("SELECT $1::int AS x", [1]); expect(fakeRelease).toHaveBeenCalled(); }); it("exec delegates to Pool.connect().query with multi-statement", async () => { const fakeQuery = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }); const fakeRelease = vi.fn(); const fakeConnect = vi.fn().mockResolvedValue({ query: fakeQuery, release: fakeRelease }); const fakePool = { connect: fakeConnect }; vi.doMock("pg", () => ({ Pool: vi.fn(() => fakePool) })); const { createDb } = await import("../src/create-db.js"); const db = await createDb({ mode: "pg", databaseUrl: "postgres://localhost/test" }); await db.exec("CREATE TABLE x (a int); CREATE TABLE y (b int);"); expect(fakeQuery).toHaveBeenCalledWith("CREATE TABLE x (a int); CREATE TABLE y (b int);"); }); });