import { readFileSync } from 'node:fs'; import { strictEqual, ok, throws } from 'node:assert'; import { describe, it } from 'node:test'; import { findLib, init, free, loadBundle, loadBundleDefault, kernelRoot, } from '../src/lib.js'; const fixtureDir = '../../test/fixtures'; const libPath = findLib(); describe('library discovery', () => { it('findLib returns an existing .so path', () => { ok(libPath.endsWith('.so') || libPath.endsWith('.dylib') || libPath.endsWith('.dll')); ok(readFileSync(libPath)); }); }); describe('context lifecycle', () => { it('init creates a valid context', () => { const ctx = init(libPath); ok(ctx); free(ctx); }); it('kernel root is available', () => { const ctx = init(libPath); try { const root = kernelRoot(ctx); ok(root > 0, 'kernel root should be a positive index'); } finally { free(ctx); } }); }); describe('bundle loading', () => { it('loadBundleDefault loads id.arboricx', () => { const ctx = init(libPath); try { const bundle = readFileSync(`${fixtureDir}/id.arboricx`); const root = loadBundleDefault(ctx, bundle); ok(root > 0, 'loaded root should be a positive index'); } finally { free(ctx); } }); it('loadBundleDefault loads true.arboricx', () => { const ctx = init(libPath); try { const bundle = readFileSync(`${fixtureDir}/true.arboricx`); const root = loadBundleDefault(ctx, bundle); ok(root > 0); } finally { free(ctx); } }); it('loadBundle loads named export from id.arboricx', () => { const ctx = init(libPath); try { const bundle = readFileSync(`${fixtureDir}/id.arboricx`); const root = loadBundle(ctx, bundle, 'id'); ok(root > 0); } finally { free(ctx); } }); it('loadBundle fails for missing export name', () => { const ctx = init(libPath); try { const bundle = readFileSync(`${fixtureDir}/id.arboricx`); throws(() => loadBundle(ctx, bundle, 'nonexistent'), /failed/); } finally { free(ctx); } }); it('loadBundleDefault fails for invalid bytes', () => { const ctx = init(libPath); try { throws(() => loadBundleDefault(ctx, Buffer.from('not a bundle')), /failed/); } finally { free(ctx); } }); });