Files

61 lines
5.5 KiB
JavaScript

'use strict';
const test=require('node:test');
const assert=require('node:assert/strict');
const {OrderWorkflow}=require('../src/order-workflow.cjs');
function publish(workflow,rule,arrival,detail){const saved=workflow.saveRule(rule),check=workflow.checkRule(saved.id,{arrival,detail,provenance:'bpm-readonly'});assert.equal(check.valid,true,JSON.stringify(check.issues));return workflow.publishRule(saved.id,{version:saved.version,checkId:check.checkId});}
const arrival={source:'portal-v4',taskId:'task-plan-1',processName:'海外运营订单评审',activityName:'订单专员(生效)',arrivedAt:'2026-10-22T01:00:00.000Z',subject:'列表摘要原文'};
const rule={id:'rule-plan',enabled:true,processName:'海外运营订单评审',nodeName:'订单专员(生效)',productLine:'海外运营',templateId:'tpl-overseas',adapters:{V13:{orderNo:'order',serialNo:'serial',productCode:'product',modelFull:'full',modelShort:'short',customer:'customer',brief:{source:'arrival',path:'subject'}}}};
const detail={tid:'100001',pid:'900001',processVersion:'V13',processInstanceId:'900001',businessVersion:'BV-1',nodeName:'订单专员(生效)',fields:{order:'000123',serial:'SERIAL-EXAMPLE',product:'MODEL-EXAMPLE-001',full:'MODEL-EXAMPLE-001',short:'MODEL-EXAMPLE',customer:'示例客户'},processSteps:[{name:'订单专员(生效)',status:'处理中'}]};
test('order identity retains tid pid current order number and reports upstream revision only when present',()=>{
const workflow=new OrderWorkflow();publish(workflow,rule,arrival,detail);
const result=workflow.identify({arrival,detail});
assert.equal(result.order.tid,'100001');
assert.equal(result.order.pid,'900001');
assert.equal(result.order.businessVersion,'BV-1');
assert.equal(result.order.brief,'列表摘要原文');
assert.equal(result.order.businessTimeSource,'list.receivedAt');
for(const key of ['tid','pid']){
const missing={...detail};delete missing[key];
assert.throws(()=>workflow.identify({arrival,detail:missing}),error=>error.code==='VERSION_MISSING');
}
});
test('workflow blocks a process step snapshot that disagrees with the current node',()=>{
const workflow=new OrderWorkflow();publish(workflow,rule,arrival,detail);
assert.throws(()=>workflow.identify({arrival,detail:{...detail,processSteps:[{name:'其他节点',status:'处理中'}]}}),error=>error.code==='NODE_CONFLICT');
});
function ready(workflow,taskId,businessVersion,productLine='海外运营',templateId='tpl-overseas'){
const order=workflow.identify({arrival:{...arrival,taskId},detail:{...detail,businessVersion}}).order;
workflow.start(order.versionKey);workflow.markDetailReady(order.versionKey,{capturedAt:'2026-10-22T01:01:00.000Z'});workflow.markPdfSuccess(order.versionKey,{jobId:'pdf-'+taskId});workflow.markArchiveSuccess(order.versionKey,{archiveId:'archive-'+taskId,path:'/test/'+taskId+'.pdf'});workflow.createNotification(order.versionKey,{productLine,templateId,brief:order.brief,subject:'通知标题',occurredAt:order.businessTime});
return order;
}
test('freezeBatches groups by category and template and freezes the template snapshot',()=>{
const workflow=new OrderWorkflow();publish(workflow,rule,arrival,detail);
const first=ready(workflow,'task-a','BV-A');
const second=ready(workflow,'task-b','BV-B');
const batches=workflow.freezeBatches('2026-10-22T03:00:00.000Z',{groups:{'海外运营':{to:['test@example.test'],cc:['cc@example.test']}},templates:{'tpl-overseas':{id:'tpl-overseas',version:4,subject:'{{产品线}} {{订单号}}',text:'{{日期}}\\n{{订单列表}}',attachments:[]}}});
assert.equal(batches.length,1);
assert.deepEqual(batches[0].orderVersionKeys,[first.versionKey,second.versionKey]);
assert.equal(batches[0].templateVersion,4);
assert.equal(batches[0].text.includes('列表摘要原文'),true);
assert.deepEqual(batches[0].attachments,[]);
assert.deepEqual(batches[0].to,['test@example.test']);
});
test('freezeBatches blocks missing recipients and keeps unrelated categories separate',()=>{
const workflow=new OrderWorkflow();publish(workflow,rule,arrival,detail);
ready(workflow,'task-c','BV-C','海外运营','tpl-overseas');
const otherRule={...rule,id:'rule-other',processName:'国内订单评审',nodeName:'订单专员',productLine:'国内',templateId:'tpl-domestic',adapters:{V13:rule.adapters.V13}};
publish(workflow,otherRule,{...arrival,processName:otherRule.processName,activityName:otherRule.nodeName},{...detail,nodeName:otherRule.nodeName,processSteps:[{name:otherRule.nodeName,status:'处理中'}]});
const otherArrival={...arrival,taskId:'task-d',processName:'国内订单评审',activityName:'订单专员'};
const otherDetail={...detail,nodeName:'订单专员',businessVersion:'BV-D',processSteps:[{name:'订单专员',status:'处理中'}]};
const other=workflow.identify({arrival:otherArrival,detail:otherDetail}).order;workflow.start(other.versionKey);workflow.markDetailReady(other.versionKey);workflow.markPdfSuccess(other.versionKey,{jobId:'pdf-d'});workflow.markArchiveSuccess(other.versionKey,{archiveId:'archive-d'});workflow.createNotification(other.versionKey,{productLine:'国内',templateId:'tpl-domestic',brief:other.brief,subject:'国内',occurredAt:other.businessTime});
const batches=workflow.freezeBatches('2026-10-22T03:00:00.000Z',{groups:{'海外运营':{to:['test@example.test'],cc:[]}},templates:{'tpl-overseas':{id:'tpl-overseas',version:1,subject:'x',text:'x',attachments:[]},'tpl-domestic':{id:'tpl-domestic',version:1,subject:'x',text:'x',attachments:[]}}});
assert.equal(batches.length,1);assert.equal(workflow.get(other.versionKey).status,'blocked');
});