import {createServer} from 'node:http'; import {existsSync} from 'node:fs'; import {readFile,writeFile} from 'node:fs/promises'; import {basename,resolve} from 'node:path'; import {spawn} from 'node:child_process'; import {createChromeProfile,removeChromeProfile} from './chrome-profile.mjs'
const root=resolve(new URL('..',import.meta.url).pathname), reportPath=resolve(root,'config/chrome-native-linear-pattern-history-verification.json'); const waitForPort=child=>new Promise((ok,no)=>{let out='';const timer=setTimeout(()=>no(new Error(out)),30000);const data=c=>{out+=String(c);const m=out.match(/http:\/\/127\.0\.0\.1:(\d+)/);if(m){clearTimeout(timer);ok(Number(m[1]))}};child.stdout.on('data',data);child.stderr.on('data',data)}); const vite=spawn('./nodew',['node_modules/vite/bin/vite.js','--host','127.0.0.1'],{cwd:root,stdio:['ignore','pipe','pipe']}); const vitePort=await waitForPort(vite); let resolveReport; const reportPromise=new Promise(r=>{resolveReport=r}); const server=createServer(async(req,res)=>{res.setHeader('Cross-Origin-Opener-Policy','same-origin');res.setHeader('Cross-Origin-Embedder-Policy','require-corp');res.setHeader('Cross-Origin-Resource-Policy','same-origin');if(req.method==='POST'&&req.url==='/__native-linear-pattern-history-report'){let body='';req.on('data',c=>body+=String(c));req.on('end',async()=>{const report=JSON.parse(body);await writeFile(reportPath,JSON.stringify(report,null,2)+'\n');resolveReport(report);res.writeHead(204);res.end()});return}if(req.url==='/chrome-native-linear-pattern-history-harness.html'){res.end(await readFile(resolve(root,'public/chrome-native-linear-pattern-history-harness.html')));return}if(req.url?.startsWith('/native/occt-history/')){const name=basename(new URL(req.url,'http://x').pathname),file=resolve(root,'native/occt-history/dist',name);if(!['bitbybit-occt-history.js','bitbybit-occt-history.wasm'].includes(name)||!existsSync(file)){res.writeHead(404);res.end();return}res.setHeader('content-type',name.endsWith('.wasm')?'application/wasm':'text/javascript');res.end(await readFile(file));return}const upstream=await fetch(`http://127.0.0.1:${vitePort}${req.url}`);res.writeHead(upstream.status,Object.fromEntries(upstream.headers.entries()));res.end(Buffer.from(await upstream.arrayBuffer()))}); await new Promise(r=>server.listen(0,'127.0.0.1',r)); const executable=process.env.CHROME_BIN||'/home/mes123456/.local/bin/google-chrome',profile=await createChromeProfile('native-linear-pattern-history'),chrome=spawn(executable,['--headless=new','--no-sandbox','--disable-gpu','--disable-dev-shm-usage',`--user-data-dir=${profile}`,`http://127.0.0.1:${server.address().port}/chrome-native-linear-pattern-history-harness.html`],{stdio:'ignore'}); const report=await Promise.race([reportPromise,new Promise(r=>setTimeout(()=>r({status:'timeout'}),180000))]);chrome.kill('SIGTERM');vite.kill('SIGTERM');server.closeAllConnections?.();server.close();await removeChromeProfile(profile);console.log(JSON.stringify(report,null,2));process.exit(report.status==='pass'?0:1)
