#!/bin/bash
# Test WASM boot in Node.js — verifies the compiled sx_browser.bc.js loads
# without errors by providing minimal DOM/browser API stubs.
set -euo pipefail

cd "$(dirname "$0")/../../.."

node -e "
  global.window = global;
  global.document = { createElement: () => ({style:{},setAttribute:()=>{},appendChild:()=>{},children:[]}), createDocumentFragment: () => ({appendChild:()=>{},children:[],childNodes:[]}), head:{appendChild:()=>{}}, body:{appendChild:()=>{}}, querySelector:()=>null, querySelectorAll:()=>[], createTextNode:(s)=>({textContent:s}), addEventListener:()=>{}, createComment:(s)=>({textContent:s||''}) };
  global.localStorage = {getItem:()=>null,setItem:()=>{},removeItem:()=>{}};
  global.CustomEvent = class { constructor(n,o){this.type=n;this.detail=(o||{}).detail||{}} };
  global.MutationObserver = class { observe(){} disconnect(){} };
  global.requestIdleCallback = (fn) => setTimeout(fn,0);
  global.matchMedia = () => ({matches:false});
  global.navigator = {serviceWorker:{register:()=>Promise.resolve()}};
  global.location = {href:'',pathname:'/',hostname:'localhost'};
  global.history = {pushState:()=>{},replaceState:()=>{}};
  global.fetch = () => Promise.resolve({ok:true,text:()=>Promise.resolve('')});
  global.setTimeout = setTimeout;
  global.clearTimeout = clearTimeout;
  try {
    require('./shared/static/wasm/sx_browser.bc.js');
    console.log('WASM boot: OK');
  } catch(e) {
    console.error('WASM boot: FAILED');
    console.error(e.message);
    process.exit(1);
  }
"
