Files
order-notification-automation/tests/bpm-display-policy.test.cjs
T

16 lines
5.6 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {loadPlaywright,browserOptions}=require('../src/runtime.cjs');
const {hydrateReadonlyDocument}=require('../src/bpm-readonly-form.cjs');
async function fixture(t){const b=await loadPlaywright().chromium.launch(browserOptions());t.after(()=>b.close());const c=await b.newContext({javaScriptEnabled:false});await c.route('**/*',r=>r.abort());const p=await c.newPage();return async(html,tables,options={})=>{await p.setContent(html);return p.evaluate(hydrateReadonlyDocument,{tables,...options});};}
const table=(name,rows)=>({TableName:name,FormTable:true,Rows:rows});
test('approved display policy removes absent display values without inventing backend evidence',async t=>{const run=await fixture(t),html='<form><input xdatabind="Main.OrderNum"><input xdatabind="Main.Legacy" value="STALE"><span xdatabind="Missing.Value">STALE</span></form>',tables=[table('Main',[{OrderNum:'123'}])];assert.ok((await run(html,tables)).blocker);const r=await run(html,tables,{allowMissingDisplayFields:true});assert.equal(r.blocker,undefined);assert.match(r.snapshot.html,/123/);assert.doesNotMatch(r.snapshot.html,/STALE/);assert.equal(r.snapshot.warnings.filter(w=>w.includes('后端未提供')).length,2);assert.equal(Object.hasOwn(tables[0].Rows[0],'Legacy'),false);});
test('display policy never drops a partially present column or a compound backend value',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1"><tr><td><input xdatabind="Items.Value"></td></tr></table></form>';for(const rows of [[{Value:'A'},{}],[{Value:{nested:'A'}}]])assert.ok((await run(html,[table('Items',rows)],{allowMissingDisplayFields:true})).blocker);});
test('validated instance id supports non-Applicant main tables while rejecting cross-instance data',async t=>{const run=await fixture(t),html='<form><input xdatabind="Notification_All.OrderNum"><table dynamicarea="0,1"><tr><td><input xdatabind="Items.Value"></td></tr></table></form>',tables=[table('Notification_All',[{TaskID:123,OrderNum:'ORDER'}]),table('Items',[{TaskID:123,Value:'VALUE'}])];const r=await run(html,tables,{expectedInstanceId:'123'});assert.equal(r.blocker,undefined);assert.match(r.snapshot.html,/VALUE/);tables[1].Rows[0].TaskID=999;assert.equal((await run(html,tables,{expectedInstanceId:'123'})).diagnostics.reason,'TASK_SCOPE_MISMATCH');});
test('lookup datasource does not override proven persisted row bindings',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1" xdatasource="TableName:LookupTable"><tr><td><input xdatabind="Items.Value"></td></tr></table></form>';const r=await run(html,[table('Items',[{TaskID:123,Value:'PERSISTED'}])],{expectedInstanceId:'123'});assert.equal(r.blocker,undefined);assert.match(r.snapshot.html,/PERSISTED/);assert.ok(r.snapshot.warnings.some(w=>w.includes('数据源')));});
test('dynamic detail rows preserve a separate backend-bound summary footer',async t=>{const run=await fixture(t),html='<form><table dynamicarea="1,1"><tr><th>Header</th></tr><tr><td><input xdatabind="Items.Value"></td></tr><tr><td><input xdatabind="Summary.Total"></td></tr></table></form>',tables=[table('Items',[{Value:2},{Value:3}]),table('Summary',[{Total:5}])];const r=await run(html,tables);assert.equal(r.blocker,undefined);assert.match(r.snapshot.html,/>2<.*>3<.*>5</s);assert.equal((r.snapshot.html.match(/<tr/g)||[]).length,4);assert.ok((await run(html.replace('Summary.Total','Items.Value'),tables)).blocker);});
test('missing optional cells remain omitted after a dynamic template expands to multiple rows',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1"><tr><td><input xdatabind="Items.Value"></td><td><input xdatabind="Items.Legacy" value="STALE"></td></tr></table></form>';const r=await run(html,[table('Items',[{Value:'A'},{Value:'B'}])],{allowMissingDisplayFields:true});assert.equal(r.blocker,undefined);assert.match(r.snapshot.html,/>A<.*>B</s);assert.doesNotMatch(r.snapshot.html,/STALE/);});
test('empty dynamic rows remove the complete template including unbound stale text',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1"><tr><td><input xdatabind="Items.Value" value="STALE"><input value="STALE-UNBOUND">STALE-TEXT</td></tr></table></form>';const r=await run(html,[table('Items',[])],{allowMissingDisplayFields:true});assert.equal(r.blocker,undefined);assert.doesNotMatch(r.snapshot.html,/STALE/);});
test('display omissions also apply to optional template datamap columns without relaxing real row mappings',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1" datamap="Value->Items.Value;Legacy->Items.Legacy"><tr><td><input xdatabind="Items.Value"><input xdatabind="Items.Legacy" value="STALE"></td></tr></table></form>';const r=await run(html,[table('Items',[{Value:'A'},{Value:'B'}])],{allowMissingDisplayFields:true});assert.equal(r.blocker,undefined);assert.doesNotMatch(r.snapshot.html,/STALE/);assert.match(r.snapshot.html,/>A<.*>B</s);assert.ok((await run(html,[table('Items',[{Value:'A',Legacy:'known'},{Value:'B'}])],{allowMissingDisplayFields:true})).blocker);});
test('a wholly unavailable optional dynamic row cannot retain stale unbound values',async t=>{const run=await fixture(t),html='<form><table dynamicarea="0,1"><tr><td><input xdatabind="Gone.Value"><input value="STALE-UNBOUND">STALE-TEXT</td></tr><tr><td><input xdatabind="Summary.Total"></td></tr></table></form>';const r=await run(html,[table('Summary',[{Total:5}])],{allowMissingDisplayFields:true});assert.equal(r.blocker,undefined);assert.doesNotMatch(r.snapshot.html,/STALE/);assert.match(r.snapshot.html,/>5</);});