feat: unify BPM health and guard session recovery with state machines

This commit is contained in:
阿白叔 committed 2026-10-24 14:49:15 +08:00
1 parent 97e06328c3
commit 42d19c707d
23 files changed
+681 -90

No files matched your search

+53 -7
View File
@@ -8,6 +8,7 @@ const {setTimeout:delay}=require('node:timers/promises');
async function waitUntil(predicate){const deadline=Date.now()+10000;while(!predicate()){assert.ok(Date.now()<deadline,'expected browser request did not arrive');await delay(25);}}
let browser,server,base,jpeg;
const source=path.join(__dirname,'../../web/modules/bpm/view.js');
const {connectionSnapshot}=require('../../src/modules/bpm/connection-status.cjs');
before(async()=>{
browser=await loadPlaywright().chromium.launch(browserOptions());
const page=await browser.newPage({viewport:{width:1280,height:800}});jpeg=await page.screenshot({type:'jpeg'});await page.close();
@@ -40,6 +41,9 @@ async function fixture(t,{user={username:'alice',role:'operator'},connections,ac
if(url.pathname.endsWith('/save')||url.pathname.endsWith('/check'))result={connection:{id:'one',name:'业务连接',owner:'alice',status:'connected',identity:'Alice',checkedAt:new Date().toISOString(),checks:{portal:{ok:true,message:'已登录'}}}};
if(url.pathname.endsWith('/account/check'))result={available:body?.username!=='already-used'};
if(url.pathname.endsWith('/enable')||url.pathname.endsWith('/disable')){const id=url.pathname.split('/').at(-2),item=connections.find(c=>c.id===id);item.status=url.pathname.endsWith('/enable')?'unchecked':'disabled';item.version=(item.version||0)+1;result={...item};}
if(result.connections)result.connections=result.connections.map(c=>connectionSnapshot(c));
if(result.connection)result.connection=connectionSnapshot(result.connection);
if(result.id&&result.status)result=connectionSnapshot(result);
await route.fulfill({json:result});
});
await page.goto(base);
@@ -83,6 +87,7 @@ test('auto-renew setup rejects a duplicate username before sending credentials a
test('portal login displays session, business and monitoring checks without legacy controls',async t=>{
const f=await fixture(t,{connections:[{id:'one',name:'新版 BPM',owner:'alice',status:'connected',checks:{portal:{ok:true,message:'新版已登录'}}}]});
await f.page.getByText('状态详情',{exact:true}).click();
assert.equal(await f.page.getByText('旧版入口',{exact:true}).count(),0);
assert.equal(await f.page.getByText('bpm.skyworthdigital.com',{exact:true}).count(),0);
assert.match(await f.page.locator('.bpm-checks').innerText(),/登录会话.*新版已登录/);
@@ -92,7 +97,7 @@ test('portal login displays session, business and monitoring checks without lega
await f.page.getByRole('button',{name:'刷新',exact:true}).click();
await f.page.getByRole('button',{name:'检查并保存',exact:true}).click();
await f.page.getByText('连接已检查并保存。',{exact:true}).waitFor();
assert.equal(await f.page.getByText('已连接',{exact:true}).count(),1);
assert.equal(await f.page.locator('[data-connection-status] .bpm-status-green').count(),1);
assert.match(await f.page.locator('.bpm-checks').innerText(),/登录会话.*已登录/);
assert.deepEqual(f.calls.filter(c=>c.path.endsWith('/navigate')).map(c=>c.body),[{target:'main'},{target:'reload'}]);
});
@@ -130,7 +135,7 @@ test('account editor saves exact credentials, clears secret input, requires fres
assert.equal(await f.page.getByRole('button',{name:'填入托管账号',exact:true}).count(),0);
});
test('saving a replacement account blocks new remote control and shows the automatically connected identity',async t=>{
const f=await fixture(t,{holdAccountSave:true,savedAccountConnection:{id:'one',name:'业务连接',owner:'alice',status:'connected',identity:'replacement',checkedAt:'2026-10-16T01:00:00.000Z',checks:{portal:{ok:true,message:'已登录'}}},connections:[{id:'one',name:'业务连接',owner:'alice',status:'connected',identity:'old-user',checkedAt:'2026-10-15T00:00:00.000Z',checks:{portal:{ok:true,message:'已登录'}}}]});
const f=await fixture(t,{holdAccountSave:true,savedAccountConnection:{id:'one',name:'业务连接',owner:'alice',status:'connected',identity:'replacement',checkedAt:new Date().toISOString(),checks:{portal:{ok:true,message:'已登录'}}},connections:[{id:'one',name:'业务连接',owner:'alice',status:'connected',identity:'old-user',checkedAt:'2026-10-15T00:00:00.000Z',checks:{portal:{ok:true,message:'已登录'}}}]});
await f.page.setViewportSize({width:360,height:800});await f.page.getByRole('button',{name:/^(管理账号|开启自动续登)$/}).click();
await f.page.getByLabel('BPM 账号',{exact:true}).fill('replacement');await f.page.getByLabel('托管密码',{exact:true}).fill('replacement-secret');
assert.equal(await f.page.evaluate(()=>document.documentElement.scrollWidth<=window.innerWidth),true);
@@ -139,7 +144,7 @@ test('saving a replacement account blocks new remote control and shows the autom
await f.page.locator('[data-account-form]').evaluate(form=>form.dispatchEvent(new Event('submit',{bubbles:true,cancelable:true})));
await f.page.waitForTimeout(50);assert.equal(f.calls.filter(c=>c.path.endsWith('/account')&&c.body).length,1);
f.releaseAccountSave();await f.page.getByText('登录账号 replacement',{exact:true}).waitFor();
assert.equal(await f.page.getByText('已连接',{exact:true}).count(),1);
assert.equal(await f.page.locator('[data-connection-status] .bpm-status-green').count(),1);
assert.equal(await f.page.getByText('登录账号 old-user',{exact:true}).count(),0);
assert.equal(await f.page.getByText('上次检查 2026-10-15T00:00:00.000Z',{exact:true}).count(),0);
assert.equal(await f.page.getByRole('button',{name:'远程登录',exact:true}).isEnabled(),true);
@@ -185,7 +190,7 @@ test('scaled clicks, IME, paste and editing keys reach the remote browser in ord
await f.page.getByRole('button',{name:'检查并保存',exact:true}).click();
assert.equal(f.calls.filter(c=>c.path.endsWith('/input')).length,1);
assert.equal(f.calls.some(c=>c.path.endsWith('/save')),false);
f.releaseInput();await f.page.getByText('已连接',{exact:true}).waitFor();
f.releaseInput();await f.page.getByText('连接已检查并保存。',{exact:true}).waitFor();
const actions=f.calls.filter(c=>c.path.endsWith('/input')).map(c=>c.body);
// Browser click coordinates round to whole CSS pixels: at half scale, allow two remote pixels.
assert.equal(actions[0].type,'click');assert.ok(Math.abs(actions[0].x-640)<=2,JSON.stringify(actions[0]));assert.ok(Math.abs(actions[0].y-400)<=2,JSON.stringify(actions[0]));
@@ -252,7 +257,7 @@ test('narrow screens keep the frame inside the page and native typing forwards t
await f.page.getByRole('button',{name:'远程登录',exact:true}).click();
const frame=f.page.locator('[data-bpm-frame]');await frame.waitFor({state:'visible'});await frame.click({position:{x:50,y:50}});
await f.page.keyboard.type('abc');await f.page.keyboard.press('Backspace');await f.page.keyboard.press('Tab');
await f.page.getByRole('button',{name:'检查并保存',exact:true}).click();await f.page.getByText('已连接',{exact:true}).waitFor();
await f.page.getByRole('button',{name:'检查并保存',exact:true}).click();await f.page.getByText('连接已检查并保存。',{exact:true}).waitFor();
assert.deepEqual(f.calls.filter(c=>c.body?.type==='text').map(c=>c.body.text),['a','b','c']);
assert.ok(await f.page.evaluate(()=>document.documentElement.scrollWidth<=window.innerWidth));
});
@@ -307,14 +312,55 @@ for(const pendingAction of ['open','save'])test(`document unload sends authentic
test('admin pauses and resumes using explicit buttons and the current version',async t=>{const f=await fixture(t,{user:{username:'admin',role:'admin'},connections:[{id:'toggle',name:'可恢复连接',owner:'alice',status:'connected',version:4,identity:'fixture'}]});await f.page.getByRole('button',{name:'停用连接',exact:true}).click();await f.page.getByRole('button',{name:'启用连接',exact:true}).waitFor();assert.equal(await f.page.getByRole('button',{name:'检查连接',exact:true}).count(),0);assert.deepEqual(f.calls.find(c=>c.path.endsWith('/disable')).body,{version:4});await f.page.getByRole('button',{name:'启用连接',exact:true}).click();await f.page.getByRole('button',{name:'停用连接',exact:true}).waitFor();assert.deepEqual(f.calls.find(c=>c.path.endsWith('/enable')).body,{version:5});assert.equal(await f.page.getByRole('switch').count(),0);});
test('connection cards distinguish authenticated session, todo permission and passive-listener status',async t=>{const f=await fixture(t,{connections:[{id:'one',name:'分层检测',owner:'alice',status:'connected',health:{portal:{status:'healthy',checkedAt:new Date().toISOString()},todo:{status:'forbidden',checkedAt:new Date().toISOString()}},monitoring:{status:'listening',lastSyncAt:new Date().toISOString()}}]});await f.page.getByText('订单接口 · 无读取权限',{exact:false}).waitFor();await f.page.getByText('到达监听 · 推送已连接',{exact:false}).waitFor();await f.page.getByRole('button',{name:'开启自动续登',exact:true}).click();await f.page.getByLabel('BPM 账号',{exact:true}).fill('not-yet-saved');await new Promise(r=>setTimeout(r,5200));assert.equal(await f.page.getByLabel('BPM 账号',{exact:true}).inputValue(),'not-yet-saved');});
test('connection cards distinguish authenticated session, todo permission and passive-listener status',async t=>{const f=await fixture(t,{connections:[{id:'one',name:'分层检测',owner:'alice',status:'connected',health:{portal:{status:'healthy',checkedAt:new Date().toISOString()},todo:{status:'forbidden',checkedAt:new Date().toISOString()}},monitoring:{status:'listening',lastSyncAt:new Date().toISOString()}}]});await f.page.getByText('状态详情',{exact:true}).click();await f.page.getByText('订单接口 · 无读取权限',{exact:false}).waitFor();await f.page.getByText('到达监听 · 推送已连接',{exact:false}).waitFor();await f.page.getByRole('button',{name:'开启自动续登',exact:true}).click();await f.page.getByLabel('BPM 账号',{exact:true}).fill('not-yet-saved');await new Promise(r=>setTimeout(r,5200));assert.equal(await f.page.getByLabel('BPM 账号',{exact:true}).inputValue(),'not-yet-saved');});
test('a delayed background snapshot cannot resurrect a connection after deletion',async t=>{
const connection={id:'off',name:'旧连接',owner:'alice',status:'disabled',version:1},f=await fixture(t,{user:{username:'admin',role:'admin'},connections:[connection]});let arrive,release;const arrived=new Promise(r=>arrive=r),gate=new Promise(r=>release=r);
await f.page.route('**/api/workspace',async route=>{arrive();await gate;await route.fulfill({json:{connections:[connection],capabilities:{remoteLogin:true}}});});await arrived;f.page.once('dialog',dialog=>dialog.accept());await f.page.getByRole('button',{name:'删除',exact:true}).click();await f.page.getByText('连接已删除。',{exact:true}).waitFor();const response=f.page.waitForResponse(r=>r.url().endsWith('/api/workspace'));release();await response;await f.page.waitForTimeout(100);assert.equal(await f.page.getByRole('heading',{name:'旧连接',exact:true}).count(),0);
});
test('source read state and local delivery are separate and handover uses connection version',async t=>{
const connection={id:'one',name:'隔离监听连接',owner:'boss',version:7,status:'connected',monitoring:{status:'listening',nextReconcileAt:'2026-10-21T10:00:00Z',sources:{todo:{status:'healthy',lastSuccessAt:'2026-10-21T09:00:00Z'},inform:{status:'failed',lastFailureAt:'2026-10-21T09:01:00Z',retryAt:'2026-10-21T09:02:00Z'}},delivery:{status:'retrying',pending:2,message:'隔离本地投递失败'}}};
const f=await fixture(t,{user:{username:'boss',role:'admin'},connections:[connection]});await f.page.getByText(/待办源读取 · healthy/).waitFor();await f.page.getByText(/通知源读取 · failed/).waitFor();await f.page.getByText(/本地执行器投递 · retrying · 待交付 2/).waitFor();let submitted;
const f=await fixture(t,{user:{username:'boss',role:'admin'},connections:[connection]});await f.page.getByText('状态详情',{exact:true}).click();await f.page.getByText(/待办源读取 · healthy/).waitFor();await f.page.getByText(/通知源读取 · failed/).waitFor();await f.page.getByText(/本地执行器投递 · retrying · 待交付 2/).waitFor();let submitted;
await f.page.route('**/api/connections/one/owner',route=>{submitted=route.request().postDataJSON();return route.fulfill({json:{connection:{...connection,owner:'alice',status:'disabled',version:8},cleanupPending:false}});});
await f.page.locator('[data-owner-transfer] select').selectOption('alice');await f.page.getByRole('button',{name:'确认转交',exact:true}).click();await f.page.getByRole('button',{name:'启用连接',exact:true}).waitFor();assert.deepEqual(submitted,{version:7,owner:'alice'});assert.match(await f.page.locator('[data-bpm-feedback]').innerText(),/连接已暂停/);
});
test('account traffic lights summarize errors, warnings and complete health with collapsed diagnostics',async t=>{
const now=new Date().toISOString(),good={status:'connected',health:{portal:{status:'healthy',checkedAt:now},todo:{status:'healthy',checkedAt:now}},monitoring:{status:'listening',sources:{todo:{status:'healthy',lastSuccessAt:now},inform:{status:'healthy',lastSuccessAt:now}},delivery:{status:'healthy',pending:0}}};
const cases=[
['健康',good,'green','green'],
['首次读取',{...good,monitoring:{...good.monitoring,sources:{todo:{status:'pending'},inform:{status:'pending'}}}},'green','yellow'],
['读取失败',{...good,monitoring:{...good.monitoring,sources:{todo:{status:'healthy'},inform:{status:'failed'}}}},'green','red'],
['会话过期',{...good,health:{...good.health,portal:{status:'login_required',checkedAt:now}}},'red','red'],
['检查过期',{...good,health:{...good.health,portal:{status:'healthy',checkedAt:'2020-01-01T00:00:00Z'}}},'yellow','green'],
['重连',{...good,monitoring:{...good.monitoring,status:'reconnecting'}},'green','yellow'],
['投递失败',{...good,monitoring:{...good.monitoring,delivery:{status:'error',pending:1}}},'green','red'],
['未检查',{status:'unchecked'},'yellow','yellow'],
['已停用',{...good,status:'disabled'},'yellow','yellow']
];
const f=await fixture(t,{connections:cases.map(([name,value],i)=>({...value,id:String(i),name,owner:'alice'}))});
for(const [i,[, ,color,syncColor]] of cases.entries()){
const card=f.page.locator('article').nth(i);
assert.equal(await card.locator('[data-connection-status] .bpm-status-'+color).count(),1);
assert.equal(await card.locator('[data-sync-status] .bpm-status-'+syncColor).count(),1);
assert.equal(await card.locator('details').getAttribute('open'),null);
assert.equal(await card.getByText('待办源读取最近失败',{exact:false}).isVisible(),false);
}
const first=f.page.locator('article').first();
await first.getByText('状态详情',{exact:true}).click();
assert.equal(await first.getByText('待办源读取最近失败',{exact:false}).isVisible(),true);
await f.page.waitForTimeout(5300);
assert.notEqual(await first.locator('details').getAttribute('open'),null,'polling preserves expanded diagnostics');
assert.equal(f.calls.some(c=>c.body),false,'status display only reads state');
});
test('UI renders backend conclusions without reinterpreting raw connection flags',async t=>{
const c={id:'one',name:'后端结论',owner:'alice',status:'error',health:{portal:{status:'login_required'}},connectionState:{account:{status:'healthy',color:'green',label:'健康',message:'后端已核实会话'},sync:{status:'pending',color:'yellow',label:'警告',message:'等待后端对账'}}};
const f=await fixture(t,{connections:[]});
await f.page.route('**/api/workspace',route=>route.fulfill({json:{connections:[c],capabilities:{remoteLogin:true},members:[]}}));
await f.page.waitForTimeout(5300);
assert.equal(await f.page.locator('[data-connection-status] .bpm-status-green').count(),1);
assert.equal(await f.page.locator('[data-sync-status] .bpm-status-yellow').count(),1);
await f.page.getByText('后端已核实会话',{exact:true}).waitFor();
});