#!/usr/bin/env node
/**
 * Test the full WASM + platform stack in Node.
 * Loads the kernel, registers FFI stubs, loads .sx web files.
 */
const path = require('path');
const fs = require('fs');

// Load js_of_ocaml kernel (WASM needs browser; JS works in Node)
require(path.join(__dirname, '../_build/default/browser/sx_browser.bc.js'));

const K = globalThis.SxKernel;
console.log('Engine:', K.engine());

// Register FFI stubs (no real DOM in Node, but the primitives must exist)
K.registerNative("host-global", (args) => {
  const name = args[0];
  return globalThis[name] || null;
});
K.registerNative("host-get", (args) => {
  const [obj, prop] = args;
  if (obj == null) return null;
  const v = obj[prop];
  return v === undefined ? null : v;
});
K.registerNative("host-set!", (args) => {
  const [obj, prop, val] = args;
  if (obj != null) obj[prop] = val;
});
K.registerNative("host-call", (args) => {
  const [obj, method, ...rest] = args;
  if (obj == null) return null;
  if (typeof obj[method] === 'function') {
    try { return obj[method].apply(obj, rest); } catch(e) { return null; }
  }
  return null;
});
K.registerNative("host-new", (args) => null);
K.registerNative("host-callback", (args) => {
  const fn = args[0];
  if (typeof fn === 'function') return fn;
  if (fn && fn.__sx_handle !== undefined)
    return (...a) => K.callFn(fn, a);
  return () => {};
});
K.registerNative("host-typeof", (args) => {
  const obj = args[0];
  if (obj == null) return "nil";
  return typeof obj;
});
K.registerNative("host-await", (args) => {
  const [promise, callback] = args;
  if (promise && typeof promise.then === 'function') {
    const cb = typeof callback === 'function' ? callback :
      (callback && callback.__sx_handle !== undefined) ?
        (v) => K.callFn(callback, [v]) : () => {};
    promise.then(cb);
  }
});

// Load .sx web files in order
const root = path.join(__dirname, '../../..');
const sxFiles = [
  'spec/render.sx',        // HTML_TAGS, VOID_ELEMENTS, BOOLEAN_ATTRS, parse-element-args
  'web/signals.sx',
  'web/deps.sx',
  'web/router.sx',
  'web/page-helpers.sx',
  'lib/bytecode.sx',
  'lib/compiler.sx',
  'lib/vm.sx',
  'web/lib/dom.sx',
  'web/lib/browser.sx',
  'web/adapter-html.sx',
  'web/adapter-sx.sx',
  // Skip adapter-dom.sx, engine.sx, orchestration.sx, boot.sx — need real DOM
];

let totalExprs = 0;
for (const f of sxFiles) {
  const src = fs.readFileSync(path.join(root, f), 'utf8');
  const result = K.load(src);
  if (typeof result === 'string' && result.startsWith('Error')) {
    console.error(`  FAIL loading ${f}: ${result}`);
    process.exit(1);
  }
  totalExprs += (typeof result === 'number' ? result : 0);
}
console.log(`Loaded ${totalExprs} expressions from ${sxFiles.length} .sx files`);

// Test the loaded stack
const tests = [
  // Signals
  ['(let ((s (signal 0))) (reset! s 42) (deref s))', 42],
  ['(let ((s (signal 10))) (swap! s inc) (deref s))', 11],
  // Computed
  ['(let ((a (signal 2)) (b (computed (fn () (* (deref a) 3))))) (deref b))', 6],
  // Render (OCaml renderer uses XHTML-style void tags)
  ['(render-to-html (quote (div :class "foo" "bar")))', '<div class="foo">bar</div>'],
  ['(render-to-html (quote (br)))', '<br />'],
  // Compiler + VM
  ['(let ((c (compile (quote (+ 1 2))))) (get c "bytecode"))', { check: v => v && v._type === 'list' }],
  // dom.sx loaded (functions exist even without real DOM)
  ['(type-of dom-create-element)', 'lambda'],
  ['(type-of dom-listen)', 'lambda'],
  // browser.sx loaded
  ['(type-of console-log)', 'lambda'],
];

let passed = 0, failed = 0;
for (const [expr, expected] of tests) {
  try {
    const result = K.eval(expr);
    let ok;
    if (expected && typeof expected === 'object' && expected.check) {
      ok = expected.check(result);
    } else {
      ok = result === expected;
    }
    if (ok) {
      passed++;
    } else {
      console.log(`  FAIL: ${expr}`);
      console.log(`    got: ${JSON.stringify(result)}, expected: ${JSON.stringify(expected)}`);
      failed++;
    }
  } catch(e) {
    console.log(`  ERROR: ${expr}: ${e.message || e}`);
    failed++;
  }
}

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