#!/usr/bin/env node
/**
 * Debug specific HS behavioral tests to trace why they fail.
 * Reuses the hs-run-fast.js setup but runs individual tests with verbose output.
 */
const fs = require('fs');
const path = require('path');
const PROJECT = path.resolve(__dirname, '..');
const WASM_DIR = path.join(PROJECT, 'shared/static/wasm');
const SX_DIR = path.join(WASM_DIR, 'sx');

// Load and run the setup from hs-run-fast.js up to the test loop
eval(fs.readFileSync(path.join(WASM_DIR, 'sx_browser.bc.js'), 'utf8'));
const K = globalThis.SxKernel;

// ─── Copy DOM mock from runner ──────────────────────────────
class El {
  constructor(t) { this.tagName=t.toUpperCase(); this.nodeName=this.tagName; this.nodeType=1; this.id=''; this.className=''; this.classList=new CL(this); this.style={}; this.attributes={}; this.children=[]; this.childNodes=[]; this.parentElement=null; this.parentNode=null; this.textContent=''; this.innerHTML=''; this._listeners={}; this.dataset={}; this.open=false; this.value=''; this.checked=false; this.disabled=false; this.type=''; this.name=''; this.selectedIndex=-1; this.options=[]; }
  setAttribute(n,v) { this.attributes[n]=String(v); if(n==='id')this.id=v; if(n==='class'){this.className=v;this.classList._sync(v);} if(n==='value')this.value=v; if(n==='disabled')this.disabled=true; }
  getAttribute(n) { return this.attributes[n]!==undefined?this.attributes[n]:null; }
  removeAttribute(n) { delete this.attributes[n]; if(n==='disabled')this.disabled=false; }
  hasAttribute(n) { return n in this.attributes; }
  addEventListener(e,f) { if(!this._listeners[e])this._listeners[e]=[]; this._listeners[e].push(f); }
  removeEventListener(e,f) { if(this._listeners[e])this._listeners[e]=this._listeners[e].filter(x=>x!==f); }
  dispatchEvent(ev) { ev.target=ev.target||this; ev.currentTarget=this; const fns=[...(this._listeners[ev.type]||[])]; for(const f of fns){if(ev._si)break;try{f.call(this,ev);}catch(e){}} if(ev.bubbles&&!ev._sp&&this.parentElement){this.parentElement.dispatchEvent(ev);} return !ev.defaultPrevented; }
  appendChild(c) { if(c.parentElement)c.parentElement.removeChild(c); c.parentElement=this; c.parentNode=this; this.children.push(c); this.childNodes.push(c); this._syncText(); return c; }
  removeChild(c) { this.children=this.children.filter(x=>x!==c); this.childNodes=this.childNodes.filter(x=>x!==c); c.parentElement=null; c.parentNode=null; this._syncText(); return c; }
  insertBefore(n,r) { if(n.parentElement)n.parentElement.removeChild(n); const i=this.children.indexOf(r); if(i>=0){this.children.splice(i,0,n);this.childNodes.splice(i,0,n);}else{this.children.push(n);this.childNodes.push(n);} n.parentElement=this;n.parentNode=this; this._syncText(); return n; }
  replaceChild(n,o) { const i=this.children.indexOf(o); if(i>=0){this.children[i]=n;this.childNodes[i]=n;} n.parentElement=this;n.parentNode=this; o.parentElement=null;o.parentNode=null; this._syncText(); return o; }
  querySelector(s) { return fnd(this,s); } querySelectorAll(s) { return fndAll(this,s); }
  closest(s) { let e=this; while(e){if(mt(e,s))return e; e=e.parentElement;} return null; }
  matches(s) { return mt(this,s); } contains(o) { if(o===this)return true; for(const c of this.children)if(c===o||c.contains(o))return true; return false; }
  cloneNode(d) { const e=new El(this.tagName.toLowerCase()); Object.assign(e.attributes,this.attributes); e.id=this.id; e.className=this.className; e.classList._sync(this.className); Object.assign(e.style,this.style); e.textContent=this.textContent; e.innerHTML=this.innerHTML; e.value=this.value; if(d)for(const c of this.children)e.appendChild(c.cloneNode(true)); return e; }
  focus(){} blur(){} click(){this.dispatchEvent(new Ev('click',{bubbles:true}));} remove(){if(this.parentElement)this.parentElement.removeChild(this);}
  _syncText() { const t = this.children.map(c => c.textContent || '').join(''); if (t) this.textContent = t; }
  _setInnerHTML(html) { for (const c of this.children) { c.parentElement = null; c.parentNode = null; } this.children = []; this.childNodes = []; this.innerHTML = html; this.textContent = html.replace(/<[^>]*>/g, ''); }
  get firstElementChild() { return this.children[0]||null; }
  get lastElementChild() { return this.children[this.children.length-1]||null; }
  get nextElementSibling() { if(!this.parentElement)return null; const i=this.parentElement.children.indexOf(this); return this.parentElement.children[i+1]||null; }
  get previousElementSibling() { if(!this.parentElement)return null; const i=this.parentElement.children.indexOf(this); return i>0?this.parentElement.children[i-1]:null; }
  showModal(){this.open=true;this.setAttribute('open','');} show(){this.open=true;} close(){this.open=false;this.removeAttribute('open');}
  getAnimations(){return [];} getBoundingClientRect(){return{top:0,left:0,width:100,height:100,right:100,bottom:100};} scrollIntoView(){}
  get ownerDocument() { return document; }
  get offsetParent() { return this.parentElement; }
  insertAdjacentHTML(pos, html) { if(typeof html!=='string')html=String(html); if(pos==='beforeend'||pos==='beforeEnd'){this.innerHTML=(this.innerHTML||'')+html;this.textContent=(this.textContent||'')+html.replace(/<[^>]*>/g,'');}else if(pos==='afterbegin'||pos==='afterBegin'){this.innerHTML=html+(this.innerHTML||'');this.textContent=html.replace(/<[^>]*>/g,'')+(this.textContent||'');} }
}
class CL { constructor(e){this._el=e;this._set=new Set();} _sync(str){this._set=new Set((str||'').split(/\s+/).filter(Boolean));} add(...c){for(const x of c)this._set.add(x);this._el.className=[...this._set].join(' ');this._el.attributes['class']=this._el.className;} remove(...c){for(const x of c)this._set.delete(x);this._el.className=[...this._set].join(' ');this._el.attributes['class']=this._el.className;} toggle(c,f){if(f!==undefined){if(f)this.add(c);else this.remove(c);return f;} if(this._set.has(c)){this.remove(c);return false;}else{this.add(c);return true;}} contains(c){return this._set.has(c);} get length(){return this._set.size;} [Symbol.iterator](){return this._set[Symbol.iterator]();} }
class Ev { constructor(t,o={}){this.type=t;this.bubbles=o.bubbles||false;this.cancelable=o.cancelable!==false;this.defaultPrevented=false;this._sp=false;this._si=false;this.target=null;this.currentTarget=null;this.detail=o.detail||null;} preventDefault(){this.defaultPrevented=true;} stopPropagation(){this._sp=true;} stopImmediatePropagation(){this._sp=true;this._si=true;} }
function mt(e,s) { if(!e||!e.tagName)return false; s=s.trim(); if(s.startsWith('#'))return e.id===s.slice(1); if(s.startsWith('.'))return e.classList.contains(s.slice(1)); if(s.startsWith('[')){const m=s.match(/^\[([^\]=]+)(?:="([^"]*)")?\]$/);if(m)return m[2]!==undefined?e.getAttribute(m[1])===m[2]:e.hasAttribute(m[1]);} if(s.includes('.')){const[tag,cls]=s.split('.');return e.tagName.toLowerCase()===tag&&e.classList.contains(cls);} if(s.includes('#')){const[tag,id]=s.split('#');return e.tagName.toLowerCase()===tag&&e.id===id;} return e.tagName.toLowerCase()===s.toLowerCase(); }
function fnd(e,s) { for(const c of(e.children||[])){if(mt(c,s))return c;const f=fnd(c,s);if(f)return f;} return null; }
function fndAll(e,s) { const r=[];for(const c of(e.children||[])){if(mt(c,s))r.push(c);r.push(...fndAll(c,s));}return r; }
const _body = new El('body'); const _html = new El('html'); _html.appendChild(_body);
const document = { body:_body, documentElement:_html, createElement(t){return new El(t);}, createElementNS(n,t){return new El(t);}, createDocumentFragment(){const f=new El('fragment');f.nodeType=11;return f;}, createTextNode(t){return{nodeType:3,textContent:t,data:t};}, getElementById(i){return fnd(_body,'#'+i);}, querySelector(s){return fnd(_body,s);}, querySelectorAll(s){return fndAll(_body,s);}, createEvent(t){return new Ev(t);}, addEventListener(){}, removeEventListener(){} };
globalThis.document=document; globalThis.window=globalThis; globalThis.HTMLElement=El; globalThis.Element=El;
globalThis.Event=Ev; globalThis.CustomEvent=Ev; globalThis.NodeList=Array; globalThis.HTMLCollection=Array;
globalThis.getComputedStyle=(e)=>e?e.style:{}; globalThis.requestAnimationFrame=(f)=>{f();return 0;};
globalThis.cancelAnimationFrame=()=>{}; globalThis.MutationObserver=class{observe(){}disconnect(){}};
globalThis.ResizeObserver=class{observe(){}disconnect(){}}; globalThis.IntersectionObserver=class{observe(){}disconnect(){}};
globalThis.navigator={userAgent:'node'}; globalThis.location={href:'http://localhost/',pathname:'/',search:'',hash:''};
globalThis.history={pushState(){},replaceState(){},back(){},forward(){}};

K.registerNative('host-global',a=>{const n=a[0];return(n in globalThis)?globalThis[n]:null;});
K.registerNative('host-get',a=>{if(a[0]==null)return null;const v=a[0][a[1]];return v===undefined?null:v;});
K.registerNative('host-set!',a=>{if(a[0]!=null){const v=a[2]; if(a[1]==='innerHTML'&&a[0] instanceof El){const s=String(v!=null?v:'');a[0]._setInnerHTML(s);a[0][a[1]]=a[0].innerHTML;} else if(a[1]==='textContent'&&a[0] instanceof El){const s=String(v!=null?v:'');a[0].textContent=s;a[0].innerHTML=s;for(const c of a[0].children){c.parentElement=null;c.parentNode=null;}a[0].children=[];a[0].childNodes=[];} else{a[0][a[1]]=v;}} return a[2];});
K.registerNative('host-call',a=>{const[o,m,...r]=a;if(o==null){const f=globalThis[m];return typeof f==='function'?f.apply(null,r):null;}if(o&&typeof o[m]==='function'){try{const v=o[m].apply(o,r);return v===undefined?null:v;}catch(e){return null;}}return null;});
K.registerNative('host-new',a=>{const C=typeof a[0]==='string'?globalThis[a[0]]:a[0];return typeof C==='function'?new C(...a.slice(1)):null;});
K.registerNative('host-callback',a=>{const fn=a[0];if(typeof fn==='function'&&fn.__sx_handle===undefined)return fn;if(fn&&fn.__sx_handle!==undefined)return function(){const r=K.callFn(fn,Array.from(arguments));if(globalThis._driveAsync)globalThis._driveAsync(r);return r;};return function(){};});
K.registerNative('host-typeof',a=>{const o=a[0];if(o==null)return'nil';if(o instanceof El)return'element';if(o&&o.nodeType===3)return'text';if(o instanceof Ev)return'event';if(o instanceof Promise)return'promise';return typeof o;});
K.registerNative('host-await',a=>{});
K.registerNative('load-library!',()=>false);
globalThis._driveAsync=function driveAsync(r,d){d=d||0;if(d>500||!r||!r.suspended)return;const req=r.request;const items=req&&(req.items||req);const op=items&&items[0];const opName=typeof op==='string'?op:(op&&op.name)||String(op);function doResume(v){try{const x=r.resume(v);driveAsync(x,d+1);}catch(e){}}if(opName==='io-sleep'||opName==='wait')doResume(null);else if(opName==='io-fetch')doResume({ok:true,text:''});else if(opName==='io-settle')doResume(null);else if(opName==='io-wait-event')doResume(null);else if(opName==='io-transition')doResume(null);};
K.eval('(define SX_VERSION "hs-test-1.0")');K.eval('(define SX_ENGINE "ocaml-vm-sandbox")');
K.eval('(define parse sx-parse)');K.eval('(define serialize sx-serialize)');

const WEB=['render','core-signals','signals','deps','router','page-helpers','freeze','dom','browser','adapter-html','adapter-sx','adapter-dom','boot-helpers','hypersx','engine','orchestration','boot'];
const HS=['hs-tokenizer','hs-parser','hs-compiler','hs-runtime','hs-integration'];
K.beginModuleLoad();
for(const mod of[...WEB,...HS]){const sp=path.join(SX_DIR,mod+'.sx');const lp=path.join(PROJECT,'lib/hyperscript',mod.replace(/^hs-/,'')+'.sx');let s;try{s=fs.existsSync(sp)?fs.readFileSync(sp,'utf8'):fs.readFileSync(lp,'utf8');}catch(e){continue;}try{K.load(s);}catch(e){}}
K.endModuleLoad();

// Load test registry
K.eval('(define _test-registry (list))');K.eval('(define _test-suite "")');
K.eval('(define push-suite (fn (name) (set! _test-suite name)))');
K.eval('(define pop-suite (fn () (set! _test-suite "")))');
K.eval('(define try-call (fn (thunk) (set! _test-registry (append _test-registry (list {:suite _test-suite :thunk thunk}))) {:ok true}))');
K.eval('(define report-pass (fn (n) true))');
K.eval('(define report-fail (fn (n e) true))');
K.eval(`(define _run-test-thunk
  (fn (thunk)
    (guard (exn
             (true {:ok false :error (if (string? exn) exn (str exn))}))
      (thunk)
      {:ok true})))`);
for(const f of['spec/harness.sx','spec/tests/test-framework.sx','spec/tests/test-hyperscript-behavioral.sx']){
  try{K.load(fs.readFileSync(path.join(PROJECT,f),'utf8'));}catch(e){}}

const testCount = K.eval('(len _test-registry)');
console.log(`Loaded ${testCount} tests\n`);

// Run specific tests
const tests = (process.argv[2] || '0,1,65,83').split(',').map(Number);

for (const i of tests) {
  _body.children=[]; _body.childNodes=[]; _body.innerHTML=''; _body.textContent='';

  const suite = K.eval(`(get (nth _test-registry ${i}) "suite")`) || '';
  const name = K.eval(`(get (nth _test-registry ${i}) "name")`) || `test-${i}`;

  K.setStepLimit(500000);
  let ok = false, err = null;
  try {
    K.eval(`(define _test-result (_run-test-thunk (get (nth _test-registry ${i}) "thunk")))`);
    const isOk = K.eval('(get _test-result "ok")');
    if (isOk === true) { ok = true; }
    else {
      const errMsg = K.eval('(get _test-result "error")');
      err = errMsg ? String(errMsg).slice(0, 150) : 'unknown error';
    }
  } catch(e) { err = (e.message || '').slice(0, 150); }
  K.setStepLimit(0);

  console.log(`Test ${i} [${suite}] ${name}`);
  if (ok) console.log(`  PASS`);
  else console.log(`  FAIL: ${(err||'').slice(0,200)}`);

  // Show DOM state
  const bodyHTML = _body.innerHTML;
  const bodyChildren = _body.children.length;
  if (bodyChildren > 0) {
    const el = _body.children[0];
    console.log(`  DOM: <${el.tagName.toLowerCase()} class="${el.className}" id="${el.id}">`);
    console.log(`    innerHTML: ${JSON.stringify(el.innerHTML).slice(0,100)}`);
    console.log(`    textContent: ${JSON.stringify(el.textContent).slice(0,100)}`);
    console.log(`    listeners: ${JSON.stringify(Object.keys(el._listeners))} (${el._listeners.click?.length || 0} click)`);
    console.log(`    style: ${JSON.stringify(el.style)}`);
  }
  console.log('');
}
