50 lines
3.6 KiB
JavaScript
50 lines
3.6 KiB
JavaScript
'use strict';
|
|
const test=require('node:test'),assert=require('node:assert/strict');
|
|
const {applyBodyTemplate}=require('../../src/modules/order-rules/template.cjs');
|
|
const maxBytes=20971520;
|
|
function order(id,receivedAt){
|
|
return {id,event:{orderNo:id,brief:'订单号:'+id,productLine:'测试产品线',receivedAt},
|
|
admittedAt:'2026-10-23T02:00:00Z',createdAt:'2026-10-23T02:00:00Z',
|
|
sample:{receivedAt:'2026-10-25T00:00:00Z',fields:{businessDate:'2026-10-26'}},
|
|
ruleSnapshot:{name:'订单通知',bodyTemplateId:'normal',huizhouTemplateId:'hui'},
|
|
archive:{files:[{path:'\\\\synthetic\\orders\\'+id+'.pdf'}]}};
|
|
}
|
|
function fixture(mode,orders,parts={}){
|
|
const snapshot={notificationMode:mode,cutoff:'2026-10-23T03:00:00Z',orderNos:orders.map(item=>item.event.orderNo),subject:'原标题',attachments:[]};
|
|
const template={id:mode==='huizhou'?'hui':'normal',version:1,status:'active',notificationMode:mode,
|
|
subject:'业务日期{{ 日期 }} {{订单号}}',text:'日期={{日期}}\n{{订单列表}}',html:'<p>日期={{日期}}</p>{{订单列表}}',attachments:[],...parts};
|
|
return {snapshot,template};
|
|
}
|
|
for(const mode of ['normal','huizhou']){
|
|
test(`${mode} date variable uses unique Shanghai event dates in order appearance, ignoring admission and cutoff`,()=>{
|
|
const orders=[order('A','2026-10-20T16:30:00Z'),order('B','2026-10-20T23:30:00+08:00'),order('C','2026-10-21T01:00:00+08:00')];
|
|
const {snapshot,template}=fixture(mode,orders),before=structuredClone({snapshot,orders});
|
|
const batch=applyBodyTemplate(snapshot,template,orders,maxBytes);
|
|
assert.equal(batch.subject,'业务日期2026-10-21、2026-10-20 A、B、C');
|
|
assert.match(batch.text,/日期=2026-10-21、2026-10-20\n/);
|
|
assert.match(batch.html,/<p>日期=2026-10-21、2026-10-20<\/p>/);
|
|
assert.deepEqual({snapshot,orders},before);
|
|
assert.equal(batch.cutoff,'2026-10-23T03:00:00Z');
|
|
});
|
|
for(const receivedAt of [undefined,null,'','invalid-time','2026-99-99T00:00:00Z',0]){
|
|
test(`${mode} date variable blocks missing or invalid authoritative receivedAt ${JSON.stringify(receivedAt)}`,()=>{
|
|
const orders=[order('valid','2026-10-20T01:00:00Z'),order('invalid',receivedAt)],{snapshot,template}=fixture(mode,orders);
|
|
assert.throws(()=>applyBodyTemplate(snapshot,template,orders,maxBytes),error=>error.status===409&&/日期.*event\.receivedAt/.test(error.message));
|
|
});
|
|
}
|
|
for(const part of ['subject','text','html'])test(`${mode} date validation also applies when only ${part} uses the date variable`,()=>{
|
|
const orders=[order('missing',undefined)],{snapshot,template}=fixture(mode,orders,{subject:'通知 {{订单号}}',text:'{{订单列表}}',html:'{{订单列表}}',[part]:'{{日期}}'});
|
|
assert.throws(()=>applyBodyTemplate(snapshot,template,orders,maxBytes),error=>error.status===409&&/日期.*event\.receivedAt/.test(error.message));
|
|
});
|
|
test(`${mode} missing event still reports the authoritative date field reason`,()=>{
|
|
const orders=[order('missing-event',undefined)],{snapshot,template}=fixture(mode,orders);
|
|
delete orders[0].event;
|
|
assert.throws(()=>applyBodyTemplate(snapshot,template,orders,maxBytes),error=>error.status===409&&/日期.*event\.receivedAt/.test(error.message));
|
|
});
|
|
test(`${mode} templates without a date variable still render legacy orders lacking receivedAt`,()=>{
|
|
const orders=[order('legacy',undefined)],{snapshot,template}=fixture(mode,orders,{subject:'通知 {{订单号}}',text:'{{订单列表}}',html:'{{订单列表}}'});
|
|
const batch=applyBodyTemplate(snapshot,template,orders,maxBytes);
|
|
assert.equal(batch.subject,'通知 legacy');assert.match(batch.text,/订单号:legacy/);
|
|
});
|
|
}
|