import { expect, test } from "bun:test"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { check, migrate, migrations, openDb } from "./db.ts"; test("migrations apply once, in order, and the file checks out", () => { const dir = mkdtempSync(join(tmpdir(), "app-db-")); const db = openDb(dir); expect(check(db).version).toBe(migrations.length); // Opening again finds nothing to do — a migration that has run is history. expect(migrate(db)).toBe(migrations.length); expect(() => openDb(dir)).not.toThrow(); expect(check(db)).toEqual({ version: migrations.length, integrity: "ok" }); expect( db.query<{ journal_mode: string }, []>("PRAGMA journal_mode").get() ?.journal_mode, ).toBe("wal"); expect( db.query<{ foreign_keys: number }, []>("PRAGMA foreign_keys").get() ?.foreign_keys, ).toBe(1); });