39 lines
19 KiB
JavaScript
39 lines
19 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 {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
|
|
async function fixture(t,hash='schedule') {const dir=await fs.mkdtemp(path.join(os.tmpdir(),'web-closure-'));const runtime=await startTestDashboard({stateDir:dir});const 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(5000);await page.goto('http://127.0.0.1:'+runtime.server.address().port+'/#'+hash);await login(page);return {page,runtime};}
|
|
test('settings open directly with horizontal tabs, retired hashes redirect, and mobile stays inside viewport',{timeout:45000},async t=>{const {page}=await fixture(t,'business-settings');await page.locator('#order-rules-panel').waitFor();assert.equal(await page.locator('#settings-tabs a').count(),2);await page.getByRole('link',{name:'订单通知管理',exact:true}).click();await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.locator('#schedule-form').waitFor();assert.equal(await page.locator('#send-mode').count(),0);await page.goBack();await page.goBack();await page.locator('#order-rules-panel').waitFor();for(const [hash,title] of [['supervisor','系统健康'],['connections','系统健康'],['notifications','分类通知规则']]){await page.evaluate(h=>location.hash=h,hash);await page.getByRole('heading',{name:title,exact:true}).first().waitFor();}await page.evaluate(()=>location.hash='system-settings');await page.getByRole('heading',{name:'BPM 连接',exact:true}).first().waitFor();assert.equal(await page.locator('#settings-tabs a').count(),7);await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.equal(await page.locator('#settings-tabs').evaluate(e=>getComputedStyle(e).overflowX),'auto');});
|
|
test('dirty navigation offers stay discard save, suppresses duplicate saves and preserves failed inputs',{timeout:45000},async t=>{const {page}=await fixture(t);await page.locator('#schedule-form').waitFor();await page.locator('#time-first').fill('09:11');await page.getByRole('link',{name:'业务日历',exact:true}).first().click();await page.getByRole('dialog',{name:'未保存的修改'}).waitFor();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await page.locator('#time-first').inputValue(),'09:11');await page.getByRole('link',{name:'业务日历',exact:true}).first().click();await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.locator('#day-form').waitFor();await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.locator('#time-first').fill('09:12');let requests=0;await page.route('**/api/settings',async route=>{if(route.request().method()==='GET')return route.continue();requests++;await new Promise(r=>setTimeout(r,100));await route.fulfill({status:409,json:{error:'版本已更新,请核对'}});});await page.getByRole('button',{name:'保存发送计划',exact:true}).dblclick();await page.getByRole('alert').getByText('版本已更新,请核对',{exact:true}).waitFor();assert.equal(requests,1);assert.equal(await page.locator('#time-first').inputValue(),'09:12');await page.unroute('**/api/settings');await page.getByRole('link',{name:'业务日历',exact:true}).first().click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();await page.locator('#day-form').waitFor();await page.getByRole('link',{name:'发送计划',exact:true}).first().click();assert.equal(await page.locator('#time-first').inputValue(),'09:12');});
|
|
test('password creation is discard-only and assistant settings have a distinct route',{timeout:45000},async t=>{const {page}=await fixture(t,'account');await page.route('**/api/assistant',route=>route.fulfill({json:{settings:{version:1,runtimePath:'',apiBase:'',model:'',timeoutSeconds:60,dailyLimit:30,enabled:false,allowActions:false},conversations:[],usage:{calls:0,limit:30}}}));await page.locator('#account-current').fill('unsaved-test-secret');await page.getByRole('link',{name:'系统设置',exact:true}).click();await page.getByRole('dialog',{name:'未保存的修改'}).waitFor();assert.equal(await page.getByRole('button',{name:'保存并继续',exact:true}).count(),0);await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.getByRole('link',{name:'助手设置',exact:true}).click();await page.locator('#assistant-settings-form').waitFor();await page.getByRole('link',{name:'ZCode 助手',exact:true}).click();await page.locator('#assistant-compose').waitFor();assert.equal(await page.locator('#assistant-settings-form').count(),0);});
|
|
test('execution scan, evidence-only field draft and check emit versioned contracts',{timeout:45000},async t=>{
|
|
const {page}=await fixture(t,'order-rules'),writes=[];
|
|
await page.route('**/api/order-execution**',async route=>{if(route.request().method()==='POST'){writes.push({url:route.request().url(),body:route.request().postDataJSON()});return route.fulfill({json:{ok:true}});}return route.fulfill({json:{tasks:[{id:'task1',version:4,status:'failed',error:'归档失败',connectionId:'connection1'}],capabilities:{liveTodo:false,pdfExport:false,automaticArchive:false}}});});
|
|
const sample={id:'sample1',processName:'订单流程',processVersion:'v7',sample:{title:'订单号:TITLE-1',serialNum:'SER1'},fields:[{path:'Order.Number',label:'订单编号',value:'A123'},{path:'Order.Text',label:'正文',value:'通知'}]};
|
|
await page.route('**/api/order-rules/fields*',route=>route.fulfill({json:{samples:[sample],adapters:[]}}));
|
|
await page.route('**/api/order-rules/field-mappings/draft',route=>{const body=route.request().postDataJSON();writes.push({url:route.request().url(),body});return route.fulfill({json:{...body,id:'mapping1',version:1,sources:{...body.sources,processName:{type:'metadata',key:'processName'}}}});});
|
|
await page.route('**/api/order-rules/field-mappings/check',route=>{const body=route.request().postDataJSON();writes.push({url:route.request().url(),body});return route.fulfill({json:{id:'check1',version:1,sampleId:'sample1',valid:true,result:{event:{orderNo:'A123',brief:'订单号:TITLE-1',titleOrders:[{orderNo:'TITLE-1'}]},filenames:[],affectedCategories:[],issues:[]}}});});
|
|
await openRuleRoute(page,'notification-execution');await page.getByText('归档失败',{exact:true}).waitFor();await page.getByLabel('扫描连接',{exact:true}).selectOption('connection1');await page.evaluate(()=>{crypto.randomUUID=undefined;});await page.getByRole('button',{name:'扫描待办',exact:true}).click();await page.getByText('扫描请求已提交,请核对任务状态。',{exact:true}).waitFor();assert.equal(writes[0].body.connectionId,'connection1');assert.match(writes[0].body.idempotencyKey,/^[a-z0-9-]+$/i);
|
|
await page.getByLabel('任务重试依据',{exact:true}).fill('已恢复隔离归档目录');await page.getByRole('button',{name:'重试失败任务',exact:true}).click();await page.getByText('重试请求已提交,请核对任务状态。',{exact:true}).waitFor();assert.equal(writes[1].body.version,4);
|
|
await openRuleRoute(page,'rule-fields');await page.locator('#fm-manual').evaluate(e=>e.open=true);await page.locator('#fm-orderNo').selectOption('Order.Number');assert.equal(await page.locator('[data-fm-binding] input[type=text]').count(),0);await page.getByRole('button',{name:'保存草稿',exact:true}).click();await page.getByText('字段对应草稿已保存。',{exact:true}).waitFor();const draft=writes[2].body;assert.deepEqual({...draft,idempotencyKey:undefined},{sampleId:'sample1',processName:'订单流程',processVersion:'v7',sources:{orderNo:{type:'form',path:'Order.Number'},brief:{type:'title'}},version:0,idempotencyKey:undefined});assert.equal(typeof draft.idempotencyKey,'string');assert.equal(await page.evaluate(()=>PageLifecycle.dirty()),false);
|
|
await page.getByRole('button',{name:'检查识别结果',exact:true}).click();await page.locator('#fm-result .fm-title-order').getByText('订单号:TITLE-1',{exact:true}).waitFor();assert.deepEqual({...writes[3].body,idempotencyKey:undefined},{id:'mapping1',version:1,sampleId:'sample1',idempotencyKey:undefined});assert.equal(await page.locator('#fm-activate').isDisabled(),true);
|
|
});
|
|
test('calendar staged changes save through navigation and rule tab changes use the same guard',{timeout:45000},async t=>{const {page,runtime}=await fixture(t,'calendar');await page.route('**/api/order-rules/fields*',route=>route.fulfill({json:{samples:[],adapters:[]}}));await page.getByLabel('安排说明',{exact:true}).fill('隔离测试调休日');await page.getByRole('button',{name:'应用到此日期',exact:true}).click();await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.getByRole('button',{name:'保存并继续',exact:true}).click();await page.locator('#schedule-form').waitFor();assert.equal(runtime.workspace.snapshot().calendar.overrides[0].note,'隔离测试调休日');await openRuleRoute(page,'order-rules');await page.getByLabel('规则名称',{exact:true}).fill('尚未保存规则');await openRuleRoute(page,'rule-fields');await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await page.getByLabel('规则名称',{exact:true}).inputValue(),'尚未保存规则');assert.equal(await page.evaluate(()=>{const e=new Event('beforeunload',{cancelable:true});window.dispatchEvent(e);return e.defaultPrevented;}),true);await openRuleRoute(page,'rule-fields');await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.waitForFunction(()=>location.hash==='#rule-fields');await page.locator('#fm-editor').waitFor();});
|
|
test('manual resend is only offered for failed batches and sends frozen identity with version and reason',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');const original=await page.request.get(new URL('/api/order-rules',page.url()).href);const snapshot=await original.json();snapshot.batches=[{id:'failed1',version:8,status:'failed',categoryId:'test',subject:'隔离失败批次',to:['frozen@example.test'],orderNos:['A1']},{id:'unknown1',version:1,status:'unknown',categoryId:'test',subject:'待核验批次',to:[],orderNos:[]}];let body;await page.route('**/api/order-rules',route=>route.fulfill({json:snapshot}));await page.route('**/api/order-rules/batches/failed1/resend',route=>{body=route.request().postDataJSON();return route.fulfill({json:{ok:true}});});await openRuleRoute(page,'notification-history');await page.locator('[data-rule-resend]').waitFor();assert.equal(await page.locator('[data-rule-resend]').count(),1);await page.getByLabel('人工补发依据',{exact:true}).fill('已确认原发送明确失败');await page.getByRole('button',{name:'人工补发此失败批次',exact:true}).click();await page.getByText('人工补发请求已提交,请核对发送记录。',{exact:true}).waitFor();assert.equal(body.version,8);assert.equal(body.note,'已确认原发送明确失败');assert.equal(typeof body.idempotencyKey,'string');assert.equal(Object.hasOwn(body,'to'),false);});
|
|
test('late page responses cannot overwrite new form and dirty browser history keeps current inputs',{timeout:45000},async t=>{const {page}=await fixture(t,'calendar');await page.locator('#day-form').waitFor();let release;const gate=new Promise(r=>release=r);await page.route('**/api/settings',async route=>{if(route.request().method()!=='GET')return route.continue();await gate;return route.continue();});await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.getByRole('link',{name:'业务日历',exact:true}).first().click();release();await page.locator('#day-form').waitFor();assert.equal(await page.locator('#schedule-form').count(),0);await page.unroute('**/api/settings');await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.locator('#time-first').fill('10:13');await page.goBack();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.match(page.url(),/#schedule$/);assert.equal(await page.locator('#time-first').inputValue(),'10:13');});
|
|
test('write timeout queries state without resubmission and retains uncertain inputs',{timeout:45000},async t=>{const {page}=await fixture(t,'schedule');await page.locator('#schedule-form').waitFor();await page.clock.install();let posts=0,gets=0;await page.route('**/api/settings',async route=>{if(route.request().method()==='GET'){gets++;return route.continue();}posts++;await route.abort('timedout');});await page.locator('#time-first').fill('10:14');await page.getByRole('button',{name:'保存发送计划',exact:true}).click();await page.locator('#operation-uncertain').waitFor();assert.equal(posts,1);await page.waitForFunction(()=>document.querySelector('#flash')?.textContent.includes('已查询最新状态'));assert.equal(gets,1);assert.equal(await page.locator('#time-first').inputValue(),'10:14');});
|
|
test('record history opens legacy notifications read-only after retired address redirects',{timeout:45000},async t=>{const {page}=await fixture(t,'notifications');await page.getByRole('heading',{name:'分类通知规则',exact:true}).waitFor();assert.equal(await page.locator('#notification-settings').count(),0);await page.route('**/api/notifications',route=>route.fulfill({json:{items:[{sourceId:'OLD1',industry:'原分类',subject:'历史通知',brief:'原正文',status:'sent'}],batches:[{id:'old-batch',industry:'原分类',subject:'历史批次',status:'sent',to:['old@example.test']}]}}));await page.getByRole('link',{name:'订单与导出记录',exact:true}).click();await page.getByRole('button',{name:'查看旧通知历史(只读)',exact:true}).click();await page.getByText('历史通知',{exact:true}).waitFor();assert.equal(await page.locator('#legacy-notification-history form').count(),0);});
|
|
test('a delayed execution response cannot replace the selected field directory',{timeout:45000},async t=>{const {page}=await fixture(t,'order-rules');let release;const gate=new Promise(r=>release=r);await page.route('**/api/order-execution',async route=>{await gate;return route.fulfill({json:{tasks:[],capabilities:{},fields:{samples:[],adapters:[]}}});});await page.route('**/api/order-rules/fields*',route=>route.fulfill({json:{samples:[],adapters:[]}}));await openRuleRoute(page,'notification-execution');await openRuleRoute(page,'rule-fields');await page.locator('#fm-editor').waitFor();release();await page.waitForTimeout(300);assert.equal(await page.locator('#fm-editor').count(),1);assert.equal(await page.locator('#execution-scan').count(),0);});
|
|
test('operator scans but cannot configure fields or resend, viewer has no scan action',{timeout:45000},async t=>{const {page,runtime}=await fixture(t,'order-rules');const base=new URL(page.url()).origin;for(const role of ['operator','viewer'])runtime.store.addUser({username:role,password:'Test-password-123',role},'admin');for(const role of ['operator','viewer']){const context=await page.context().browser().newContext(),member=await context.newPage();member.setDefaultTimeout(5000);await member.route('**/api/order-execution',route=>route.fulfill({json:{tasks:[{id:'x',version:1,status:'failed'}],capabilities:{},fields:{samples:[],adapters:[]}}}));await member.route('**/api/order-rules/fields*',route=>route.fulfill({json:{samples:[],adapters:[]}}));await member.goto(base+'/#order-rules');await member.getByLabel('账号',{exact:true}).fill(role);await member.getByLabel('密码',{exact:true}).fill('Test-password-123');await member.getByRole('button',{name:'登录',exact:true}).click();await openRuleRoute(member,'notification-execution');await member.getByRole('heading',{name:'持久执行任务',exact:true}).waitFor();assert.equal(await member.getByRole('button',{name:'扫描待办',exact:true}).count(),role==='operator'?1:0);assert.equal(await member.getByRole('button',{name:'重试失败任务',exact:true}).count(),0);await openRuleRoute(member,'rule-fields');await member.locator('#fm-editor').waitFor();assert.equal(await member.getByRole('button',{name:'保存草稿',exact:true}).count(),0);await context.close();}});
|
|
test('staying after browser Back preserves history so a second Back can discard to its original page',{timeout:45000},async t=>{const {page}=await fixture(t,'calendar');await page.locator('#day-form').waitFor();await page.getByRole('link',{name:'发送计划',exact:true}).first().click();await page.locator('#time-first').fill('11:17');await page.goBack();await page.getByRole('button',{name:'留在此页',exact:true}).click();await page.goBack();await page.getByRole('button',{name:'放弃并继续',exact:true}).click();await page.locator('#day-form').waitFor();await page.goForward();await page.locator('#schedule-form').waitFor();});
|
|
test('template categories use explicit versioned bindings and keep recipient editing in rules',{timeout:45000},async t=>{const {page}=await fixture(t,'schedule');const bindings=[];let saved;await page.route('**/api/mail',route=>route.fulfill({json:{channels:[],templates:[{id:'t1',status:'active',subject:'已确认模板',scope:'原产品线',attachmentCount:0}],receipts:[],sends:[]}}));await page.route('**/api/mail/templates/categories',route=>{if(route.request().method()==='POST'){saved=route.request().postDataJSON();bindings.push({...saved,version:1});return route.fulfill({json:{ok:true}});}return route.fulfill({json:{categories:[{id:'cat1',name:'Excel 分类一'}],bindings}});});await page.getByRole('link',{name:'邮件模板',exact:true}).click();await page.getByLabel('Excel 分类一',{exact:true}).selectOption('t1');assert.equal(await page.locator('#group-editor,#save-groups').count(),0);await page.getByRole('button',{name:'保存关联',exact:true}).click();await page.getByText('类别模板关联已保存。',{exact:true}).waitFor();assert.deepEqual(saved,{categoryId:'cat1',templateId:'t1',version:0});await page.getByLabel('Excel 分类一',{exact:true}).selectOption('');await page.getByRole('button',{name:'保存关联',exact:true}).click();await page.getByText('类别模板关联已保存。',{exact:true}).waitFor();assert.equal(saved.templateId,'');assert.equal(saved.version,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();
|
|
}
|