'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path');
const {startTestDashboard,login}=require('../fixtures/dashboard-runtime.cjs'),{loadPlaywright,browserOptions}=require('../../src/platform/browser.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};
async function setup(t){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'template-editor-ui-')),runtime=await startTestDashboard({stateDir:dir,mailHistory:true}),browser=await loadPlaywright().chromium.launch(browserOptions());t.after(async()=>{await browser.close();await runtime.close();assert.equal(path.dirname(await fs.realpath(dir)).toLowerCase(),(await fs.realpath(os.tmpdir())).toLowerCase());await fs.rm(dir,{recursive:true,force:true});});const channel=runtime.mail.store.saveChannel(config),page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(10000);const base='http://127.0.0.1:'+runtime.server.address().port;return {runtime,page,base,channel};}
test('active templates edit HTML, preview safely and retain binding after reload without sending mail',{timeout:90000},async t=>{
const {runtime,page,base,channel}=await setup(t),store=runtime.mail.store;let template=store.saveTemplate(null,{channelId:channel.id,subject:'原模板',scope:'海外',notificationMode:'normal',format:'text',body:'大家好:\n{{订单明细}}'},'admin');template=store.confirm(template.id,{version:template.version,subject:template.subject,scope:template.scope,notificationMode:'normal',useText:false},'admin');
await page.goto(base+'/#templates');await login(page);await page.getByRole('button',{name:'查看版本',exact:true}).click();await page.getByRole('button',{name:'编辑模板',exact:true}).click();
await page.getByLabel('正文格式',{exact:true}).selectOption('html');await page.getByLabel('HTML正文(源码)',{exact:true}).fill('
大家好:
');await page.getByLabel('模板标题',{exact:true}).fill('更新模板 {{订单号}}');
await page.getByRole('button',{name:'预览正文',exact:true}).click();await page.frameLocator('iframe[title="编辑正文预览"]').getByText('{{订单明细}}',{exact:true}).waitFor();
assert.equal(await page.frameLocator('iframe[title="编辑正文预览"]').getByText('大家好:',{exact:true}).evaluate(e=>getComputedStyle(e).color),'rgb(18, 52, 86)');
await page.route('**/api/mail/templates/'+template.id+'/save',r=>r.fulfill({status:503,json:{error:'隔离保存失败'}}));await page.getByRole('button',{name:'保存模板',exact:true}).click();await page.getByText('隔离保存失败',{exact:true}).waitFor();assert.equal(store.template(template.id).version,template.version);assert.match(await page.getByLabel('HTML正文(源码)',{exact:true}).inputValue(),//);await page.getByRole('button',{name:'取消',exact:true}).click();await page.getByRole('button',{name:'留在此页',exact:true}).click();await page.unroute('**/api/mail/templates/'+template.id+'/save');
await page.getByRole('button',{name:'保存模板',exact:true}).click();await page.getByText('模板已保存。',{exact:true}).waitFor();assert.equal(store.template(template.id).version,template.version+1);assert.doesNotMatch(store.template(template.id).html,/script|bad\(/);assert.equal(store.template(template.id).status,'active');
await page.reload();await page.getByRole('button',{name:'查看版本',exact:true}).click();await page.getByRole('button',{name:'编辑模板',exact:true}).click();assert.equal(await page.getByLabel('模板标题',{exact:true}).inputValue(),'更新模板 {{订单号}}');assert.match(await page.getByLabel('HTML正文(源码)',{exact:true}).inputValue(),//);assert.deepEqual(store.sends(),[]);
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);
});
test('new page templates save as drafts and HTML account signatures survive account reload',{timeout:90000},async t=>{
const {runtime,page,base,channel}=await setup(t),store=runtime.mail.store,seed=store.saveTemplate(null,{channelId:channel.id,subject:'已有模板',scope:'已有',notificationMode:'normal',format:'text',body:'{{订单明细}}'},'admin');store.confirm(seed.id,{version:seed.version,subject:seed.subject,scope:seed.scope,notificationMode:'normal',useText:false},'admin');await page.goto(base+'/#templates');await login(page);await page.getByRole('button',{name:'新建模板',exact:true}).click();await page.getByLabel('模板标题',{exact:true}).fill('新建业务模板');await page.getByRole('button',{name:'保存模板',exact:true}).click();await page.getByText('模板已保存。',{exact:true}).waitFor();assert.equal(store.templates()[0].status,'pending');assert.equal(await page.getByLabel('模板状态',{exact:true}).inputValue(),'pending');
await page.getByRole('link',{name:'邮箱管理',exact:true}).click();await page.getByRole('button',{name:'管理邮箱',exact:true}).click();await page.getByLabel('启用邮件签名',{exact:true}).check();await page.getByLabel('签名格式',{exact:true}).selectOption('html');await page.getByLabel('签名内容',{exact:true}).fill('Best Regards!
隔离测试团队
');await page.getByRole('button',{name:'保存邮箱',exact:true}).click();await page.getByText('邮箱已保存。',{exact:true}).waitFor();
await page.reload();await page.getByRole('button',{name:'管理邮箱',exact:true}).click();assert.equal(await page.getByLabel('签名格式',{exact:true}).inputValue(),'html');assert.match(await page.getByLabel('签名内容',{exact:true}).inputValue(),/隔离测试团队/);assert.match(store.signature(channel.id).text,/Best Regards!/);assert.equal(store.channelSecret(channel.id).smtp.password,config.smtp.password);assert.deepEqual(store.sends(),[]);
});