// Exercises the built Vue app in separate Brave and Firefox processes. import { installBrowserContext } from './browser-context.ts'; import { crossBrowserDriver } from './cross-browser-driver.ts'; import { isRecord } from '../src/lib/validation.ts'; import { createServer } from 'node:http'; import { spawn, type ChildProcess } from 'node:child_process'; import { readFile, mkdtemp, rm } from 'node:fs/promises'; import { dirname, extname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { tmpdir } from 'node:os'; const types: Record = {'.html':'text/html','.js':'text/javascript','.css':'text/css','.png':'image/png'}; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const state: Record = {}; const profiles: string[] = []; const children: ChildProcess[] = []; const provider = process.argv.includes('--default-network') ? 'google' : 'none'; const reversed = process.argv.includes('--reverse'); const driver = `(${installBrowserContext.toString()})(); (${crossBrowserDriver.toString()})(${JSON.stringify(provider)});`; let finish: () => void = () => {}; const result = new Promise(resolve => { finish = resolve; }); const server = createServer(async (req,res) => { try { const pathname=new URL(req.url ?? '/','http://localhost').pathname; if(pathname==='/.test/driver.js'){res.setHeader('Content-Type','text/javascript');res.end(driver);return;} if(pathname.startsWith('/.test/')){ const key=pathname.slice('/.test/'.length); if(req.method==='POST'){ let body='';for await(const part of req){body+=part;if(body.length>262144)throw new Error('Too big');} const previous=state[key]; state[key]=JSON.parse(body); if(key.endsWith('Stage'))console.log(key+': '+state[key]); if(key.endsWith('Diagnostic')&&JSON.stringify(previous)!==JSON.stringify(state[key]))console.log(key+': '+JSON.stringify(state[key])); if(key.endsWith('Result')){ console.log(key+': '+JSON.stringify(state[key])); if(!isRecord(state[key]) || !state[key].ok ||(state.senderResult&&state.receiverResult))finish(); } } res.setHeader('Content-Type','application/json');res.end(JSON.stringify(state[key]??null));return; } const base=join(root,'dist');const path=resolve(base,'.'+(pathname==='/'?'/index.html':pathname)); if(!path.startsWith(base+'/'))throw new Error('Invalid path'); let body: string | Buffer = await readFile(path); if(pathname==='/')body=body.toString().replace('',''); res.setHeader('Content-Type',types[extname(path)]||'application/octet-stream');res.end(body); }catch{res.statusCode=404;res.end();} }); await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve)); const address = server.address(); if (!address || typeof address === 'string') throw new Error('Missing server address'); const origin='http://127.0.0.1:'+address.port; let timer: ReturnType | undefined; try{ for(const name of ['brave','firefox']){ const profile=await mkdtemp(join(tmpdir(),'hellcon-'+name+'-'));profiles.push(profile); const role=(name==='brave')!==reversed?'sender':'receiver'; const args=name==='brave'?['--headless','--no-sandbox','--disable-gpu','--no-first-run','--no-default-browser-check','--user-data-dir='+profile,origin+'/?role='+role]:['--headless','--no-remote','--profile',profile,origin+'/?role='+role]; const child=spawn(process.env[name==='brave'?'BRAVE_PATH':'FIREFOX_PATH']||'/usr/bin/'+name,args,{stdio:['ignore','ignore','pipe']});children.push(child); let errors='';child.stderr.on('data',chunk=>{errors+=chunk;}); child.on('error',err=>{console.error(name,err.message);finish();}); child.on('exit',code=>{if(code){console.error(name+' exited: '+errors.slice(-1500));finish();}}); } timer=setTimeout(()=>{console.error('Cross-browser check timed out.', JSON.stringify({sender:state.senderDiagnostic,receiver:state.receiverDiagnostic}));finish();},provider==='none'?15000:90000); await result; if(!isRecord(state.senderResult) || !state.senderResult.ok || !isRecord(state.receiverResult) || !state.receiverResult.ok)process.exitCode=1; else console.log('PASS: '+(reversed?'Firefox → Brave':'Brave → Firefox')+' full UI transfer, fingerprint verification and exact file contents.'); }finally{ clearTimeout(timer); for(const child of children)child.kill(); await Promise.all(children.map(child=>new Promise(resolve=>{if(child.exitCode!==null||child.signalCode!==null)resolve();else child.once('exit', () => resolve());}))); server.closeAllConnections();await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); for(const profile of profiles)await rm(profile,{recursive:true,force:true,maxRetries:5,retryDelay:200}); }