Files
order-notification-automation/tests/app/backend-refresh-performance.test.cjs

49 lines
3.8 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs');
const {OrderExecutionCoordinator}=require('../../src/modules/order-execution/coordinator.cjs');
const {OrderRuleDispatcher}=require('../../src/modules/order-rules/dispatcher.cjs');
test('health reads execution progress without field evidence, candidates, scans or provider probes',t=>{
const stateDir=fs.mkdtempSync(path.join(os.tmpdir(),'lean-health-'));
const tasks=[
{id:'blocked',connectionId:'mine',row:{processName:'Current process'},status:'blocked',orderId:'o1',error:'Capture blocked',updatedAt:'2026-10-27T01:00:00Z'},
{id:'complete',connectionId:'mine',row:{processName:'Completed process'},status:'archived',orderId:'o2',reason:'Archived progress',updatedAt:'2026-10-27T02:00:00Z'},
{id:'private',connectionId:'other',row:{processName:'PRIVATE_OTHER'},status:'failed',updatedAt:'2026-10-27T03:00:00Z'}
];
const execution=Object.create(OrderExecutionCoordinator.prototype);
execution.store={capabilities:{liveTodo:true,pdfExport:false,automaticArchive:true},
all(kind){assert.equal(kind,'execution-tasks','health must only enumerate execution tasks');return structuredClone(tasks);},
get(kind,id){assert.equal(kind,'orders');return {status:id==='o1'?'pending':'sent',error:'Order detail'};},
fields(){assert.fail('health must not decrypt captured fields');}
};
execution.snapshot=()=>assert.fail('full execution snapshot is not a health read');
execution.provider={capabilities:()=>assert.fail('must not probe')};
const health=new RuntimeHealth({stateDir,execution,workspace:{snapshot:()=>({connections:[{id:'mine',owner:'worker'},{id:'other',owner:'other'}]})}});
t.after(()=>{health.close();fs.rmSync(stateDir,{recursive:true,force:true});});
const group=health.snapshot({username:'worker',role:'operator'}).groups.find(group=>group.key==='execution');
assert.equal(group.status,'error');
assert.deepEqual(group.items.filter(item=>!item.global).map(item=>[item.id,item.status,item.detail]),[['complete','healthy','Order detail'],['blocked','error','Capture blocked']]);
assert.doesNotMatch(JSON.stringify(group),/PRIVATE_OTHER/);
const first=execution.healthSnapshot();first.capabilities.liveTodo=false;first.tasks.find(task=>task.id==='complete').status='unknown';
assert.equal(execution.healthSnapshot().capabilities.liveTodo,true);
assert.equal(execution.healthSnapshot().tasks.find(task=>task.id==='complete').status,'sent');
});
test('paused dispatcher expires prepared automatic queues without assembling rules or execution snapshots',async()=>{
const now=new Date('2026-10-27T04:00:00Z'),finished=[],batches=[
{id:'expired',status:'prepared',cutoff:'2026-10-27T03:00:00Z'},
{id:'boundary',status:'prepared',cutoff:'2026-10-27T03:30:00Z'},
{id:'manual',status:'prepared',manual:true,cutoff:'2026-10-27T03:00:00Z'},
{id:'unknown',status:'unknown',cutoff:'2026-10-27T03:00:00Z'}
];let intakeChecks=0;
const dispatcher=new OrderRuleDispatcher({clock:()=>now,store:{
capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true},settings:()=>({enabled:false}),
expireIntakes(){intakeChecks++;},all(kind){assert.equal(kind,'batches');return structuredClone(batches);},
snapshot(){assert.fail('polling must not assemble a full rule snapshot');},finishSending(...args){finished.push(args);}
},mail:{exclusive:()=>assert.fail('paused dispatcher must not send')},providersReady:()=>true});
await dispatcher.tick();
assert.equal(intakeChecks,1);assert.equal(finished.length,1);assert.equal(finished[0][0],'expired');assert.equal(finished[0][1],'failed');
assert.deepEqual(finished[0][3],{outcome:'not_submitted',phase:'queue_expired',at:'2026-10-27T04:00:00.000Z'});
});