Files
order-notification-automation/tests/app/web-closure-review-ui.test.cjs
T

49 lines
21 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'),{loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
async function fixture(t,hash='schedule'){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'closure-review-')),runtime=await startTestDashboard({stateDir:dir}),browser=await loadPlaywright().chromium.launch(browserOptions());t.after(async()=>{await browser.close();await runtime.close();await fs.rm(dir,{recursive:true,force:true});});const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(6000);await page.goto('http://127.0.0.1:'+runtime.server.address().port+'/#'+hash);await login(page);return {page,runtime};}
test('a successful delayed save commits only submitted inputs and later edits remain dirty',{timeout:45000},async t=>{const {page}=await fixture(t);await page.locator('#schedule-form').waitFor();let release,arrive;const arrived=new Promise(r=>arrive=r),gate=new Promise(r=>release=r);await page.route('**/api/settings',async route=>{if(route.request().method()==='GET')return route.continue();const response=await route.fetch();arrive();await gate;return route.fulfill({response});});await page.locator('#time-first').fill('09:11');await page.getByRole('button',{name:'保存发送计划',exact:true}).click();await arrived;await page.locator('#time-first').fill('09:12');release();await page.getByText('发送计划已保存。',{exact:true}).waitFor();assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),true);await page.getByRole('link',{name:'业务日历',exact:true}).first().click();await page.getByRole('dialog',{name:'未保存的修改'}).waitFor();assert.equal(await page.locator('#time-first').inputValue(),'09:12');});
for(const destination of ['internal','route'])test('Save and Continue preserves currentTarget for rule settings: '+destination,{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');await openRuleRoute(page,'notification-settings');await page.locator('#order-rule-settings').waitFor();const input=page.locator('#order-rule-settings [name=maxSize]');await input.fill('12');const posts=[];await page.route('**/api/order-rules/settings',async route=>{posts.push(route.request().postDataJSON());return route.continue();});if(destination==='internal')await openRuleRoute(page,'order-notifications');else await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();if(destination==='internal')await page.getByRole('heading',{name:'本时段范围',exact:true}).waitFor();else await page.locator('#schedule-form').waitFor();assert.equal(posts.length,1);assert.equal(posts[0].maxMessageBytes,12582912);});
async function mailFixture(page){const drafts=['t1','t2'].map((id,i)=>({id,version:1,status:'pending',subject:'模板'+(i+1),scope:'',channelId:'mail1',sender:'fixture@example.test',attachmentCount:0,attachments:[],text:'正文'}));await page.route('**/api/mail',route=>route.fulfill({json:{channels:[],templates:drafts,receipts:[],sends:[]}}));await page.route('**/api/mail/templates/categories',route=>route.fulfill({json:{categories:[],bindings:[]}}));await page.route(/\/api\/mail\/templates\/t[12]$/,route=>route.fulfill({json:drafts.find(d=>route.request().url().endsWith('/'+d.id))}));await page.route('**/api/mail/templates/*/preview',route=>route.fulfill({contentType:'text/html',body:'<p>正文</p>'}));return drafts;}
test('switching a template immediately retires its old editable form while the next detail loads',{timeout:45000},async t=>{
const {page}=await fixture(t),drafts=await mailFixture(page);await page.getByRole('link',{name:'邮件模板',exact:true}).click();await page.locator('[data-template=t1]').click();await page.waitForFunction(()=>document.querySelector('#template-subject')?.value==='模板1');
let release,arrive;const gate=new Promise(r=>release=r),arrived=new Promise(r=>arrive=r);await page.route('**/api/mail/templates/t2',async route=>{arrive();await gate;await route.fulfill({json:drafts[1]});});
await page.locator('[data-template=t2]').click();await arrived;const staleInputs=await page.locator('#template-subject').count();release();await page.waitForFunction(()=>document.querySelector('#template-subject')?.value==='模板2');assert.equal(staleInputs,0);
await page.route('**/api/mail/templates/t1',route=>route.fulfill({status:503,json:{error:'模板读取中断'}}));await page.locator('[data-template=t1]').click();await page.locator('#mail-template-detail').getByText(/模板读取中断/).waitFor();assert.equal(await page.locator('#template-subject').count(),0);
});
test('switching a dirty template supports stay discard and save before selecting another',{timeout:45000},async t=>{const {page}=await fixture(t);const drafts=await mailFixture(page);let saved;await page.route('**/api/mail/templates/t1/confirm',route=>{saved=route.request().postDataJSON();drafts[0].status='active';drafts[0].subject=saved.subject;return route.fulfill({json:{ok:true}});});await page.getByRole('link',{name:'邮件模板',exact:true}).click();await page.locator('[data-template=t1]').click();await page.locator('#template-subject').fill('未保存模板一');await page.locator('[data-template=t2]').click();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await page.locator('#template-subject').inputValue(),'未保存模板一');await page.locator('[data-template=t2]').click();await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.waitForFunction(()=>document.querySelector('#template-subject')?.value==='模板2');await page.locator('[data-template=t1]').click();await page.locator('#template-subject').fill('保存模板一');await page.locator('[data-template=t2]').click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();await page.waitForFunction(()=>document.querySelector('#template-subject')?.value==='模板2');assert.equal(saved.subject,'保存模板一');});
test('failed uncertain-result readback stays uncertain and never claims state was queried',{timeout:45000},async t=>{const {page}=await fixture(t);await page.locator('#schedule-form').waitFor();let posts=0,reads=0;await page.route('**/api/settings',route=>{if(route.request().method()==='POST'){posts++;return route.abort('timedout');}reads++;return route.fulfill({status:503,json:{error:'State unavailable'}});});await page.locator('#time-first').fill('10:01');await page.getByRole('button',{name:'保存发送计划',exact:true}).click();await page.waitForFunction(()=>document.querySelector('#operation-uncertain')?.textContent.includes('查询失败'));const text=await page.locator('#operation-uncertain').innerText();assert.match(text,/结果待核实/);assert.doesNotMatch(text,/已查询最新状态/);assert.equal(posts,1);assert.equal(reads,1);});
test('delayed readback cannot notify after leaving its originating page',{timeout:45000},async t=>{const {page}=await fixture(t);await page.locator('#schedule-form').waitFor();let release,arrive;const gate=new Promise(r=>release=r),arrived=new Promise(r=>arrive=r);await page.route('**/api/settings',async route=>{if(route.request().method()==='POST')return route.abort('timedout');arrive();await gate;return route.fulfill({json:{version:2,schedule:{times:['09:00','17:00']}}});});await page.locator('#time-first').fill('10:02');await page.getByRole('button',{name:'保存发送计划',exact:true}).click();await arrived;await page.getByRole('link',{name:'系统设置',exact:true}).click();await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.getByRole('heading',{name:'BPM 连接',exact:true}).first().waitFor();await page.evaluate(()=>document.querySelector('#flash')?.remove());release();await page.waitForTimeout(250);assert.equal(await page.locator('#flash').count(),0);assert.equal(await page.locator('#operation-uncertain').count(),0);});
test('execution refreshes after actions and on visible refresh while retaining other task notes',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');let tasks=[{id:'t1',connectionId:'c1',status:'failed',version:1,error:'失败一'},{id:'t2',connectionId:'c1',status:'failed',version:1,error:'失败二'}],gets=0;const retried=[];await page.route('**/api/order-execution',route=>{gets++;return route.fulfill({json:{tasks,capabilities:{},fields:{samples:[],adapters:[]}}});});await page.route('**/api/order-execution/tasks/t1/retry',route=>{retried.push(route.request().postDataJSON());tasks[0]={...tasks[0],version:2,error:'再次失败'};return route.fulfill({json:{ok:true}});});await page.route('**/api/order-execution/scan',route=>{tasks.push({id:'new1',status:'archived',version:1,title:'新扫描任务'});return route.fulfill({json:{ok:true}});});await openRuleRoute(page,'notification-execution');await page.locator('[data-execution-retry=t2] textarea').fill('保留另一任务的处理依据');await page.locator('[data-execution-retry=t1] textarea').fill('核实恢复后再次尝试');await page.locator('[data-execution-retry=t1] button').click();await page.getByText('再次失败',{exact:true}).waitFor();assert.equal(await page.locator('[data-execution-retry=t2] textarea').inputValue(),'保留另一任务的处理依据');await page.locator('[data-execution-retry=t1] textarea').fill('核实恢复后第二次尝试');await page.locator('[data-execution-retry=t1] button').click();await page.getByText('重试请求已提交,请核对任务状态。',{exact:true}).waitFor();assert.deepEqual(retried.map(r=>r.version),[1,2]);await page.getByLabel('扫描连接',{exact:true}).selectOption('c1');await page.getByRole('button',{name:'扫描待办',exact:true}).click();await page.locator('[data-execution-task=new1]').waitFor();assert.match(await page.locator('[data-execution-task=new1]').innerText(),/新扫描任务/);tasks[0]={...tasks[0],version:3,status:'archived',error:''};await page.getByRole('button',{name:'刷新时段与状态',exact:true}).click();await page.waitForFunction(()=>!document.querySelector('[data-execution-retry=t1]'));assert.equal(await page.locator('[data-execution-retry=t2] textarea').inputValue(),'保留另一任务的处理依据');assert.ok(gets>=5);});
test('rerendering rule saves lock editable fields until the saved response has replaced the form',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');await openRuleRoute(page,'order-rules');let release,arrive;const gate=new Promise(r=>release=r),arrived=new Promise(r=>arrive=r);await page.route('**/api/order-rules/rules',async route=>{const response=await route.fetch();arrive();await gate;return route.fulfill({response});});await page.getByLabel('规则名称',{exact:true}).fill('提交中的草稿');await page.getByRole('button',{name:'保存草稿',exact:true}).click();await arrived;assert.equal(await page.getByLabel('规则名称',{exact:true}).isDisabled(),true);release();await page.getByText('规则草稿已保存。',{exact:true}).waitFor();assert.equal(await page.getByLabel('规则名称',{exact:true}).isDisabled(),false);assert.equal(await page.getByLabel('规则名称',{exact:true}).inputValue(),'提交中的草稿');assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),false);});
test('invalid readback payload is rejected even when HTTP succeeds',{timeout:45000},async t=>{const {page}=await fixture(t);await page.locator('#schedule-form').waitFor();await page.route('**/api/settings',route=>route.request().method()==='POST'?route.abort('timedout'):route.fulfill({json:{schedule:'not a schedule'}}));await page.locator('#time-first').fill('10:03');await page.getByRole('button',{name:'保存发送计划',exact:true}).click();await page.waitForFunction(()=>document.querySelector('#operation-uncertain')?.textContent.includes('查询失败'));assert.doesNotMatch(await page.locator('#operation-uncertain').innerText(),/已查询最新状态/);});
test('uncertain calendar write reads its workspace calendar rather than overview',{timeout:45000},async t=>{const {page}=await fixture(t,'calendar');await page.locator('#day-note').waitFor();let reads=0;await page.route('**/api/calendar',route=>route.abort('timedout'));await page.route('**/api/workspace',route=>{reads++;return route.fulfill({json:{calendar:{version:8,overrides:[]}}});});await page.locator('#day-note').fill('隔离测试日历安排');await page.getByRole('button',{name:'应用到此日期',exact:true}).click();await page.getByRole('button',{name:'保存日历',exact:true}).click();await page.waitForFunction(()=>document.querySelector('#operation-uncertain')?.textContent.includes('配置版本 8'));assert.equal(reads,1);});
test('rule durable save survives refresh failure, retains version and permits Save and Continue',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');await openRuleRoute(page,'order-rules');let failRead=false;const versions=[];await page.route('**/api/order-rules',route=>failRead?route.fulfill({status:503,json:{error:'refresh unavailable'}}):route.continue());await page.route('**/api/order-rules/rules',async route=>{versions.push(route.request().postDataJSON().version);const response=await route.fetch();failRead=true;return route.fulfill({response});});await page.getByLabel('规则名称',{exact:true}).fill('首次持久保存');await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText(/已保存.*刷新失败/).waitFor();assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),false);await page.getByLabel('规则名称',{exact:true}).fill('再次持久保存');await page.locator('a[href="#system-settings"]').first().click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();await page.getByRole('heading',{name:'BPM 连接',exact:true}).first().waitFor();assert.deepEqual(versions,[1,2]);});
test('field mapping save retains returned version after followup GET fails',{timeout:45000},async t=>{
const {page}=await fixture(t,'order-rules');let saved=false;const versions=[];
await page.route('**/api/order-rules/fields*',route=>saved?route.fulfill({status:503,json:{error:'fields unavailable'}}):route.fulfill({json:{samples:[{id:'s1',processName:'测试流程',processVersion:'1',sample:{title:'订单号:A1'},fields:[{path:'order',label:'订单号',value:'A1'},{path:'brief',label:'摘要',value:'摘要'}]}],adapters:[]}}));
await page.route('**/api/order-rules/field-mappings/draft',route=>{const input=route.request().postDataJSON();versions.push(input.version);saved=true;return route.fulfill({json:{...input,id:'a1',version:versions.length,sources:{...input.sources,receivedAt:{type:'metadata',key:'receivedAt'}}}});});
await openRuleRoute(page,'rule-fields');await page.locator('#fm-manual').evaluate(e=>e.open=true);await page.locator('#fm-orderNo').selectOption('order');await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText(/已保存.*刷新失败/).waitFor();assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),false);
await page.getByText(/可选业务信息 ·/).evaluate(e=>e.parentElement.open=true);await page.locator('#fm-customer').selectOption('brief');await page.locator('a[href="#order-notifications"]').first().click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();await page.getByRole('heading',{name:'本时段范围',exact:true}).waitFor();assert.deepEqual(versions,[0,1]);
});
test('category binding save retains response version without repeating committed mutation',{timeout:45000},async t=>{const {page}=await fixture(t);await mailFixture(page);let saved=false;const versions=[];await page.route('**/api/mail/templates/categories',route=>{if(route.request().method()==='GET')return saved?route.fulfill({status:503,json:{error:'bindings unavailable'}}):route.fulfill({json:{categories:[{id:'r1',name:'测试类别'}],bindings:[]}});const input=route.request().postDataJSON();versions.push(input.version);saved=true;return route.fulfill({json:{...input,version:versions.length}});});await page.getByRole('link',{name:'邮件模板',exact:true}).click();await page.locator('[data-category=r1] button').click();await page.getByText(/已保存.*刷新失败/).waitFor();await page.locator('[data-category=r1] button').click();await page.waitForFunction(()=>document.querySelector('[data-category=r1]')?.getAttribute('aria-busy')===null);assert.deepEqual(versions,[0,1]);});
test('owner retry and persisted scan progress refresh together without replacing recovery notes',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');await page.evaluate(async()=>{currentUser={username:'owner',role:'operator'};OrderRuleViews.configure({api,esc,formatDate,notify,user:currentUser});await renderView();});await page.route('**/api/workspace',route=>route.fulfill({json:{connections:[{id:'c1',owner:'owner',name:'owned'}]}}));let query={status:'scanning',startedAt:'2026-10-19T03:00:00Z',rowCount:2,targetCount:2,processedCount:0},blockers=['扫描尚未完成'];let tasks=[{id:'t1',status:'failed',canRetry:true,version:1,connectionId:'c1'},{id:'unknown',status:'unknown',canRetry:false},{id:'conflict',status:'blocked',conflict:true,canRetry:false}];await page.route('**/api/order-execution',route=>route.fulfill({json:{tasks,query,blockers,capabilities:{liveTodo:true}}}));await openRuleRoute(page,'notification-execution');await page.locator('[data-execution-retry=t1]').waitFor();await page.locator('[data-execution-retry=t1] textarea').fill('保留负责人的恢复依据');assert.equal(await page.locator('[data-execution-retry]').count(),1);await page.locator('#execution-query').getByText(/扫描中/).waitFor();tasks=[];query={status:'failed',error:'zero-row provider failure',targetCount:0,rowCount:0,finishedAt:'2026-10-19T03:00:10Z'};blockers=['扫描失败,冻结受阻'];await page.locator('#order-rule-refresh').click();await page.locator('#execution-query').getByText(/zero-row provider failure/).waitFor();assert.equal(await page.locator('[data-execution-retry=t1] textarea').inputValue(),'保留负责人的恢复依据');assert.match(await page.locator('#execution-capabilities').innerText(),/冻结受阻/);});
test('confirmed template and created user stay committed when list refresh fails',{timeout:45000},async t=>{const {page}=await fixture(t);await mailFixture(page);await page.route('**/api/mail/templates/t1/confirm',route=>route.fulfill({json:{id:'t1',status:'active',version:2}}));await page.getByRole('link',{name:'邮件模板',exact:true}).click();await page.locator('[data-template=t1]').click();await page.route('**/api/mail',route=>route.fulfill({status:503,json:{error:'mail list unavailable'}}));await page.getByRole('button',{name:'确认生效',exact:true}).click();await page.getByText(/模板已确认生效.*刷新失败/).waitFor();assert.equal(await page.getByRole('button',{name:'确认生效',exact:true}).count(),0);await page.getByRole('link',{name:'系统设置',exact:true}).click();await page.getByRole('link',{name:'团队账号',exact:true}).click();await page.locator('#new-username').fill('isolated-new');await page.locator('#new-password').fill('Fixture-password-123');await page.route('**/api/users',route=>route.request().method()==='POST'?route.fulfill({json:{id:'new-user'}}):route.fulfill({status:503,json:{error:'users unavailable'}}));await page.getByRole('button',{name:'创建账号',exact:true}).click();await page.getByText(/账号已创建.*刷新失败/).waitFor();assert.equal(await page.locator('#new-password').inputValue(),'');});
test('assistant settings retain persisted version after refresh failure',{timeout:45000},async t=>{const {page}=await fixture(t);await page.route('**/api/assistant',route=>route.fulfill({json:{settings:{version:1,runtimePath:'',apiBase:'',model:'',timeoutSeconds:30,dailyLimit:10}}}));await page.getByRole('link',{name:'系统设置',exact:true}).click();await page.getByRole('link',{name:'助手设置',exact:true}).click();await page.locator('#assistant-model').waitFor();const versions=[];await page.route('**/api/assistant/settings',async route=>{versions.push(route.request().postDataJSON().version);return route.fulfill({json:{version:versions.at(-1)+1}});});await page.route('**/api/assistant',route=>route.fulfill({status:503,json:{error:'assistant refresh unavailable'}}));await page.locator('#assistant-model').fill('fixture-model-one');await page.getByRole('button',{name:'保存助手设置',exact:true}).click();await page.getByText(/助手设置已保存.*刷新失败/).waitFor();await page.locator('#assistant-model').fill('fixture-model-two');await page.getByRole('button',{name:'保存助手设置',exact:true}).click();await page.waitForFunction(()=>!document.querySelector('#assistant-settings-form')?.hasAttribute('aria-busy'));assert.equal(versions[1],versions[0]+1);});
async function openRuleRoute(page,hash){
if(!await page.locator('a[href="#'+hash+'"]').count()){
const business=['order-rules','rule-fields'].includes(hash);
await page.locator('a[href="#'+(business?'business-settings':'order-notifications')+'"]').first().click();
if(hash==='notification-settings')await page.locator('a[href="#schedule"]').first().click();
}
await page.locator('a[href="#'+hash+'"]').first().click();
}