// Optional operator-only functional checker. Requires locally installed playwright. // Run: node verify_site.mjs /absolute/path/to/submission/site /absolute/path/to/evidence // It copies the submission into a disposable directory; never modifies the original. import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import net from 'node:net'; import {spawn} from 'node:child_process'; import {chromium} from 'playwright'; const site=process.argv[2], evidence=process.argv[3]; if(!site||!evidence)throw new Error('Usage: node verify_site.mjs SITE_DIRECTORY EVIDENCE_DIRECTORY'); await fs.mkdir(evidence,{recursive:true}); const temp=await fs.mkdtemp(path.join(os.tmpdir(),'qwen-kit-site-')); await fs.cp(site,temp,{recursive:true}); await fs.rm(path.join(temp,'submissions.jsonl'),{force:true}); const listener=net.createServer(); await new Promise(resolve=>listener.listen(0,'127.0.0.1',resolve)); const port=listener.address().port; await new Promise(resolve=>listener.close(resolve)); const child=spawn(process.env.PYTHON||'python3',[path.join(temp,'server.py')],{cwd:temp,env:{...process.env,PORT:String(port)},stdio:['ignore','pipe','pipe']}); let serverLog=''; child.stderr.on('data',x=>serverLog+=x);child.stdout.on('data',x=>serverLog+=x); const origin=`http://127.0.0.1:${port}`; let browser; const checks=[]; function check(name,pass,detail){checks.push({name,pass:Boolean(pass),detail});} try{ let ready=false; for(let i=0;i<40;i++){try{const r=await fetch(origin);if(r.ok){ready=true;break}}catch{}await new Promise(r=>setTimeout(r,100));} if(!ready)throw new Error('Server did not start: '+serverLog); browser=await chromium.launch({headless:true,channel:process.env.BROWSER_CHANNEL||'chrome'}); const page=await browser.newPage({viewport:{width:1280,height:900}}); await page.goto(origin); for(const [seats,expected] of [[1,268.8],[2,537.6],[3,725.76],[8,1935.36]]){ await page.locator('#seats').fill(String(seats)); const actual=await page.locator('#total').innerText(); check(`total_${seats}`,Math.abs(Number(actual.replace(/[^0-9.-]/g,''))-expected)<0.005,actual); } for(const value of ['','0','-1','1.5','9']){ await page.locator('#seats').fill(value); const total=await page.locator('#total').innerText(); const valid=await page.locator('#seats').evaluate(el=>el.checkValidity()); check(`invalid_seats_${value||'blank'}`,!valid&&!/(?:CAD\s*[-+]?\d|\$\s*[-+]?\d|\b\d+\.\d{2}\b)/i.test(total),{total,valid}); } await page.locator('#seats').fill('3'); await page.locator('#name').fill('Test Operator'); await page.locator('#email').fill('operator@example.test'); await page.locator('button[type="submit"]').click(); await page.waitForTimeout(400); let records=[]; try{records=(await fs.readFile(path.join(temp,'submissions.jsonl'),'utf8')).trim().split('\n').filter(Boolean).map(JSON.parse)}catch{} check('frontend_persistence',records.length===1&&records[0].seats===3,records); check('visible_success',/saved|success|thank|received|confirm/i.test(await page.locator('#status').innerText()),await page.locator('#status').innerText()); for(const payload of [{name:'',email:'a@example.test',seats:1},{name:'A',email:'bad',seats:1},{name:'A',email:'a@example.test',seats:1.5},{name:'A',email:'a@example.test',seats:0},{name:'A',email:'a@example.test',seats:9}]){ const r=await fetch(origin+'/api/leads',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)}); check('server_reject_'+JSON.stringify(payload),r.status===400,r.status); } let after=[];try{after=(await fs.readFile(path.join(temp,'submissions.jsonl'),'utf8')).trim().split('\n').filter(Boolean)}catch{} check('invalid_not_persisted',after.length===records.length,after.length); for(const width of [390,1280]){ await page.setViewportSize({width,height:900}); await page.waitForTimeout(500); const dimensions=await page.evaluate(()=>({scroll:document.documentElement.scrollWidth,viewport:innerWidth})); check('no_overflow_'+width,dimensions.scroll<=dimensions.viewport,dimensions); await page.screenshot({path:path.join(evidence,`site-${width}.png`),fullPage:true}); } // Evidence checks are deliberately partial. Human review must assess readability, // hidden browser validation bypasses, content preservation and truthful handoff. }catch(error){checks.push({name:'checker_error',pass:false,detail:String(error)});} finally{ if(browser)await browser.close(); child.kill(); await new Promise(resolve=>{if(child.exitCode!==null||child.signalCode!==null)resolve();else{child.once('exit',resolve);setTimeout(resolve,2000)}}); await fs.rm(temp,{recursive:true,force:true}); await fs.writeFile(path.join(evidence,'checks.json'),JSON.stringify(checks,null,2)); await fs.writeFile(path.join(evidence,'server.log'),serverLog); } console.log(JSON.stringify({passed:checks.filter(x=>x.pass).length,total:checks.length,checks},null,2)); process.exitCode=checks.every(x=>x.pass)?0:1;