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

9 lines
2.7 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');const {signalrHub}=require('../fixtures/signalr-hub.cjs');
let Client;try{Client=require('../../src/modules/bpm/signalr-client.cjs').BpmSignalRClient;}catch(e){if(e.code!=='MODULE_NOT_FOUND')throw e;}
const state={cookies:[],origins:[]};
test('real hub handshake accepts only known change hints and sends no business invocation',async t=>{assert.equal(typeof Client,'function');const hub=await signalrHub(t),events=[];let notify;const received=new Promise(r=>notify=r);const client=new Client({baseURL:hub.baseURL,allowLocal:true}),handle=await client.connect(state,{onEvent:name=>{events.push(name);if(events.length===2)notify();}});hub.emit({type:1,target:'worklistChanged',arguments:[{private:'not passed'}]});hub.emit({type:1,target:'executeApproval',arguments:[]});hub.emit({type:1,target:'informlistChanged',arguments:[]});await received;await handle.stop();assert.deepEqual(events,['worklistChanged','informlistChanged']);assert.deepEqual(hub.messages[0],{protocol:'json',version:1});assert.equal(hub.messages.some(m=>m.type===1),false);});
test('missing handshake and heartbeat fail with bounded safe reasons and release the connection',async t=>{assert.equal(typeof Client,'function');const missing=await signalrHub(t,{handshake:false});await assert.rejects(new Client({baseURL:missing.baseURL,allowLocal:true,handshakeMs:70}).connect(state),e=>e.code==='BPM_HUB_HANDSHAKE');const quiet=await signalrHub(t,{pings:false});let close;const closed=new Promise(r=>close=r);const handle=await new Client({baseURL:quiet.baseURL,allowLocal:true,heartbeatMs:80,pingMs:30}).connect(state,{onClose:close});assert.equal((await closed).reason,'heartbeat_timeout');await handle.stop();});
test('abort and unauthorized negotiation never expose credentials or leave retry work running',async t=>{assert.equal(typeof Client,'function');const denied=await signalrHub(t,{authStatus:401});await assert.rejects(new Client({baseURL:denied.baseURL,allowLocal:true}).connect(state),e=>e.status===401&&!e.message.includes('fixture-token'));const controller=new AbortController();controller.abort();await assert.rejects(new Client({baseURL:denied.baseURL,allowLocal:true}).connect(state,{signal:controller.signal}),e=>e.name==='AbortError');});
test('oversized or malformed hub frames close safely instead of delivering a forged event',async t=>{assert.equal(typeof Client,'function');const hub=await signalrHub(t),events=[];let close;const closed=new Promise(r=>close=r);const handle=await new Client({baseURL:hub.baseURL,allowLocal:true,maxFrameBytes:256}).connect(state,{onEvent:n=>events.push(n),onClose:close});hub.raw('x'.repeat(300));assert.equal((await closed).reason,'invalid_frame');assert.deepEqual(events,[]);await handle.stop();});