#!/usr/bin/env node
// Test WASM build of SX kernel
const path = require('path');
const build_dir = path.join(__dirname, '../_build/default/browser');

async function main() {
  // Load WASM module — require.main.filename must point to build dir
  // so the WASM loader finds .wasm assets via path.dirname(require.main.filename)
  require.main.filename = path.join(build_dir, 'test_wasm.js');
  require(path.join(build_dir, 'sx_browser.bc.wasm.js'));

  // Wait for WASM init
  await new Promise(r => setTimeout(r, 2000));

  const sx = globalThis.SxKernel;
  if (!sx) {
    console.error('FAIL: SxKernel not available');
    process.exit(1);
  }

  console.log('Engine:', sx.engine());

  // Basic tests
  const tests = [
    ['(+ 1 2)', 3],
    ['(- 10 3)', 7],
    ['(* 6 7)', 42],
    ['(/ 10 2)', 5],
    ['(= 5 5)', true],
    ['(< 3 5)', true],
    ['(> 5 3)', true],
    ['(not false)', true],
    ['(inc 5)', 6],
    ['(dec 5)', 4],
    ['(len (list 1 2 3))', 3],
    ['(len "hello")', 5],
    ['(first (list 10 20))', 10],
    ['(nth "hello" 0)', 'h'],
    ['(nth "hello" 4)', 'o'],
    ['(str "a" "b")', 'ab'],
    ['(join ", " (list "a" "b" "c"))', 'a, b, c'],
    ['(let ((x 10) (y 20)) (+ x y))', 30],
    ['(if true "yes" "no")', 'yes'],
    ['(cond (= 1 2) "one" :else "other")', 'other'],
    ['(case 2 1 "one" 2 "two" :else "other")', 'two'],
    ['(render-to-html (list (quote div) "hello"))', '<div>hello</div>'],
    ['(render-to-html (list (quote span) (list (quote b) "bold")))', '<span><b>bold</b></span>'],
    ['(let ((add (fn (a b) (+ a b)))) (add 3 4))', 7],
    ['(let ((x 10)) (let ((f (fn () x))) (f)))', 10],
    ['(len (filter (fn (x) (> x 2)) (list 1 2 3 4 5)))', 3],
    ['(let ((fact (fn (n) (if (<= n 1) 1 (* n (fact (- n 1))))))) (fact 5))', 120],
  ];

  let passed = 0, failed = 0;
  for (const [expr, expected] of tests) {
    const result = sx.eval(expr);
    const ok = typeof expected === 'object'
      ? result && result._type === expected._type
      : result === expected;
    if (ok) {
      console.log(`  PASS: ${expr} = ${JSON.stringify(result)}`);
      passed++;
    } else {
      console.log(`  FAIL: ${expr} = ${JSON.stringify(result)} (expected ${JSON.stringify(expected)})`);
      failed++;
    }
  }

  console.log(`\n${passed} passed, ${failed} failed`);
  process.exit(failed > 0 ? 1 : 0);
}

main().catch(e => { console.error(e); process.exit(1); });
