11import type { IncomingMessage , ServerResponse } from 'http'
22import { describe , expect , it , vi } from 'vitest'
3+ import { env } from '@/env'
34import type { IRoomManager } from '@/rooms'
45import { createHttpHandler } from '@/routes/http'
56
@@ -11,6 +12,7 @@ function createMocks(req: Partial<IncomingMessage>) {
1112 const roomManager = {
1213 getTotalActiveConnections : vi . fn ( ) . mockResolvedValue ( 0 ) ,
1314 isReady : vi . fn ( ) . mockReturnValue ( true ) ,
15+ emitToRoom : vi . fn ( ) ,
1416 } as unknown as IRoomManager
1517
1618 return {
@@ -20,6 +22,7 @@ function createMocks(req: Partial<IncomingMessage>) {
2022 setHeader,
2123 writeHead,
2224 end,
25+ roomManager,
2326 }
2427}
2528
@@ -58,4 +61,29 @@ describe('createHttpHandler', () => {
5861
5962 expect ( writeHead ) . toHaveBeenCalledWith ( 200 , { 'Content-Type' : 'application/json' } )
6063 } )
64+
65+ it ( 'fans workflow-tree changes out to the workspace workflows room' , async ( ) => {
66+ const workspaceId = 'workspace-1'
67+ const body = JSON . stringify ( { workspaceId } )
68+ const request = {
69+ method : 'POST' ,
70+ url : '/api/workspace-workflows-changed' ,
71+ headers : { 'x-api-key' : env . INTERNAL_API_SECRET } ,
72+ on ( event : string , callback : ( chunk ?: Buffer ) => void ) {
73+ if ( event === 'data' ) callback ( Buffer . from ( body ) )
74+ if ( event === 'end' ) callback ( )
75+ return this
76+ } ,
77+ } as unknown as IncomingMessage
78+ const { handler, res, roomManager, writeHead } = createMocks ( request )
79+
80+ await handler ( request , res )
81+
82+ expect ( roomManager . emitToRoom ) . toHaveBeenCalledWith (
83+ { type : 'workspace-workflows' , id : workspaceId } ,
84+ 'workspace-workflows-changed' ,
85+ { workspaceId, timestamp : expect . any ( Number ) }
86+ )
87+ expect ( writeHead ) . toHaveBeenCalledWith ( 200 , { 'Content-Type' : 'application/json' } )
88+ } )
6189} )
0 commit comments