import assert from "node:assert/strict"; import fs from "node:fs"; import { describe, it } from "node:test"; import Database from "better-sqlite3"; import { assertCircuitDeviceRowQuantity, calculateCircuitDeviceRowQuantity, } from "../src/domain/calculations/circuit-device-row-quantity.js"; describe("circuit device-row quantity", () => { it("adds the manual share and every indivisible external quantity", () => { assert.equal( calculateCircuitDeviceRowQuantity(2, [ { effectiveQuantity: 2 }, { effectiveQuantity: 1 }, ]), 5 ); assert.doesNotThrow(() => assertCircuitDeviceRowQuantity({ quantity: 5, manualQuantity: 2, externalObjects: [{ effectiveQuantity: 2 }, { effectiveQuantity: 1 }], })); }); it("rejects invalid shares and a stale materialized total", () => { assert.throws( () => calculateCircuitDeviceRowQuantity(-1, []), /Manual quantity/ ); assert.throws( () => calculateCircuitDeviceRowQuantity(0, [{ effectiveQuantity: 0 }]), /External effective quantity/ ); assert.throws( () => assertCircuitDeviceRowQuantity({ quantity: 2, manualQuantity: 1, externalObjects: [{ effectiveQuantity: 2 }], }), /does not match/ ); }); it("preserves existing row quantities in migration 0005", () => { const database = new Database(":memory:"); try { database.exec("CREATE TABLE circuit_device_rows (id text PRIMARY KEY, quantity integer NOT NULL)"); database.exec("INSERT INTO circuit_device_rows (id, quantity) VALUES ('row-1', 7)"); const migration = fs .readFileSync("src/db/migrations/0005_stale_gorilla_man.sql", "utf8") .replaceAll("--> statement-breakpoint", ""); database.exec(migration); assert.deepEqual( database.prepare("SELECT quantity, manual_quantity FROM circuit_device_rows").get(), { quantity: 7, manual_quantity: 7 } ); } finally { database.close(); } }); });