Files
order-notification-automation/tests/health-http-ui.test.cjs
T

24 lines
3.7 KiB
JavaScript

'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path');
async function fixture(t){
const {Store}=require('../src/dashboard-store.cjs'),{RuntimeHealth}=require('../src/runtime-health.cjs'),{createDashboard}=require('../src/dashboard.cjs');
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'health-http-')),store=new Store(path.join(dir,'dashboard'));store.bootstrap('admin','Health-test-password-123');
const health=new RuntimeHealth({stateDir:dir,workspace:{snapshot:()=>({connections:[{id:'c',owner:'admin',name:'<img src=x onerror=alert(1)>',status:'connected',checkedAt:new Date().toISOString(),lastConnectedAt:new Date().toISOString()}]})}}),watchdog={token:'a'.repeat(64),instanceId:'health-fixture',startedAt:new Date().toISOString()};
const server=createDashboard({store,health,watchdog,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{server.closeAllConnections();await new Promise(r=>server.close(r));health.close();await fs.rm(dir,{recursive:true,force:true});});
const base='http://127.0.0.1:'+server.address().port;return {base};
}
test('health API is authenticated and watchdog requires a secret token and exact instance',async t=>{
const {base}=await fixture(t);assert.equal((await fetch(base+'/api/health')).status,401);assert.equal((await fetch(base+'/_internal/health')).status,404);
const ping=await fetch(base+'/_internal/health',{headers:{'x-order-watchdog':'a'.repeat(64)}});assert.equal(ping.status,200);assert.equal((await ping.json()).instanceId,'health-fixture');
const login=await fetch(base+'/api/login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:JSON.stringify({username:'admin',password:'Health-test-password-123'})}),cookie=login.headers.get('set-cookie').split(';')[0];
const response=await fetch(base+'/api/health',{headers:{Cookie:cookie}});assert.equal(response.status,200);assert.doesNotMatch(await response.text(),/a{64}|password|health-fixture/);
});
test('health page shows historical checks, escapes labels, and recovers from an offline refresh',{timeout:60000},async t=>{
const {base}=await fixture(t),{loadPlaywright,browserOptions}=require('../src/runtime.cjs'),browser=await loadPlaywright().chromium.launch(browserOptions());t.after(()=>browser.close());const page=await browser.newPage({viewport:{width:1280,height:900}}),errors=[];page.on('pageerror',e=>errors.push(e.message));
await page.goto(base+'/#health');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Health-test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();
await page.getByRole('heading',{name:'系统健康',exact:true}).waitFor();await page.getByText('上次检查结果,非实时连通性检测',{exact:true}).waitFor();assert.equal(await page.locator('#health-groups img').count(),0);
await page.route('**/api/health',route=>route.abort());await page.getByRole('button',{name:'刷新健康状态',exact:true}).click();await page.getByText('暂时无法连接服务,保留上次状态;恢复后会自动更新。',{exact:true}).waitFor();
await page.unroute('**/api/health');await page.getByRole('button',{name:'刷新健康状态',exact:true}).click();await page.getByText('健康状态已更新',{exact:true}).waitFor();
await fs.mkdir('output/verification',{recursive:true});await page.screenshot({path:'output/verification/service-health-desktop.png',fullPage:true});await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(errors,[]);
});