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

23 lines
2.7 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {MailService}=require('../../src/modules/mail/service.cjs');
function fixture({verify=()=>{},beforeSend=()=>{},unknown=false}={}){
const rows=[],messages=[],template={id:'template',version:1,status:'active',channelId:'channel',subject:'Template',text:'Hello {{订单号}}',html:'',attachments:[{filename:'private.pdf',content:'cGRm',contentType:'application/pdf'}]};
const store={channelSecret:()=>({id:'channel',version:1,email:'sender@company.test',name:'Sender',smtp:{}}),guard(){},template:()=>({...template}),sends:()=>rows,findSampleSend:(id,key)=>rows.find(r=>r.channel_id===id&&r.template_id===key),beginSend:(id,key)=>{rows.push({id:'send-'+rows.length,channel_id:id,template_id:key,status:'sending'});return rows.at(-1).id;},finishSend:(id,status)=>{rows.find(r=>r.id===id).status=status;}};
const service=new MailService({store,beforeSend,smtpFactory:()=>({verify:async()=>verify(template),sendMail:async message=>{messages.push(message);if(unknown)throw Error('connection lost');return {accepted:['2639266652@qq.com'],rejected:[]};},close(){}})});
return {service,rows,messages,template};
}
test('legacy template test enforces fixed QQ envelope without source attachments',async()=>{
const f=fixture(),result=await f.service.sendTest('channel','template');assert.equal(result.to,'2639266652@qq.com');assert.deepEqual(f.messages[0].to,['2639266652@qq.com']);assert.deepEqual(f.messages[0].cc,[]);assert.deepEqual(f.messages[0].attachments,[]);
});
for(const action of ['sendTest','sendSample'])for(const phase of ['before','after'])test(action+' enforces active period '+phase+' SMTP verification',async()=>{
let verified=false;const f=fixture({verify:()=>{verified=true;},beforeSend:()=>{if(phase==='before'||verified)throw Object.assign(Error('expired'),{status:409});}});
await assert.rejects(()=>action==='sendTest'?f.service.sendTest('channel','template'):f.service.sendSample('channel',{idempotencyKey:'one',subject:'Sample',text:'Hello'}),e=>e.status===409);assert.equal(f.messages.length,0);assert.equal(f.rows.length,0);assert.equal(verified,phase==='after');
});
test('template changed during SMTP verification is not submitted',async()=>{
const f=fixture({verify:t=>{t.version++;}});await assert.rejects(()=>f.service.sendTest('channel','template'),e=>e.status===409);assert.equal(f.messages.length,0);
});
test('uncertain template test cannot be repeated',async()=>{
const f=fixture({unknown:true});await assert.rejects(()=>f.service.sendTest('channel','template'));assert.equal(f.rows[0].status,'unknown');await assert.rejects(()=>f.service.sendTest('channel','template'),e=>e.status===409);assert.equal(f.messages.length,1);
});