import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { closeDatabase, type Db, initDatabase } from "../db/database.js"; import { createRpcHandler } from "../db/rpcHandler.js"; describe("model RPC", () => { let db: Db; let rpc: ReturnType; beforeEach(() => { db = initDatabase({ inMemory: true }); db.prepare("INSERT INTO companies (id, ticker, name, sector, price, change_pct, thesis) VALUES ('aapl', 'AAPL', 'Apple Inc.', 'Technology', 0, 0, '')").run(); rpc = createRpcHandler(db); }); afterEach(() => { closeDatabase(db); }); it("rejects out-of-range model cell updates without throwing", async () => { const result = await rpc("model.updateCell", { companyId: "aapl", tab: "income", row: 0, col: 999, value: "42", }); expect(result).toEqual({ ok: true, data: { ok: false, affectedCells: [] } }); }); it("returns an empty model when no rows exist yet", async () => { const result = await rpc("model.get", { companyId: "aapl", tab: "operating" }); expect(result).toEqual({ ok: true, data: { headers: [], rows: [] } }); }); it("returns a typed failure when model storage is unavailable", async () => { db.prepare("DROP TABLE models").run(); const result = await rpc("model.get", { companyId: "aapl", tab: "operating" }); expect(result.ok).toBe(false); if (result.ok) return; expect(result.error.code).toBe("INTERNAL_ERROR"); expect(result.error.message).toBe("Could not load model for company."); }); });