|
| 1 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { assertFts5Available, openGraphDatabase } from "../db/database.js"; |
| 6 | +import type { SqliteDatabase } from "../db/sqlite.js"; |
| 7 | + |
| 8 | +const roots: string[] = []; |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); |
| 12 | + vi.doUnmock("../db/sqlite.js"); |
| 13 | +}); |
| 14 | + |
| 15 | +/** Minimal SqliteDatabase fake: only `exec` is exercised by assertFts5Available. */ |
| 16 | +function fakeDb(execImpl: (sql: string) => void): SqliteDatabase { |
| 17 | + return { |
| 18 | + prepare: () => { |
| 19 | + throw new Error("not used by this test"); |
| 20 | + }, |
| 21 | + exec: execImpl, |
| 22 | + pragma: () => {}, |
| 23 | + transaction: (fn) => fn(), |
| 24 | + close: () => {}, |
| 25 | + open: true, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Load a fresh `database.js` whose `assertFts5Available` probes against |
| 31 | + * `fakeExec` instead of a real `:memory:` connection, by mocking the |
| 32 | + * `openSqlite` it imports from `sqlite.js`. `assertFts5Available` no longer |
| 33 | + * takes a `SqliteDatabase` parameter (PR #168 review: it must not touch the |
| 34 | + * caller's real graph database) — it opens its own throwaway connection |
| 35 | + * internally, so exercising the error paths now goes through this module |
| 36 | + * mock rather than an injected fake. |
| 37 | + */ |
| 38 | +async function assertFts5AvailableWith(fakeExec: (sql: string) => void) { |
| 39 | + vi.resetModules(); |
| 40 | + vi.doMock("../db/sqlite.js", () => ({ |
| 41 | + openSqlite: () => fakeDb(fakeExec), |
| 42 | + })); |
| 43 | + const fresh = await import("../db/database.js"); |
| 44 | + return fresh.assertFts5Available; |
| 45 | +} |
| 46 | + |
| 47 | +describe("assertFts5Available", () => { |
| 48 | + it("does not throw when FTS5 statements succeed", () => { |
| 49 | + // node:sqlite is compiled with FTS5 in every environment these tests run |
| 50 | + // in (see sqlite.ts's module comment), so this exercises the real probe |
| 51 | + // against a real throwaway :memory: connection end to end. |
| 52 | + expect(() => assertFts5Available()).not.toThrow(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("raises an actionable, Node-version-specific message on the exact SQLite error from issue #110", async () => { |
| 56 | + const probe = await assertFts5AvailableWith(() => { |
| 57 | + throw new Error("no such module: fts5"); |
| 58 | + }); |
| 59 | + |
| 60 | + expect(() => probe()).toThrowError( |
| 61 | + new RegExp(`Node \\(${process.version.replace(/[.+]/g, "\\$&")}\\).*FTS5.*no such module: fts5`, "s"), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it("re-throws an unrelated exec failure unchanged, rather than misattributing it to FTS5", async () => { |
| 66 | + const probe = await assertFts5AvailableWith(() => { |
| 67 | + throw new Error("database is locked"); |
| 68 | + }); |
| 69 | + |
| 70 | + expect(() => probe()).toThrowError("database is locked"); |
| 71 | + expect(() => probe()).not.toThrow(/FTS5/); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("openGraphDatabase FTS5 preflight", () => { |
| 76 | + it("still opens normally on a machine whose SQLite build has FTS5 (the common case)", () => { |
| 77 | + const root = mkdtempSync(join(tmpdir(), "mex-database-fts5-")); |
| 78 | + roots.push(root); |
| 79 | + |
| 80 | + const db = openGraphDatabase(join(root, "graph.db")); |
| 81 | + try { |
| 82 | + // exercised implicitly by openGraphDatabase not throwing; assert the FTS5 |
| 83 | + // tables schema.sql defines actually exist, confirming the preflight probe |
| 84 | + // did not somehow prevent or corrupt the real schema application |
| 85 | + const tables = db |
| 86 | + .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%_fts%'") |
| 87 | + .all() as Array<{ name: string }>; |
| 88 | + expect(tables.length).toBeGreaterThan(0); |
| 89 | + } finally { |
| 90 | + db.close(); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it("closes the database handle when the FTS5 preflight fails, instead of leaking it open", async () => { |
| 95 | + const root = mkdtempSync(join(tmpdir(), "mex-database-fts5-close-")); |
| 96 | + roots.push(root); |
| 97 | + |
| 98 | + vi.resetModules(); |
| 99 | + vi.doMock("../db/sqlite.js", async () => { |
| 100 | + const actual = await vi.importActual<typeof import("../db/sqlite.js")>("../db/sqlite.js"); |
| 101 | + let realGraphDb: SqliteDatabase | undefined; |
| 102 | + return { |
| 103 | + ...actual, |
| 104 | + openSqlite: (path: string, options?: { readOnly?: boolean; immutable?: boolean }) => { |
| 105 | + if (path === ":memory:") { |
| 106 | + // The FTS5 preflight's own throwaway connection: fail it. |
| 107 | + return { |
| 108 | + prepare: () => { |
| 109 | + throw new Error("not used by this test"); |
| 110 | + }, |
| 111 | + exec: () => { |
| 112 | + throw new Error("no such module: fts5"); |
| 113 | + }, |
| 114 | + pragma: () => {}, |
| 115 | + transaction: <T>(fn: () => T) => fn(), |
| 116 | + close: () => {}, |
| 117 | + open: true, |
| 118 | + } satisfies SqliteDatabase; |
| 119 | + } |
| 120 | + realGraphDb = actual.openSqlite(path, options); |
| 121 | + return realGraphDb; |
| 122 | + }, |
| 123 | + __getRealGraphDb: () => realGraphDb, |
| 124 | + }; |
| 125 | + }); |
| 126 | + |
| 127 | + const fresh = await import("../db/database.js"); |
| 128 | + const sqliteMock = (await import("../db/sqlite.js")) as unknown as { |
| 129 | + __getRealGraphDb: () => SqliteDatabase | undefined; |
| 130 | + }; |
| 131 | + |
| 132 | + expect(() => fresh.openGraphDatabase(join(root, "graph.db"))).toThrow(/FTS5/); |
| 133 | + expect(sqliteMock.__getRealGraphDb()?.open).toBe(false); |
| 134 | + }); |
| 135 | +}); |
0 commit comments