/** Optional, isolated own-component Electron frame probe; never launches the user's app. * Build first: node scripts/test-image-lifecycle.mjs --build-only * Then: electron scripts/verify-b04-image-native.cjs * Requires a permitted graphical environment. Never pass --no-sandbox. */ const {app,BrowserWindow,session}=require('electron') const fs=require('node:fs'),path=require('node:path'),os=require('node:os') const fixture=path.resolve(process.argv[2]||'') if(!process.argv[2]||!fs.existsSync(path.join(fixture,'index.html')))throw Error('Supply build-only fixture directory') const profile=fs.mkdtempSync(path.join(os.tmpdir(),'ycode-b04-native-')) app.setPath('userData',profile) let timer,win,finished=false,phase='waiting-for-app' const started=Date.now(), rendererErrors=[] const scheduling={backgroundThrottling:false,show:true,deadlineMs:20000} function mark(value){phase=value;console.log(JSON.stringify({phase,elapsedMs:Date.now()-started}))} function finish(error,report){ if(finished)return;finished=true;clearTimeout(timer) const result={result:error?'FAIL':'PASS',error:error?String(error):undefined,report,phase,elapsedMs:Date.now()-started,scheduling,rendererErrors,limits:'Synthetic own-component Electron fixture, not the installed application or original reference UI.'} fs.writeFileSync(path.join(fixture,'native-report.json'),JSON.stringify(result,null,2)) console.log(JSON.stringify({result:result.result,error:result.error,output:path.join(fixture,'native-report.json')})) if(win&&!win.isDestroyed())win.destroy() app.exit(error?1:0) } // Bound startup as well as page loading and real rAF completion. timer=setTimeout(()=>finish(Error('Bounded native probe timed out after 20s; phase='+phase)),20000) app.whenReady().then(async()=>{ if(finished)return mark('creating-window') session.defaultSession.webRequest.onBeforeRequest((details,callback)=>callback({cancel:!['file:','data:','blob:','devtools:'].some(p=>details.url.startsWith(p))})) win=new BrowserWindow({width:1000,height:800,show:true,webPreferences:{nodeIntegration:false,contextIsolation:true,sandbox:true,backgroundThrottling:false}}) win.webContents.on('render-process-gone',(_,details)=>finish(Error('Renderer gone: '+details.reason))) win.webContents.on('console-message',event=>{if(event.level==='error'||event.level>=3)rendererErrors.push(event.message)}) win.webContents.on('did-fail-load',(_,code,description)=>finish(Error('Page load failed: '+code+' '+description))) mark('loading-fixture') await win.loadFile(path.join(fixture,'index.html')) if(finished)return mark('waiting-for-native-frames') const report=await win.webContents.executeJavaScript(`new Promise((resolve,reject)=>{const check=()=>{if(window.__b04Error)return reject(Error(window.__b04Error));if(window.__b04Report)return resolve(window.__b04Report);setTimeout(check,25)};check()})`) if(finished)return mark('checking-native-report') if(rendererErrors.length)throw Error('Renderer console errors: '+rendererErrors.join('; ')) if(!report.samples.some(s=>s.height>0))throw Error('No nonzero native layout measured') if(report.samples.some(s=>s.textLength>1000||s.height>2000))throw Error('Unexpected address-sized text/height spike') finish(null,report) }).catch(error=>finish(error))