__tests__ / db.test.ts
import { describe, it, expect, afterEach } from "vitest";
import { mkdtempSync, rmSync, existsSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { getDb, closeAllConnections } from "../src/db";
describe("getDb", () => {
let tempDir: string;
afterEach(() => {
closeAllConnections();
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
}
});
it("creates the database file and its parent directory if missing", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "nested", "brain.db");
getDb(dbPath);
expect(existsSync(dbPath)).toBe(true);
});
it("creates the entities, observations, relations tables and the FTS index", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const db = getDb(dbPath);
const tableNames = db
.prepare("SELECT name FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY name")
.all()
.map((row: any) => row.name);
expect(tableNames).toContain("entities");
expect(tableNames).toContain("observations");
expect(tableNames).toContain("relations");
expect(tableNames).toContain("observations_fts");
});
it("enables foreign key enforcement", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const db = getDb(dbPath);
const result = db.prepare("PRAGMA foreign_keys").get() as { foreign_keys: number };
expect(result.foreign_keys).toBe(1);
});
it("returns the same connection instance for the same path", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const first = getDb(dbPath);
const second = getDb(dbPath);
expect(first).toBe(second);
});
});
__tests__ / db.test.ts
import { describe, it, expect, afterEach } from "vitest";
import { mkdtempSync, rmSync, existsSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { getDb, closeAllConnections } from "../src/db";
describe("getDb", () => {
let tempDir: string;
afterEach(() => {
closeAllConnections();
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
}
});
it("creates the database file and its parent directory if missing", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "nested", "brain.db");
getDb(dbPath);
expect(existsSync(dbPath)).toBe(true);
});
it("creates the entities, observations, relations tables and the FTS index", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const db = getDb(dbPath);
const tableNames = db
.prepare("SELECT name FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY name")
.all()
.map((row: any) => row.name);
expect(tableNames).toContain("entities");
expect(tableNames).toContain("observations");
expect(tableNames).toContain("relations");
expect(tableNames).toContain("observations_fts");
});
it("enables foreign key enforcement", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const db = getDb(dbPath);
const result = db.prepare("PRAGMA foreign_keys").get() as { foreign_keys: number };
expect(result.foreign_keys).toBe(1);
});
it("returns the same connection instance for the same path", () => {
tempDir = mkdtempSync(join(tmpdir(), "brain-db-"));
const dbPath = join(tempDir, "brain.db");
const first = getDb(dbPath);
const second = getDb(dbPath);
expect(first).toBe(second);
});
});