@@ -21,6 +21,7 @@ import {
2121 CharacterBuilder ,
2222 stringToUuid ,
2323} from '../index' ;
24+ import internalMessageBus from '../../services/message-bus' ;
2425
2526const DEFAULT_MESSAGE_SERVER_ID = '00000000-0000-0000-0000-000000000000' as UUID ;
2627
@@ -181,7 +182,7 @@ describe('WebSocket Transport Integration', () => {
181182 expect ( response . status ) . toBe ( 400 ) ;
182183
183184 const data = await response . json ( ) ;
184- expect ( data . error ) . toContain ( 'Invalid transport' ) ;
185+ expect ( data . error . message ) . toContain ( 'Invalid transport' ) ;
185186 } ) ;
186187
187188 it ( 'should reject non-string transport parameter' , async ( ) => {
@@ -203,7 +204,7 @@ describe('WebSocket Transport Integration', () => {
203204 expect ( response . status ) . toBe ( 400 ) ;
204205
205206 const data = await response . json ( ) ;
206- expect ( data . error ) . toContain ( 'Transport must be a string' ) ;
207+ expect ( data . error . message ) . toContain ( 'Transport must be a string' ) ;
207208 } ) ;
208209 } ) ;
209210
@@ -274,7 +275,46 @@ describe('WebSocket Transport Integration', () => {
274275 } ) ;
275276
276277 describe ( 'WebSocket + Socket.IO Integration' , ( ) => {
277- it ( 'should receive agent response via Socket.IO after HTTP acknowledgment' , async ( ) => {
278+ it ( 'should emit new_message to internal bus for agent processing' , async ( ) => {
279+ // Arrange - Setup listener on internal bus to verify message is emitted
280+ const authorId = stringToUuid ( 'ws-bus-user' ) ;
281+ let receivedMessage : any = null ;
282+
283+ const messageHandler = ( data : any ) => {
284+ receivedMessage = data ;
285+ } ;
286+ internalMessageBus . on ( 'new_message' , messageHandler ) ;
287+
288+ // Act - Send message via HTTP with websocket transport
289+ const response = await fetch ( `${ baseUrl } /api/messaging/sessions/${ sessionId } /messages` , {
290+ method : 'POST' ,
291+ headers : { 'Content-Type' : 'application/json' } ,
292+ body : JSON . stringify ( {
293+ content : 'Hello, should emit to internal bus' ,
294+ author_id : authorId ,
295+ transport : 'websocket' ,
296+ } ) ,
297+ } ) ;
298+
299+ // Small delay to allow async event emission
300+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
301+
302+ // Cleanup
303+ internalMessageBus . off ( 'new_message' , messageHandler ) ;
304+
305+ // Assert HTTP response is immediate
306+ expect ( response . status ) . toBe ( 201 ) ;
307+ const httpData = await response . json ( ) ;
308+ expect ( httpData . success ) . toBe ( true ) ;
309+ expect ( httpData . agentResponse ) . toBeUndefined ( ) ;
310+
311+ // Assert message was emitted to internal bus
312+ expect ( receivedMessage ) . toBeDefined ( ) ;
313+ expect ( receivedMessage . content ) . toBe ( 'Hello, should emit to internal bus' ) ;
314+ expect ( receivedMessage . channel_id ) . toBeDefined ( ) ;
315+ } ) ;
316+
317+ it ( 'should connect Socket.IO client and join channel' , async ( ) => {
278318 // Arrange - Create new session for this test to get channelId
279319 const authorId = stringToUuid ( 'ws-socketio-user' ) ;
280320 const entityId = stringToUuid ( 'ws-socketio-entity' ) ;
@@ -288,7 +328,6 @@ describe('WebSocket Transport Integration', () => {
288328 } ) ,
289329 } ) ;
290330 const newSessionData = await newSessionResponse . json ( ) ;
291- const testSessionId = newSessionData . sessionId ;
292331 const channelId = newSessionData . channelId ;
293332
294333 // Connect Socket.IO client
@@ -302,33 +341,8 @@ describe('WebSocket Transport Integration', () => {
302341 messageServerId : DEFAULT_MESSAGE_SERVER_ID ,
303342 } ) ;
304343
305- // Setup listener for agent response
306- const agentResponsePromise = clientFixture . waitForEvent < {
307- text : string ;
308- senderId : string ;
309- } > ( 'messageBroadcast' ) ;
310-
311- // Act - Send message via HTTP with websocket transport
312- const response = await fetch ( `${ baseUrl } /api/messaging/sessions/${ testSessionId } /messages` , {
313- method : 'POST' ,
314- headers : { 'Content-Type' : 'application/json' } ,
315- body : JSON . stringify ( {
316- content : 'Hello, waiting for Socket.IO response' ,
317- author_id : authorId ,
318- transport : 'websocket' ,
319- } ) ,
320- } ) ;
321-
322- // Assert HTTP response is immediate
323- expect ( response . status ) . toBe ( 201 ) ;
324- const httpData = await response . json ( ) ;
325- expect ( httpData . success ) . toBe ( true ) ;
326- expect ( httpData . agentResponse ) . toBeUndefined ( ) ;
327-
328- // Assert agent response comes via Socket.IO
329- const socketResponse = await agentResponsePromise ;
330- expect ( socketResponse ) . toBeDefined ( ) ;
331- expect ( socketResponse . text ) . toBeDefined ( ) ;
344+ // Assert client is connected and joined
345+ expect ( clientFixture . isConnected ( ) ) . toBe ( true ) ;
332346 } ) ;
333347 } ) ;
334348} ) ;
0 commit comments