import { readFileSync } from 'node:fs'; import { strictEqual, ok } from 'node:assert'; import { describe, it } from 'node:test'; import { findLib, init, free, leaf, stem, fork, app, reduce, toBool, toString, toNumber, loadBundleDefault, ofString, ofNumber, } from '../src/lib.js'; const libPath = findLib(); describe('tree construction', () => { it('leaf returns a positive index', () => { const ctx = init(libPath); try { const idx = leaf(ctx); ok(idx > 0); } finally { free(ctx); } }); it('stem wraps a child', () => { const ctx = init(libPath); try { const l = leaf(ctx); const s = stem(ctx, l); ok(s > 0); ok(s !== l); } finally { free(ctx); } }); it('fork combines left and right', () => { const ctx = init(libPath); try { const a = leaf(ctx); const b = leaf(ctx); const f = fork(ctx, a, b); ok(f > 0); ok(f !== a && f !== b); } finally { free(ctx); } }); }); describe('reduction — booleans', () => { it('true.arboricx reduces to boolean true', () => { const ctx = init(libPath); try { const bundle = readFileSync('../../test/fixtures/true.arboricx'); const root = loadBundleDefault(ctx, bundle); const result = reduce(ctx, root, 1_000_000n); strictEqual(toBool(ctx, result), true); } finally { free(ctx); } }); it('false.arboricx reduces to boolean false', () => { const ctx = init(libPath); try { const bundle = readFileSync('../../test/fixtures/false.arboricx'); const root = loadBundleDefault(ctx, bundle); const result = reduce(ctx, root, 1_000_000n); strictEqual(toBool(ctx, result), false); } finally { free(ctx); } }); }); describe('reduction — id', () => { it('id applied to string returns the string', () => { const ctx = init(libPath); try { const bundle = readFileSync('../../test/fixtures/id.arboricx'); const idRoot = loadBundleDefault(ctx, bundle); const arg = ofString(ctx, 'hello'); const applied = app(ctx, idRoot, arg); const result = reduce(ctx, applied, 1_000_000n); strictEqual(toString(ctx, result), 'hello'); } finally { free(ctx); } }); }); describe('reduction — numbers', () => { it('ofNumber round-trips through toNumber', () => { const ctx = init(libPath); try { const num = ofNumber(ctx, 42); strictEqual(toNumber(ctx, num), 42); } finally { free(ctx); } }); });