Skip to content

Commit

Permalink
feat(cc-sdk): fixed-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adhmenon committed Nov 12, 2024
1 parent 8d7bad7 commit 22fdd15
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ export class WebSocketManager extends EventTarget {
});
}

async reconnect() {
await this.connect().catch(() => {
LoggerProxy.logger.error(`[WebSocketStatus] | Error in connecting Websocket`);
});
}

close(shouldReconnect: boolean, reason = 'Unknown') {
if (!this.isSocketClosed && this.shouldReconnect) {
this.shouldReconnect = shouldReconnect;
Expand Down
6 changes: 3 additions & 3 deletions packages/@webex/plugin-cc/src/services/core/aqm-reqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ export default class AqmReqs {
private readonly onMessage = (msg: any) => {
const event = JSON.parse(msg.detail);
if (event.type === 'Welcome') {
LoggerProxy.logger.info(`Welcome message from Notifs Websocket${event}`);
LoggerProxy.logger.info(`Welcome message from Notifs Websocket`);

return;
}

if (event.keepalive === 'true') {
LoggerProxy.logger.info(`Keepalive from web socket ${event}`);
LoggerProxy.logger.info(`Keepalive from web socket`);

return;
}
Expand Down Expand Up @@ -241,7 +241,7 @@ export default class AqmReqs {

if (!isHandled) {
LoggerProxy.logger.info(
`event=missingEventHandler | [AqmReqs] missing routing message handler${event}`
`event=missingEventHandler | [AqmReqs] missing routing message handler`
);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { WebSocketManager } from '../../../../../../src/services/core/WebSocket/WebSocketManager';
import { WebexSDK, SubscribeRequest } from '../../../../../../src/types';
import { SUBSCRIBE_API, WCC_API_GATEWAY } from '../../../../../../src/services/constants';
import LoggerProxy from '../../../../../../src/logger-proxy';

jest.mock('../../../../../../src/services/core/HttpRequest');
jest.mock('../../../../../../src/logger-proxy', () => ({
Expand All @@ -10,6 +11,7 @@ jest.mock('../../../../../../src/logger-proxy', () => ({
logger: {
log: jest.fn(),
error: jest.fn(),
info: jest.fn(),
},
initialize: jest.fn(),
},
Expand Down Expand Up @@ -49,6 +51,13 @@ describe('WebSocketManager', () => {
let mockWebex: WebexSDK;
let mockWorker: any;

const fakeSubscribeRequest: SubscribeRequest = {
force: true,
isKeepAliveEnabled: false,
clientType: 'WebexCCSDK',
allowMultiLogin: true,
};

beforeEach(() => {
jest.clearAllMocks();

Expand Down Expand Up @@ -77,7 +86,6 @@ describe('WebSocketManager', () => {
setTimeout(() => {
MockWebSocket.inst.onopen();
MockWebSocket.inst.onmessage({ data: JSON.stringify({ type: "Welcome" }) });
webSocketManager.close(false);
}, 1);

console.log = jest.fn();
Expand All @@ -97,17 +105,13 @@ describe('WebSocketManager', () => {

(mockWebex.request as jest.Mock).mockResolvedValueOnce(subscribeResponse);

const subscribeRequest: SubscribeRequest = {
routingId: 'test-routing-id',
};

await webSocketManager.initWebSocket({ body: subscribeRequest });
await webSocketManager.initWebSocket({ body: fakeSubscribeRequest });

expect(mockWebex.request).toHaveBeenCalledWith({
service: WCC_API_GATEWAY,
resource: SUBSCRIBE_API,
method: 'POST',
body: subscribeRequest,
body: fakeSubscribeRequest,
});
});

Expand All @@ -120,11 +124,7 @@ describe('WebSocketManager', () => {

(mockWebex.request as jest.Mock).mockResolvedValueOnce(subscribeResponse);

const subscribeRequest: SubscribeRequest = {
routingId: 'test-routing-id',
};

await webSocketManager.initWebSocket({ body: subscribeRequest });
await webSocketManager.initWebSocket({ body: fakeSubscribeRequest });

webSocketManager.close(true, 'Test reason');

Expand All @@ -141,11 +141,7 @@ describe('WebSocketManager', () => {

(mockWebex.request as jest.Mock).mockResolvedValueOnce(subscribeResponse);

const subscribeRequest: SubscribeRequest = {
routingId: 'test-routing-id',
};

await webSocketManager.initWebSocket({ body: subscribeRequest });
await webSocketManager.initWebSocket({ body: fakeSubscribeRequest });

setTimeout(() => {
MockWebSocket.inst.onopen();
Expand All @@ -160,7 +156,7 @@ describe('WebSocketManager', () => {
expect(MockWebSocket.inst.send).toHaveBeenCalledWith(JSON.stringify({ keepalive: 'true' }));
});

it('should handle WebSocket close due to network issue', async () => {
it('should handle web socket close and webSocketOnCloseHandler', async () => {
const subscribeResponse = {
body: {
webSocketUrl: 'wss://fake-url',
Expand All @@ -169,21 +165,34 @@ describe('WebSocketManager', () => {

(mockWebex.request as jest.Mock).mockResolvedValueOnce(subscribeResponse);

const subscribeRequest: SubscribeRequest = {
routingId: 'test-routing-id',
};

await webSocketManager.initWebSocket({ body: subscribeRequest });
await webSocketManager.initWebSocket({ body: fakeSubscribeRequest });
webSocketManager.shouldReconnect = true;

// Mock navigator.onLine to simulate network issue
Object.defineProperty(global, 'navigator', {
value: {
onLine: false,
},
configurable: true,
});
setTimeout(() => {
MockWebSocket.inst.onopen();
mockWorker.onmessage({
data: {
type: 'closeSocket'
}
MockWebSocket.inst.onclose({
wasClean: false,
code: 1006,
reason: 'network issue',
target: MockWebSocket.inst,
});
}, 1);

expect(MockWebSocket.inst.close).toHaveBeenCalled();
// Wait for the close event to be handled
await new Promise((resolve) => setTimeout(resolve, 10));

expect(mockWorker.postMessage).toHaveBeenCalledWith({ type: 'terminate' });
expect(LoggerProxy.logger.info).toHaveBeenCalledWith(
'[WebSocketStatus] | desktop online status is false'
);
expect(LoggerProxy.logger.error).toHaveBeenCalledWith(
'[WebSocketStatus] | event=webSocketClose | WebSocket connection closed REASON: network issue'
);
});
});
Loading

0 comments on commit 22fdd15

Please sign in to comment.