Files
pigway-console-adapter-fram…/tests/ui.cjs
T

28 lines
2.1 KiB
JavaScript

const {chromium}=require('playwright');
const fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict');
(async()=>{
const browser=await chromium.launch({headless:true,channel:'chrome'});
try {
const source=fs.readFileSync(path.join(__dirname,'../plugin/ui/index.js'),'utf8').replace(/^import .*;\n/gm,'').replace('export function hostCard','function hostCard');
const labels={service_ready:{zh:'服务就绪',en:'Service ready'},device_online:{zh:'画框在线',en:'Frame online'},battery_percent:{zh:'电量',en:'Battery'}};
const entry={host:{name:'Frame Flow'},dataCurrent:true,snapshot:{resources:[{id:'service',name:'Frame Flow',type:'service'},{id:'device:living-room',name:'渡口浪人的相框',type:'device'}],metrics:[{resource_id:'service',key:'service_ready',value:1,unit:'boolean',label:labels.service_ready},{resource_id:'device:living-room',key:'device_online',value:1,unit:'boolean',label:labels.device_online},{resource_id:'device:living-room',key:'battery_percent',value:28,unit:'percent',label:labels.battery_percent}],alerts:[]}};
for(const lang of ['zh','en'])for(const width of [1440,820,390]) {
const page=await browser.newPage({viewport:{width,height:1000}});
await page.goto('http://192.168.89.19/');
const result=await page.evaluate(({source,entry,lang})=>{
const state={LANG:lang};const text=(zh,en)=>lang==='zh'?zh:en;
const esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
const html=eval(source+'\nhostCard(entry)');
document.body.innerHTML=`<main style="max-width:100%;padding:16px">${html}</main>`;
return {text:document.body.innerText,overflow:document.documentElement.scrollWidth>innerWidth};
},{source,entry,lang});
assert(result.text.includes('渡口浪人的相框'));
assert(!result.text.includes('living-room'));
assert(result.text.includes(lang==='zh'?'服务就绪':'Service ready'));
assert.equal(result.overflow,false);
await page.close();
}
console.log('6 browser checks passed');
} finally {await browser.close()}
})().catch(e=>{console.error(e);process.exit(1)});