|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import Database from "better-sqlite3"; |
| 3 | +import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +/** |
| 7 | + * These tests exercise the database layer against a real SQLite instance |
| 8 | + * using a temporary directory, bypassing the singleton to avoid polluting |
| 9 | + * the development database. |
| 10 | + */ |
| 11 | + |
| 12 | +const TEST_DIR = path.join(process.cwd(), "storage", "__test__"); |
| 13 | +const TEST_DB_PATH = path.join(TEST_DIR, "test.sqlite"); |
| 14 | +const MIGRATION_PATH = path.join(process.cwd(), "db", "0001_init.sql"); |
| 15 | + |
| 16 | +function freshDb() { |
| 17 | + if (!existsSync(TEST_DIR)) mkdirSync(TEST_DIR, { recursive: true }); |
| 18 | + if (existsSync(TEST_DB_PATH)) rmSync(TEST_DB_PATH); |
| 19 | + |
| 20 | + const db = new Database(TEST_DB_PATH); |
| 21 | + db.exec("PRAGMA journal_mode=WAL;"); |
| 22 | + db.exec("PRAGMA foreign_keys=ON;"); |
| 23 | + return db; |
| 24 | +} |
| 25 | + |
| 26 | +describe("database initialization", () => { |
| 27 | + let db: Database.Database; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + db = freshDb(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + db.close(); |
| 35 | + if (existsSync(TEST_DB_PATH)) rmSync(TEST_DB_PATH); |
| 36 | + }); |
| 37 | + |
| 38 | + it("enables WAL journal mode", () => { |
| 39 | + const row = db.prepare("PRAGMA journal_mode;").get() as { journal_mode: string }; |
| 40 | + expect(row.journal_mode).toBe("wal"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("enables foreign keys", () => { |
| 44 | + const row = db.prepare("PRAGMA foreign_keys;").get() as { foreign_keys: number }; |
| 45 | + expect(row.foreign_keys).toBe(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it("applies the init migration without errors", () => { |
| 49 | + const migrationSql = readFileSync(MIGRATION_PATH, "utf8"); |
| 50 | + expect(() => db.exec(migrationSql)).not.toThrow(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("creates expected core tables after migration", () => { |
| 54 | + const migrationSql = readFileSync(MIGRATION_PATH, "utf8"); |
| 55 | + db.exec(migrationSql); |
| 56 | + |
| 57 | + const tables = db |
| 58 | + .prepare("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;") |
| 59 | + .all() as { name: string }[]; |
| 60 | + |
| 61 | + const tableNames = tables.map((t) => t.name); |
| 62 | + |
| 63 | + expect(tableNames).toContain("sessions"); |
| 64 | + expect(tableNames).toContain("traces"); |
| 65 | + expect(tableNames).toContain("events"); |
| 66 | + expect(tableNames).toContain("tool_calls"); |
| 67 | + expect(tableNames).toContain("workflow_runs"); |
| 68 | + expect(tableNames).toContain("workflow_steps"); |
| 69 | + expect(tableNames).toContain("artifacts"); |
| 70 | + expect(tableNames).toContain("capabilities"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("migration is idempotent — running twice does not error", () => { |
| 74 | + const migrationSql = readFileSync(MIGRATION_PATH, "utf8"); |
| 75 | + db.exec(migrationSql); |
| 76 | + expect(() => db.exec(migrationSql)).not.toThrow(); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("transaction support", () => { |
| 81 | + let db: Database.Database; |
| 82 | + |
| 83 | + beforeEach(() => { |
| 84 | + db = freshDb(); |
| 85 | + const migrationSql = readFileSync(MIGRATION_PATH, "utf8"); |
| 86 | + db.exec(migrationSql); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + db.close(); |
| 91 | + if (existsSync(TEST_DB_PATH)) rmSync(TEST_DB_PATH); |
| 92 | + }); |
| 93 | + |
| 94 | + it("commits all writes on success", () => { |
| 95 | + const run = db.transaction(() => { |
| 96 | + db.prepare( |
| 97 | + "INSERT INTO capabilities (id, kind, name, metadata_json) VALUES (?, ?, ?, ?)", |
| 98 | + ).run("test:a", "skill", "a", "{}"); |
| 99 | + db.prepare( |
| 100 | + "INSERT INTO capabilities (id, kind, name, metadata_json) VALUES (?, ?, ?, ?)", |
| 101 | + ).run("test:b", "skill", "b", "{}"); |
| 102 | + }); |
| 103 | + |
| 104 | + run(); |
| 105 | + |
| 106 | + const rows = db.prepare("SELECT id FROM capabilities WHERE id LIKE 'test:%'").all() as { id: string }[]; |
| 107 | + expect(rows).toHaveLength(2); |
| 108 | + }); |
| 109 | + |
| 110 | + it("rolls back all writes on failure", () => { |
| 111 | + const run = db.transaction(() => { |
| 112 | + db.prepare( |
| 113 | + "INSERT INTO capabilities (id, kind, name, metadata_json) VALUES (?, ?, ?, ?)", |
| 114 | + ).run("test:c", "skill", "c", "{}"); |
| 115 | + throw new Error("deliberate failure"); |
| 116 | + }); |
| 117 | + |
| 118 | + expect(() => run()).toThrow("deliberate failure"); |
| 119 | + |
| 120 | + const rows = db.prepare("SELECT id FROM capabilities WHERE id = 'test:c'").all(); |
| 121 | + expect(rows).toHaveLength(0); |
| 122 | + }); |
| 123 | +}); |
0 commit comments