57 lines
7.0 KiB
JavaScript
57 lines
7.0 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 {WorkspaceStore}=require('../../src/modules/workspace/store.cjs');
|
|
const {BpmScanStore}=require('../../src/modules/bpm/scan-store.cjs');
|
|
const {BpmScanner}=require('../../src/modules/bpm/scanner.cjs');
|
|
const {BpmArrivalMonitor}=require('../../src/modules/bpm/arrival-monitor.cjs');
|
|
async function until(predicate){const end=Date.now()+1500;while(!predicate()){if(Date.now()>end)assert.fail('expected monitor transition');await new Promise(resolve=>setTimeout(resolve,5));}}
|
|
function fixture(t,{interval=1440,onTodoReconciled}={}){
|
|
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'arrival-recovery-')),workspace=new WorkspaceStore(path.join(dir,'workspace')),scanStore=new BpmScanStore(path.join(dir,'scan'));
|
|
let scanner,monitor;t.after(async()=>{await monitor?.stop();await scanner?.stop();scanStore.close();workspace.close();fs.rmSync(dir,{recursive:true,force:true});});
|
|
const admin={username:'admin',role:'admin'},connection=workspace.createConnection({name:'合成连接',owner:'admin'},admin,[admin]);
|
|
let now=Date.parse('2026-10-21T01:00:00Z'),todoCalls=0,informCalls=0,failed=false,callbacks,hold,informHold;
|
|
workspace.updateConnectionSession(connection.id,{status:'connected',identity:'synthetic',checkedAt:new Date(now).toISOString(),checks:{portal:{ok:true,message:'synthetic'}}},admin);
|
|
scanStore.saveConfig({enabled:true,intervalMinutes:interval});
|
|
const row={source:'todo',portalSource:'portal-v4',taskId:'T',instanceId:'I',revision:1,processName:'合成订单',nodeName:'通知',processVersion:'1',serialNum:'S',title:'订单号:TEST',receivedAt:'2026-10-21T00:30:00Z'};
|
|
const manager={sessions:new Map(),epoch:()=>0,ensure:async()=>({status:'connected'}),run:async(_,fn)=>fn(),authorize(){},vault:()=>({read:async()=>({storageState:{cookies:[],origins:[]}})}),scanArrivals:async()=>{informCalls++;if(informHold)await informHold;return [];}};
|
|
const provider={scan:async()=>{todoCalls++;if(hold)await hold;if(failed)throw Object.assign(Error('synthetic network failure'),{code:'ETIMEDOUT'});return [structuredClone(row)];}};
|
|
scanner=new BpmScanner({workspace,store:scanStore,manager,settings:()=>({schedule:{weekends:true}})});
|
|
monitor=new BpmArrivalMonitor({workspace,manager,scanner,provider,onTodoReconciled,clock:()=>now,settings:()=>({schedule:{weekends:true}}),hubClient:{connect:async(_,options)=>{callbacks=options;return {stop:async()=>{}};}},debounceMs:1,sourceRetryMs:[100,200,1000]});
|
|
return {workspace,scanStore,connection,admin,monitor,row,get now(){return now;},get calls(){return todoCalls;},get informCalls(){return informCalls;},advance:ms=>now+=ms,fail:value=>failed=value,hint:()=>callbacks.onEvent('worklistChanged'),hintInform:()=>callbacks.onEvent('informlistChanged'),hold:value=>hold=value,holdInform:value=>informHold=value};
|
|
}
|
|
test('reducing a live reconciliation interval applies from the last completed reconciliation',async t=>{
|
|
const f=fixture(t);await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
|
f.advance(10*60000);f.scanStore.saveConfig({enabled:true,intervalMinutes:1});await f.monitor.tick();
|
|
await until(()=>f.calls===2);assert.ok(Date.parse(f.monitor.snapshot(f.connection.id).nextReconcileAt)<=f.now+60000);
|
|
});
|
|
test('failed todo sources keep an independent retry with backoff despite hint bursts',async t=>{
|
|
const f=fixture(t);await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
|
f.fail(true);f.hint();await until(()=>f.scanStore.status().runs.some(run=>run.kind==='todo'&&run.status==='error'));
|
|
let state=f.monitor.snapshot(f.connection.id);assert.equal(state.sources.todo.status,'error');assert.equal(state.sources.inform.status,'healthy');
|
|
const calls=f.calls;f.fail(false);for(let i=0;i<20;i++)f.hint();f.advance(50);await f.monitor.tick();await new Promise(resolve=>setTimeout(resolve,15));assert.equal(f.calls,calls);
|
|
f.advance(51);await f.monitor.tick();await until(()=>f.calls===calls+1&&f.monitor.snapshot(f.connection.id).sources.todo.status==='healthy');
|
|
});
|
|
test('only fully committed authorized todo reads publish trusted execution hints',async t=>{
|
|
const hints=[],f=fixture(t,{onTodoReconciled:payload=>hints.push(payload)});await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
|
assert.equal(hints.length,1);assert.equal(hints[0].connectionId,f.connection.id);assert.deepEqual(hints[0].rows,[f.row]);assert.ok(hints[0].runId);assert.ok(Date.parse(hints[0].startedAt)<=Date.parse(hints[0].finishedAt));
|
|
let release;f.hold(new Promise(resolve=>release=resolve));f.hint();await until(()=>f.calls===2);f.workspace.disableConnection(f.connection.id,f.admin);
|
|
const stopping=f.monitor.stopConnection(f.connection.id);release();await stopping;assert.equal(hints.length,1);
|
|
});
|
|
test('global automatic pause revokes the in-flight todo ticket without blocking later manual sync',async t=>{
|
|
const hints=[],f=fixture(t,{onTodoReconciled:payload=>hints.push(payload)});await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
|
let release;f.hold(new Promise(resolve=>release=resolve));f.hint();await until(()=>f.calls===2);f.scanStore.saveConfig({enabled:false,intervalMinutes:1440});const stopping=f.monitor.reconfigure();release();await stopping;
|
|
assert.equal(hints.length,1);assert.equal(f.scanStore.status().runs.filter(run=>run.kind==='todo'&&run.status==='success').length,1);
|
|
f.hold(null);await f.monitor.startTodoScan(f.connection.id,{user:f.admin,sessionId:'manual'}).promise;assert.equal(hints.length,2);
|
|
});
|
|
test('retiring the listener revokes an in-flight inform commit ticket',async t=>{
|
|
const f=fixture(t);await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
|
let release;f.holdInform(new Promise(resolve=>release=resolve));f.hintInform();await until(()=>f.informCalls===2);const stopping=f.monitor.stopConnection(f.connection.id);release();await stopping;
|
|
assert.equal(f.scanStore.status().runs.filter(run=>run.kind==='inform'&&run.status==='success').length,1);
|
|
});
|
|
test('local hint delivery retries its encrypted committed payload without re-reading BPM or changing read success',async t=>{
|
|
let attempts=0;const f=fixture(t,{onTodoReconciled:()=>{attempts++;if(attempts===1)throw Error('synthetic queue failure');}});await f.monitor.tick();await until(()=>attempts===1&&!f.monitor.entries.get(f.connection.id).sync);
|
|
const run=f.scanStore.status().runs.find(item=>item.kind==='todo');assert.equal(run.status,'success');assert.equal(f.monitor.snapshot(f.connection.id).delivery.status,'error');
|
|
const stored=f.scanStore.db.prepare('SELECT payload FROM execution_deliveries WHERE run_id=?').get(run.id);assert.ok(stored);assert.equal(Buffer.from(stored.payload).includes(Buffer.from(f.row.title)),false);
|
|
f.advance(101);await f.monitor.tick();await until(()=>attempts===2&&f.monitor.snapshot(f.connection.id).delivery.pending===0);assert.equal(f.calls,1);assert.equal(f.scanStore.status().runs.find(item=>item.id===run.id).status,'success');
|
|
});
|