Files
order-notification-automation/tests/order-rules/configuration-ui.test.cjs
T

75 lines
10 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){
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'rule-configuration-ui-')),runtime=await startTestDashboard({stateDir:dir});let browser;
t.after(async()=>{await browser?.close();await runtime.close();await fs.rm(dir,{recursive:true,force:true});});
const rule=runtime.orderRules.get('rules','bs-change').draft,actor=runtime.store.users()[0],connection=runtime.workspace.createConnection({name:'Isolated configuration sample',owner:'admin'},actor,[actor]);
runtime.orderRules.put('capture-samples',{id:'configuration-sample',provenance:'server-provider',processName:rule.processNames[0],capturedAt:'2026-10-27T01:00:00Z',sample:{connectionId:connection.id,source:'todo',processVersion:'1.4',nodeName:rule.nodeNames[0],serialNum:'CONFIG-SAMPLE',receivedAt:'2026-10-27T01:00:00Z',fields:{private:'DO-NOT-RETURN'}},snapshot:{html:'PRIVATE-HTML',fields:[{path:'private',value:'DO-NOT-RETURN'}]}});
browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(5000);
const base='http://127.0.0.1:'+runtime.server.address().port,requests=[];page.on('request',r=>{if(r.url().startsWith(base+'/api/'))requests.push({path:new URL(r.url()).pathname+new URL(r.url()).search,method:r.method(),body:r.postDataJSON()});});
return {page,runtime,requests,base,enter:async()=>{await page.goto(base+'/#order-rules');await login(page);await page.getByLabel('规则名称',{exact:true}).waitFor();await page.locator('[data-rule-id="bs-change"]').click();}};
}
test('rules render without business-history reads and samples load only when requested',{timeout:60000},async t=>{
const f=await fixture(t),all=f.runtime.orderRules.all.bind(f.runtime.orderRules);let allowSamples=false;
f.runtime.orderRules.all=kind=>{assert.ok(!['orders','batches','execution-tasks','audit'].includes(kind),'configuration must not read '+kind);if(kind==='capture-samples')assert.equal(allowSamples,true,'samples must load on demand');return all(kind);};
await f.enter();
assert.equal(f.requests.filter(r=>r.path==='/api/order-rules/configuration').length,1);
assert.equal(f.requests.some(r=>r.path.includes('view=summary')||r.path.includes('view=directory')),false);
assert.equal(f.requests.some(r=>r.path.startsWith('/api/order-rules/configuration/samples')),false);
allowSamples=true;await f.page.getByRole('button',{name:'读取已采集订单',exact:true}).click();
await f.page.locator('#order-rule-sample-source option[value="capture:configuration-sample"]').waitFor({state:'attached'});
assert.equal(await f.page.getByRole('button',{name:'检查识别结果',exact:true}).isDisabled(),false);
assert.equal(f.requests.filter(r=>r.path.startsWith('/api/order-rules/configuration/samples')).length,1);
allowSamples=false;await f.page.getByRole('button',{name:'刷新规则与字段',exact:true}).click();await f.page.waitForFunction(()=>!document.querySelector('#order-rule-samples-load').disabled);
assert.equal(f.requests.filter(r=>r.path.startsWith('/api/order-rules/configuration/samples')).length,1);
assert.equal(await f.page.locator('#order-rule-preview').isDisabled(),true);
});
test('save keeps the editor and uses returned versions without reloading resource catalogs',{timeout:60000},async t=>{
const f=await fixture(t);await f.enter();const initialVersion=f.runtime.orderRules.get('rules','bs-change').draft.version;
const reads=f.requests.filter(r=>r.method==='GET'&&r.path==='/api/order-rules/configuration').length;
await f.page.getByLabel('收件人',{exact:true}).fill('OWNER@EXAMPLE.TEST\nSECOND@EXAMPLE.TEST');await f.page.getByLabel('抄送人',{exact:true}).fill('COPY@EXAMPLE.TEST\nOTHER@EXAMPLE.TEST');
await f.page.locator('#order-rule-editor').evaluate(form=>form.dataset.probe='keep-editor');
for(const name of ['First configuration save','Second configuration save']){
await f.page.getByLabel('规则名称',{exact:true}).fill(name);await f.page.getByRole('button',{name:'保存草稿',exact:true}).click();
await f.page.getByText('规则草稿已保存。',{exact:true}).waitFor();await f.page.waitForFunction(()=>!document.querySelector('#order-rule-editor button[type=submit]').disabled);
assert.equal(await f.page.locator('#order-rule-editor').getAttribute('data-probe'),'keep-editor');
assert.equal(await f.page.evaluate(()=>PageLifecycle.dirty()),false);
assert.equal(await f.page.getByLabel('收件人',{exact:true}).inputValue(),'owner@example.test\nsecond@example.test');assert.equal(await f.page.getByLabel('抄送人',{exact:true}).inputValue(),'copy@example.test\nother@example.test');assert.doesNotMatch(await f.page.locator('#rule-validation-issues').textContent(),/请配置收件人/);
}
assert.equal(f.requests.filter(r=>r.method==='GET'&&r.path==='/api/order-rules/configuration').length,reads);
const versions=f.requests.filter(r=>r.method==='POST'&&r.path==='/api/order-rules/rules').map(r=>r.body.version);
assert.deepEqual(versions,[initialVersion,initialVersion+1]);assert.match(await f.page.locator('#rule-version-state').textContent(),new RegExp('草稿 v'+(initialVersion+2)));
});
test('a delayed sample read cannot replace a different category or clear unsaved inputs',{timeout:60000},async t=>{
const f=await fixture(t);await f.enter();let release,arrive;const gate=new Promise(r=>release=r),arrived=new Promise(r=>arrive=r);
await f.page.route('**/api/order-rules/configuration/samples?*',async route=>{const response=await route.fetch();arrive();await gate;await route.fulfill({response}).catch(()=>{});});
await f.page.getByRole('button',{name:'读取已采集订单',exact:true}).click();await arrived;
const other=await f.page.locator('[data-rule-id]').evaluateAll(nodes=>nodes.find(n=>n.dataset.ruleId!=='bs-change').dataset.ruleId);
await f.page.locator('[data-rule-id="'+other+'"]').click();await f.page.getByLabel('规则名称',{exact:true}).fill('Keep this unsaved category');release();
await f.page.waitForTimeout(150);assert.equal(await f.page.getByLabel('规则名称',{exact:true}).inputValue(),'Keep this unsaved category');assert.equal(await f.page.evaluate(()=>PageLifecycle.dirty()),true);
assert.equal(await f.page.locator('#order-rule-sample-source option[value="capture:configuration-sample"]').count(),0);
});
test('discard before pausing restores saved fields and keeps a truthful dirty baseline',{timeout:60000},async t=>{
const f=await fixture(t),rules=f.runtime.orderRules,{captured}=require('../fixtures/activate-field-mapping.cjs').activateReferenceMapping(rules);
const draft=rules.saveRule({...rules.get('rules','bs-change').draft,to:['owner@example.test'],archiveDirectory:'\\\\fixture\\orders',filenameDate:'appDate'},'admin');rules.capturedPreview(captured.id);rules.publish(draft.id,{version:draft.version},'admin');
await f.enter();const savedName=await f.page.getByLabel('规则名称',{exact:true}).inputValue();await f.page.getByLabel('规则名称',{exact:true}).fill('DISCARD THIS EDIT');
await f.page.locator('[data-rule-toggle="bs-change"]').click();await f.page.getByRole('button',{name:'放弃并继续',exact:true}).click();await f.page.getByText('此规则已暂停。',{exact:true}).waitFor();
assert.equal(await f.page.getByLabel('规则名称',{exact:true}).inputValue(),savedName);assert.equal(await f.page.evaluate(()=>PageLifecycle.dirty()),false);
await f.page.getByLabel('规则名称',{exact:true}).fill(savedName+' edit');assert.equal(await f.page.evaluate(()=>PageLifecycle.dirty()),true);await f.page.getByLabel('规则名称',{exact:true}).fill(savedName);assert.equal(await f.page.evaluate(()=>PageLifecycle.dirty()),false);
});
async function mockTestConfiguration(page){
await page.route('**/api/order-rules/configuration',async route=>{const response=await route.fetch(),data=await response.json();data.restrictedTest={id:'isolated-run',version:1,enabled:true,ruleCount:data.rules.length,readyCount:data.rules.length,uncovered:[]};data.rules=data.rules.map(rule=>({...rule,testStatus:'enabled',testVersion:rule.version}));await route.fulfill({json:data});});
}
test('a paused test receipt updates the shared notice and all category test statuses',{timeout:60000},async t=>{
const f=await fixture(t);await mockTestConfiguration(f.page);await f.page.route('**/api/order-rules/rules',async route=>{const response=await route.fetch(),result=await response.json();result.restrictedTest={id:'isolated-run',version:2,enabled:false,ruleCount:15,readyCount:0,uncovered:[]};result.testStatus='paused';await route.fulfill({json:result});});
await f.enter();await f.page.getByLabel('规则名称',{exact:true}).fill('Save after test paused');await f.page.getByRole('button',{name:'保存草稿',exact:true}).click();await f.page.getByText('规则草稿已保存。',{exact:true}).waitFor();
assert.doesNotMatch(await f.page.locator('#rule-category-list').textContent(),/受限测试中/);assert.match(await f.page.locator('#rule-restricted-state').textContent(),/已暂停/);assert.equal(await f.page.getByRole('button',{name:'应用到测试',exact:true}).count(),0);
});
test('uncertain test application verifies through lightweight configuration without historical reads',{timeout:60000},async t=>{
const f=await fixture(t);await mockTestConfiguration(f.page);const all=f.runtime.orderRules.all.bind(f.runtime.orderRules);f.runtime.orderRules.all=kind=>{assert.ok(!['orders','batches','execution-tasks','audit','capture-samples'].includes(kind),'uncertain application must not read '+kind);return all(kind);};
await f.enter();await f.page.route('**/api/order-rules/restricted-test/apply',route=>route.abort('connectionreset'));await f.page.getByRole('button',{name:'应用到测试',exact:true}).click();await f.page.locator('#operation-uncertain').waitFor();await f.page.waitForFunction(()=>document.querySelector('#operation-uncertain')?.textContent.includes('已查询最新状态'));
assert.equal(f.requests.some(r=>r.method==='GET'&&r.path==='/api/order-rules'),false);assert.equal(f.requests.filter(r=>r.method==='GET'&&r.path==='/api/order-rules/configuration').length,2);
});