'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 {workspaceApi}=require('../../src/modules/workspace/api.cjs'); const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs'); test('workspace and health share independent account and sync conclusions, respect cadence and ownership without probes',async t=>{ const now=Date.now(),at=new Date(now).toISOString(),old=new Date(now-12*60000).toISOString(); const connection={id:'mine',owner:'worker',name:'业务连接',status:'connected',checkedAt:at,checks:{portal:{ok:true}}}; const workspace={snapshot:()=>({connections:[connection,{...connection,id:'other',owner:'other',name:'PRIVATE_OTHER'}],capabilities:{}})}; const manager={health:{freshMs:300000,snapshot:()=>({portal:{status:'healthy',checkedAt:at}})},ensure:()=>assert.fail('status must not authenticate')}; const config={enabled:true,intervalMinutes:10}; const monitoring={status:'listening',sources:{todo:{status:'running',lastSuccessAt:old},inform:{status:'healthy',lastSuccessAt:at}},delivery:{status:'error',pending:1}}; const monitor={snapshot:()=>monitoring,scanner:{store:{config:()=>config}}}; let response;await workspaceApi({workspace,store:{users:()=>[]},user:{username:'worker',role:'operator'},route:'/api/workspace',method:'GET',bpmSessions:manager,bpmMonitor:monitor,reply:(code,value)=>{assert.equal(code,200);response=value;}}); assert.equal(response.connections[0].connectionState.account.status,'healthy'); assert.equal(response.connections[0].connectionState.sync.reason,'DELIVERY_FAILED'); const stateDir=fs.mkdtempSync(path.join(os.tmpdir(),'connection-status-')); const health=new RuntimeHealth({stateDir,clock:()=>now,workspace,bpmSessions:manager,monitor,scanner:{store:{status:()=>({config,runs:[]})}}}); t.after(()=>{health.close();fs.rmSync(stateDir,{recursive:true,force:true});}); let snapshot=health.snapshot({username:'worker',role:'operator'}); assert.equal(snapshot.groups.find(g=>g.key==='bpm').items[0].status,'healthy'); const sync=snapshot.groups.find(g=>g.key==='bpm-sync');assert.equal(sync.items[0].reason,'DELIVERY_FAILED');assert.equal(sync.items.length,1); assert.doesNotMatch(JSON.stringify(snapshot),/PRIVATE_OTHER/); monitoring.delivery={status:'healthy',pending:0}; snapshot=health.snapshot({role:'admin'});assert.equal(snapshot.groups.find(g=>g.key==='bpm-sync').items[0].status,'healthy'); config.intervalMinutes=1; snapshot=health.snapshot({role:'admin'});assert.equal(snapshot.groups.find(g=>g.key==='bpm-sync').items[0].reason,'SOURCE_STALE'); });