import assert from "node:assert/strict"; import test from "node:test"; import { DbcTable } from "../src/dbc/table"; import { DbcSchema } from "../src/dbc/types"; const schema: DbcSchema = { name: "Synthetic", build: 12340, recordSize: 12, columns: [ { name: "ID", kind: "int", bits: 32, offset: 0, isId: true }, { name: "Scale", kind: "float", bits: 32, offset: 4 }, { name: "Name", kind: "string", bits: 32, offset: 8 } ] }; test("reads, edits and writes WDBC records and strings", () => { const table = new DbcTable(makeFixture(), schema); assert.equal(table.header.recordCount, 2); assert.equal(table.getCell(0, 0), "1"); assert.equal(table.getCell(0, 1), "1.5"); assert.equal(table.getCell(0, 2), "hello"); assert.equal(table.getCell(1, 2), "world"); table.setCell(0, 1, "2.25"); table.setCell(0, 2, "Привет"); table.setCell(1, 0, "42"); const reparsed = new DbcTable(table.serialize(), schema); assert.equal(reparsed.getCell(0, 1), "2.25"); assert.equal(reparsed.getCell(0, 2), "Привет"); assert.equal(reparsed.getCell(1, 0), "42"); assert.equal(reparsed.getCell(1, 2), "world"); }); test("adds, duplicates and deletes rows with unique generated IDs", () => { const table = new DbcTable(makeFixture(), schema); const added = table.appendRow(); assert.equal(added, 2); assert.equal(table.getCell(added, 0), "3"); assert.equal(table.getCell(added, 2), ""); const duplicate = table.duplicateRow(0); assert.equal(duplicate, 1); assert.equal(table.getCell(duplicate, 0), "4"); assert.equal(table.getCell(duplicate, 2), "hello"); const removed = table.deleteRow(duplicate); assert.equal(table.rows.length, 3); table.insertRow(duplicate, removed); assert.equal(table.getCell(duplicate, 0), "4"); const reparsed = new DbcTable(table.serialize(), schema); assert.equal(reparsed.header.recordCount, 4); }); test("validates integer ranges and duplicate IDs", () => { const table = new DbcTable(makeFixture(), schema); assert.throws(() => table.setCell(0, 0, "2"), /already used/); assert.throws(() => table.setCell(0, 0, "2147483648"), /between/); assert.throws(() => table.setCell(0, 1, "not-a-float"), /floating-point/); }); function makeFixture(): Uint8Array { const strings = Buffer.from("\0hello\0world\0", "utf8"); const bytes = Buffer.alloc(20 + 24 + strings.length); bytes.write("WDBC", 0, "ascii"); bytes.writeUInt32LE(2, 4); bytes.writeUInt32LE(3, 8); bytes.writeUInt32LE(12, 12); bytes.writeUInt32LE(strings.length, 16); bytes.writeInt32LE(1, 20); bytes.writeFloatLE(1.5, 24); bytes.writeUInt32LE(1, 28); bytes.writeInt32LE(2, 32); bytes.writeFloatLE(-3.25, 36); bytes.writeUInt32LE(7, 40); strings.copy(bytes, 44); return bytes; }