Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | import { PusherUtils } from '../../src/utils/pusher'; |
| 3 | |
| 4 | // Capture the options passed to the Pusher constructor so the |
| 5 | // channelAuthorization.customHandler can be exercised in isolation. |
| 6 | const { constructorCalls } = vi.hoisted(() => ({ |
| 7 | constructorCalls: [] as Array<{ key: string; options: Record<string, unknown> }>, |
| 8 | })); |
| 9 | |
| 10 | vi.mock('pusher-js', () => { |
| 11 | class FakePusher { |
| 12 | connection = { bind: vi.fn() }; |
| 13 | subscribe = vi.fn(); |
| 14 | unsubscribe = vi.fn(); |
| 15 | bind = vi.fn(); |
| 16 | constructor(key: string, options: Record<string, unknown>) { |
| 17 | constructorCalls.push({ key, options }); |
| 18 | } |
| 19 | } |
| 20 | return { default: FakePusher }; |
| 21 | }); |
| 22 | |
| 23 | type CustomHandler = ( |
| 24 | params: { socketId: string; channelName: string }, |
| 25 | callback: (error: Error | null, data: unknown) => void |
| 26 | ) => void; |
| 27 | |
| 28 | const baseURL = 'https://backend.composio.dev'; |
| 29 | const apiKey = 'test-api-key'; |
| 30 | const authEndpoint = `${baseURL}/api/v3/internal/sdk/realtime/auth`; |
| 31 | |
| 32 | async function getCustomHandler(): Promise<CustomHandler> { |
| 33 | await PusherUtils.getPusherClient(baseURL, apiKey); |
| 34 | const { options } = constructorCalls.at(-1)!; |
| 35 | return options.channelAuthorization.customHandler as CustomHandler; |
| 36 | } |
| 37 | |
| 38 | describe('PusherUtils channelAuthorization', () => { |
| 39 | beforeEach(() => { |
| 40 | constructorCalls.length = 0; |
| 41 | // Reset the cached singleton so getPusherClient rebuilds the client. |
| 42 | (PusherUtils as unknown as { pusherClient?: unknown }).pusherClient = undefined; |
| 43 | }); |
| 44 | |
| 45 | afterEach(() => { |
| 46 | vi.unstubAllGlobals(); |
| 47 | }); |
| 48 | |
| 49 | it('configures the realtime auth endpoint with ajax transport', async () => { |
| 50 | await PusherUtils.getPusherClient(baseURL, apiKey); |
| 51 | const { options } = constructorCalls.at(-1)!; |
| 52 | |
| 53 | expect(options.channelAuthorization.endpoint).toBe(authEndpoint); |
| 54 | expect(options.channelAuthorization.transport).toBe('ajax'); |
| 55 | expect(typeof options.channelAuthorization.customHandler).toBe('function'); |
| 56 | }); |
| 57 | |
| 58 | it('POSTs socket_id/channel_name as JSON and returns the parsed auth payload', async () => { |
| 59 | const authData = { auth: 'app_key:signature' }; |
| 60 | const fetchMock = vi.fn().mockResolvedValue({ text: async () => JSON.stringify(authData) }); |
| 61 | vi.stubGlobal('fetch', fetchMock); |
| 62 | |
| 63 | const customHandler = await getCustomHandler(); |
| 64 | const callback = vi.fn(); |
| 65 | customHandler({ socketId: '123.456', channelName: 'private-abc' }, callback); |
| 66 | |
| 67 | await vi.waitFor(() => expect(callback).toHaveBeenCalled()); |
| 68 | |
| 69 | expect(fetchMock).toHaveBeenCalledWith( |
| 70 | authEndpoint, |
| 71 | expect.objectContaining({ |
| 72 | method: 'POST', |
| 73 | headers: { 'x-api-key': apiKey, 'Content-Type': 'application/json' }, |
| 74 | body: JSON.stringify({ socket_id: '123.456', channel_name: 'private-abc' }), |
| 75 | }) |
| 76 | ); |
| 77 | expect(callback).toHaveBeenCalledWith(null, authData); |
| 78 | }); |
| 79 | |
| 80 | it('reports an error to the callback when the response is not valid JSON', async () => { |
| 81 | const fetchMock = vi.fn().mockResolvedValue({ text: async () => 'not-json' }); |
| 82 | vi.stubGlobal('fetch', fetchMock); |
| 83 | |
| 84 | const customHandler = await getCustomHandler(); |
| 85 | const callback = vi.fn(); |
| 86 | customHandler({ socketId: '1', channelName: 'c' }, callback); |
| 87 | |
| 88 | await vi.waitFor(() => expect(callback).toHaveBeenCalled()); |
| 89 | |
| 90 | const [error, data] = callback.mock.calls[0]; |
| 91 | expect(error).toBeInstanceOf(Error); |
| 92 | expect(data).toBeNull(); |
| 93 | }); |
| 94 | |
| 95 | it('reports an error to the callback when the auth request rejects', async () => { |
| 96 | const fetchMock = vi.fn().mockRejectedValue(new Error('network down')); |
| 97 | vi.stubGlobal('fetch', fetchMock); |
| 98 | |
| 99 | const customHandler = await getCustomHandler(); |
| 100 | const callback = vi.fn(); |
| 101 | customHandler({ socketId: '1', channelName: 'c' }, callback); |
| 102 | |
| 103 | await vi.waitFor(() => expect(callback).toHaveBeenCalled()); |
| 104 | |
| 105 | const [error, data] = callback.mock.calls[0]; |
| 106 | expect(error).toBeInstanceOf(Error); |
| 107 | expect((error as Error).message).toBe('network down'); |
| 108 | expect(data).toBeNull(); |
| 109 | }); |
| 110 | }); |