Skip to content

Commit

Permalink
feat(cc-sdk): added-tests-for-edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adhmenon committed Dec 6, 2024
1 parent 441c992 commit 05b9ace
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
1 change: 0 additions & 1 deletion packages/@webex/plugin-cc/src/services/task/TaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export default class TaskManager extends EventEmitter {
break;
case CC_EVENTS.AGENT_WRAPPEDUP:
this.removeCurrentTaskFromCollection();
this.currentTask.emit(TASK_EVENTS.TASK_WRAPUP, this.currentTask);
break;
default:
break;
Expand Down
18 changes: 5 additions & 13 deletions packages/@webex/plugin-cc/src/services/task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,11 @@ export default class Task extends EventEmitter implements ITask {
* @throws Error
* @example
* ```typescript
* task.hold(taskId).then(()=>{}).catch(()=>{})
* task.hold().then(()=>{}).catch(()=>{})
* ```
* */
public async hold(): Promise<TaskResponse> {
try {
if (!this.data.mediaResourceId || this.data.mediaResourceId.length === 0) {
throw new Error('MediaResourceId is required');
}

return this.contact.hold({
interactionId: this.data.interactionId,
data: {mediaResourceId: this.data.mediaResourceId},
Expand All @@ -140,15 +136,11 @@ export default class Task extends EventEmitter implements ITask {
* @throws Error
* @example
* ```typescript
* task.resume(taskId).then(()=>{}).catch(()=>{})
* task.resume().then(()=>{}).catch(()=>{})
* ```
*/
public async resume(): Promise<TaskResponse> {
try {
if (!this.data.mediaResourceId || this.data.mediaResourceId.length === 0) {
throw new Error('MediaResourceId is required');
}

return this.contact.unHold({
interactionId: this.data.interactionId,
data: {mediaResourceId: this.data.mediaResourceId},
Expand All @@ -165,7 +157,7 @@ export default class Task extends EventEmitter implements ITask {
* @throws Error
* @example
* ```typescript
* task.end(taskId).then(()=>{}).catch(()=>{})
* task.end().then(()=>{}).catch(()=>{})
* ```
*/
public async end(): Promise<TaskResponse> {
Expand All @@ -179,12 +171,12 @@ export default class Task extends EventEmitter implements ITask {

/**
* This is used to wrap up the task.
* @param data - WrapupPayLoad
* @param wrapupPayload - WrapupPayLoad
* @returns Promise<TaskResponse>
* @throws Error
* @example
* ```typescript
* task.wrapup(taskId, data).then(()=>{}).catch(()=>{})
* task.wrapup(wrapupPayload).then(()=>{}).catch(()=>{})
* ```
*/
public async wrapup(wrapupPayload: WrapupPayLoad): Promise<TaskResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,74 @@ describe('TaskManager', () => {
taskManager.unregisterIncomingCallEvent();
expect(webCallingServiceOffSpy).toHaveBeenCalledWith(LINE_EVENTS.INCOMING_CALL, webCallingServiceOffSpy.mock.calls[0][1]);
});

it('should emit TASK_END event on AGENT_WRAPUP event', () => {
// Need to setup the task with current task
const firstPayload = {
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',
},
};
webSocketManagerMock.emit('message', JSON.stringify(firstPayload));

const wrapupPayload = {
data: {
type: CC_EVENTS.AGENT_WRAPUP,
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',
},
};

const taskEmitSpy = jest.spyOn(taskManager.currentTask, 'emit');

webSocketManagerMock.emit('message', JSON.stringify(wrapupPayload));

expect(taskManager.currentTask.updateTaskData).toHaveBeenCalledWith(wrapupPayload.data);
expect(taskEmitSpy).toHaveBeenCalledWith(TASK_EVENTS.TASK_END, taskManager.currentTask);
});

it('should remove currentTask from taskCollection on AGENT_WRAPPEDUP event', () => {
const payload = {
data: {
type: CC_EVENTS.AGENT_WRAPPEDUP,
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',
},
};

taskManager.taskCollection[taskId] = taskManager.currentTask;

webSocketManagerMock.emit('message', JSON.stringify(payload));

expect(taskManager.getTask(taskId)).toBeUndefined();
});
});
10 changes: 0 additions & 10 deletions packages/@webex/plugin-cc/test/unit/spec/services/task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ describe('Task', () => {
expect(getErrorDetailsSpy).toHaveBeenCalledWith(error, 'hold', CC_FILE);
});

it('should throw an error if mediaResourceId is missing in hold method', async () => {
task.data.mediaResourceId = '';
await expect(task.hold()).rejects.toThrow();
});

it('should resume the task and return the expected response', async () => {
const expectedResponse: TaskResponse = { data: { interactionId: taskId } } as AgentContact;
contactMock.unHold.mockResolvedValue(expectedResponse);
Expand All @@ -244,11 +239,6 @@ describe('Task', () => {
expect(getErrorDetailsSpy).toHaveBeenCalledWith(error, 'resume', CC_FILE);
});

it('should throw an error if mediaResourceId is missing in hold method', async () => {
task.data.mediaResourceId = '';
await expect(task.resume()).rejects.toThrow();
});

it('should end the task and return the expected response', async () => {
const expectedResponse: TaskResponse = { data: { interactionId: taskId } } as AgentContact;
contactMock.end.mockResolvedValue(expectedResponse);
Expand Down

0 comments on commit 05b9ace

Please sign in to comment.