Files
order-notification-automation/tests/order-rules/business-mail-content.test.cjs
T

48 lines
5.6 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {extractTitle}=require('../../src/modules/order-rules/title.cjs');
const {buildBatch}=require('../../src/modules/order-rules/batch.cjs');
const {applyBodyTemplate}=require('../../src/modules/order-rules/template.cjs');
const rule={id:'overseas-notice',version:1,categoryId:'overseas-notice',name:'海外订单通知书',intro:'大家好:\n\n以下为海外订单通知书,请查收!',signature:'Best Regards!\n示例团队',to:['team@example.test'],cc:[],attachPdf:false};
const title='订单号:TEST-100机芯号:CORE-A产品型号:MODEL-A客户简称:示例甲出货方式:CBU总数量:0订单号:HA100机芯号:CORE-B产品型号:MODEL-B客户简称:示例乙出货方式:SKD总数量:200';
function order(id,brief=title,directory='\\\\example-nas\\orders\\overseas'){return {id,status:'pending',event:{orderNo:id,brief,receivedAt:'2026-10-20T01:00:00Z'},ruleSnapshot:rule,archive:{archivedAt:'2026-10-20T01:01:00Z',files:[{name:id+'.pdf',path:directory+'\\'+id+'.pdf',size:100,sha256:'a'.repeat(64),kind:'pdf'}]}};}
test('title output separates shipping and customer for every summary row and keeps zero and missing distinct',()=>{
const result=extractTitle(title);assert.equal(result.issues.length,0);
assert.deepEqual(result.orders.map(({orderNo,coreNo,productModel,customerShort,shippingMethod,totalQuantity})=>({orderNo,coreNo,productModel,customerShort,shippingMethod,totalQuantity})),[
{orderNo:'TEST-100',coreNo:'CORE-A',productModel:'MODEL-A',customerShort:'示例甲',shippingMethod:'CBU',totalQuantity:'0'},
{orderNo:'HA100',coreNo:'CORE-B',productModel:'MODEL-B',customerShort:'示例乙',shippingMethod:'SKD',totalQuantity:'200'}]);
assert.equal(extractTitle('订单号:TEST-2客户简称:示例丙总数量:5').orders[0].shippingMethod,undefined);assert.equal(result.brief.includes('出货方式:CBU'),true);
});
test('overseas mail has summary paragraphs and one actual directory after the list without per-file clutter',()=>{
const one=order('TEST-100'),two=order('TEST-200','订单号:TEST-200机芯号:C产品型号:M客户简称:示例丙出货方式:CBU总数量:5');
const batch=buildBatch(rule,[one,two],'2026-10-20T03:00:00Z');
assert.equal(batch.text,[rule.intro,title.replace('订单号:HA100','\n订单号:HA100')+'\n'+two.event.brief,'存放地址:\\\\example-nas\\orders\\overseas',rule.signature].join('\n\n'));
assert.equal((batch.text.match(/存放地址/g)||[]).length,1);assert.doesNotMatch(batch.text,/\.pdf/);assert.equal(batch.attachments.length,0);assert.deepEqual(batch.orderIds,['TEST-100','TEST-200']);
assert.match(batch.html,/file:\/\/example-nas\/orders\/overseas/);assert.doesNotMatch(batch.html,/TEST-100\.pdf/);
});
test('overseas directory comes from frozen evidence and all actual directories are retained and escaped',()=>{
const batch=buildBatch({...rule,archiveDirectory:'\\\\wrong\\configured'},[order('A','订单号:A客户简称:<script>','\\\\example-nas\\orders\\one'),order('B','订单号:B','\\\\example-nas\\orders\\two')],'2026-10-20T03:00:00Z');
assert.match(batch.text,/orders\\one/);assert.match(batch.text,/orders\\two/);assert.doesNotMatch(batch.text,/wrong/);assert.doesNotMatch(batch.html,/<script>/);assert.match(batch.html,/&lt;script&gt;/);
});
test('business templates receive separate summary directory and category variables without duplicating content',()=>{
const orders=[order('A','订单号:A机芯号:C出货方式:CBU总数量:5')],snapshot=buildBatch(rule,orders,'2026-10-20T03:00:00Z');
const template={id:'t',version:1,text:'以下为{{类别名称}}\n\n{{订单明细}}\n\n存放地址:{{存放地址}}\n\n团队签名',html:'<p>{{类别名称}}</p><div>{{订单明细}}</div><p>存放地址:{{存放地址}}</p>',subject:'{{类别名称}}',attachments:[]};
const batch=applyBodyTemplate(snapshot,template,orders,20971520);
assert.equal(batch.text,'以下为海外订单通知书\n\n订单号:A机芯号:C出货方式:CBU总数量:5\n\n存放地址:\\\\example-nas\\orders\\overseas\n\n团队签名');
assert.equal((batch.html.match(/订单号:A/g)||[]).length,1);assert.doesNotMatch(batch.text,/\.pdf/);assert.deepEqual(batch.to,['team@example.test']);
});
test('a template that omits business variables still retains the complete overseas summary and directory',()=>{
const orders=[order('A','订单号:A客户简称:<b>')],snapshot=buildBatch(rule,orders,'2026-10-20T03:00:00Z');
const batch=applyBodyTemplate(snapshot,{id:'t',version:1,text:'固定前言',html:'<p>固定前言</p>',attachments:[]},orders,20971520);
assert.match(batch.text,/订单号:A/);assert.match(batch.text,/存放地址:\\\\example-nas\\orders\\overseas/);assert.match(batch.html,/&lt;b&gt;/);assert.doesNotMatch(batch.html,/<b>/);
});
test('legacy summary objects without text never discard the recorded notification brief',()=>{
const item=order('A','订单号:A客户简称:保留旧摘要');item.event.titleOrders=[{orderNo:'A'}];
assert.match(buildBatch(rule,[item],'2026-10-20T03:00:00Z').text,/保留旧摘要/);
});
test('the order-list variable keeps order lines separate from clickable archive directories',()=>{
const orders=[order('A','订单号:A')],snapshot=buildBatch(rule,orders,'2026-10-20T03:00:00Z');
const batch=applyBodyTemplate(snapshot,{id:'legacy',version:1,text:'旧格式\n{{订单列表}}',html:'<p>旧格式</p>{{订单列表}}',attachments:[]},orders,20971520);
assert.equal(batch.text,'旧格式\n订单号:A\n\n存放地址:\\\\example-nas\\orders\\overseas');assert.match(batch.html,/href="file:\/\/example-nas\/orders\/overseas"/);assert.equal((batch.html.match(/订单号:A/g)||[]).length,1);
});