'use strict'; const test=require('node:test'),assert=require('node:assert/strict'); const {loadForm,loadData,historyURL}=require('../../src/modules/bpm/order-form.cjs'); const {BpmHistoryClient}=require('../../src/modules/bpm/history-client.cjs'); const formURL='https://bpm.skyworthdigital.com/YZSoft/Forms/XForm/FixtureV1.aspx?tid=21'; const response=({status,location,body=''})=>({status:()=>status,headers:()=>location?{location}:{},body:async()=>Buffer.from(body)}); const failures=[ {name:'HTTP401',status:401,want:401}, {name:'HTTP403',status:403,want:403}, {name:'HTTP502',status:502,want:502}, {name:'known same-origin gateway',status:302,location:'/skyworth/sso/open_sso_url',want:401}, {name:'known company SSO',status:302,location:'https://sso.skyworthdigital.com/realms/skyworthdigital/protocol/openid-connect/auth',want:401}, {name:'unknown same-origin redirect',status:302,location:'/maintenance',want:502}, {name:'unrelated login host',status:302,location:'https://sso.skyworthdigital.com.evil.invalid/realms/login',want:502}, {name:'redirect without location',status:302,want:502} ]; for(const fixture of failures){ test('native form HTML preserves '+fixture.name+' classification without following rejected redirects',async()=>{ const calls=[],request={get:async(url,options)=>{calls.push({url,options});return response(fixture);}}; await assert.rejects(loadForm(request,formURL,historyURL,{history:true}),cause=>cause.status===fixture.want); assert.deepEqual(calls,[{url:formURL,options:{maxRedirects:0,timeout:20000}}]); }); test('native readonly data preserves '+fixture.name+' classification and its request policy',async()=>{ const calls=[],request={post:async(url,options)=>{calls.push({url,options});return response(fixture);}}; await assert.rejects(loadData(request,new URL(formURL),{history:true}),cause=>cause.status===fixture.want); assert.deepEqual(calls,[{url:'https://bpm.skyworthdigital.com/YZSoft/BPM/XMLService/DataProvider.ashx',options:{data:'GetFormReadData21',headers:{'Content-Type':'text/xml'},maxRedirects:0,timeout:20000}}]); }); for(const phase of ['portal','business'])test('native history '+phase+' preserves '+fixture.name+' classification without a second request',async()=>{ const calls=[];let disposed=false; const client=new BpmHistoryClient({requestFactory:async()=>({get:async(route,options)=>{calls.push({route,options});return response(phase==='business'&&route==='/'?{status:200,body:'Yiez.clientId="fixture-token";'}:fixture);},dispose:async()=>{disposed=true;}})}); await assert.rejects(client.scan({cookies:[],origins:[]},['Fixture']),cause=>cause.status===fixture.want); assert.equal(calls.length,phase==='portal'?1:2);assert.equal(calls.every(call=>call.options.maxRedirects===0),true);assert.equal(disposed,true); }); } test('native history treats only an explicit login form as expired when a 200 portal has no token',async()=>{ for(const [body,want]of [['
',401],['

Maintenance

',502]]){ let calls=0,disposed=false;const client=new BpmHistoryClient({requestFactory:async()=>({get:async()=>{calls++;return response({status:200,body});},dispose:async()=>{disposed=true;}})}); await assert.rejects(client.scan({},['Fixture']),cause=>cause.status===want);assert.equal(calls,1);assert.equal(disposed,true); } }); test('native history resolves relative redirects against the actual business request path',async()=>{ const client=new BpmHistoryClient({requestFactory:async()=>({get:async route=>response(route==='/'?{status:200,body:'Yiez.clientId="fixture-token";'}:{status:302,location:'login'}),dispose:async()=>{}})}); await assert.rejects(client.scan({},['Fixture']),cause=>cause.status===502); }); test('native form follows only its already validated temporary SSO to history form chain',async()=>{ const entry='https://bpm.skyworthdigital.com/sso/ssologin.aspx?'+new URLSearchParams({ReturnUrl:'/YZSoft/Forms/Read.aspx?tid=21',todo_token:'FIXTURE',todo_account:'fixture'}),calls=[]; const request={get:async(url,options)=>{calls.push(url);assert.equal(options.maxRedirects,0);return response(url===entry?{status:302,location:'/YZSoft/Forms/Read.aspx?tid=21'}:url.endsWith('/Read.aspx?tid=21')?{status:302,location:formURL}:{status:200,body:'
fixture
'});}}; const loaded=await loadForm(request,entry,historyURL,{history:true});assert.equal(loaded.url.href,formURL);assert.equal(loaded.bytes.toString(),'
fixture
');assert.deepEqual(calls,[entry,'https://bpm.skyworthdigital.com/YZSoft/Forms/Read.aspx?tid=21',formURL]); });