fix: reconcile failed BPM handshakes and reject revoked session writes

This commit is contained in:
abaishu0730 committed 2026-10-26 09:18:39 +08:00
1 parent 42d19c707d
commit 8d010a05cd
7 files changed
+71 -21

No files matched your search

+11 -1
View File
@@ -10,7 +10,7 @@ function fixture(t){assert.equal(typeof Monitor,'function');const dir=fs.mkdtemp
const hubClient={connect:async(_,options)=>{connects++;callbacks=options;if(hubFailure)throw Object.assign(Error('hub denied'),{status:hubFailure});return {stop:async()=>{}};}};
const monitor=new Monitor({workspace,manager,scanner,provider,hubClient,settings:()=>({schedule:{weekends:true}}),clock:()=>now,debounceMs:15,pollMs:10,retryMs:[20,40]});
t.after(async()=>{await monitor.stop();await scanner.stop();store.close();workspace.close();fs.rmSync(dir,{recursive:true,force:true});});
return {workspace,store,monitor,manager,connection,actor,get todoCalls(){return todoCalls;},get connects(){return connects;},hint(name){callbacks.onEvent(name);},disconnect(){callbacks.onClose({reason:'disconnected'});},bumpEpoch:()=>epoch++,setHubFailure:value=>hubFailure=value,setTodo:value=>todo=value,setFailure:value=>failed=value,setHold:value=>hold=value,advance:ms=>now+=ms,until};
return {workspace,store,monitor,manager,connection,actor,hubClient,get todoCalls(){return todoCalls;},get connects(){return connects;},hint(name){callbacks.onEvent(name);},disconnect(){callbacks.onClose({reason:'disconnected'});},bumpEpoch:()=>epoch++,setHubFailure:value=>hubFailure=value,setTodo:value=>todo=value,setFailure:value=>failed=value,setHold:value=>hold=value,advance:ms=>now+=ms,until};
}
test('initial and reconnected subscriptions reconcile real todo and inform sources separately, including equal-count replacement',async t=>{const f=fixture(t);f.monitor.start();await until(()=>f.store.list({kind:'todo'}).total===1&&f.store.list().total===1);assert.equal(f.monitor.snapshot(f.connection.id).status,'listening');f.setTodo([{source:'todo',portalSource:'portal-v4',taskId:'T2',instanceId:'I2',revision:1,processName:'测试订单评审',nodeName:'通知节点',serialNum:'S2',title:'订单号:TEST-2',receivedAt:'2026-10-20T00:10:00Z'}]);f.disconnect();f.advance(100);await until(()=>f.connects>=2&&f.store.list({kind:'todo'}).total===2);assert.equal(f.store.list().total,1);assert.equal(f.store.list({kind:'todo'}).groups[0].items.some(r=>r.taskId==='T2'),true);});
test('hint bursts coalesce and a hint during synchronization schedules exactly one followup',async t=>{const f=fixture(t);f.monitor.start();await until(()=>f.todoCalls===1&&f.monitor.snapshot(f.connection.id).lastSyncAt);let release;f.setHold(new Promise(r=>release=r));for(let i=0;i<20;i++)f.hint('worklistChanged');await until(()=>f.todoCalls===2);for(let i=0;i<20;i++)f.hint('worklistChanged');release();f.setHold(null);await until(()=>f.todoCalls===3);await new Promise(r=>setTimeout(r,60));assert.equal(f.todoCalls,3);});
@@ -22,6 +22,16 @@ test('company calendar pauses automatic synchronization while retaining the list
test('identity changes retire old hints and establish a fresh subscription before reconciliation',async t=>{const f=fixture(t);await f.monitor.tick();await until(()=>f.todoCalls===1&&f.monitor.snapshot(f.connection.id).lastSyncAt);f.bumpEpoch();f.hint('worklistChanged');await new Promise(r=>setTimeout(r,45));assert.equal(f.todoCalls,1);await f.monitor.tick();await f.monitor.tick();await until(()=>f.connects===2&&f.todoCalls===2);});
test('a denied push channel falls back immediately to authorized readonly lists',async t=>{const f=fixture(t);f.setHubFailure(403);f.monitor.start();await until(()=>f.store.list({kind:'todo'}).total===1);assert.equal(f.monitor.snapshot(f.connection.id).status,'polling');});
test('handshake failure plus duplicate close callbacks immediately reconciles lists once and preserves retry backoff',async t=>{
const f=fixture(t);let close;
f.hubClient.connect=async(_,options)=>{close=options.onClose;close({reason:'handshake_timeout'});close({reason:'handshake_timeout'});throw Object.assign(Error('isolated handshake timeout'),{status:502});};
await f.monitor.tick();const entry=f.monitor.entries.get(f.connection.id);await entry.connecting;
await until(()=>f.store.list({kind:'todo'}).total===1&&f.store.list().total===1);
assert.equal(entry.status,'polling');assert.equal(entry.failures,1);assert.equal(entry.nextRetry,Date.parse('2026-10-20T01:00:00Z')+20);assert.equal(f.todoCalls,1);
close({reason:'late_close'});await new Promise(r=>setTimeout(r,40));assert.equal(f.todoCalls,1);
await f.monitor.stopConnection(f.connection.id);close({reason:'late_close'});await new Promise(r=>setTimeout(r,25));assert.equal(f.todoCalls,1);
});
test('verified session recovery releases listener auth backoff without waiting five minutes',async t=>{
const f=fixture(t);f.manager.ensure=async()=>({status:'login_required'});
+4 -3
View File
@@ -138,13 +138,14 @@ test('concurrent startup never exceeds three contexts and disabling in flight cl
assert.equal(browser.contexts().length,3);
await manager.releaseSession(principal.sessionId);
assert.equal(browser.contexts().length,0);
const opening=manager.open(connection,principal);
const nextPrincipal={...principal,sessionId:'new-dashboard-session-after-logout'};
const opening=manager.open(connection,nextPrincipal);
const disabling=manager.disable('a');
await Promise.allSettled([opening,disabling]);
assert.equal(browser.contexts().length,0);
await assert.rejects(manager.open(connection,principal),e=>e.status===409);
await assert.rejects(manager.open(connection,nextPrincipal),e=>e.status===409);
await manager.shutdown();
await assert.rejects(manager.open({...connection,id:'b'},principal),e=>e.status===503);
await assert.rejects(manager.open({...connection,id:'b'},nextPrincipal),e=>e.status===503);
});
test('immediate save after remote login Enter waits for pending login navigation and verifies the portal', async t => {
@@ -47,3 +47,24 @@ test('ensure joins an ongoing recovery after its transient browser lease has bee
const b=manager.ensure(c,p);const second=Promise.allSettled([b]);release();await first;
const [outcome]=await second;assert.equal(outcome.status,'rejected');assert.equal(outcome.reason.status,503);
});
for(const revokeKind of ['permissions','logout'])for(const mode of ['save','check','ensure'])for(const stage of ['capture','storage'])test(revokeKind+' revoked '+mode+' during '+stage+' cannot persist cookies or advance identity',async t=>{
const {manager,writes}=fixture(t,async()=>result('connected'));manager.accessClient=null;
let release,started;const gate=new Promise(r=>release=r),begun=new Promise(r=>started=r),lease={id:c.id,connection:{...c},sessionId:p.sessionId,sessionStorage:{},expiresAt:Date.now()+60000,page:{url:()=>manager.urls.main},context:{storageState:async()=>{if(stage==='storage'){started();await gate;}return {cookies:[],origins:[]};},close:async()=>{}}};
manager.touch=()=>{};manager.verify=async()=>result('connected');manager.captureSessions=async()=>{if(stage==='capture'){started();await gate;}};manager.create=async()=>{manager.sessions.set(c.id,lease);return lease;};
if(mode==='save'){lease.stateTicket=manager.sessionState.begin(c.id,'manual');manager.sessions.set(c.id,lease);}
const pending=manager[mode](c,p),rejected=assert.rejects(pending,e=>[401,409].includes(e.status));await begun;
const revoked=revokeKind==='logout'?manager.releaseSession(p.sessionId):manager.releaseSessionHashes([crypto.createHash('sha256').update(p.sessionId).digest('hex')]);release();await Promise.all([revoked,rejected]);
assert.equal(writes.length,0);assert.equal(manager.epoch(c.id),0);assert.equal(manager.health.recent(c.id),false);assert.equal(manager.sessions.size,0);
manager.accessClient={check:async()=>({...result('connected'),storageState:{cookies:[],origins:[]}})};
const other={...p,sessionId:'unrelated-valid-session'};assert.equal((await manager.check(c,other)).status,'connected');assert.equal(writes.length,1);
});
test('queued revocation cleanup does not close a replacement lease owned by another valid caller',async t=>{
const {manager}=fixture(t,async()=>result('connected'));manager.accessClient=null;
let release,started;const gate=new Promise(r=>release=r),begun=new Promise(r=>started=r);manager.touch=()=>{};manager.verify=async()=>result('connected');manager.captureSessions=async()=>{started();await gate;};
manager.create=async(connection,principal)=>{const lease={id:connection.id,connection:{...connection},sessionId:principal.sessionId,sessionStorage:{},expiresAt:Date.now()+60000,page:{url:()=>manager.urls.main},context:{storageState:async()=>({cookies:[],origins:[]}),close:async()=>{}}};manager.sessions.set(connection.id,lease);return lease;};
const old=manager.check(c,p),rejected=assert.rejects(old,e=>[401,409].includes(e.status));await begun;
const other={...p,sessionId:'valid-replacement-caller'},opening=manager.open(c,other),revoked=manager.releaseSession(p.sessionId);release();await Promise.all([rejected,opening,revoked]);
assert.equal(manager.sessions.get(c.id)?.sessionId,other.sessionId);assert.equal(manager.sessionSnapshot(c.id).phase,'manual');
});