Files
Agent-Workbench/scripts/verify-embedded-browser.mjs
T

99 lines
20 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID, createHash } from 'node:crypto';
import { _electron, expect } from 'playwright/test';
import { fixture, until } from '../tests/helpers.mjs';
import { previewProject, freePreviewPort } from '../tests/fixtures/preview-project.mjs';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
import { projectEnvironment } from '../src/core/process-environment.mjs';
import { execFileSync } from 'node:child_process';
import { verifyBrowsingMobile } from './verify-browsing-mobile.mjs';
if(process.argv.includes('--help')) { console.log('node scripts/verify-embedded-browser.mjs [--app-root <absolute package>] [--native-review] — actual Electron + owned React/Vite + 390px mobile original observation; no real model or production state.'); process.exit(0); }
const options=process.argv.slice(2).filter(value=>value!=='--native-review');
assert.ok(options.length===0||options.length===2&&options[0]==='--app-root'&&path.isAbsolute(options[1]));
const appRoot=fs.realpathSync.native(options[1]??process.cwd()),root=path.resolve('.qa','embedded-browser-'+randomUUID());fs.mkdirSync(root,{recursive:true});
const nodePath=options[1]?path.join(appRoot,'runtime/node.exe'):process.execPath;assert.ok(fs.existsSync(nodePath));
const hash=bytes=>createHash('sha256').update(bytes).digest('hex');
const files=(folder,relative='')=>fs.readdirSync(path.join(folder,relative),{withFileTypes:true}).sort((a,b)=>a.name.localeCompare(b.name)).flatMap(entry=>entry.isDirectory()?files(folder,path.join(relative,entry.name)):[path.join(relative,entry.name)]);
const inputNames=['dist-client','dist-server','dist-desktop',...['src','tests','scripts'].filter(folder=>fs.existsSync(path.join(appRoot,folder)))].flatMap(folder=>files(path.join(appRoot,folder)).map(name=>path.join(folder,name))).concat(['package.json','package-lock.json','MANIFEST.json'].filter(name=>fs.existsSync(path.join(appRoot,name))));
const inputSnapshot=()=>inputNames.map(name=>({path:name.replaceAll('\\','/'),bytes:fs.statSync(path.join(appRoot,name)).size,sha256:hash(fs.readFileSync(path.join(appRoot,name)))}));
const inputs=inputSnapshot();let sourceCommit=null,verifierSourceState='archive-without-git';try{sourceCommit=execFileSync('git',['rev-parse','HEAD'],{encoding:'utf8',windowsHide:true}).trim();verifierSourceState=execFileSync('git',['status','--porcelain','--','src','scripts','tests','package.json','package-lock.json'],{encoding:'utf8',windowsHide:true}).trim()?'modified-source-with-exact-input-manifest':'clean';}catch{}
fs.writeFileSync(path.join(root,'inputs.json'),JSON.stringify({sourceCommit,verifierSourceState,appRoot,inputs},null,2));
const cleanups=[],f=fixture({after:callback=>cleanups.push(callback)}),p=await previewProject(f);
fs.writeFileSync(path.join(p.root,'index.html'),'<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><title>所属React真实预览</title></head><body><div id="root"></div><script type="module" src="/src.jsx"></script></body></html>');
const source=`import React,{useState} from 'react';import{createRoot}from'react-dom/client';function App(){const[name,setName]=useState(''),[saved,setSaved]=useState('尚未保存');return <main style={{padding:32,fontFamily:'sans-serif'}}><h1>真实React工程预览</h1><label>项目名称<input aria-label="项目名称" value={name} onChange={e=>setName(e.target.value)}/></label><button onClick={()=>setSaved('已保存:'+name)}>保存计划</button><p role="status">{saved}</p><a href="/next.html">下一页</a><button onClick={()=>window.open('https://example.com/')}>尝试新窗口</button><input aria-label="密码" type="password"/><a href="/download.txt" download>原下载链接</a></main>}createRoot(document.getElementById('root')).render(<App/>);`;
fs.writeFileSync(path.join(p.root,'src.jsx'),source);fs.writeFileSync(path.join(p.root,'next.html'),'<!doctype html><meta charset="UTF-8"><h1>所属第二页</h1><a href="/">返回</a>');
fs.writeFileSync(path.join(p.root,'download.txt'),'原下载字节\r\nowned-download\r\n');
p.session.title='内置浏览器真实验收';f.store.save(p.session);f.store.close();
const port=await freePreviewPort(),controller=new HostController({appRoot,dataRoot:f.data,port,nodePath,timeoutMs:60000});
const results=[],errors=[];let application,page,nativeId,stage='host startup',previewJob,version=null;
const record=(name,detail={})=>{results.push({name,passed:true,...detail});console.log(name+': passed');};
async function nativeReview(label){if(!process.argv.includes('--native-review'))return;const pending=path.join(root,'native-review-'+label+'.json'),release=path.join(root,'native-continue-'+label);fs.writeFileSync(pending,JSON.stringify({label,pid:await application.evaluate(()=>process.pid),root}));console.log('NATIVE_REVIEW '+pending);while(!fs.existsSync(release))await new Promise(resolve=>setTimeout(resolve,300));}
const api=async(action,input)=>{const response=await fetch(controller.url+'/api/sessions/'+p.session.id+'/'+action,{method:input===undefined?'GET':'POST',headers:{'X-Workbench-Token':controller.token,'Content-Type':'application/json'},body:input===undefined?undefined:JSON.stringify(input),signal:AbortSignal.timeout(45000)});const value=await response.json();if(!response.ok)throw Object.assign(new Error('HTTP '+response.status+': '+value.error),{status:response.status});return value;};
const state=()=>controller.api('/sessions/'+p.session.id);
async function capture(name){
const native=await application.browserWindow(page),png=await native.evaluate(async win=>(await win.capturePage()).toPNG().toString('base64'));
const filename=path.join(root,name+'.png');fs.writeFileSync(filename,Buffer.from(png,'base64'));fs.writeFileSync(path.join(root,name+'.html'),await page.content());
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);return filename;
}
async function site(code){return application.evaluate(async({webContents},{id,code})=>{const contents=webContents.fromId(id);if(!contents)throw new Error('Original native contents ended');return contents.executeJavaScript(code);},{id:nativeId,code});}
async function nativeVisible(){return application.evaluate(({BrowserWindow},id)=>BrowserWindow.getAllWindows()[0].contentView.children.some(view=>view.webContents?.id===id&&view.getVisible()),nativeId);}
async function observe(){await api('browsing/tabs/'+(await currentTab()).id+'/observe',{});return currentObservation();}
async function currentTab(){return(await api('browsing')).tabs.filter(tab=>tab.status==='open').at(-1);}
async function currentObservation(){const snapshot=await api('browsing'),tab=snapshot.tabs.filter(tab=>tab.status==='open').at(-1);return snapshot.observations.find(value=>value.id===tab?.observationId);}
async function act(input){const tab=await currentTab(),observation=await currentObservation();return api('browsing/tabs/'+tab.id+'/act',{...input,observationId:observation.id,revision:observation.revision,approved:true});}
function save(extra){const receipt={schemaVersion:1,kind:'actual-electron-owned-preview',root,appRoot,version,nodePath,nodeSha256:hash(fs.readFileSync(nodePath)),stage,results,checks:results,errors,realModelRequests:0,sourceCommit,verifierSourceState,inputManifestSha256:hash(fs.readFileSync(path.join(root,'inputs.json'))),screenshots:'BrowserWindow images show the main renderer only. Independent native WebContents requires native OS screenshots or the original observation PNG.',...extra};fs.writeFileSync(path.join(root,'result.json'),JSON.stringify(receipt,null,2));fs.writeFileSync('.qa/embedded-browser-latest.json',JSON.stringify(receipt,null,2));}
try{
const host=await controller.start(),executable=desktopExecutableIdentity(appRoot).path,packaged=executable.toLowerCase().endsWith('agentworkbench.exe');version=(await controller.health()).version;
application=await _electron.launch({executablePath:executable,args:[...(packaged?[]:[path.join(appRoot,'dist-desktop/main.cjs')]),'--app-root',appRoot,'--data-root',f.data,'--node',nodePath,'--port',String(port),'--hidden-window'],cwd:appRoot,env:projectEnvironment(),timeout:60000});
page=await application.firstWindow();page.on('pageerror',error=>errors.push(error.message));await page.locator('.connection.connected').waitFor({timeout:30000});
const native=await application.browserWindow(page);await native.evaluate(win=>{win.showInactive();win.setContentSize(1440,1000);});
await page.getByRole('combobox',{name:'当前项目',exact:true}).selectOption(p.project.id);await page.getByRole('button',{name:'查看任务记录',exact:true}).click();
stage='owned Vite UI plan';const service=page.getByRole('region',{name:'项目开发预览'});await service.getByRole('spinbutton',{name:'预览端口'}).fill(String(p.port));await service.getByRole('button',{name:'创建启动计划',exact:true}).click();
await service.getByRole('button',{name:'核对并批准预览',exact:true}).click();await expect(page.getByRole('dialog',{name:'确认项目预览'})).toContainText('Vite 配置');await capture('desktop-preview-approval');
await page.getByRole('button',{name:'确认启动预览',exact:true}).click();await until(async()=>{previewJob=(await state()).execution.previewJobs.at(-1);return previewJob&&!['queued','starting'].includes(previewJob.status);},40000);assert.equal(previewJob.status,'ready',JSON.stringify(previewJob));
record('actual owned React/Vite UI approval, OS listener identity and HTTP readiness',{hostPid:host.identity.pid,previewPid:previewJob.process.pid});
await service.getByRole('button',{name:'打开浏览区',exact:true}).click();await page.getByRole('button',{name:'打开目标',exact:true}).click();await expect(page.getByLabel('浏览目标地址')).toHaveValue(previewJob.url);await page.getByRole('button',{name:'确认授权并打开',exact:true}).click();
await expect(page.getByRole('dialog')).toHaveCount(0,{timeout:30000});stage='native page';await until(async()=>!!(await currentTab()),30000);
nativeId=await application.evaluate(({webContents},origin)=>webContents.getAllWebContents().find(value=>value.getURL().startsWith(origin))?.id,new URL(previewJob.url).origin);assert.ok(nativeId);
await until(()=>site("document.querySelector('h1')?.textContent==='真实React工程预览'"),20000);await until(nativeVisible,10000);
assert.deepEqual(await site("({node:typeof require,process:typeof process,bridge:typeof window.desktopWorkbench})"),{node:'undefined',process:'undefined',bridge:'undefined'});
assert.equal(await site(`fetch(${JSON.stringify(controller.url+'/api/bootstrap')}).then(()=>false,()=>true)`),true);
record('same actual native page, isolated Node/preload and blocked workbench API');
stage='resize and zoom';const separator=page.getByRole('separator',{name:'调整检视区宽度'}),separatorBox=await separator.boundingBox(),beforeWidth=await page.locator('.inspector').evaluate(node=>node.getBoundingClientRect().width);
await page.mouse.move(separatorBox.x+3,separatorBox.y+80);await page.mouse.down();await until(async()=>!await nativeVisible());await page.mouse.move(separatorBox.x-110,separatorBox.y+80);await page.mouse.up();await until(nativeVisible);
assert.ok(await page.locator('.inspector').evaluate(node=>node.getBoundingClientRect().width)>beforeWidth+90);
await separator.focus();await page.keyboard.press('ArrowLeft');await expect(separator).toHaveAttribute('aria-valuenow',String(Math.round(beforeWidth+145)));
await native.evaluate(win=>win.webContents.setZoomFactor(1.25));await until(async()=>await nativeVisible());
await until(async()=>{const box=await page.locator('.native-browser-slot').boundingBox(),bounds=await application.evaluate(({BrowserWindow},id)=>BrowserWindow.getAllWindows()[0].contentView.children.find(view=>view.webContents?.id===id)?.getBounds(),nativeId);return bounds&&Math.abs(bounds.x-Math.round(box.x*1.25))<=2&&Math.abs(bounds.width-Math.floor(box.width*1.25))<=2;});
await native.evaluate(win=>win.webContents.setZoomFactor(1));await until(nativeVisible);record('drag and keyboard width adjustment, native occlusion during drag and 125% zoom geometry');
await capture('desktop-native-browser');await nativeReview('desktop');
stage='real React input action';let observation=await observe(),input=observation.controls.find(value=>value.label==='项目名称');assert.ok(input);
await act({action:'fill',ref:input.ref,value:'专业工作台验收'});observation=await currentObservation();let button=observation.controls.find(value=>value.label==='保存计划');await act({action:'click',ref:button.ref});
assert.equal(await site("document.querySelector('[role=status]').textContent"),'已保存:专业工作台验收');record('fixed original-control fill and click operate real React controlled state');
stage='stale page';observation=await observe();await site("document.querySelector('h1').textContent='人工更改的页面'");
await assert.rejects(api('browsing/tabs/'+(await currentTab()).id+'/act',{action:'click',ref:button.ref,observationId:observation.id,revision:observation.revision,approved:true}),/409/);
assert.match((await api('browsing')).actions.at(-1).error,/变化/);
record('actual DOM mutation invalidates previous observation without submitting');
stage='HMR';fs.writeFileSync(path.join(p.root,'src.jsx'),source.replace('真实React工程预览','热更新后的React预览'));await until(()=>site("document.querySelector('h1')?.textContent==='热更新后的React预览'"),20000);await observe();record('actual source update reaches same native WebContents through Vite live reload');
stage='phone original observation';await verifyBrowsingMobile({controller,session:await state(),root,record});
stage='modal visibility';await page.getByRole('button',{name:'打开目标',exact:true}).click();await until(async()=>!await nativeVisible(),8000);await capture('native-hidden-for-modal');await page.getByRole('dialog').getByRole('button',{name:'取消',exact:true}).click();await until(nativeVisible,8000);record('native page hides for main-window authorization modal and restores');
stage='popup';let before=await application.evaluate(({webContents})=>webContents.getAllWebContents().length);await site("[...document.querySelectorAll('button')].find(value=>value.textContent==='尝试新窗口').click()");assert.equal(await application.evaluate(({webContents})=>webContents.getAllWebContents().length),before);record('unapproved popup is denied in actual Electron');
stage='navigation';observation=await observe();const link=observation.controls.find(value=>value.label==='下一页');await act({action:'click',ref:link.ref});await until(()=>site("document.querySelector('h1')?.textContent==='所属第二页'"));await act({action:'back'});await until(()=>site("document.querySelector('h1')?.textContent==='热更新后的React预览'"));await act({action:'forward'});assert.equal(await site("document.querySelector('h1').textContent"),'所属第二页');await act({action:'back'});record('actual native navigation, back and forward preserve original scoped receipts');
stage='download';await observe();await act({action:'download',url:previewJob.url+'download.txt'});const downloaded=(await api('browsing')).actions.findLast(value=>value.downloadArtifactId);assert.ok(downloaded?.downloadSha256);record('explicit exact-URL download keeps original binary artifact and SHA',{sha256:downloaded.downloadSha256});
stage='profile isolation';await site("document.cookie='awb_ephemeral=private;SameSite=Lax'");const firstTab=await currentTab(),firstNativeId=nativeId;
await api('browsing/tabs',{url:previewJob.url,previewJobId:previewJob.id,interact:true,persistent:true,approved:true});let second=await currentTab();nativeId=await application.evaluate(({webContents},{origin,first})=>webContents.getAllWebContents().find(value=>value.id!==first&&value.getURL().startsWith(origin))?.id,{origin:new URL(previewJob.url).origin,first:firstNativeId});
assert.equal(await site("document.cookie.includes('awb_ephemeral')"),false);await site("document.cookie='awb_persistent=stored;Max-Age=3600;SameSite=Lax'");await api('browsing/tabs/'+second.id+'/close',{approved:true});
await api('browsing/tabs',{url:previewJob.url,previewJobId:previewJob.id,profileId:second.profileId,interact:true,approved:true});second=await currentTab();nativeId=await application.evaluate(({webContents},{origin,first})=>webContents.getAllWebContents().find(value=>value.id!==first&&value.getURL().startsWith(origin))?.id,{origin:new URL(previewJob.url).origin,first:firstNativeId});assert.equal(await site("document.cookie.includes('awb_persistent=stored')"),true);
await api('browsing/tabs/'+second.id+'/clear',{approved:true});assert.equal(await application.evaluate(async({session},id)=>(await session.fromPartition('persist:awb-browsing-'+id).cookies.get({})).length,second.profileId),0);nativeId=firstNativeId;assert.equal(await site("document.cookie.includes('awb_ephemeral=private')"),true);record('actual independent temporary/persistent sessions, explicit reuse and selective cookie/storage/cache revocation');
stage='mobile';await native.evaluate(win=>win.setContentSize(390,844));await until(()=>page.evaluate(()=>innerWidth===390));await page.getByRole('button',{name:'查看任务记录',exact:true}).click();await page.getByRole('button',{name:'浏览',exact:true}).click();await until(nativeVisible,10000);await capture('mobile-native-browser');await nativeReview('mobile');
await page.getByRole('button',{name:'打开目标',exact:true}).click();await until(async()=>!await nativeVisible());await capture('mobile-authorize-modal');await page.getByRole('dialog').getByRole('button',{name:'取消',exact:true}).click();await page.getByRole('button',{name:'返回任务',exact:true}).click();await until(async()=>!await nativeVisible());record('390px native geometry, modal occlusion and return-to-task with no horizontal overflow');
stage='close view';await native.evaluate(win=>win.setContentSize(1440,1000));await page.getByRole('button',{name:'浏览',exact:true}).click();await until(nativeVisible);await page.getByRole('button',{name:'关闭浏览标签页',exact:true}).click();await until(async()=>!(await currentTab()));assert.equal((await state()).execution.previewJobs.at(-1).status,'ready');record('closing actual view leaves owned development service running');
stage='host lifetime';await application.close();application=null;assert.equal((await controller.health()).pid,host.identity.pid);assert.equal((await state()).execution.previewJobs.at(-1).status,'ready');await api('preview-jobs/'+previewJob.id+'/stop',{});assert.equal((await state()).execution.previewJobs.at(-1).status,'stopped');record('window close retains host and preview; explicit service stop confirms original process tree');
assert.deepEqual(errors,[]);assert.deepEqual(inputSnapshot(),inputs,'Verification inputs changed during the actual browser flow');stage='complete';save({passed:true,success:true});console.log('EVIDENCE '+root);console.log(JSON.stringify({passed:true,root,groups:results.length}));
}catch(error){if(application&&page)await capture('original-failure').catch(()=>{});if(await controller.health()){fs.writeFileSync(path.join(root,'original-session.json'),JSON.stringify(await state(),null,2));fs.writeFileSync(path.join(root,'original-browsing.json'),JSON.stringify(await api('browsing'),null,2));}save({passed:false,error:error.stack});console.error(JSON.stringify({passed:false,stage,root,error:error.message}));process.exitCode=1;}
finally{if(application)await application.close().catch(()=>{});if(await controller.health())await controller.stop();for(const cleanup of cleanups.reverse())await cleanup();}