Files
order-notification-automation/tests/order-rules/scheduled-mail-policy.test.cjs
T

75 lines
7.7 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {buildBatch}=require('../../src/modules/order-rules/batch.cjs');
const {applyBodyTemplate}=require('../../src/modules/order-rules/template.cjs');
const {ruleTemplate}=require('../../src/modules/mail/rule-template.cjs');
const {OrderRuleDispatcher}=require('../../src/modules/order-rules/dispatcher.cjs');
const cutoff='2026-10-23T03:00:00.000Z';
const rule={id:'native',categoryId:'native',version:3,name:'订单通知',intro:'大家好',signature:'业务部',attachPdf:false,to:['2639266652@qq.com'],cc:[],bodyTemplateId:'template-1'};
function order(){return {id:'o1',status:'pending',ruleSnapshot:structuredClone(rule),event:{orderNo:'O-1',receivedAt:'2026-10-23T02:00:00Z',productLine:'终端',brief:'前置来源\n订单号:O-1 客户简称:甲\n总数量:0\n订单号:O-2 客户简称:乙',titleOrders:[{orderNo:'O-1',text:'前置来源\n订单号:O-1 客户简称:甲\n总数量:0'},{orderNo:'O-2',text:'订单号:O-2 客户简称:乙'}]},archive:{archivedAt:'2026-10-23T02:30:00Z',files:[{name:'O-1.pdf',path:'C:\\archive\\O-1.pdf',size:100,sha256:'a'.repeat(64),kind:'pdf'},{name:'notice.txt',path:'C:\\archive\\notice.txt',size:10,sha256:'b'.repeat(64),kind:'notice'}]}};}
const template={id:'template-1',version:5,status:'active',bindingVersion:3,subject:'{{类别名称}} {{订单号}}',text:'各位好:\n固定说明\n{{订单列表}}\n团队签名',html:'<p>各位好:</p><p>固定说明</p><div>{{订单列表}}</div><p>团队签名</p>',attachments:[]};
test('native scheduled order list has one line per observed title order and archive directories outside list',()=>{
const input=order(),raw=structuredClone(input),snapshot=buildBatch(rule,[input],cutoff),batch=applyBodyTemplate(snapshot,template,[input],20971520);
assert.equal(batch.text,'各位好:\n固定说明\n订单号:O-1 客户简称:甲 总数量:0\n订单号:O-2 客户简称:乙\n团队签名\n\n存放地址:C:\\archive');
assert.deepEqual(batch.orderNos,['O-1','O-2']);assert.deepEqual(batch.attachments,[]);assert.deepEqual(input,raw);
assert.match(batch.html,/href="file:\/\/\/C:\/archive"/);assert.doesNotMatch(batch.text,/前置来源|\.pdf|\.txt/);
assert.deepEqual(batch.template,{id:'template-1',version:5,bindingVersion:3});
});
test('legacy per-order text recovery keeps declared title order instead of primary order order',()=>{
const input=order();input.event.titleOrders=[{orderNo:'O-2'},{orderNo:'O-1'}];
const snapshot=buildBatch(rule,[input],cutoff);
assert.deepEqual(snapshot.orderNos,['O-2','O-1']);assert.equal(snapshot.text.split('\n\n')[1],'订单号:O-2客户简称:乙\n订单号:O-1客户简称:甲总数量:0');
});
test('new native freeze rejects legacy attach-PDF intent and still requires archived PDF evidence',()=>{
assert.throws(()=>buildBatch({...rule,attachPdf:true},[order()],cutoff),/附件|attachPdf/);
const input=order();input.archive.files=input.archive.files.filter(f=>f.kind!=='pdf');
assert.throws(()=>buildBatch(rule,[input],cutoff),/PDF/);
});
test('native template cannot silently discard configured attachments',()=>{
const input=order(),snapshot=buildBatch(rule,[input],cutoff);
assert.throws(()=>applyBodyTemplate(snapshot,{...template,attachments:[{filename:'x.pdf'}]},[input],20971520),/附件/);
});
test('template archive placeholder preserves one label and clickable link',()=>{
const input=order(),snapshot=buildBatch(rule,[input],cutoff);
const batch=applyBodyTemplate(snapshot,{...template,html:'<p>{{订单列表}}</p><p>存放地址:{{存放地址}}</p>'},[input],20971520);
assert.equal((batch.html.match(/存放地址/g)||[]).length,1);assert.match(batch.html,/href="file:\/\/\/C:\/archive"/);
});
test('text-only template still renders archive links in HTML without changing greeting or signature',()=>{
const input=order(),snapshot=buildBatch(rule,[input],cutoff);
const batch=applyBodyTemplate(snapshot,{...template,html:''},[input],20971520);
assert.match(batch.html,/各位好:/);assert.match(batch.html,/团队签名/);assert.match(batch.html,/href="file:\/\/\/C:\/archive"/);
});
test('text-only template preserves nested archive directory links without rewriting generated HTML',()=>{
const input=order();input.archive.files.push({...input.archive.files[0],path:'C:\\archive\\sub\\O-2.pdf'});
const snapshot=buildBatch(rule,[input],cutoff),batch=applyBodyTemplate(snapshot,{...template,html:''},[input],20971520);
assert.match(batch.html,/href="file:\/\/\/C:\/archive">C:\\archive<\/a>/);
assert.match(batch.html,/href="file:\/\/\/C:\/archive\/sub">C:\\archive\\sub<\/a>/);
});
test('direct category template records immutable rule binding version and fails unresolved binding',()=>{
const resolved=ruleTemplate({template:()=>({...structuredClone(template),bindingVersion:91})},rule.categoryId,rule);
assert.equal(resolved.bindingVersion,3);resolved.bindingVersion=99;assert.equal(rule.version,3);
assert.throws(()=>ruleTemplate({template:()=>null},rule.categoryId,rule),/模板/);
});
test('one frozen category cannot mix body template bindings even with the same rule version',()=>{
const one=order(),two=order();two.id='o2';two.ruleSnapshot.bodyTemplateId='other-template';
assert.throws(()=>buildBatch(rule,[one,two],cutoff),/模板|版本/);
});
function dispatcherFixture(){
const result={smtp:0,begun:0,reads:0,finished:[],messages:[]},config={enabled:true,channelId:'mail',restrictedTest:false};
const store={settings:()=>config,snapshot:()=>({capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),discoveryBlockers:()=>[],batchIncomplete:()=>false,beginSending:()=>result.begun++,finishSending:(id,status,message)=>result.finished.push({id,status,message})};
const mail={store:{channelSecret:()=>({id:'mail',version:1,email:'sender@example.test',smtp:{}}),guard:()=>{}},smtpFactory:()=>{result.smtp++;return {verify:async()=>{},sendMail:async message=>{result.messages.push(message);return {accepted:[...message.to,...message.cc],rejected:[]};},close:()=>{}};}};
const dispatcher=new OrderRuleDispatcher({store,mail,settings:()=>({}),clock:()=>new Date(cutoff),providersReady:()=>true,attachmentReader:async()=>{result.reads++;return Buffer.from('x');}});
const batch={id:'b1',status:'prepared',cutoff,to:['2639266652@qq.com'],cc:[],attachments:[],subject:'订单',text:'正文',html:'<p>正文</p>'};return {dispatcher,result,config,batch};
}
for(const mutate of [b=>b.to=['other@qq.com'],b=>b.to=['2639266652@qq.com','other@qq.com'],b=>b.cc=['copy@qq.com'],b=>b.attachments=[{name:'old.pdf',path:'C:\\archive\\old.pdf',size:1,sha256:'a'.repeat(64),kind:'pdf'}]])test('SMTP server policy blocks unsafe frozen envelope '+String(mutate),async()=>{
const f=dispatcherFixture();mutate(f.batch);const frozen=structuredClone(f.batch);await f.dispatcher.dispatch(f.batch,f.config);
assert.equal(f.result.smtp,0);assert.equal(f.result.begun,0);assert.equal(f.result.reads,0);assert.equal(f.result.finished[0].status,'failed');assert.match(f.result.finished[0].message,/QQ|禁止.*附件/);assert.deepEqual(f.batch,frozen);
});
test('SMTP dispatch accepts only QQ without Cc or attachments even when restricted config disabled',async()=>{
const f=dispatcherFixture();await f.dispatcher.dispatch(f.batch,f.config);
assert.equal(f.result.finished[0].status,'sent');assert.deepEqual(f.result.messages[0].to,['2639266652@qq.com']);assert.deepEqual(f.result.messages[0].cc,[]);assert.deepEqual(f.result.messages[0].attachments,[]);
});
for(const status of ['sent','unknown'])test('dispatch cannot replay immutable '+status+' batch',async()=>{
const f=dispatcherFixture();f.batch.status=status;await f.dispatcher.dispatch(f.batch,f.config);assert.equal(f.result.smtp,0);assert.equal(f.result.begun,0);assert.deepEqual(f.result.finished,[]);
});