96 lines
17 KiB
JavaScript
96 lines
17 KiB
JavaScript
'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');
|
|
const {activateReferenceMapping}=require('../fixtures/activate-field-mapping.cjs');
|
|
const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
|
|
const mixed='订单号:O-100机芯号:CORE-O客户简称:普通客户总数量:10\n业务模式:惠州数字毛利:绿标订单号:H-200机芯号:CORE-H客户简称:惠州创维数字技术有限公司总数量:20';
|
|
async function fixture(t){
|
|
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'hui-ui-')),runtime=await startTestDashboard({stateDir:dir,mailHistory:true}),browser=await loadPlaywright().chromium.launch(browserOptions());
|
|
t.after(async()=>{await browser.close();await runtime.close();await fs.rm(dir,{recursive:true,force:true});});
|
|
const mail=runtime.mail.store,channel=mail.saveChannel({name:'隔离模板邮箱',email:'orders@example.test',senders:['author@example.test'],enabled:false,smtp:{host:'smtp.example.test',port:465,security:'tls',user:'orders',password:'Isolated-test-password'},imap:{host:'imap.example.test',port:993,security:'tls',user:'orders',password:'Isolated-test-password'}});let uid=0;
|
|
function pending(subject){const n=++uid,parsed={subject,scope:'',text:'业务访问 https://example.test/business\n{{订单明细}}',html:'',sender:'author@example.test',attachments:[]},raw=Buffer.from('isolated template '+n);return mail.ingest(channel.id,'isolated',n,parsed,raw,channel.version);}
|
|
function template(mode,subject){const d=pending(subject);return mail.confirm(d.id,{version:d.version,subject,scope:'',notificationMode:mode,useText:false},'admin');}
|
|
const normal=template('normal','普通 {{订单号}}'),hui=template('huizhou','惠州 {{订单号}}');runtime.orderRules.templateResolver=(category,rule,mode='normal')=>require('../../src/modules/mail/rule-template.cjs').ruleTemplate(mail,category,rule,mode);
|
|
const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(6000);
|
|
const errors=[];page.on('pageerror',e=>errors.push(e.message));t.after(()=>assert.deepEqual(errors,[]));
|
|
const base='http://127.0.0.1:'+runtime.server.address().port;
|
|
return {runtime,page,browser,base,normal,hui,pending,async open(route='#order-rules'){await page.goto(base+'/'+route);await login(page);},configure(extra={}){const old=runtime.orderRules.get('rules','bs-change').draft;return runtime.orderRules.saveRule({...old,to:['owner@example.test'],cc:['copy@example.test'],archiveDirectory:'\\\\fixture\\orders',filenameDate:'receivedAt',bodyTemplateId:normal.id,huizhouEnabled:true,huizhouTemplateId:hui.id,...extra},'admin');}};
|
|
}
|
|
|
|
test('Hui selector filters modes, saves and reloads without extra recipients, retaining failed and unsaved edits',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{page,runtime}=f;await f.open();await page.locator('[data-rule-id="bs-change"]').click();
|
|
const toggle=page.getByRole('switch',{name:'额外发送惠州通知',exact:true}),choice=page.getByLabel('惠州邮件模板',{exact:true});
|
|
assert.equal(await toggle.isChecked(),false);assert.equal(await choice.isVisible(),false);
|
|
assert.deepEqual(await page.getByLabel('邮件模板',{exact:true}).locator('option').evaluateAll(options=>options.map(o=>o.value)),['',f.normal.id]);
|
|
await toggle.check();assert.equal(await choice.isVisible(),true);assert.equal(await choice.evaluate(e=>e.required),true);
|
|
assert.deepEqual(await choice.locator('option').evaluateAll(options=>options.map(o=>o.value)),['',f.hui.id]);
|
|
assert.equal(await page.locator('[name=to]').count(),1);assert.equal(await page.locator('[name=cc]').count(),1);assert.equal(await page.locator('[name=attachPdf]').count(),1);
|
|
await choice.selectOption(f.hui.id);await page.getByLabel('收件人',{exact:true}).fill('owner@example.test');await page.getByLabel('抄送人',{exact:true}).fill('copy@example.test');
|
|
await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText('规则草稿已保存。',{exact:true}).waitFor();
|
|
let draft=runtime.orderRules.get('rules','bs-change').draft;assert.equal(draft.huizhouEnabled,true);assert.equal(draft.huizhouTemplateId,f.hui.id);assert.deepEqual(draft.to,['owner@example.test']);assert.deepEqual(draft.cc,['copy@example.test']);
|
|
assert.equal(await toggle.isChecked(),true);assert.equal(await choice.inputValue(),f.hui.id);
|
|
let requests=0;await page.route('**/api/order-rules/rules',async route=>{requests++;await new Promise(r=>setTimeout(r,100));await route.fulfill({status:503,json:{error:'隔离保存失败'}});});
|
|
await toggle.uncheck();await page.getByRole('button',{name:'保存草稿',exact:true}).dblclick();await page.getByText('隔离保存失败',{exact:true}).waitFor();assert.equal(requests,1);
|
|
assert.equal(await toggle.isChecked(),false);assert.equal(await choice.isVisible(),false);assert.equal(await choice.evaluate(e=>e.required),false);assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),true);
|
|
await page.locator('[data-rule-id="bs-notice"]').click();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await toggle.isChecked(),false);
|
|
await page.locator('[data-rule-id="bs-notice"]').click();await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.locator('[data-rule-id="bs-change"]').click();assert.equal(await toggle.isChecked(),true);
|
|
await page.unroute('**/api/order-rules/rules');await toggle.uncheck();await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText('规则草稿已保存。',{exact:true}).waitFor();draft=runtime.orderRules.get('rules','bs-change').draft;assert.equal(draft.huizhouEnabled,false);assert.equal(draft.huizhouTemplateId,f.hui.id);
|
|
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.equal(runtime.orderRules.settings().enabled,false);
|
|
});
|
|
|
|
test('viewers can inspect Hui choices but cannot edit rules or confirm template modes',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{page,runtime}=f;f.configure();runtime.store.addUser({username:'viewer',password:'Test-password-123',role:'viewer'},'admin');
|
|
await page.goto(f.base+'/#order-rules');await page.getByLabel('账号',{exact:true}).fill('viewer');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.locator('[data-rule-id="bs-change"]').click();
|
|
assert.equal(await page.getByRole('switch',{name:'额外发送惠州通知',exact:true}).isDisabled(),true);assert.equal(await page.getByLabel('惠州邮件模板',{exact:true}).isDisabled(),true);assert.equal(await page.getByRole('button',{name:'保存草稿',exact:true}).count(),0);
|
|
const pending=f.pending('只读模板');await page.goto(f.base+'/#templates');await page.locator('[data-template="'+pending.id+'"]').click();assert.equal(await page.getByLabel('通知模式',{exact:true}).isDisabled(),true);assert.equal(await page.getByRole('button',{name:'确认生效',exact:true}).count(),0);
|
|
});
|
|
|
|
test('template confirmation sends selected Hui mode and lists mode-specific published usage',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{page,runtime}=f,rule=f.configure(),record=runtime.orderRules.get('rules',rule.id);record.published={...rule,status:'enabled'};runtime.orderRules.put('rules',record);
|
|
const pending=f.pending('新惠州模板');await f.open('#templates');await page.locator('[data-template="'+pending.id+'"]').click();await page.getByLabel('通知模式',{exact:true}).selectOption('huizhou');assert.match(await page.locator('#template-review .card-header').innerText(),/惠州通知/);assert.doesNotMatch(await page.locator('#template-review .card-header').innerText(),/普通通知/);
|
|
await page.getByRole('button',{name:'关闭',exact:true}).click();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await page.getByLabel('通知模式',{exact:true}).inputValue(),'huizhou');
|
|
let failed=true;await page.route('**/api/mail/templates/'+pending.id+'/confirm',r=>failed?r.fulfill({status:503,json:{error:'隔离模板确认失败'}}):r.continue());await page.getByRole('button',{name:'确认生效',exact:true}).click();await page.getByText('隔离模板确认失败',{exact:true}).waitFor();assert.equal(await page.getByLabel('通知模式',{exact:true}).inputValue(),'huizhou');assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),true);
|
|
failed=false;await page.getByRole('button',{name:'确认生效',exact:true}).click();await page.getByText('模板已确认生效。',{exact:true}).waitFor();assert.equal(runtime.mail.store.template(pending.id).notificationMode,'huizhou');assert.equal(runtime.mail.store.template(f.normal.id).status,'active');
|
|
await page.getByLabel('模板状态',{exact:true}).selectOption('active');const normalRow=page.locator('tr').filter({has:page.locator('[data-template="'+f.normal.id+'"]')});assert.match(await normalRow.innerText(),/普通通知/);
|
|
const huiRow=page.locator('tr').filter({has:page.locator('[data-template="'+pending.id+'"]')});assert.match(await huiRow.innerText(),/惠州通知/);
|
|
const usage=page.locator('#category-templates .list-group-item').filter({hasText:'备损订单更改通知书'});assert.match(await usage.innerText(),/普通通知:普通 \{\{订单号\}\}/);assert.match(await usage.innerText(),/惠州通知:惠州 \{\{订单号\}\}/);assert.doesNotMatch(await usage.innerText(),/新惠州模板/);
|
|
});
|
|
|
|
test('real trial separates mixed Hui mail and exposes no-match and uncertain selections',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{runtime,page}=f,reference=require('../../src/modules/order-rules/catalog.cjs').adapters.find(a=>a.id==='bs-change');activateReferenceMapping(runtime.orderRules,'bs-change',{title:mixed,fields:{[reference.fields.orderNo]:'O-100'}});f.configure();await f.open();await page.locator('[data-rule-id="bs-change"]').click();await page.getByRole('button',{name:'检查识别结果',exact:true}).click();
|
|
const normal=page.locator('[data-notification-preview="normal"]'),hui=page.locator('[data-notification-preview="huizhou"]');await hui.waitFor();
|
|
assert.match(await normal.locator('pre').textContent(),/O-100/);assert.match(await normal.locator('pre').textContent(),/H-200/);assert.match(await hui.locator('pre').textContent(),/H-200/);assert.doesNotMatch((await hui.locator('dd').first().textContent())+(await hui.locator('pre').textContent()),/O-100|CORE-O/);assert.match(await hui.textContent(),/owner@example.test/);assert.match(await hui.textContent(),/copy@example.test/);assert.equal(await hui.locator('dd').last().textContent(),await normal.locator('dd').last().textContent());assert.match(await hui.locator('dd').last().textContent(),/不附带 PDF/);assert.doesNotMatch(await page.locator('#order-rule-preview-result').textContent(),/额外通知规则尚未确认/);
|
|
await fs.mkdir('output/verification',{recursive:true});await page.screenshot({path:'output/verification/huizhou-task3-preview-desktop.png',fullPage:true});
|
|
for(const [title,status] of [['订单号:O-100客户简称:普通客户','empty'],['订单号:HA200客户简称:未知客户','blocked']]){
|
|
// Use the real capture/mapping pipeline for each selection; storage is isolated per test.
|
|
const sample={...structuredClone(reference.sample),demo:false,title,taskId:'ui-'+status,fields:{...reference.sample.fields,[reference.fields.orderNo]:status==='empty'?'O-100':'HA200'}};
|
|
const captured=runtime.orderRules.recordCapture({sample,taskId:'ui-'+status,snapshot:{processVersion:sample.processVersion,fields:Object.entries(sample.fields).map(([path,value])=>({path,value}))}});
|
|
await page.getByRole('button',{name:'刷新规则与字段',exact:true}).click();await page.locator('[data-rule-id="bs-change"]').click();await page.locator('#order-rule-sample-source').selectOption('capture:'+captured.id);await page.getByRole('button',{name:'检查识别结果',exact:true}).click();await page.locator('[data-notification-preview="huizhou"][data-notification-status="'+status+'"]').waitFor();
|
|
assert.match(await page.locator('[data-notification-preview="huizhou"]').textContent(),status==='empty'?/未匹配惠州明细/:/需核实|不能|无法|不足|确认/);
|
|
}
|
|
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);await page.screenshot({path:'output/verification/huizhou-task3-preview-mobile.png',fullPage:true});
|
|
});
|
|
|
|
test('record lists and details show independent results and batch modes without changing recovery targets',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{runtime,page}=f;
|
|
runtime.orderRules.put('orders',{id:'dual-order',categoryId:'bs-change',status:'sent',batchId:'normal-batch',event:{connectionId:'c',orderNo:'O-100',receivedAt:'2026-10-21T01:00:00Z'},notifications:{huizhou:{status:'unknown',batchId:'hui-batch',error:'惠州连接结果不明',orderNos:['H-200'],issues:['仅惠州待核实']}},createdAt:'2026-10-21T01:00:00Z'});
|
|
for(const [id,mode,status] of [['normal-batch','normal','sent'],['hui-batch','huizhou','unknown']])runtime.orderRules.put('batches',{id,version:1,notificationMode:mode,status,categoryId:'bs-change',subject:id,orderIds:['dual-order'],orderNos:mode==='huizhou'?['H-200']:['O-100','H-200'],text:'冻结正文',to:['owner@example.test'],cc:[],attachments:[]});
|
|
await f.open('#notification-orders');const row=page.locator('tr').filter({has:page.getByText('O-100',{exact:true})});await row.waitFor();assert.match(await row.innerText(),/普通通知.*已提交邮件服务器/s);assert.match(await row.innerText(),/惠州通知.*待核实/s);
|
|
await row.getByRole('button',{name:'查看详情',exact:true}).click();const dialog=page.getByRole('dialog',{name:'业务详情'});await dialog.getByRole('heading',{name:'惠州通知',exact:true}).waitFor();assert.match(await dialog.textContent(),/H-200/);assert.match(await dialog.textContent(),/惠州连接结果不明/);assert.equal(await dialog.getByRole('button',{name:'人工补发原冻结批次',exact:true}).count(),0);
|
|
await page.getByRole('button',{name:'关闭详情',exact:true}).click();await page.goto(f.base+'/#notification-batches?scope=all');const batch=page.locator('tr').filter({has:page.getByText('hui-batch',{exact:true})});await batch.waitFor();assert.match(await batch.innerText(),/惠州通知/);await batch.getByRole('button',{name:'查看详情',exact:true}).click();await dialog.getByRole('heading',{name:'保存核验结论',exact:true}).waitFor();assert.match(await dialog.textContent(),/通知模式惠州通知/);assert.equal(await dialog.getByRole('button',{name:'人工补发原冻结批次',exact:true}).count(),0);
|
|
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);
|
|
});
|
|
|
|
test('order list retains execution failure and conflict causes beside independent notification results',{timeout:90000},async t=>{
|
|
const f=await fixture(t),{runtime,page}=f;
|
|
const cases=[{id:'execution-failed',status:'failed',error:'归档读取校验失败,通知尚待处理'},{id:'execution-conflict',status:'blocked',error:'新来源修订与已归档版本冲突,请核对',conflict:true}];
|
|
for(const item of cases){
|
|
runtime.orderRules.put('orders',{id:item.id,categoryId:'bs-change',status:'sent',error:'普通路已有结果说明',event:{connectionId:'c',orderNo:item.id,receivedAt:'2026-10-21T01:00:00Z'},notifications:{huizhou:{status:'pending',batchId:null,error:'惠州路独立等待原因',orderNos:['H-200'],issues:[]}}});
|
|
runtime.orderRules.put('execution-tasks',{id:'task-'+item.id,orderId:item.id,connectionId:'c',version:1,status:item.status,error:item.error,conflict:item.conflict||false,row:{orderNo:item.id}});
|
|
}
|
|
runtime.orderRules.put('execution-tasks',{id:'bare-execution',categoryIds:['bs-change'],connectionId:'c',version:1,status:'failed',error:'尚未生成订单的采集失败',row:{orderNo:'bare-execution'}});
|
|
await f.open('#notification-orders');
|
|
for(const item of cases){const row=page.locator('tr').filter({has:page.getByText(item.id,{exact:true})});await row.waitFor();const text=await row.innerText();assert.ok(text.includes(item.error),text);assert.match(text,/普通通知.*已提交邮件服务器/s);assert.match(text,/惠州通知.*等待汇总/s);assert.match(text,/普通路已有结果说明/);assert.match(text,/惠州路独立等待原因/);}
|
|
const bare=page.locator('tr').filter({has:page.getByText('bare-execution',{exact:true})});assert.match(await bare.innerText(),/尚未生成订单的采集失败/);assert.doesNotMatch(await bare.innerText(),/普通通知|惠州通知/);
|
|
});
|