From 4c6a2bc2a7eec293e4724c18310b4183ebcc5008 Mon Sep 17 00:00:00 2001 From: Priya Date: Wed, 4 Dec 2024 23:37:16 +0530 Subject: [PATCH] fix(plugin-cc): ut added for off listeners --- .../unit/spec/services/task/TaskManager.ts | 148 ++++++++++++++++-- .../test/unit/spec/services/task/index.ts | 10 +- 2 files changed, 140 insertions(+), 18 deletions(-) diff --git a/packages/@webex/plugin-cc/test/unit/spec/services/task/TaskManager.ts b/packages/@webex/plugin-cc/test/unit/spec/services/task/TaskManager.ts index 5a761725c1e..375f0d51343 100644 --- a/packages/@webex/plugin-cc/test/unit/spec/services/task/TaskManager.ts +++ b/packages/@webex/plugin-cc/test/unit/spec/services/task/TaskManager.ts @@ -1,12 +1,14 @@ import 'jsdom-global/register'; import EventEmitter from 'events'; -import { LoginOption } from '../../../../../src/types'; -import { LINE_EVENTS } from '@webex/calling'; +import { LoginOption, WebexSDK } from '../../../../../src/types'; +import { CALL_EVENT_KEYS, CallingClientConfig, LINE_EVENTS } from '@webex/calling'; import { CC_EVENTS } from '../../../../../src/services/config/types'; import TaskManager from '../../../../../src/services/task/TaskManager'; import * as contact from '../../../../../src/services/task/contact' import Task from '../../../../../src/services/task'; import { TASK_EVENTS } from '../../../../../src/services/task/types'; +import WebCallingService from '../../../../../src/services/WebCallingService'; +import config from '../../../../../src/config'; jest.mock('./../../../../../src/services/task', () => { return jest.fn().mockImplementation(() => { @@ -17,19 +19,70 @@ jest.mock('./../../../../../src/services/task', () => { }); describe('TaskManager', () => { + let mockCall; + let webSocketManagerMock; + let onSpy; let taskManager; let contactMock; - let webCallingServiceMock ; - let webSocketManagerMock; + let taskDataMock; + let webCallingService; + let webex: WebexSDK; + const taskId = '0ae913a4-c857-4705-8d49-76dd3dde75e4'; beforeEach(() => { contactMock = contact; - webCallingServiceMock = new EventEmitter(); webSocketManagerMock = new EventEmitter(); - webCallingServiceMock.loginOption = LoginOption.BROWSER; + webex = { + logger: { + log: jest.fn(), + error: jest.fn(), + info: jest.fn() + }, + } as unknown as WebexSDK; + + webCallingService = new WebCallingService( + webex, + config.cc.callingClientConfig as CallingClientConfig + ); + + mockCall = { + on: jest.fn(), + off: jest.fn(), + answer: jest.fn(), + mute: jest.fn(), + isMuted: jest.fn().mockReturnValue(true), + end: jest.fn() + }; + + taskDataMock = { + type: CC_EVENTS.AGENT_CONTACT_RESERVED, + agentId: "723a8ffb-a26e-496d-b14a-ff44fb83b64f", + eventTime: 1733211616959, + eventType: "RoutingMessage", + interaction: {}, + interactionId: taskId, + orgId: "6ecef209-9a34-4ed1-a07a-7ddd1dbe925a", + trackingId: "575c0ec2-618c-42af-a61c-53aeb0a221ee", + mediaResourceId: '0ae913a4-c857-4705-8d49-76dd3dde75e4', + destAgentId: 'ebeb893b-ba67-4f36-8418-95c7492b28c2', + owner: '723a8ffb-a26e-496d-b14a-ff44fb83b64f', + queueMgr: 'aqm', + }; + + webCallingService.loginOption = LoginOption.BROWSER; + webCallingService.call = mockCall; + onSpy = jest.spyOn(webCallingService, 'on'); - taskManager = new TaskManager(contactMock, webCallingServiceMock, webSocketManagerMock); + taskManager = new TaskManager(contactMock, webCallingService, webSocketManagerMock); + taskManager.currentTask = { + accept: jest.fn(), + decline: jest.fn(), + updateTaskData: jest.fn(), + unregisterWebCallListeners: jest.fn(), + data: taskDataMock + } + taskManager.call = mockCall; }); afterEach(() => { @@ -37,12 +90,20 @@ describe('TaskManager', () => { }); it('should initialize TaskManager and register listeners', () => { + const incomingCallCb = onSpy.mock.calls[0][1]; + const taskEmitSpy = jest.spyOn(taskManager, 'emit'); + expect(taskManager).toBeInstanceOf(TaskManager); - expect(webCallingServiceMock.listenerCount(LINE_EVENTS.INCOMING_CALL)).toBe(1); + expect(webCallingService.listenerCount(LINE_EVENTS.INCOMING_CALL)).toBe(1); expect(webSocketManagerMock.listenerCount('message')).toBe(1); + expect(onSpy).toHaveBeenCalledWith(LINE_EVENTS.INCOMING_CALL, incomingCallCb); + + incomingCallCb(mockCall); + + expect(taskEmitSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_INCOMING, taskManager.currentTask); }); - it('should handle WebSocket message for AGENT_CONTACT_RESERVED and emit task:incoming', () => { + it('should handle WebSocket message for AGENT_CONTACT_RESERVED and emit task:incoming for browser case', () => { const payload = { data: { type: CC_EVENTS.AGENT_CONTACT_RESERVED, @@ -60,18 +121,11 @@ describe('TaskManager', () => { }, }; - taskManager.call = { - answer: jest.fn(), - mute: jest.fn(), - isMuted: jest.fn().mockReturnValue(true), - end: jest.fn() - }; - const taskIncomingSpy = jest.spyOn(taskManager, 'emit'); webSocketManagerMock.emit('message', JSON.stringify(payload)); - expect(Task).toHaveBeenCalledWith(contactMock, webCallingServiceMock , payload.data); + expect(Task).toHaveBeenCalledWith(contactMock, webCallingService , payload.data); expect(taskIncomingSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_INCOMING, taskManager.currentTask); expect(taskManager.getTask(payload.data.interactionId)).toBe(taskManager.currentTask); expect(taskManager.getAllTasks()).toHaveProperty(payload.data.interactionId); @@ -100,6 +154,35 @@ describe('TaskManager', () => { expect(taskAssignedSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_ASSIGNED, taskManager.currentTask); }); + it('should handle WebSocket message for AGENT_CONTACT_RESERVED and emit task:incoming for extension case', () => { + webCallingService.loginOption = LoginOption.EXTENSION; + const payload = { + data: { + type: CC_EVENTS.AGENT_CONTACT_RESERVED, + agentId: "723a8ffb-a26e-496d-b14a-ff44fb83b64f", + eventTime: 1733211616959, + eventType: "RoutingMessage", + interaction: {}, + interactionId: "0ae913a4-c857-4705-8d49-76dd3dde75e4", + orgId: "6ecef209-9a34-4ed1-a07a-7ddd1dbe925a", + trackingId: "575c0ec2-618c-42af-a61c-53aeb0a221ee", + mediaResourceId: '0ae913a4-c857-4705-8d49-76dd3dde75e4', + destAgentId: 'ebeb893b-ba67-4f36-8418-95c7492b28c2', + owner: '723a8ffb-a26e-496d-b14a-ff44fb83b64f', + queueMgr: 'aqm', + }, + }; + + const taskIncomingSpy = jest.spyOn(taskManager, 'emit'); + + webSocketManagerMock.emit('message', JSON.stringify(payload)); + + expect(Task).toHaveBeenCalledWith(contactMock, webCallingService , payload.data); + expect(taskIncomingSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_INCOMING, taskManager.currentTask); + expect(taskManager.getTask(payload.data.interactionId)).toBe(taskManager.currentTask); + expect(taskManager.getAllTasks()).toHaveProperty(payload.data.interactionId); + }); + it('should return task by ID', () => { const taskId = 'task123'; const mockTask = { @@ -178,4 +261,35 @@ describe('TaskManager', () => { expect(allTasks).toHaveProperty(taskId1, mockTask1); expect(allTasks).toHaveProperty(taskId2, mockTask2); }); + + it('test call listeners being switched off on call end', () => { + const taskEmitSpy = jest.spyOn(taskManager, 'emit'); + const webCallingServiceOffSpy = jest.spyOn(webCallingService, 'off'); + const callOffSpy = jest.spyOn(mockCall, 'off'); + const payload = { + data: { + type: CC_EVENTS.CONTACT_ENDED, + agentId: "723a8ffb-a26e-496d-b14a-ff44fb83b64f", + eventTime: 1733211616959, + eventType: "RoutingMessage", + interaction: {}, + interactionId: taskId, + orgId: "6ecef209-9a34-4ed1-a07a-7ddd1dbe925a", + trackingId: "575c0ec2-618c-42af-a61c-53aeb0a221ee", + mediaResourceId: '0ae913a4-c857-4705-8d49-76dd3dde75e4', + destAgentId: 'ebeb893b-ba67-4f36-8418-95c7492b28c2', + owner: '723a8ffb-a26e-496d-b14a-ff44fb83b64f', + queueMgr: 'aqm', + }, + }; + + webSocketManagerMock.emit('message', JSON.stringify(payload)); + + expect(taskEmitSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_UNASSIGNED, taskManager.currentTask); + expect(taskManager.currentTask.unregisterWebCallListeners).toHaveBeenCalledWith(); + expect(callOffSpy).toHaveBeenCalledWith(CALL_EVENT_KEYS.REMOTE_MEDIA, callOffSpy.mock.calls[0][1]); + + taskManager.unregisterIncomingCallEvent(); + expect(webCallingServiceOffSpy).toHaveBeenCalledWith(LINE_EVENTS.INCOMING_CALL, webCallingServiceOffSpy.mock.calls[0][1]); + }); }); \ No newline at end of file diff --git a/packages/@webex/plugin-cc/test/unit/spec/services/task/index.ts b/packages/@webex/plugin-cc/test/unit/spec/services/task/index.ts index 10b915ca628..c54b37cea0f 100644 --- a/packages/@webex/plugin-cc/test/unit/spec/services/task/index.ts +++ b/packages/@webex/plugin-cc/test/unit/spec/services/task/index.ts @@ -7,6 +7,7 @@ import * as Utils from '../../../../../src/services/core/Utils'; import { CC_EVENTS } from '../../../../../src/services/config/types'; import config from '../../../../../src/config'; import WebCallingService from '../../../../../src/services/WebCallingService'; +import { TASK_EVENTS } from '../../../../../src/services/task/types'; jest.mock('@webex/calling'); @@ -90,7 +91,14 @@ describe('Task', () => { }); it('test the on spy', async () => { - expect(onSpy).toHaveBeenCalledWith(CALL_EVENT_KEYS.REMOTE_MEDIA, onSpy.mock.calls[0][1]); + const taskEmitSpy = jest.spyOn(task, 'emit'); + const remoteMediaCb = onSpy.mock.calls[0][1]; + + expect(onSpy).toHaveBeenCalledWith(CALL_EVENT_KEYS.REMOTE_MEDIA, remoteMediaCb); + + remoteMediaCb(mockTrack) + + expect(taskEmitSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_MEDIA, mockTrack); }); it('test updating the task data', async () => {