Skip to content

Commit

Permalink
Added unit testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Eswar2103 committed Aug 16, 2024
1 parent cfaca17 commit 0a18775
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
10 changes: 7 additions & 3 deletions server/src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,16 @@ function createAndSendInteractionLog(response, method, params, ws) {
success: true,
response: "",
};

interactionLog.params = params;
interactionLog.method = method;
interactionLog.response = response;
ws && ws.send(JSON.stringify({ FireboltInteraction: interactionLog }));
logger.debug(`Sent interaction log for user ${config.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`);
if (ws) {
ws.send(JSON.stringify({ FireboltInteraction: interactionLog }));
logger.debug(`Sent interaction log for user ${config.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`);
} else {
logger.error(`Error in createAndSendInteractionLog: ws object is not provided`);
}
}
} catch (error) {
logger.error(`Error in createAndSendInteractionLog: ${error}`);
Expand Down
10 changes: 9 additions & 1 deletion server/test/suite/messageHandler.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import * as messageHandler from "../../src/messageHandler.mjs";
import { logger } from "../../src/logger.mjs";
import * as fireboltOpenRpc from "../../src/fireboltOpenRpc.mjs";
import { methodTriggers } from "../../src/triggers.mjs";
import { config } from "../../src/config.mjs";
import * as userManagement from "../../src/userManagement.mjs";

test(`messageHandler.handleMessage works properly and return when message doesn't have any id`, async () => {
const spy = jest.spyOn(logger, "info");
Expand Down Expand Up @@ -165,6 +167,7 @@ test(`messageHandler.handleMessage works properly, message param is false`, asyn
});

test(`messageHandler.handleMessage works properly, for logger.debug`, async () => {
config.interactionService = {enabled: true, user: "12345"};
fireboltOpenRpc.testExports.methodMaps["core"] = {
"lifecycle.onInactive": {
name: "lifecycle.onInactive",
Expand Down Expand Up @@ -199,14 +202,18 @@ test(`messageHandler.handleMessage works properly, for logger.debug`, async () =
},
};

userManagement.testExports.associateUserWithWs("12345", { send: () => {} });
const debugSpy = jest.spyOn(logger, "debug");
const dummyMsgFour =
'{"jsonrpc":"2.0","method":"rpc.discover","params":{"listen":true},"id":1}';
await messageHandler.handleMessage(dummyMsgFour, "12345", { send: () => {} });
expect(debugSpy).toHaveBeenCalled();
userManagement.testExports.user2ws.delete("12345");
delete config.interactionService;
});

test(`messageHandler.handleMessage works properly for error scenarios`, async () => {
test.only(`messageHandler.handleMessage works properly for error scenarios`, async () => {
config.interactionService = {enabled: true, user: "12345"};
fireboltOpenRpc.testExports.methodMaps["core"] = {
"lifecycle.onInactive": {
name: "lifecycle.onInactive",
Expand Down Expand Up @@ -241,6 +248,7 @@ test(`messageHandler.handleMessage works properly for error scenarios`, async ()
'{"jsonrpc":"2.0","method":"rpc.discover","params":{"listen":true},"id":1}';
await messageHandler.handleMessage(dummyMsgFive, "12345", { send: () => {} });
expect(errorSpy).toHaveBeenCalled();
delete config.interactionService;
});

test(`messageHandler.handleMessage works properly for developerNotes`, async () => {
Expand Down
26 changes: 26 additions & 0 deletions server/test/suite/util.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import {jest} from '@jest/globals';
import * as fs from 'fs';
import * as util from '../../src/util.mjs';
import { logger } from "../../src/logger.mjs";
import { config } from "../../src/config.mjs";

test(`util.delay works properly`, () => {
jest.useFakeTimers();
Expand Down Expand Up @@ -98,3 +100,27 @@ test(`util.mergeArrayOfStrings works properly`, () => {
);
expect(result).toEqual(["test"]);
});

test(`util.createAndSendInteractionLog works properly when disabled`, () => {
config.interactionService = {enabled: false, user: "12345"};
const debugSpy = jest.spyOn(logger, "debug");
util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, {send: () => {}} );
expect(debugSpy).toHaveBeenCalledTimes(0);
delete config.interactionService;
});

test(`util.createAndSendInteractionLog works properly when enabled`, () => {
config.interactionService = {enabled: true, user: "12345"};
const debugSpy = jest.spyOn(logger, "debug");
util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, {send: () => {}} );
expect(debugSpy).toHaveBeenCalled();
delete config.interactionService;
});

test(`util.createAndSendInteractionLog works properly without ws object`, () => {
config.interactionService = {enabled: true, user: "12345"};
const errorSpy = jest.spyOn(logger, "error");
util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, undefined );
expect(errorSpy).toHaveBeenCalled();
delete config.interactionService;
});

0 comments on commit 0a18775

Please sign in to comment.