Files
fpv-rid-workbench/scripts/test-realtime-menu-status.mjs
T
JiuContinent 281af4fe3d ```
refactor(registry): 生产环境数据库访问迁移至 SQLx 并统一测试端口策略

将 rusqlite 从生产依赖移至 dev-dependencies,正式数据库操作全部
由 SQLx 完成;rusqlite 仅保留用于测试夹具及旧数据库兼容验证。

UI 验收脚本移除 HTTP 代理和动态端口分配,统一通过
ui-test-port.mjs 的 assertUiPortAvailable 直连 8787 端口,
确保测试与正式实例串行执行。

新增 doc/registry/rust/axum-sqlx-architecture.md 记录模块职责、
数据兼容、退出资源回收及验证维护规范。

修复接收器页面按钮缺少显式 height 导致的布局偏差。
```
2026-10-27 20:04:34 +08:00

43 lines
8.5 KiB
JavaScript

import {UI_URL, assertUiPortAvailable} from './ui-test-port.mjs';
const base=UI_URL;
await assertUiPortAvailable();
// 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 {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';
let child,browser,output='';const errors=[],csp=[];
const wait=ms=>new Promise(r=>setTimeout(r,ms));
function start(){child=spawn(exe,['--data-root',root,'--simulation-api'],{env:{...process.env,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{
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);await page.locator('#rt-dock-restore').click();
const nav=page.getByRole('navigation',{name:'模块菜单'});assert.equal(await nav.locator('button:visible').count(),4);assert.equal(await nav.locator('#rt-blacklist-menu').getAttribute('aria-label'),'黑白名单管理');
assert.deepEqual(await nav.locator('button:visible').evaluateAll(es=>es.map(e=>e.getAttribute('aria-label'))),['应用总览','实时工作台','电子围栏','黑白名单管理']);
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(),1);await page.getByRole('button',{name:'新增条目',exact:true}).click();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-current'),null);
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(),4);
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(),4);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(),4);await corner();
assert.deepEqual(await snapshot(),before);assert.deepEqual(errors,[]);assert.deepEqual(csp,[]);report={status:'PASS',root,exe,frames,checks:['left-menu-full-page','both-list-kinds-visible','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;}await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));await writeFile(path.join(root,'server.log'),output);}