// Real SQLite, Rust/Wasm, Worker and WebGL acceptance. No physical transports opened. import {mkdtemp,readFile,writeFile} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import path from 'node:path'; import http from 'node:http'; import net from 'node:net'; import {spawn} from 'node:child_process'; import {createRequire} from 'node:module'; import assert from 'node:assert/strict'; const {chromium}=createRequire(import.meta.url)('playwright'); const root=await mkdtemp(path.join(tmpdir(),'RID-menu-status-')); const exe=process.argv[2]||'D:/Rust_Contact/util/target/Rid_Drone_Receivev/release/fpv-rid-registry-fullstack.exe'; const probe=net.createServer();await new Promise(r=>probe.listen(0,'127.0.0.1',r));const backend=probe.address().port;await new Promise(r=>probe.close(r)); let base,child,browser,output='';const errors=[],csp=[],sockets=new Set(); const proxy=http.createServer((req,res)=>{const headers={...req.headers,host:'127.0.0.1:8787'};if(headers.origin===base)headers.origin='http://127.0.0.1:8787';const upstream=http.request({hostname:'127.0.0.1',port:backend,path:req.url,method:req.method,headers},r=>{res.writeHead(r.statusCode,r.headers);r.pipe(res)});upstream.on('error',()=>{res.writeHead(502);res.end()});req.pipe(upstream)}); proxy.on('upgrade',(req,socket,head)=>{const upstream=net.connect(backend,'127.0.0.1');sockets.add(socket);sockets.add(upstream);upstream.on('connect',()=>{const headers={...req.headers,host:'127.0.0.1:8787'};if(headers.origin===base)headers.origin='http://127.0.0.1:8787';const nl=String.fromCharCode(13,10);upstream.write(['GET '+req.url+' HTTP/1.1',...Object.entries(headers).map(([k,v])=>k+': '+v),'',''].join(nl));if(head.length)upstream.write(head);socket.pipe(upstream);upstream.pipe(socket)});upstream.on('error',()=>socket.destroy());socket.on('error',()=>upstream.destroy());socket.on('close',()=>{upstream.destroy();sockets.delete(socket);sockets.delete(upstream)})}); const wait=ms=>new Promise(r=>setTimeout(r,ms)); function start(){child=spawn(exe,['--data-root',root,'--simulation-api'],{env:{...process.env,RID_TEST_PORT:String(backend),PATH:'C:\\Windows\\System32'},windowsHide:true,stdio:['ignore','pipe','pipe']});child.stdout.on('data',b=>output+=b);child.stderr.on('data',b=>output+=b);child.on('error',e=>output+=e.message)} async function stop(){if(child&&child.exitCode===null){const done=new Promise(r=>child.once('exit',r));child.kill();await done}} async function ready(){for(let i=0;i<150;i++){if(child.exitCode!==null)throw Error(output);try{if((await fetch(base+'/api/target-lists')).ok)return}catch{}await wait(100)}throw Error('server timeout '+output)} async function snapshot(){return(await fetch(base+'/api/target-lists')).json()} async function save(id,kind,note='验收备注'){const current=await snapshot();const r=await fetch(base+'/api/target-lists/'+id,{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify({list_kind:kind,note,expected_revision:current.revision})});assert.equal(r.status,200);return r.json()} let report={}; try{ await new Promise(r=>proxy.listen(0,'127.0.0.1',r));base='http://127.0.0.1:'+proxy.address().port;start();await ready(); for(const asset of ['workspace-native.css','index.html']){const response=await fetch(base+'/'+(asset==='index.html'?'realtime':asset));assert.equal(await response.text(),await readFile(path.join('dist/web-ui',asset),'utf8'));} await save('MENU-BLACK','black');await save('MENU-WHITE','white');const before=await snapshot(); browser=await chromium.launch({headless:true,channel:'msedge'});const context=await browser.newContext({viewport:{width:1600,height:1000}});const page=await context.newPage();page.on('pageerror',e=>errors.push(e.message));page.on('console',m=>{if(/violates.*Content Security|Refused.*policy/i.test(m.text()))csp.push(m.text())}); await page.goto(base+'/realtime');await page.waitForFunction(()=>document.querySelector('.rt-status')?.textContent.includes('已连接'));await page.waitForFunction(()=>!document.querySelector('#rt-list-manager-from-directory')?.disabled);await wait(300); const nav=page.getByRole('navigation',{name:'主菜单'});assert.equal(await nav.locator('button:visible').count(),1);assert.equal(await nav.locator('button:visible').getAttribute('aria-label'),'黑名单管理'); for(const name of ['应用首页','应用总览','设备登记','实时目标工作台','壁纸与外观'])assert.equal(await nav.getByRole('button',{name,exact:true}).isVisible(),false); await page.getByRole('button',{name:'黑名单管理',exact:true}).click();await page.locator('#rt-list-manager').waitFor();await page.getByRole('heading',{name:'黑名单管理',exact:true}).waitFor(); const managerTabs=page.getByRole('group',{name:'管理名单分类'});assert.equal(await managerTabs.getByRole('button',{name:/^黑名单/}).getAttribute('aria-pressed'),'true');await page.locator('.rt-list-row[data-target-id="MENU-BLACK"]').waitFor();assert.equal(await page.locator('.rt-list-row[data-target-id="MENU-WHITE"]').count(),0);assert.equal(await page.locator('#rt-list-entry-kind').inputValue(),'black'); await page.keyboard.press('Escape');await page.locator('#rt-list-manager').waitFor({state:'hidden'});await page.waitForFunction(()=>document.activeElement.id==='rt-blacklist-menu');assert.equal(await page.locator('#rt-blacklist-menu').getAttribute('aria-expanded'),'false'); await page.getByRole('button',{name:'黑名单管理',exact:true}).press('Enter');await page.locator('#rt-list-manager').waitFor();await page.getByRole('button',{name:'关闭名单管理'}).click();await page.locator('#rt-list-manager').waitFor({state:'hidden'}); async function corner(){const result=await page.evaluate(()=>{const root=document.getElementById('rt-layout'),e=root.querySelector('.mc-map-infobar'),r=root.getBoundingClientRect(),b=e.getBoundingClientRect(),scale=r.width/root.clientWidth;return {right:(r.right-b.right)/scale,bottom:(r.bottom-b.bottom)/scale,z:Number(getComputedStyle(e).zIndex),pointer:getComputedStyle(e).pointerEvents,rootOverflow:document.documentElement.scrollWidth>innerWidth}});assert.ok(Math.abs(result.right-16)<1,JSON.stringify(result));assert.ok(Math.abs(result.bottom-16)<1,JSON.stringify(result));assert.ok(result.z>10);assert.equal(result.pointer,'none');assert.equal(result.rootOverflow,false);return result;} await corner();await page.getByRole('button',{name:'隐藏事件面板',exact:true}).click();await page.locator('#rt-dock-restore').waitFor();await corner();await page.getByRole('button',{name:'收起目标目录',exact:true}).click();await page.getByRole('button',{name:'收起详情',exact:true}).click();await corner();await page.screenshot({path:path.join(root,'desktop-collapsed.png')}); await page.locator('#rt-dock-restore').click();await page.locator('#rt-dock').waitFor();await corner();const overDock=await page.evaluate(()=>{const info=document.querySelector('.mc-map-infobar').getBoundingClientRect(),dock=document.querySelector('#rt-dock').getBoundingClientRect();return info.left>=dock.left&&info.right<=dock.right&&info.top>=dock.top&&info.bottom<=dock.bottom});assert.ok(overDock,'Status must overlay the dock rather than avoiding it'); await page.getByRole('button',{name:'隐藏事件面板',exact:true}).click();await page.locator('#rt-dock-restore').waitFor();await page.locator('#toggle-menu').click();await corner();assert.equal(await nav.isVisible(),false);await page.locator('#toggle-menu').click();assert.equal(await nav.locator('button:visible').count(),1); const frames=[];for(const[width,height]of [[1600,1000],[1280,720],[390,844],[360,740]]){await page.setViewportSize({width,height});await wait(300);frames.push({width,height,...await corner()});assert.equal(await nav.locator('button:visible').count(),1);await page.screenshot({path:path.join(root,`menu-status-${width}.png`)});} await page.setViewportSize({width:1600,height:1000});await wait(300);await page.getByRole('button',{name:'返回应用总览',exact:true}).click();await page.locator('body[data-screen=home]').waitFor();assert.equal(await page.locator('#rt-blacklist-menu').isVisible(),false);await page.locator('#home-app-register').click();await page.locator('body[data-screen=register]').waitFor();assert.equal(await page.locator('.reference-rail [data-open=register]').isVisible(),true);assert.equal(await page.locator('#rt-blacklist-menu').isVisible(),false); await page.getByRole('button',{name:'返回应用总览',exact:true}).click();await page.locator('#home-app-realtime').click();await page.waitForFunction(()=>document.querySelector('.rt-status')?.textContent.includes('已连接'));assert.equal(await nav.locator('button:visible').count(),1);await corner(); assert.deepEqual(await snapshot(),before);assert.deepEqual(errors,[]);assert.deepEqual(csp,[]);report={status:'PASS',root,exe,frames,checks:['one-blacklist-menu','blacklist-filter-and-default-kind','keyboard-open-and-return-focus','fixed-bottom-right-dock-and-panels','overlays-dock','menu-collapse','four-window-sizes','other-app-navigation-preserved','list-data-unchanged'],errors,csp};console.log(JSON.stringify(report)); }finally{if(browser){if(report.status!=='PASS'){const p=browser.contexts()[0]?.pages()[0];if(p){await p.screenshot({path:path.join(root,'failure.png')}).catch(()=>{});console.log(JSON.stringify({root,errors,csp,diagnostic:await p.evaluate(()=>({screen:document.body.dataset.screen,menu:[...document.querySelectorAll('.reference-rail button')].map(e=>({id:e.id,label:e.getAttribute('aria-label'),height:e.getBoundingClientRect().height})),manager:document.getElementById('rt-list-manager')?.hidden})).catch(()=>null)}));}}await browser.close();}if(child&&child.exitCode===null){const done=new Promise(r=>child.once('exit',r));child.kill();await done;}for(const socket of sockets)socket.destroy();const closed=new Promise(r=>proxy.close(r));proxy.closeAllConnections();await closed;await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));await writeFile(path.join(root,'server.log'),output);}