|
1 | 1 | import { GenesysCloudWebrtcSdk } from '../../src/client'; |
2 | 2 | import { SimpleMockSdk } from '../test-utils'; |
3 | 3 | import { CommunicationStates } from '../../src/types/enums'; |
4 | | -import { SubscriptionEvent } from '../../src/types/interfaces'; |
5 | | -import { handleConversationUpdate, setupStreamingClient, handleDisconnectedEvent } from '../../src/client-private'; |
| 4 | +import { ICustomerData, IPersonDetails, SubscriptionEvent } from '../../src/types/interfaces'; |
| 5 | +import { handleConversationUpdate, setupStreamingClient, handleDisconnectedEvent, proxyStreamingClientEvents } from '../../src/client-private'; |
6 | 6 | import { ConversationUpdate } from '../../src/'; |
7 | 7 | import StreamingClient from 'genesys-cloud-streaming-client'; |
| 8 | +import { NotificationsApi } from "purecloud-platform-client-v2"; |
8 | 9 |
|
9 | 10 | jest.mock('genesys-cloud-streaming-client'); |
10 | 11 |
|
@@ -90,7 +91,7 @@ describe('setupStreamingClient', () => { |
90 | 91 | describe('handleConversationUpdate', () => { |
91 | 92 | it('should call sessionManager.handleConversationUpdate with the transformed event', () => { |
92 | 93 | mockSdk.sessionManager = { handleConversationUpdateRaw: jest.fn(), handleConversationUpdate: jest.fn() } as any; |
93 | | - |
| 94 | + |
94 | 95 | const userId = '444kjskdk'; |
95 | 96 | const participant1 = { |
96 | 97 | id: '7b809e10-fb79-4420-9d5f-69d232ddf490', |
@@ -219,3 +220,99 @@ describe('handleDisconnectedEvent', () => { |
219 | 220 | ); |
220 | 221 | }); |
221 | 222 | }); |
| 223 | + |
| 224 | +describe('proxyStreamingClientEvents', () => { |
| 225 | + it('should handle JWT auth path', async () => { |
| 226 | + const { handleConversationUpdateSpy, mockConversationUpdate } = await createEventDataAndCallProxyFunction(); |
| 227 | + |
| 228 | + expect(handleConversationUpdateSpy).toHaveBeenCalledWith(new ConversationUpdate(mockConversationUpdate.eventBody)); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should handle JWT auth path with no conversation id', async () => { |
| 232 | + const { handleConversationUpdateSpy, conversationUpdateHandler } = await createEventDataAndCallProxyFunction({ conversationId: undefined }); |
| 233 | + |
| 234 | + expect(conversationUpdateHandler).toBe(undefined); |
| 235 | + expect(handleConversationUpdateSpy).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should handle JWT auth path with non-agent conference', async () => { |
| 239 | + const { handleConversationUpdateSpy, conversationUpdateHandler } = await createEventDataAndCallProxyFunction({ jabberId: 'adhoc-123@conference.com' }); |
| 240 | + |
| 241 | + expect(conversationUpdateHandler).toBe(undefined); |
| 242 | + expect(handleConversationUpdateSpy).not.toHaveBeenCalled(); |
| 243 | + }); |
| 244 | + |
| 245 | + it('should handle JWT auth path with missing conference jid', async () => { |
| 246 | + const { handleConversationUpdateSpy, conversationUpdateHandler } = await createEventDataAndCallProxyFunction({ jabberId: undefined }); |
| 247 | + |
| 248 | + expect(conversationUpdateHandler).toBe(undefined); |
| 249 | + expect(handleConversationUpdateSpy).not.toHaveBeenCalled(); |
| 250 | + }); |
| 251 | + |
| 252 | + it('should handle JWT auth path with guest with userId', async () => { |
| 253 | + const { handleConversationUpdateSpy, conversationUpdateHandler } = await createEventDataAndCallProxyFunction({ userId: 'user123'}); |
| 254 | + |
| 255 | + expect(conversationUpdateHandler).toBe(undefined); |
| 256 | + expect(handleConversationUpdateSpy).not.toHaveBeenCalled(); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should still handle non-JWT auth path', async () => { |
| 260 | + const { handleConversationUpdateSpy, mockConversationUpdate } = await createEventDataAndCallProxyFunction({ |
| 261 | + userId: 'user123', |
| 262 | + isJwtAuth: false |
| 263 | + }); |
| 264 | + |
| 265 | + expect(handleConversationUpdateSpy).toHaveBeenCalledWith(new ConversationUpdate(mockConversationUpdate.eventBody)); |
| 266 | + expect(mockSdk._customerData).toBe(undefined); |
| 267 | + }); |
| 268 | + |
| 269 | + async function createEventDataAndCallProxyFunction(options = {} as { userId?: string, jabberId?: string, conversationId?: string, isJwtAuth?: boolean }) { |
| 270 | + const userId = 'userId' in options ? options.userId : undefined; |
| 271 | + const jabberId = 'jabberId' in options ? options.jabberId : 'agent-123@conference.com'; |
| 272 | + const conversationId = 'conversationId' in options ? options.conversationId : 'conv123'; |
| 273 | + const isJwtAuth = options.isJwtAuth ?? true; |
| 274 | + |
| 275 | + mockSdk._personDetails = { |
| 276 | + id: userId, |
| 277 | + chat: { jabberId: jabberId } |
| 278 | + } as IPersonDetails; |
| 279 | + |
| 280 | + if (isJwtAuth) { // we only assign this on jwt path |
| 281 | + mockSdk._customerData = { |
| 282 | + conversation: { id: conversationId } |
| 283 | + } as ICustomerData; |
| 284 | + } |
| 285 | + |
| 286 | + Object.defineProperty(mockSdk, 'isJwtAuth', { get: () => isJwtAuth }); |
| 287 | + |
| 288 | + let conversationUpdateHandler; |
| 289 | + |
| 290 | + mockSdk._streamingConnection = { |
| 291 | + on: jest.fn((event, handler) => { |
| 292 | + if (event === `notify:v2.guest.conversations.${conversationId}`) { |
| 293 | + if (conversationUpdateHandler) throw new Error('handler already assigned'); |
| 294 | + conversationUpdateHandler = handler; |
| 295 | + } |
| 296 | + }), |
| 297 | + webrtcSessions: { on: jest.fn() }, |
| 298 | + notifications: { |
| 299 | + subscribe: jest.fn((topic, handler) => { |
| 300 | + if (topic === `v2.users.${userId}.conversations`) { |
| 301 | + if (conversationUpdateHandler) throw new Error('handler already assigned'); |
| 302 | + conversationUpdateHandler = handler; |
| 303 | + } |
| 304 | + }) |
| 305 | + } |
| 306 | + } as unknown as StreamingClient; |
| 307 | + |
| 308 | + await proxyStreamingClientEvents.call(mockSdk); |
| 309 | + |
| 310 | + mockSdk.sessionManager = { handleConversationUpdate: jest.fn(), handleConversationUpdateRaw: jest.fn() } as never; |
| 311 | + const mockConversationUpdate = { eventBody: {}, metadata: { correlationId: '' }, topicName: '' }; |
| 312 | + if (conversationUpdateHandler) conversationUpdateHandler(mockConversationUpdate); |
| 313 | + |
| 314 | + const handleConversationUpdateSpy = mockSdk.sessionManager.handleConversationUpdate; |
| 315 | + |
| 316 | + return { handleConversationUpdateSpy, conversationUpdateHandler, mockConversationUpdate}; |
| 317 | + } |
| 318 | +}); |
0 commit comments