Files
order-notification-automation/tests/app/runtime-health.test.cjs
T

23 lines
4.2 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');
test('health keeps success/failure history across restart, filters BPM ownership, and never checks external systems',async t=>{
const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs'),stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'order-health-'));let health;
t.after(async()=>{health?.close();await fs.rm(stateDir,{recursive:true,force:true});});
const connections=[{id:'mine',name:'我的连接',owner:'worker',status:'connected',checkedAt:'2026-10-18T01:00:00Z',lastConnectedAt:'2026-10-18T01:00:00Z',password:'PRIVATE'},{id:'other',name:'PRIVATE_OTHER',owner:'other',status:'connected'}];
const deps={stateDir,workspace:{snapshot:()=>({connections})},scanner:{store:{status:()=>({config:{enabled:false},runs:[{connectionId:'mine',status:'error',finishedAt:'2026-10-18T02:00:00Z'}]})},scan:()=>assert.fail('must not scan')},storage:{store:{servers:()=>[{id:'s',name:'共享',enabled:true,state:{checkedAt:'2026-10-18T01:00:00Z',readable:true,writable:true,message:'password=PRIVATE C:/PRIVATE/file'}}]},check:()=>assert.fail('must not write/check')},mail:{store:{channels:()=>[]},verify:()=>assert.fail('must not verify/send')}};
health=new RuntimeHealth(deps);let data=health.snapshot({username:'worker',role:'operator'});assert.doesNotMatch(JSON.stringify(data),/PRIVATE|OTHER/);assert.equal(data.groups.find(g=>g.key==='bpm').items.length,1);assert.equal(data.groups.find(g=>g.key==='mail').status,'unconfigured');
connections[0].status='error';connections[0].checkedAt='2026-10-18T03:00:00Z';data=health.snapshot({username:'worker',role:'operator'});const item=data.groups.find(g=>g.key==='bpm').items[0];assert.equal(item.status,'error');assert.equal(item.lastSuccessAt,'2026-10-18T01:00:00.000Z');assert.equal(item.lastFailureAt,'2026-10-18T03:00:00.000Z');
health.close();health=new RuntimeHealth(deps);assert.equal(health.snapshot({username:'worker',role:'operator'}).groups.find(g=>g.key==='bpm').items[0].lastSuccessAt,item.lastSuccessAt);
assert.equal(health.snapshot({username:'admin',role:'admin'}).groups.find(g=>g.key==='bpm').items.length,2);
});
test('a damaged module is reported unknown without hiding other modules or claiming real-time connectivity',async t=>{
const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs'),stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'order-health-errors-'));const health=new RuntimeHealth({stateDir,workspace:{snapshot:()=>({connections:[]})},storage:{store:{servers:()=>{throw Error('PRIVATE failure');}}},mail:{store:{channels:()=>[{id:'m',name:'邮箱',enabled:false,state:{checkedAt:null,smtp:false,imap:false}}]}}});t.after(async()=>{health.close();await fs.rm(stateDir,{recursive:true,force:true});});
const data=health.snapshot({role:'admin'});assert.equal(data.groups.find(g=>g.key==='storage').status,'unknown');assert.equal(data.groups.find(g=>g.key==='mail').items[0].status,'pending');assert.equal(data.runtime.managed,false);assert.doesNotMatch(JSON.stringify(data),/PRIVATE/);
});
test('execution health reports saved blockers and filters task ownership without probing providers',async t=>{
const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs'),stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'order-health-execution-'));
const health=new RuntimeHealth({stateDir,workspace:{snapshot:()=>({connections:[{id:'mine',owner:'worker'},{id:'other',owner:'other'}]})},execution:{snapshot:()=>({capabilities:{liveTodo:true,pdfExport:false,automaticArchive:false},tasks:[{id:'a',connectionId:'mine',processName:'目标流程',status:'blocked',error:'缺少订单字段',updatedAt:'2026-10-19T01:00:00Z'},{id:'b',connectionId:'other',processName:'PRIVATE_OTHER',status:'failed'}]}),refreshCapabilities:()=>assert.fail('health must not probe')}});t.after(async()=>{health.close();await fs.rm(stateDir,{recursive:true,force:true});});
const group=health.snapshot({username:'worker',role:'operator'}).groups.find(g=>g.key==='execution');assert.ok(group);assert.equal(group.status,'error');assert.doesNotMatch(JSON.stringify(group),/PRIVATE_OTHER/);assert.ok(group.items.some(i=>i.detail.includes('缺少订单字段')));
});