'use strict'; const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'); const {MailStore}=require('../../src/modules/mail/store.cjs'); const config={name:'隔离邮箱',email:'orders@example.test',smtp:{host:'smtp.example.test',port:465,security:'tls',user:'orders',password:'test-only-password'},imap:{host:'imap.example.test',port:993,security:'tls',user:'orders',password:'test-only-password'},senders:['author@example.test'],enabled:false}; function fixture(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'mail-edit-'));let store=new MailStore(dir);const channel=store.saveChannel(config);t.after(()=>{store.close();assert.equal(path.dirname(fs.realpathSync(dir)).toLowerCase(),fs.realpathSync(os.tmpdir()).toLowerCase());fs.rmSync(dir,{recursive:true,force:true});});return {get store(){return store;},channel,reopen(){store.close();store=new MailStore(dir);}};} test('editable HTML templates retain their ID, history and category bindings across restart',t=>{ const f=fixture(t),s=f.store,input={channelId:f.channel.id,subject:'通知 {{订单号}}',scope:'试产',notificationMode:'normal',format:'html',body:'
大家好:
| {{订单明细}} |
更新 &内容
Best Regards!
测试签名 &团队
'}}); assert.equal(saved.state.smtp,true);assert.equal(s.channelSecret(c.id).smtp.password,config.smtp.password);assert.match(saved.signature.text,/Best Regards!/);assert.doesNotMatch(saved.signature.html,/script|bad\(/); const {appendSignature}=require('../../src/modules/mail/content.cjs'),base={subject:'通知',text:'订单号:TEST',html:'订单号:TEST
',attachments:[]};const frozen=appendSignature(base,s.signature(c.id),100000);assert.match(frozen.text,/测试签名.*&团队/);assert.equal(frozen.signature.version,saved.version);assert.equal(appendSignature(frozen,s.signature(c.id),100000).html,frozen.html); s.saveChannel({...config,id:c.id,version:saved.version,signature:{enabled:true,format:'text',body:'新签名'}});assert.doesNotMatch(frozen.text,/新签名/);f.reopen();assert.equal(f.store.publicChannel(c.id).signature.text,'新签名');assert.equal(f.store.signature(c.id).channelId,c.id); }); test('invalid template bodies, variable names and incompatible live modes are rejected without a revision',t=>{ const f=fixture(t),s=f.store,input={channelId:f.channel.id,subject:'通知',scope:'',notificationMode:'normal',format:'text',body:'正文 {{订单列表}}'};let t1=s.saveTemplate(null,input,'admin');t1=s.confirm(t1.id,{version:t1.version,subject:t1.subject,scope:'',notificationMode:'normal',useText:false},'admin');const before=s.templates(); for(const patch of [{body:'{{不存在}}'},{format:'html',body:''},{notificationMode:'huizhou'},{body:'x'.repeat(200001)}])assert.throws(()=>s.saveTemplate(t1.id,{version:t1.version,...input,...patch},'admin')); assert.deepEqual(s.templates(),before);assert.throws(()=>s.saveChannel({...config,id:f.channel.id,version:f.channel.version,signature:{enabled:true,format:'html',body:''}}));assert.equal(s.publicChannel(f.channel.id).version,f.channel.version); }); test('new editing and preview APIs enforce admin permissions without SMTP side effects',async()=>{ const {mailApi}=require('../../src/modules/mail/api.cjs');for(const role of ['viewer','operator'])for(const route of ['/api/mail/templates','/api/mail/templates/test/save','/api/mail/preview'])await assert.rejects(mailApi({mail:{store:{}},user:{username:role,role},route,method:'POST',body:{},reply(){throw Error('unexpected write');}}),e=>e.status===403); });