import { expect, test } from "bun:test"; import { Database } from "bun:sqlite"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; // Seed a data directory with the note table an earlier version of this app // left behind: no item_id, so it does not fit the current schema. const dir = mkdtempSync(join(tmpdir(), "app-")); process.env.FORGE_APP_DATA = dir; const seed = new Database(join(dir, "app.db")); seed.exec( "CREATE TABLE note (id INTEGER PRIMARY KEY, body TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now')))", ); seed.exec("INSERT INTO note (body) VALUES ('an old note from before items existed')"); seed.close(); const { db, createItem, addNote, listNotes, listItems } = await import("./db.ts"); test("moves an old, incompatible note table aside instead of crashing", () => { const row = db.query<{ body: string }, []>("SELECT body FROM note_legacy").get(); expect(row?.body).toBe("an old note from before items existed"); }); test("the collection works normally after migrating", () => { const id = createItem("a test item"); addNote(id, "sighting", "hello"); expect(listNotes(id)).toHaveLength(1); expect(listItems()[0]?.note_count).toBe(1); });