import { readFileSync } from "node:fs"; import { strictEqual, ok, throws } from "node:assert"; import { describe, it } from "node:test"; import { parseBundle, parseManifest, } from "../src/bundle.js"; import { parseNodeSection as bundleParseNodeSection, } from "../src/bundle.js"; import { verifyNodeHashes, parseNodeSection as parseNodes, } from "../src/merkle.js"; const fixtureDir = "test/fixtures"; describe("bundle parsing", () => { it("valid bundle parses header and sections", () => { const bundle = parseBundle( readFileSync(`${fixtureDir}/id.tri.bundle`) ); strictEqual(bundle.version, "1.0"); strictEqual(bundle.sectionCount, 2); ok(bundle.sections.has(1)); // manifest ok(bundle.sections.has(2)); // nodes }); it("parseManifest returns valid JSON", () => { const manifest = parseManifest( readFileSync(`${fixtureDir}/id.tri.bundle`) ); strictEqual(manifest.schema, "arborix.bundle.manifest.v1"); strictEqual(manifest.bundleType, "tree-calculus-executable-object"); strictEqual(manifest.closure, "complete"); strictEqual(manifest.tree.calculus, "tree-calculus.v1"); strictEqual(manifest.tree.nodeHash.algorithm, "sha256"); strictEqual(manifest.runtime.semantics, "tree-calculus.v1"); strictEqual(manifest.runtime.abi, "arborix.abi.tree.v1"); }); }); describe("hash verification", () => { it("valid bundle nodes verify", () => { const data = bundleParseNodeSection( readFileSync(`${fixtureDir}/id.tri.bundle`) ); const { nodeMap } = parseNodes(data); const { verified } = verifyNodeHashes(nodeMap); ok(verified, "all node hashes should verify"); }); }); describe("errors", () => { it("bad magic fails", () => { const buf = Buffer.alloc(32, 0); buf.write("WRONGMAG", 0, 8); throws(() => parseBundle(buf), /invalid magic/); }); it("unsupported version fails", () => { const buf = Buffer.alloc(32, 0); buf.write("ARBORIX\0", 0, 8); buf.writeUInt16BE(2, 8); // major version 2 throws(() => parseBundle(buf), /unsupported bundle major version/); }); });