Files
order-notification-automation/tests/order-workflow-task-identity.test.cjs
T

46 lines
4.1 KiB
JavaScript

'use strict';
const test=require('node:test');const assert=require('node:assert/strict');
const {OrderWorkflow}=require('../src/order-workflow.cjs');
const rule={id:'r',enabled:true,status:'published',processName:'P',nodeName:'N',productLine:'C',templateId:'tpl'};
const mapping={orderNo:'no',serialNo:'sn',productCode:'product',modelFull:'model',modelShort:'short',customer:'customer',brief:{source:'arrival',path:'subject'}};
const arrival={source:'portal-v4',taskId:'task-1',processName:'P',activityName:'N',subject:'原文\n保留换行',arrivedAt:'2026-10-22T01:00:00Z'};
const detail={tid:'1',pid:'2',processName:'P',nodeName:'N',processVersion:'V1',fields:{no:'000123',sn:'S',product:'P',model:'M',short:'M',customer:'C',quantity:'10'}};
test('a real BPM notification task can be processed without inventing a business revision',()=>{
const w=new OrderWorkflow(),result=w.identify({arrival,detail,mapping,rule});assert.equal(result.status,'matched');assert.equal(result.order.businessVersion,null);assert.equal(result.order.versionSource,'bpm.taskId');assert.equal(result.order.brief,'原文\n保留换行');
assert.equal(w.identify({arrival:{...arrival,connectionId:'another-authorized-connection'},detail,mapping,rule}).status,'duplicate');assert.equal(w.listOrders().length,1);
});
test('a new BPM task is a new notification even when order number and form identifiers are unchanged',()=>{
const w=new OrderWorkflow(),first=w.identify({arrival,detail,mapping,rule}).order,second=w.identify({arrival:{...arrival,taskId:'task-2'},detail,mapping,rule}).order;assert.notEqual(first.versionKey,second.versionKey);assert.equal(first.orderNo,second.orderNo);assert.equal(w.listOrders().length,2);
});
test('changing a field or reported revision on the same task never creates another notification',()=>{
const w=new OrderWorkflow();w.identify({arrival,detail,mapping,rule});assert.throws(()=>w.identify({arrival,detail:{...detail,fields:{...detail.fields,quantity:'20'}},mapping,rule}),e=>e.code==='VERSION_CONTENT_CONFLICT');
assert.throws(()=>w.identify({arrival,detail:{...detail,businessVersion:'new-reported-value'},mapping,rule}),e=>e.code==='VERSION_CONTENT_CONFLICT');assert.equal(w.listOrders().length,1);
});
test('an old configured revision path is optional when BPM does not supply it',()=>{
const w=new OrderWorkflow(),o=w.identify({arrival,detail,mapping:{...mapping,businessVersion:'not-present'},rule}).order;assert.equal(o.businessVersion,null);assert.equal(o.versionSource,'bpm.taskId');
});
function conflictingLegacyWorkflow(){
const w=new OrderWorkflow(),original=w.identify({arrival,detail,mapping,rule}).order;
const first={...structuredClone(original),versionKey:'legacy-revision-a'},second={...structuredClone(original),versionKey:'legacy-revision-b'};
const workflow=new OrderWorkflow({store:{orders:()=>[first,second]}});
return {workflow,first,second};
}
test('multiple legacy versions for the same task require confirmation before any new archive',()=>{
const {workflow:w,first}=conflictingLegacyWorkflow();
assert.throws(()=>w.identify({arrival,detail,mapping,rule}),e=>e.code==='TASK_IDENTITY_CONFLICT');
assert.throws(()=>w.start(first.versionKey),e=>e.code==='TASK_IDENTITY_CONFLICT');
assert.equal(w.listOrders().length,2);
});
test('ambiguous legacy tasks cannot enter a new notification batch or send an already frozen batch',()=>{
const {workflow:w,first,second}=conflictingLegacyWorkflow();
for(const order of [first,second])w.update(order.versionKey,draft=>{draft.status='notification_pending';draft.notification={status:'pending',occurredAt:'2026-10-22T01:00:00Z',brief:'original',templateId:'tpl'};});
assert.throws(()=>w.freezeBatches('2026-10-22T03:00:00Z',{groups:{C:{to:['2639266652@qq.com'],cc:[]}},templates:{tpl:{id:'tpl',version:1,subject:'{{产品线}}',text:'{{订单列表}}'}}}),e=>e.code==='EMPTY_BATCH');
assert.equal(w.listBatches().length,0);assert.equal(w.get(first.versionKey).notification.status,'blocked');
w.batches.set('old-batch',{id:'old-batch',status:'prepared',orderVersionKeys:[first.versionKey]});
assert.throws(()=>w.markBatchSending('old-batch'),e=>e.code==='TASK_IDENTITY_CONFLICT');
assert.equal(w.getBatch('old-batch').status,'prepared');
});