const puppeteer=require('puppeteer');
const S=[['assembly',4.0,'a1'],['assembly',9.5,'a2'],['assembly',26.0,'a3'],
         ['walkthrough',14,'w1'],['walkthrough',40,'w2'],
         ['operation',3,'o1'],['operation',9,'o2'],['operation',20,'o3']];
(async()=>{
  const out=process.argv[2];
  const b=await puppeteer.launch({headless:'new',args:['--no-sandbox','--enable-webgl','--use-gl=swiftshader','--enable-unsafe-swiftshader','--disable-dev-shm-usage']});
  const p=await b.newPage(); await p.setViewport({width:1280,height:800});
  p.on('pageerror',e=>console.log('PAGEERROR:',e.message));
  await p.goto('http://127.0.0.1:8891/feeder-reader-viewer.html',{waitUntil:'domcontentloaded',timeout:90000});
  await p.waitForFunction(()=>!!window.__dbg,{timeout:180000});
  for (const [m,t,tag] of S){
    await p.evaluate((m,t)=>{ const d=window.__dbg; d.setMode(m); d.pause(); d.t=t; },m,t);
    await new Promise(r=>setTimeout(r,2500));          // a few frames so the loop applies state
    await p.evaluate(()=>window.__dbg.snap());
    await new Promise(r=>setTimeout(r,2500));
    const info=await p.evaluate(()=>({t:+window.__dbg.t.toFixed(1),focus:window.__dbg.focus,
      vis:window.__dbg.COMPONENTS.filter(x=>x.meshes[0].visibility>0.5).length,
      cap:document.querySelector('#caption .head').textContent.slice(0,44),
      shown:document.getElementById('caption').classList.contains('show')}));
    console.log(tag,m,JSON.stringify(info));
    await p.screenshot({path:`${out}/s-${tag}.png`});
  }
  await b.close();
})().catch(e=>{console.error('FAIL',e.message);process.exit(1)});