bundle.test.js (2342B)
1 import { readFileSync } from 'node:fs'; 2 import { strictEqual, ok, throws } from 'node:assert'; 3 import { describe, it } from 'node:test'; 4 import { 5 findLib, 6 init, 7 free, 8 loadBundle, 9 loadBundleDefault, 10 kernelRoot, 11 } from '../src/lib.js'; 12 13 const fixtureDir = '../../test/fixtures'; 14 const libPath = findLib(); 15 16 describe('library discovery', () => { 17 it('findLib returns an existing .so path', () => { 18 ok(libPath.endsWith('.so') || libPath.endsWith('.dylib') || libPath.endsWith('.dll')); 19 ok(readFileSync(libPath)); 20 }); 21 }); 22 23 describe('context lifecycle', () => { 24 it('init creates a valid context', () => { 25 const ctx = init(libPath); 26 ok(ctx); 27 free(ctx); 28 }); 29 30 it('kernel root is available', () => { 31 const ctx = init(libPath); 32 try { 33 const root = kernelRoot(ctx); 34 ok(root > 0, 'kernel root should be a positive index'); 35 } finally { 36 free(ctx); 37 } 38 }); 39 }); 40 41 describe('bundle loading', () => { 42 it('loadBundleDefault loads id.arboricx', () => { 43 const ctx = init(libPath); 44 try { 45 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 46 const root = loadBundleDefault(ctx, bundle); 47 ok(root > 0, 'loaded root should be a positive index'); 48 } finally { 49 free(ctx); 50 } 51 }); 52 53 it('loadBundleDefault loads true.arboricx', () => { 54 const ctx = init(libPath); 55 try { 56 const bundle = readFileSync(`${fixtureDir}/true.arboricx`); 57 const root = loadBundleDefault(ctx, bundle); 58 ok(root > 0); 59 } finally { 60 free(ctx); 61 } 62 }); 63 64 it('loadBundle loads named export from id.arboricx', () => { 65 const ctx = init(libPath); 66 try { 67 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 68 const root = loadBundle(ctx, bundle, 'id'); 69 ok(root > 0); 70 } finally { 71 free(ctx); 72 } 73 }); 74 75 it('loadBundle fails for missing export name', () => { 76 const ctx = init(libPath); 77 try { 78 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 79 throws(() => loadBundle(ctx, bundle, 'nonexistent'), /failed/); 80 } finally { 81 free(ctx); 82 } 83 }); 84 85 it('loadBundleDefault fails for invalid bytes', () => { 86 const ctx = init(libPath); 87 try { 88 throws(() => loadBundleDefault(ctx, Buffer.from('not a bundle')), /failed/); 89 } finally { 90 free(ctx); 91 } 92 }); 93 });