feat: complete workflow recovery and safe administration

This commit is contained in:
abaishu0730 committed 2026-10-21 12:30:11 +08:00
1 parent 1148aa6f14
commit 69583e8c33
46 files changed
+756 -120

No files matched your search

+42 -15
View File
@@ -16,24 +16,56 @@ test('category editor saves/reloads drafts, previews readable evidence without i
const server=createDashboard({store,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));
let browser;t.after(async()=>{await browser?.close();server.closeAllConnections();await new Promise(r=>server.close(r));await fs.rm(dir,{recursive:true,force:true});});
const sample={connectionId:'sample',source:'todo',instanceId:'i1',taskId:'t1',revision:'1',processName:'订单流程',nodeName:'通知',fields:{'订单.数量':0}};
const data={rules:[{id:'category-1',version:1,status:'draft',name:'示例类别',categoryId:'category-1',processNames:['订单流程'],nodeNames:['通知'],adapterIds:['a1'],archiveDirectory:'',filenameDate:'appDate',subjectPrefix:'通知',intro:'正文',signature:'团队',to:[],cc:[],attachPdf:true}],adapters:[{id:'a1',label:'订单表单',verified:true,processName:'订单流程',fields:{quantity:'订单.数量'},sample}],orders:[],batches:[],settings:{version:1,enabled:false,channelId:'',maxMessageBytes:20971520},audit:[],capabilities:{liveTodo:false,pdfExport:false,automaticArchive:false}};
const data={rules:[{id:'category-1',version:1,status:'draft',name:'示例类别',categoryId:'category-1',processNames:['订单流程'],nodeNames:['通知'],adapterIds:['a1'],archiveDirectory:'',filenameDate:'appDate',subjectPrefix:'通知',intro:'正文',signature:'团队',to:[],cc:[],attachPdf:true}],adapters:[{id:'a1',label:'订单表单',verified:true,processName:'订单流程',fields:{quantity:'订单.数量'},sample}],orders:[],batches:[],settings:{version:1,enabled:false,processingEnabled:false,processingSince:null,channelId:'',maxMessageBytes:20971520},audit:[],capabilities:{liveTodo:false,pdfExport:false,automaticArchive:false}};
data.window={start:'2026-10-20T00:00:00+08:00',end:'2026-10-20T11:00:00+08:00',label:'工作日第一时段',available:true};
const writes=[];browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(7000);const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.clock.install();
await page.route('**/api/order-rules**',async route=>{const req=route.request(),url=new URL(req.url());let result=url.pathname.endsWith('/fields')?{samples:[{id:'real-1',provenance:'server-provider',processName:'订单流程',sample:{...sample,receivedAt:'2026-10-19T09:00:00+08:00'}}]}:data;if(req.method()==='POST'){const body=req.postDataJSON();writes.push({path:url.pathname,body});if(url.pathname.endsWith('/rules')){data.rules[0]={...data.rules[0],...body,version:data.rules[0].version+1};result=data.rules[0];}else if(url.pathname.endsWith('/trial'))result={valid:true,ruleId:'category-1',checks:[{label:'数量',ok:true,detail:'零数量合法'}],issues:[],explanation:'按类别汇总一封邮件',fieldTrace:[{key:'quantity',label:'数量',path:'订单.数量',value:0}],event:{orderNo:'SAMPLE-1',quantity:0}};else if(url.pathname.endsWith('/publish')){await route.fulfill({status:409,json:{error:'实时待办接入待联调,暂不可发布'}});return;}}await route.fulfill({json:result});});
await page.route('**/api/mail',route=>route.fulfill({json:{channels:[],templates:[],receipts:[],sends:[]}}));
await page.route('**/api/order-rules**',async route=>{
const req=route.request(),url=new URL(req.url());let result;
if(req.method()==='GET'){
if(url.pathname.endsWith('/fields'))result={samples:[{id:'real-1',provenance:'server-provider',processName:'订单流程',sample:{...sample,receivedAt:'2026-10-19T09:00:00+08:00'}}]};
else if(['/api/order-rules/orders','/api/order-rules/batches'].includes(url.pathname)){
const items=url.pathname.endsWith('/orders')?data.orders:data.batches;
result={items,total:items.length,page:1,pageSize:25,totalPages:1,from:items.length?1:0,to:items.length,revision:String(items.length)};
}else if(url.pathname==='/api/order-rules'){
assert.equal(url.searchParams.get('view'),'summary','business views must request the bounded summary');
const {orders,batches,audit,...summary}=data;
result={...summary,issues:[],templateUsage:[],counts:{orders:orders.length,blocked:0,unknown:0,activeBatches:batches.length}};
}else throw Error('Unexpected fixture GET '+url.pathname);
}else{
const body=req.postDataJSON();writes.push({path:url.pathname,body});
if(url.pathname.endsWith('/rules')){data.rules[0]={...data.rules[0],...body,version:data.rules[0].version+1};result=data.rules[0];}
else if(url.pathname.endsWith('/trial'))result={valid:true,ruleId:'category-1',checks:[{label:'数量',ok:true,detail:'零数量合法'}],issues:[],explanation:'按类别汇总一封邮件',fieldTrace:[{key:'quantity',label:'数量',path:'订单.数量',value:0}],event:{orderNo:'SAMPLE-1',quantity:0}};
else if(url.pathname.endsWith('/publish'))return route.fulfill({status:409,json:{error:'实时待办接入待联调,暂不可发布'}});
else if(url.pathname.endsWith('/settings')){
if(body.enabled)return route.fulfill({status:409,json:{error:'隔离校验:邮箱及归档条件未就绪,邮件不能启用'}});
data.settings={...data.settings,...body,version:data.settings.version+1,processingSince:body.processingEnabled?'2026-10-20T02:00:00Z':data.settings.processingSince};result=data.settings;
}else if(url.pathname.endsWith('/prepare'))result={prepared:true};
else throw Error('Unexpected fixture POST '+url.pathname);
}
await route.fulfill({json:result});
});
await page.goto('http://127.0.0.1:'+server.address().port+'/#order-rules');await page.getByLabel('账号',{exact:true}).waitFor();assert.equal(await page.getByRole('button',{name:'进入演示空间',exact:true}).count(),0);await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();
await openRuleRoute(page,'order-rules');await page.getByLabel('规则名称',{exact:true}).fill('订单类别更新');await page.getByLabel('收件人',{exact:true}).fill('recipient@example.test');await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText('规则草稿已保存。',{exact:true}).waitFor();
assert.equal(data.rules[0].name,'订单类别更新');assert.deepEqual(data.rules[0].to,['recipient@example.test']);assert.deepEqual(writes.find(w=>w.path.endsWith('/rules')).body.processNames,['订单流程']);
await page.reload();await openRuleRoute(page,'order-rules');assert.equal(await page.getByLabel('规则名称',{exact:true}).inputValue(),'订单类别更新');
await page.getByRole('button',{name:'检查识别结果',exact:true}).click();await page.locator('#order-rule-preview-result').getByText('识别通过',{exact:true}).waitFor();assert.doesNotMatch(await page.locator('#order-rule-preview-result').innerText(),/订单.数量/);assert.match(await page.locator('#order-rule-preview-result').innerText(),/0/);assert.equal(data.orders.length,0);assert.equal(writes.some(x=>x.path.endsWith('/events')),false);assert.equal(writes.find(x=>x.path.endsWith('/trial')).body.sampleId,'real-1');assert.equal(await page.locator('#order-rule-sample').count(),0);
await page.getByRole('button',{name:'发布此版本',exact:true}).click();await page.getByText('实时待办接入待联调,暂不可发布',{exact:true}).waitFor();
await openRuleRoute(page,'notification-settings');assert.equal(await page.getByLabel('启用自动分类通知',{exact:true}).isDisabled(),true);assert.match(await page.locator('#order-rules-panel').innerText(),/待联调/);
await openRuleRoute(page,'notification-settings');
const processing=page.getByLabel('自动处理订单(采集、生成 PDF、归档)',{exact:true}),mail=page.getByLabel('自动发送分类邮件',{exact:true});
assert.equal(await processing.isEnabled(),true);assert.equal(await mail.isChecked(),false);
await processing.check();await page.getByRole('button',{name:'保存设置',exact:true}).click();await page.getByText('设置已保存。',{exact:true}).waitFor();
const settingsWrite=writes.find(w=>w.path.endsWith('/settings'));assert.equal(settingsWrite.body.processingEnabled,true);assert.equal(settingsWrite.body.enabled,false);assert.equal(data.settings.enabled,false);
await mail.check();await page.getByRole('button',{name:'保存设置',exact:true}).click();await page.getByText('隔离校验:邮箱及归档条件未就绪,邮件不能启用',{exact:true}).waitFor();assert.equal(data.settings.processingEnabled,true);assert.equal(data.settings.enabled,false);await mail.uncheck();
data.orders=[{id:'old',orderNo:'HISTORICAL-ORDER',categoryId:'category-1',status:'pending',receivedAt:'2026-10-19T09:00:00+08:00'},{id:'current',orderNo:'CURRENT-ORDER',categoryId:'category-1',status:'pending',receivedAt:'2026-10-20T09:00:00+08:00'},{id:'future',orderNo:'FUTURE-ORDER',categoryId:'category-1',status:'pending',receivedAt:'2026-10-20T15:00:00+08:00'}];
await page.reload();await openRuleRoute(page,'notification-batches');await page.getByText('暂无待发批次',{exact:true}).waitFor();
assert.equal(await page.getByLabel('本批截止时间(北京时间)',{exact:true}).count(),0);assert.match(await page.locator('#order-rules-panel').innerText(),/工作日第一时段/);assert.match(await page.locator('#order-rules-panel').innerText(),/CURRENT-ORDER/);assert.doesNotMatch(await page.locator('#order-rules-panel').innerText(),/HISTORICAL-ORDER|FUTURE-ORDER/);
await page.getByRole('button',{name:'整理本时段批次',exact:true}).click();assert.deepEqual(writes.find(x=>x.path.endsWith('/prepare')).body,{cutoff:data.window.end});
await page.locator('[data-rule-recheck="current"]').locator('..').locator('summary').click();await page.getByLabel('重新核对依据',{exact:true}).fill('已核实更新后的业务分类');await page.getByRole('button',{name:'重新核对此订单',exact:true}).click();await page.getByText('此订单已重新核对,请检查新匹配结果及归档状态。',{exact:true}).waitFor();assert.deepEqual(writes.find(x=>x.path.endsWith('/recheck')).body,{note:'已核实更新后的业务分类'});
data.window.available=false;await page.reload();await openRuleRoute(page,'notification-batches');assert.equal(await page.getByRole('button',{name:'整理本时段批次',exact:true}).isDisabled(),true);assert.match(await page.locator('#order-rules-panel').innerText(),/当前只预览/);
data.window.available=true;data.batches=[{id:'fresh-batch',categoryId:'category-1',subject:'自动刷新后的本时段批次',status:'prepared',windowStart:data.window.start,windowEnd:data.window.end,to:[],orderIds:[]}];await page.clock.fastForward(15001);await page.getByText('自动刷新后的本时段批次',{exact:true}).waitFor();assert.equal(await page.getByRole('button',{name:'整理本时段批次',exact:true}).isDisabled(),false);
await openRuleRoute(page,'notification-orders');await page.getByText('HISTORICAL-ORDER',{exact:true}).waitFor();await page.getByText('CURRENT-ORDER',{exact:true}).waitFor();await page.getByText('FUTURE-ORDER',{exact:true}).waitFor();assert.equal(await page.locator('[data-rule-recheck]').count(),0,'retired inline recheck is not restored');
data.batches=[{id:'old-batch',categoryId:'category-1',subject:'旧窗口仍未终结批次',status:'prepared',windowStart:'2026-10-19T00:00:00+08:00',windowEnd:'2026-10-19T11:00:00+08:00',orderCount:0}];
await openRuleRoute(page,'notification-batches');await page.getByText('旧窗口仍未终结批次',{exact:true}).waitFor();
assert.equal(await page.getByLabel('本批截止时间(北京时间)',{exact:true}).count(),0);await page.getByRole('button',{name:'整理本时段批次',exact:true}).click();await page.getByText('批次整理已完成,请核对受阻原因与冻结结果。',{exact:true}).waitFor();assert.deepEqual(writes.find(x=>x.path.endsWith('/prepare')).body,{cutoff:data.window.end});
data.window.available=false;await page.reload();await page.getByText('未在配置发送时点的整理期限内。',{exact:true}).waitFor();assert.equal(await page.getByRole('button',{name:'整理本时段批次',exact:true}).isDisabled(),true);
data.batches.push({id:'fresh-batch',categoryId:'category-1',subject:'后台新增批次',status:'prepared',windowStart:data.window.start,windowEnd:data.window.end,orderCount:0});
await page.clock.fastForward(11000);await page.getByText('记录有更新。',{exact:true}).waitFor();assert.equal(await page.getByText('后台新增批次',{exact:true}).count(),0,'polling must not replace the displayed page');
await page.locator('#content .card-header').getByRole('button',{name:'刷新记录',exact:true}).click();await page.getByText('后台新增批次',{exact:true}).waitFor();await page.getByText('旧窗口仍未终结批次',{exact:true}).waitFor();
await openRuleRoute(page,'order-rules');await page.getByLabel('规则名称',{exact:true}).fill('保留尚未保存的规则');await page.clock.fastForward(15001);assert.equal(await page.getByLabel('规则名称',{exact:true}).inputValue(),'保留尚未保存的规则');
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(errors,[]);
});
@@ -64,10 +96,5 @@ test('real catalog workflow publishes a saved and sampled draft while roles and
});
async function openRuleRoute(page,hash){
if(!await page.locator('a[href="#'+hash+'"]').count()){
const business=['order-rules','rule-fields'].includes(hash);
await page.locator('a[href="#'+(business?'business-settings':'order-notifications')+'"]').first().click();
if(hash==='notification-settings')await page.locator('a[href="#schedule"]').first().click();
}
await page.locator('a[href="#'+hash+'"]').first().click();
await page.evaluate(hash=>{const link=document.createElement('a');link.href='#'+hash;document.body.append(link);link.click();link.remove();},hash);
}