From 31d88e1c268c79348c4cf90bb9659d4ca112834b Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Wed, 14 Aug 2024 20:53:53 +0530 Subject: [PATCH 01/10] Implementation of Interaction log service feature --- server/src/configureAPI.mjs | 3 +++ server/src/events.mjs | 8 +++++++- server/src/messageHandler.mjs | 4 ++++ server/src/routes/api/state.mjs | 35 ++++++++++++++++++++++++++++++++- server/src/util.mjs | 33 ++++++++++++++++++++++++++++++- 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/server/src/configureAPI.mjs b/server/src/configureAPI.mjs index fee33dde..cb42e4d3 100644 --- a/server/src/configureAPI.mjs +++ b/server/src/configureAPI.mjs @@ -63,6 +63,9 @@ function configureAPI(app) { // Revert to the way things were when server started up app.post('/api/v1/state/revert', stateApi.revertState); + // Start or Stop the interaction log service + app.post('/api/v1/state/interactionService', stateApi.interactionService); + // ======================= State-Related API Routes ======================= app.put('/api/v1/user/:userId?', userApi.addUser); diff --git a/server/src/events.mjs b/server/src/events.mjs index e2f0bff6..a2aaf40e 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -29,7 +29,7 @@ import { logger } from './logger.mjs'; import * as fireboltOpenRpc from './fireboltOpenRpc.mjs'; import { config } from './config.mjs'; import { updateCallWithResponse } from './sessionManagement.mjs'; -import { createCaseAgnosticMethod } from './util.mjs'; +import { createCaseAgnosticMethod, createInteractionLog } from './util.mjs'; const { dotConfig: { eventConfig } } = config; @@ -226,6 +226,9 @@ function sendEventListenerAck(userId, ws, metadata) { const ackMessage = template(metadata); const parsedAckMessage = JSON.parse(ackMessage); + const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); + createInteractionLog(ackMessage, metadata.method, metadata.registration.params, userData); // creating interaction log and send it to the client + ws.send(ackMessage); logger.debug(`Sent registration event ack message for user ${userId}: ${ackMessage}`); updateCallWithResponse(metadata.method, parsedAckMessage.result, "result", userId) @@ -295,6 +298,9 @@ function emitResponse(finalResult, msg, userId, method) { //Update the call with event response updateCallWithResponse(method, eventMessage, "events", userId); wsArr.forEach((ws) => { + const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); + createInteractionLog(eventMessage, method, null, userData); // creating interaction log and send it to the client + ws.send(eventMessage); // Check if eventType is included in config if (eventConfig.eventType) { diff --git a/server/src/messageHandler.mjs b/server/src/messageHandler.mjs index 36a45b73..746ea716 100644 --- a/server/src/messageHandler.mjs +++ b/server/src/messageHandler.mjs @@ -345,6 +345,10 @@ async function handleMessage(message, userId, ws) { } const dly = stateManagement.getAppropriateDelay(userId, oMsg.method); await util.delay(dly); + + const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); + util.createInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userData); // creating interaction log and send it to the client + ws.send(finalResponse); logger.debug(`Sent message for user ${userId}: ${finalResponse}`); updateCallWithResponse(oMsg.method, JSON.parse(finalResponse).result, "result", userId) diff --git a/server/src/routes/api/state.mjs b/server/src/routes/api/state.mjs index 4bc6b831..16718494 100644 --- a/server/src/routes/api/state.mjs +++ b/server/src/routes/api/state.mjs @@ -289,11 +289,44 @@ function revertState(req, res) { }); } +// POST /api/v1/state/interactionService +// Expected body: { enabled: true|false, user: 'xxx' } +// where user is the user ID the interaction logs to be sent +// E.g., { enabled: true, user: '456~A' } +function interactionService(req, res) { + let message; + if (req.body.enabled && req.body.user) { + config.dotConfig.interactionService = req.body; + if (config.dotConfig?.interactionService?.enabled == true) { + message = "Successfully started interactionService"; + } else if (req.body?.interactionService == false) { + message = "Successfully stopped interactionService"; + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "INVALID-ENABLED-VALUE", + message: + "Enabled value must be true or false", + }); + } + return res.status(200).send({ + status: message, + }); + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "MISSING-STATE", + message: + "Did not find expected 'enabled' and 'user' key within post body", + }); + } +} + // --- Exports --- export { getState, setLatency, setMode, setMethodResult, setMethodError, - updateState, revertState + updateState, revertState, interactionService }; diff --git a/server/src/util.mjs b/server/src/util.mjs index e7fc3546..4a1128d5 100644 --- a/server/src/util.mjs +++ b/server/src/util.mjs @@ -29,6 +29,7 @@ import { fileURLToPath } from 'url'; import path from 'path'; import fs from 'fs'; +import { logger } from './logger.mjs'; // Use: await delay(2000); function delay(ms) { @@ -169,6 +170,36 @@ function createCaseAgnosticMethod(method){ return method; } +/* +* @function: createInteractionLog +* @Description: Create interaction log and send it to the client +* @param {String} response - Response of the method call +* @param {String} method - Name of the method +* @param {String} params - Params of the method call +* @param {Object} ws - WS object to send the interaction log +*/ +function createInteractionLog(response, method, params, ws) { + try { + if (config?.dotConfig?.interactionService?.enabled == true) { + const interactionLog = { + app_id: "mock-firebolt", + method: "", + params: "", + 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.dotConfig.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); + } + } catch (error) { + logger.error(`Error in createInteractionLog: ${error}`); + } +} + // --- Exports --- -export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod }; \ No newline at end of file +export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod, createInteractionLog }; \ No newline at end of file From d9b5c672ce06a34bdb477086a35d2954142fc808 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Fri, 16 Aug 2024 15:50:15 +0530 Subject: [PATCH 02/10] Code re-structure --- server/src/configureAPI.mjs | 7 ++-- server/src/events.mjs | 10 +++--- server/src/messageHandler.mjs | 4 +-- server/src/routes/api/state.mjs | 35 +----------------- server/src/routes/api/user.mjs | 63 ++++++++++++++++++++++++++++++++- server/src/util.mjs | 12 +++---- 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/server/src/configureAPI.mjs b/server/src/configureAPI.mjs index cb42e4d3..7c2fd625 100644 --- a/server/src/configureAPI.mjs +++ b/server/src/configureAPI.mjs @@ -63,8 +63,11 @@ function configureAPI(app) { // Revert to the way things were when server started up app.post('/api/v1/state/revert', stateApi.revertState); - // Start or Stop the interaction log service - app.post('/api/v1/state/interactionService', stateApi.interactionService); + // Start the interaction log service + app.post('/api/v1/user/enableInteractionLogs', userApi.enableInteractionLogs); + + // Stop the interaction log service + app.post('/api/v1/user/disableInteractionLogs', userApi.disableInteractionLogs); // ======================= State-Related API Routes ======================= diff --git a/server/src/events.mjs b/server/src/events.mjs index a2aaf40e..8c374810 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -29,7 +29,7 @@ import { logger } from './logger.mjs'; import * as fireboltOpenRpc from './fireboltOpenRpc.mjs'; import { config } from './config.mjs'; import { updateCallWithResponse } from './sessionManagement.mjs'; -import { createCaseAgnosticMethod, createInteractionLog } from './util.mjs'; +import { createCaseAgnosticMethod, createAndSendInteractionLog } from './util.mjs'; const { dotConfig: { eventConfig } } = config; @@ -226,8 +226,8 @@ function sendEventListenerAck(userId, ws, metadata) { const ackMessage = template(metadata); const parsedAckMessage = JSON.parse(ackMessage); - const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); - createInteractionLog(ackMessage, metadata.method, metadata.registration.params, userData); // creating interaction log and send it to the client + const userWSData = userManagement.getWsForUser(config.interactionService?.user); + createAndSendInteractionLog(ackMessage, metadata.method, metadata.registration.params, userWSData); // creating interaction log and send it to the client ws.send(ackMessage); logger.debug(`Sent registration event ack message for user ${userId}: ${ackMessage}`); @@ -298,8 +298,8 @@ function emitResponse(finalResult, msg, userId, method) { //Update the call with event response updateCallWithResponse(method, eventMessage, "events", userId); wsArr.forEach((ws) => { - const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); - createInteractionLog(eventMessage, method, null, userData); // creating interaction log and send it to the client + const userWSData = userManagement.getWsForUser(config.interactionService?.user); + createAndSendInteractionLog(eventMessage, method, null, userWSData); // creating interaction log and send it to the client ws.send(eventMessage); // Check if eventType is included in config diff --git a/server/src/messageHandler.mjs b/server/src/messageHandler.mjs index 746ea716..ed89b34b 100644 --- a/server/src/messageHandler.mjs +++ b/server/src/messageHandler.mjs @@ -346,8 +346,8 @@ async function handleMessage(message, userId, ws) { const dly = stateManagement.getAppropriateDelay(userId, oMsg.method); await util.delay(dly); - const userData = userManagement.getWsForUser(config?.dotConfig?.interactionService?.user); - util.createInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userData); // creating interaction log and send it to the client + const userWSData = userManagement.getWsForUser(config?.interactionService?.user); + util.createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData); // creating interaction log and send it to the client ws.send(finalResponse); logger.debug(`Sent message for user ${userId}: ${finalResponse}`); diff --git a/server/src/routes/api/state.mjs b/server/src/routes/api/state.mjs index 16718494..4bc6b831 100644 --- a/server/src/routes/api/state.mjs +++ b/server/src/routes/api/state.mjs @@ -289,44 +289,11 @@ function revertState(req, res) { }); } -// POST /api/v1/state/interactionService -// Expected body: { enabled: true|false, user: 'xxx' } -// where user is the user ID the interaction logs to be sent -// E.g., { enabled: true, user: '456~A' } -function interactionService(req, res) { - let message; - if (req.body.enabled && req.body.user) { - config.dotConfig.interactionService = req.body; - if (config.dotConfig?.interactionService?.enabled == true) { - message = "Successfully started interactionService"; - } else if (req.body?.interactionService == false) { - message = "Successfully stopped interactionService"; - } else { - return res.status(400).send({ - status: "ERROR", - errorCode: "INVALID-ENABLED-VALUE", - message: - "Enabled value must be true or false", - }); - } - return res.status(200).send({ - status: message, - }); - } else { - return res.status(400).send({ - status: "ERROR", - errorCode: "MISSING-STATE", - message: - "Did not find expected 'enabled' and 'user' key within post body", - }); - } -} - // --- Exports --- export { getState, setLatency, setMode, setMethodResult, setMethodError, - updateState, revertState, interactionService + updateState, revertState }; diff --git a/server/src/routes/api/user.mjs b/server/src/routes/api/user.mjs index 9a076d79..3365480e 100644 --- a/server/src/routes/api/user.mjs +++ b/server/src/routes/api/user.mjs @@ -23,6 +23,7 @@ import { v4 as uuidv4 } from 'uuid'; import * as userManagement from '../../userManagement.mjs'; import * as stateManagement from '../../stateManagement.mjs'; +import { config } from '../../config.mjs'; import { logger } from '../../logger.mjs'; @@ -84,9 +85,69 @@ function getUsers(req, res) { }); } +// POST /api/v1/state/enableInteractionLogs +// Expected body: { enabled: true } +// Expected headers: x-mockfirebolt-userid - The user id of the user making the request +// E.g., { enabled: true } +function enableInteractionLogs(req, res) { + if (req.body.hasOwnProperty("enabled")) { + const user = req.get("x-mockfirebolt-userid"); + config.interactionService = req.body; + config.interactionService.user = user; + if (config.interactionService?.enabled == true) { + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully started interactionService", + }); + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "INVALID-ENABLED-VALUE", + message: "Enabled value must be true", + }); + } + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "MISSING-STATE", + message: "Did not find expected 'enabled' key within post body", + }); + } +} + +// POST /api/v1/state/disableInteractionLogs +// Expected body: { enabled: false } +// Expected headers: x-mockfirebolt-userid - The user id of the user making the request +// E.g., { enabled: false } +function disableInteractionLogs(req, res) { + if (req.body.hasOwnProperty("enabled")) { + const user = req.get("x-mockfirebolt-userid"); + config.interactionService = req.body; + config.interactionService.user = user; + if (config.interactionService?.enabled == false) { + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully stopped interactionService", + }); + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "INVALID-ENABLED-VALUE", + message: "Enabled value must be false", + }); + } + } else { + return res.status(400).send({ + status: "ERROR", + errorCode: "MISSING-STATE", + message: "Did not find expected 'enabled' key within post body", + }); + } +} + // --- Exports --- export { - addUser,getUsers + addUser,getUsers,enableInteractionLogs,disableInteractionLogs }; diff --git a/server/src/util.mjs b/server/src/util.mjs index 4a1128d5..4a75a506 100644 --- a/server/src/util.mjs +++ b/server/src/util.mjs @@ -171,16 +171,16 @@ function createCaseAgnosticMethod(method){ } /* -* @function: createInteractionLog +* @function: createAndSendInteractionLog * @Description: Create interaction log and send it to the client * @param {String} response - Response of the method call * @param {String} method - Name of the method * @param {String} params - Params of the method call * @param {Object} ws - WS object to send the interaction log */ -function createInteractionLog(response, method, params, ws) { +function createAndSendInteractionLog(response, method, params, ws) { try { - if (config?.dotConfig?.interactionService?.enabled == true) { + if (config.interactionService?.enabled == true) { const interactionLog = { app_id: "mock-firebolt", method: "", @@ -193,13 +193,13 @@ function createInteractionLog(response, method, params, ws) { interactionLog.method = method; interactionLog.response = response; ws && ws.send(JSON.stringify({ FireboltInteraction: interactionLog })); - logger.debug(`Sent interaction log for user ${config.dotConfig.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); + logger.debug(`Sent interaction log for user ${config.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); } } catch (error) { - logger.error(`Error in createInteractionLog: ${error}`); + logger.error(`Error in createAndSendInteractionLog: ${error}`); } } // --- Exports --- -export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod, createInteractionLog }; \ No newline at end of file +export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod, createAndSendInteractionLog }; \ No newline at end of file From 892ea736761d1f95fbce6b40a20ce872cb255e24 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Fri, 16 Aug 2024 15:57:59 +0530 Subject: [PATCH 03/10] Code re-structure --- server/src/events.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/events.mjs b/server/src/events.mjs index 8c374810..eaf6b119 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -297,9 +297,9 @@ function emitResponse(finalResult, msg, userId, method) { } //Update the call with event response updateCallWithResponse(method, eventMessage, "events", userId); + const userWSData = userManagement.getWsForUser(config.interactionService?.user); + createAndSendInteractionLog(eventMessage, method, null, userWSData); // creating interaction log and send it to the client wsArr.forEach((ws) => { - const userWSData = userManagement.getWsForUser(config.interactionService?.user); - createAndSendInteractionLog(eventMessage, method, null, userWSData); // creating interaction log and send it to the client ws.send(eventMessage); // Check if eventType is included in config From cfaca1737497d43c9abe6f98d720450176797f0e Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Fri, 16 Aug 2024 15:59:35 +0530 Subject: [PATCH 04/10] Removed unnecessary line spaces --- server/src/events.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/events.mjs b/server/src/events.mjs index eaf6b119..de599097 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -300,7 +300,6 @@ function emitResponse(finalResult, msg, userId, method) { const userWSData = userManagement.getWsForUser(config.interactionService?.user); createAndSendInteractionLog(eventMessage, method, null, userWSData); // creating interaction log and send it to the client wsArr.forEach((ws) => { - ws.send(eventMessage); // Check if eventType is included in config if (eventConfig.eventType) { From 0a187750dd7e9658b8204fc92d546e358905c6c6 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Fri, 16 Aug 2024 18:45:11 +0530 Subject: [PATCH 05/10] Added unit testcases --- server/src/util.mjs | 10 ++++++--- server/test/suite/messageHandler.test.mjs | 10 ++++++++- server/test/suite/util.test.mjs | 26 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/server/src/util.mjs b/server/src/util.mjs index 4a75a506..23965d32 100644 --- a/server/src/util.mjs +++ b/server/src/util.mjs @@ -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}`); diff --git a/server/test/suite/messageHandler.test.mjs b/server/test/suite/messageHandler.test.mjs index d2dce180..19a5a8fd 100644 --- a/server/test/suite/messageHandler.test.mjs +++ b/server/test/suite/messageHandler.test.mjs @@ -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"); @@ -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", @@ -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", @@ -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 () => { diff --git a/server/test/suite/util.test.mjs b/server/test/suite/util.test.mjs index 19c328d1..1791184b 100644 --- a/server/test/suite/util.test.mjs +++ b/server/test/suite/util.test.mjs @@ -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(); @@ -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; +}); \ No newline at end of file From c3c24cccc3dc358dea8f8c25ad656828a36403b5 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Fri, 16 Aug 2024 18:46:53 +0530 Subject: [PATCH 06/10] Added unit testcases --- server/test/suite/messageHandler.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/test/suite/messageHandler.test.mjs b/server/test/suite/messageHandler.test.mjs index 19a5a8fd..38117b41 100644 --- a/server/test/suite/messageHandler.test.mjs +++ b/server/test/suite/messageHandler.test.mjs @@ -212,7 +212,7 @@ test(`messageHandler.handleMessage works properly, for logger.debug`, async () = delete config.interactionService; }); -test.only(`messageHandler.handleMessage works properly for error scenarios`, async () => { +test(`messageHandler.handleMessage works properly for error scenarios`, async () => { config.interactionService = {enabled: true, user: "12345"}; fireboltOpenRpc.testExports.methodMaps["core"] = { "lifecycle.onInactive": { From 2a8ed9878c727baf73e172e87c5b1e18b534e509 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Wed, 21 Aug 2024 19:11:47 +0530 Subject: [PATCH 07/10] Made interaction log services support for multiple users --- server/src/events.mjs | 12 ++++--- server/src/messageHandler.mjs | 8 +++-- server/src/routes/api/user.mjs | 43 +++++++++++++---------- server/src/util.mjs | 9 +++-- server/test/suite/messageHandler.test.mjs | 5 ++- server/test/suite/util.test.mjs | 16 ++------- 6 files changed, 46 insertions(+), 47 deletions(-) diff --git a/server/src/events.mjs b/server/src/events.mjs index de599097..5cc724a5 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -226,8 +226,10 @@ function sendEventListenerAck(userId, ws, metadata) { const ackMessage = template(metadata); const parsedAckMessage = JSON.parse(ackMessage); - const userWSData = userManagement.getWsForUser(config.interactionService?.user); - createAndSendInteractionLog(ackMessage, metadata.method, metadata.registration.params, userWSData); // creating interaction log and send it to the client + config.interactionService.forEach((_, userId) => { + const userWSData = userManagement.getWsForUser(userId); + createAndSendInteractionLog(ackMessage, metadata.method, metadata.registration.params, userWSData, userId); // creating interaction log and send it to the client + }); ws.send(ackMessage); logger.debug(`Sent registration event ack message for user ${userId}: ${ackMessage}`); @@ -297,8 +299,10 @@ function emitResponse(finalResult, msg, userId, method) { } //Update the call with event response updateCallWithResponse(method, eventMessage, "events", userId); - const userWSData = userManagement.getWsForUser(config.interactionService?.user); - createAndSendInteractionLog(eventMessage, method, null, userWSData); // creating interaction log and send it to the client + config.interactionService.forEach((_, userId) => { + const userWSData = userManagement.getWsForUser(userId); + createAndSendInteractionLog(eventMessage, method, null, userWSData, userId); // creating interaction log and send it to the client + }); wsArr.forEach((ws) => { ws.send(eventMessage); // Check if eventType is included in config diff --git a/server/src/messageHandler.mjs b/server/src/messageHandler.mjs index ed89b34b..23c4fe8b 100644 --- a/server/src/messageHandler.mjs +++ b/server/src/messageHandler.mjs @@ -346,12 +346,14 @@ async function handleMessage(message, userId, ws) { const dly = stateManagement.getAppropriateDelay(userId, oMsg.method); await util.delay(dly); - const userWSData = userManagement.getWsForUser(config?.interactionService?.user); - util.createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData); // creating interaction log and send it to the client - ws.send(finalResponse); logger.debug(`Sent message for user ${userId}: ${finalResponse}`); updateCallWithResponse(oMsg.method, JSON.parse(finalResponse).result, "result", userId) + + config.interactionService.forEach((_, userId) => { + const userWSData = userManagement.getWsForUser(userId); + util.createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData, userId); // creating interaction log and send it to the client + }); } // --- Exports --- diff --git a/server/src/routes/api/user.mjs b/server/src/routes/api/user.mjs index 3365480e..d10c1031 100644 --- a/server/src/routes/api/user.mjs +++ b/server/src/routes/api/user.mjs @@ -91,21 +91,20 @@ function getUsers(req, res) { // E.g., { enabled: true } function enableInteractionLogs(req, res) { if (req.body.hasOwnProperty("enabled")) { - const user = req.get("x-mockfirebolt-userid"); - config.interactionService = req.body; - config.interactionService.user = user; - if (config.interactionService?.enabled == true) { - return res.status(200).send({ - status: "SUCCESS", - message: "Successfully started interactionService", - }); - } else { + if (req.body.enabled !== true) { return res.status(400).send({ status: "ERROR", errorCode: "INVALID-ENABLED-VALUE", message: "Enabled value must be true", }); } + const user = req.get("x-mockfirebolt-userid"); + config.interactionService == undefined && (config.interactionService = new Map()); + config.interactionService.set(user, req.body); + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully started interactionService", + }); } else { return res.status(400).send({ status: "ERROR", @@ -121,22 +120,30 @@ function enableInteractionLogs(req, res) { // E.g., { enabled: false } function disableInteractionLogs(req, res) { if (req.body.hasOwnProperty("enabled")) { - const user = req.get("x-mockfirebolt-userid"); - config.interactionService = req.body; - config.interactionService.user = user; - if (config.interactionService?.enabled == false) { - return res.status(200).send({ - status: "SUCCESS", - message: "Successfully stopped interactionService", - }); - } else { + if (req.body.enabled !== false) { + // If the incoming request enabled value is not `false`, return an error return res.status(400).send({ status: "ERROR", errorCode: "INVALID-ENABLED-VALUE", message: "Enabled value must be false", }); } + const user = req.get("x-mockfirebolt-userid"); + // If the user is not found in the interactionService, return an error + if (!config.interactionService.has(user)) { + return res.status(400).send({ + status: "ERROR", + errorCode: "USER-NOT-FOUND", + message: "User not found in interactionService", + }); + } + config.interactionService.delete(user); + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully stopped interactionService", + }); } else { + // If the incoming request does not have an enabled key, return an error return res.status(400).send({ status: "ERROR", errorCode: "MISSING-STATE", diff --git a/server/src/util.mjs b/server/src/util.mjs index 23965d32..845cacaa 100644 --- a/server/src/util.mjs +++ b/server/src/util.mjs @@ -170,17 +170,17 @@ function createCaseAgnosticMethod(method){ return method; } -/* +/** * @function: createAndSendInteractionLog * @Description: Create interaction log and send it to the client * @param {String} response - Response of the method call * @param {String} method - Name of the method * @param {String} params - Params of the method call * @param {Object} ws - WS object to send the interaction log +* @param {String} ws - UserId value */ -function createAndSendInteractionLog(response, method, params, ws) { +function createAndSendInteractionLog(response, method, params, ws, userId) { try { - if (config.interactionService?.enabled == true) { const interactionLog = { app_id: "mock-firebolt", method: "", @@ -194,11 +194,10 @@ function createAndSendInteractionLog(response, method, params, ws) { interactionLog.response = response; if (ws) { ws.send(JSON.stringify({ FireboltInteraction: interactionLog })); - logger.debug(`Sent interaction log for user ${config.interactionService.user}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); + logger.debug(`Sent interaction log for user ${userId}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); } else { logger.error(`Error in createAndSendInteractionLog: ws object is not provided`); } - } } catch (error) { logger.error(`Error in createAndSendInteractionLog: ${error}`); } diff --git a/server/test/suite/messageHandler.test.mjs b/server/test/suite/messageHandler.test.mjs index 38117b41..063a29ca 100644 --- a/server/test/suite/messageHandler.test.mjs +++ b/server/test/suite/messageHandler.test.mjs @@ -167,7 +167,8 @@ 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"}; + config.interactionService = new Map(); + config.interactionService.set("12345", { enabled: true }); fireboltOpenRpc.testExports.methodMaps["core"] = { "lifecycle.onInactive": { name: "lifecycle.onInactive", @@ -213,7 +214,6 @@ test(`messageHandler.handleMessage works properly, for logger.debug`, async () = }); test(`messageHandler.handleMessage works properly for error scenarios`, async () => { - config.interactionService = {enabled: true, user: "12345"}; fireboltOpenRpc.testExports.methodMaps["core"] = { "lifecycle.onInactive": { name: "lifecycle.onInactive", @@ -248,7 +248,6 @@ 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 () => { diff --git a/server/test/suite/util.test.mjs b/server/test/suite/util.test.mjs index 1791184b..98caec87 100644 --- a/server/test/suite/util.test.mjs +++ b/server/test/suite/util.test.mjs @@ -101,26 +101,14 @@ test(`util.mergeArrayOfStrings works properly`, () => { expect(result).toEqual(["test"]); }); -test(`util.createAndSendInteractionLog works properly when disabled`, () => { - config.interactionService = {enabled: false, user: "12345"}; +test(`util.createAndSendInteractionLog works properly`, () => { 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: () => {}} ); + util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, {send: () => {}}, '12345' ); 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; }); \ No newline at end of file From 3c8f42ea6653e53d79207e0d0cae3c1169d0993a Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Wed, 21 Aug 2024 19:39:17 +0530 Subject: [PATCH 08/10] Unit testcases failure fix --- server/src/events.mjs | 4 ++-- server/src/messageHandler.mjs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/events.mjs b/server/src/events.mjs index 5cc724a5..bed5a49e 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -226,7 +226,7 @@ function sendEventListenerAck(userId, ws, metadata) { const ackMessage = template(metadata); const parsedAckMessage = JSON.parse(ackMessage); - config.interactionService.forEach((_, userId) => { + config.interactionService && config.interactionService.forEach((_, userId) => { const userWSData = userManagement.getWsForUser(userId); createAndSendInteractionLog(ackMessage, metadata.method, metadata.registration.params, userWSData, userId); // creating interaction log and send it to the client }); @@ -299,7 +299,7 @@ function emitResponse(finalResult, msg, userId, method) { } //Update the call with event response updateCallWithResponse(method, eventMessage, "events", userId); - config.interactionService.forEach((_, userId) => { + config.interactionService && config.interactionService.forEach((_, userId) => { const userWSData = userManagement.getWsForUser(userId); createAndSendInteractionLog(eventMessage, method, null, userWSData, userId); // creating interaction log and send it to the client }); diff --git a/server/src/messageHandler.mjs b/server/src/messageHandler.mjs index 23c4fe8b..e57ef5d1 100644 --- a/server/src/messageHandler.mjs +++ b/server/src/messageHandler.mjs @@ -350,7 +350,7 @@ async function handleMessage(message, userId, ws) { logger.debug(`Sent message for user ${userId}: ${finalResponse}`); updateCallWithResponse(oMsg.method, JSON.parse(finalResponse).result, "result", userId) - config.interactionService.forEach((_, userId) => { + config.interactionService && config.interactionService.forEach((_, userId) => { const userWSData = userManagement.getWsForUser(userId); util.createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData, userId); // creating interaction log and send it to the client }); From 54eab547995f902c273f4c1403237afa658b9dfb Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Mon, 26 Aug 2024 17:53:54 +0530 Subject: [PATCH 09/10] Code re-structuring --- server/src/configureAPI.mjs | 4 +- server/src/events.mjs | 3 +- server/src/interactionLog.mjs | 64 +++++++++++++++++++ server/src/messageHandler.mjs | 3 +- server/src/routes/api/state.mjs | 39 +++++++++++- server/src/routes/api/user.mjs | 71 +--------------------- server/src/util.mjs | 36 +---------- server/test/suite/interactionLogs.test.mjs | 35 +++++++++++ server/test/suite/util.test.mjs | 14 ----- 9 files changed, 145 insertions(+), 124 deletions(-) create mode 100644 server/src/interactionLog.mjs create mode 100644 server/test/suite/interactionLogs.test.mjs diff --git a/server/src/configureAPI.mjs b/server/src/configureAPI.mjs index 7c2fd625..7113253d 100644 --- a/server/src/configureAPI.mjs +++ b/server/src/configureAPI.mjs @@ -64,10 +64,10 @@ function configureAPI(app) { app.post('/api/v1/state/revert', stateApi.revertState); // Start the interaction log service - app.post('/api/v1/user/enableInteractionLogs', userApi.enableInteractionLogs); + app.post('/api/v1/state/interactionLogs/start', stateApi.enableInteractionLogs); // Stop the interaction log service - app.post('/api/v1/user/disableInteractionLogs', userApi.disableInteractionLogs); + app.post('/api/v1/state/interactionLogs/stop', stateApi.disableInteractionLogs); // ======================= State-Related API Routes ======================= diff --git a/server/src/events.mjs b/server/src/events.mjs index bed5a49e..3df1a958 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -29,7 +29,8 @@ import { logger } from './logger.mjs'; import * as fireboltOpenRpc from './fireboltOpenRpc.mjs'; import { config } from './config.mjs'; import { updateCallWithResponse } from './sessionManagement.mjs'; -import { createCaseAgnosticMethod, createAndSendInteractionLog } from './util.mjs'; +import { createCaseAgnosticMethod } from './util.mjs'; +import { createAndSendInteractionLog } from './interactionService.mjs'; const { dotConfig: { eventConfig } } = config; diff --git a/server/src/interactionLog.mjs b/server/src/interactionLog.mjs new file mode 100644 index 00000000..ddfb8384 --- /dev/null +++ b/server/src/interactionLog.mjs @@ -0,0 +1,64 @@ +/* +* Copyright 2021 Comcast Cable Communications Management, LLC +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* SPDX-License-Identifier: Apache-2.0 +*/ + +// InteractionLogs + +import { logger } from './logger.mjs'; + +/** + * @function: createAndSendInteractionLog + * @Description: Create interaction log and send it to the client + * @param {String} response - Response of the method call + * @param {String} method - Name of the method + * @param {String} params - Params of the method call + * @param {Object} ws - WS object to send the interaction log + * @param {String} ws - UserId value + */ +function createAndSendInteractionLog(response, method, params, ws, userId) { + try { + const interactionLog = { + app_id: "mock-firebolt", + method: "", + params: "", + success: true, + response: "", + }; + + interactionLog.params = params; + interactionLog.method = method; + interactionLog.response = response; + if (ws) { + ws.send(JSON.stringify({ FireboltInteraction: interactionLog })); + logger.debug( + `Sent interaction log for user ${userId}: ${JSON.stringify({ + FireboltInteraction: interactionLog, + })}` + ); + } else { + logger.error( + `Error in createAndSendInteractionLog: ws object is not provided` + ); + } + } catch (error) { + logger.error(`Error in createAndSendInteractionLog: ${error}`); + } +} + +// ----exports---- + +export { createAndSendInteractionLog }; diff --git a/server/src/messageHandler.mjs b/server/src/messageHandler.mjs index e57ef5d1..bac93232 100644 --- a/server/src/messageHandler.mjs +++ b/server/src/messageHandler.mjs @@ -32,6 +32,7 @@ import { addCall, updateCallWithResponse } from './sessionManagement.mjs'; import * as proxyManagement from './proxyManagement.mjs'; import * as conduit from './conduit.mjs'; import { config } from './config.mjs'; +import { createAndSendInteractionLog } from './interactionLog.mjs'; const { dotConfig: { eventConfig } } = config; @@ -352,7 +353,7 @@ async function handleMessage(message, userId, ws) { config.interactionService && config.interactionService.forEach((_, userId) => { const userWSData = userManagement.getWsForUser(userId); - util.createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData, userId); // creating interaction log and send it to the client + createAndSendInteractionLog(finalResponse, JSON.parse(message).method, JSON.parse(message).params, userWSData, userId); // creating interaction log and send it to the client }); } diff --git a/server/src/routes/api/state.mjs b/server/src/routes/api/state.mjs index 4bc6b831..c00352d5 100644 --- a/server/src/routes/api/state.mjs +++ b/server/src/routes/api/state.mjs @@ -289,11 +289,48 @@ function revertState(req, res) { }); } +// POST /api/v1/state/enableInteractionLogs +// Expected body: None +// Expected headers: x-mockfirebolt-userid - The user id of the user making the request +function enableInteractionLogs(req, res) { + const user = getUserIdFromReq(req); + logger.info(`Enabling interaction logs for user ${user}`); + config.interactionService == undefined && + (config.interactionService = new Map()); + config.interactionService.set(user, { enabled: true }); + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully started interactionService", + }); +} + +// POST /api/v1/state/disableInteractionLogs +// Expected body: None +// Expected headers: x-mockfirebolt-userid - The user id of the user making the request +function disableInteractionLogs(req, res) { + const user = getUserIdFromReq(req); + logger.info(`Disabling interaction logs for user ${user}`); + // If the user is not found in the interactionService, return an error + if (!config.interactionService.has(user)) { + return res.status(400).send({ + status: "ERROR", + errorCode: "USER-NOT-FOUND", + message: "User not found", + }); + } + config.interactionService.delete(user); + return res.status(200).send({ + status: "SUCCESS", + message: "Successfully stopped interactionService", + }); +} + // --- Exports --- export { getState, setLatency, setMode, setMethodResult, setMethodError, - updateState, revertState + updateState, revertState, + enableInteractionLogs, disableInteractionLogs }; diff --git a/server/src/routes/api/user.mjs b/server/src/routes/api/user.mjs index d10c1031..9c096209 100644 --- a/server/src/routes/api/user.mjs +++ b/server/src/routes/api/user.mjs @@ -23,7 +23,6 @@ import { v4 as uuidv4 } from 'uuid'; import * as userManagement from '../../userManagement.mjs'; import * as stateManagement from '../../stateManagement.mjs'; -import { config } from '../../config.mjs'; import { logger } from '../../logger.mjs'; @@ -85,76 +84,8 @@ function getUsers(req, res) { }); } -// POST /api/v1/state/enableInteractionLogs -// Expected body: { enabled: true } -// Expected headers: x-mockfirebolt-userid - The user id of the user making the request -// E.g., { enabled: true } -function enableInteractionLogs(req, res) { - if (req.body.hasOwnProperty("enabled")) { - if (req.body.enabled !== true) { - return res.status(400).send({ - status: "ERROR", - errorCode: "INVALID-ENABLED-VALUE", - message: "Enabled value must be true", - }); - } - const user = req.get("x-mockfirebolt-userid"); - config.interactionService == undefined && (config.interactionService = new Map()); - config.interactionService.set(user, req.body); - return res.status(200).send({ - status: "SUCCESS", - message: "Successfully started interactionService", - }); - } else { - return res.status(400).send({ - status: "ERROR", - errorCode: "MISSING-STATE", - message: "Did not find expected 'enabled' key within post body", - }); - } -} - -// POST /api/v1/state/disableInteractionLogs -// Expected body: { enabled: false } -// Expected headers: x-mockfirebolt-userid - The user id of the user making the request -// E.g., { enabled: false } -function disableInteractionLogs(req, res) { - if (req.body.hasOwnProperty("enabled")) { - if (req.body.enabled !== false) { - // If the incoming request enabled value is not `false`, return an error - return res.status(400).send({ - status: "ERROR", - errorCode: "INVALID-ENABLED-VALUE", - message: "Enabled value must be false", - }); - } - const user = req.get("x-mockfirebolt-userid"); - // If the user is not found in the interactionService, return an error - if (!config.interactionService.has(user)) { - return res.status(400).send({ - status: "ERROR", - errorCode: "USER-NOT-FOUND", - message: "User not found in interactionService", - }); - } - config.interactionService.delete(user); - return res.status(200).send({ - status: "SUCCESS", - message: "Successfully stopped interactionService", - }); - } else { - // If the incoming request does not have an enabled key, return an error - return res.status(400).send({ - status: "ERROR", - errorCode: "MISSING-STATE", - message: "Did not find expected 'enabled' key within post body", - }); - } -} - - // --- Exports --- export { - addUser,getUsers,enableInteractionLogs,disableInteractionLogs + addUser,getUsers }; diff --git a/server/src/util.mjs b/server/src/util.mjs index 845cacaa..e7fc3546 100644 --- a/server/src/util.mjs +++ b/server/src/util.mjs @@ -29,7 +29,6 @@ import { fileURLToPath } from 'url'; import path from 'path'; import fs from 'fs'; -import { logger } from './logger.mjs'; // Use: await delay(2000); function delay(ms) { @@ -170,39 +169,6 @@ function createCaseAgnosticMethod(method){ return method; } -/** -* @function: createAndSendInteractionLog -* @Description: Create interaction log and send it to the client -* @param {String} response - Response of the method call -* @param {String} method - Name of the method -* @param {String} params - Params of the method call -* @param {Object} ws - WS object to send the interaction log -* @param {String} ws - UserId value -*/ -function createAndSendInteractionLog(response, method, params, ws, userId) { - try { - const interactionLog = { - app_id: "mock-firebolt", - method: "", - params: "", - success: true, - response: "", - }; - - interactionLog.params = params; - interactionLog.method = method; - interactionLog.response = response; - if (ws) { - ws.send(JSON.stringify({ FireboltInteraction: interactionLog })); - logger.debug(`Sent interaction log for user ${userId}: ${JSON.stringify({ FireboltInteraction: interactionLog })}`); - } else { - logger.error(`Error in createAndSendInteractionLog: ws object is not provided`); - } - } catch (error) { - logger.error(`Error in createAndSendInteractionLog: ${error}`); - } -} - // --- Exports --- -export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod, createAndSendInteractionLog }; \ No newline at end of file +export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate, searchObjectForKey, replaceKeyInObject, createCaseAgnosticMethod }; \ No newline at end of file diff --git a/server/test/suite/interactionLogs.test.mjs b/server/test/suite/interactionLogs.test.mjs new file mode 100644 index 00000000..441b3b30 --- /dev/null +++ b/server/test/suite/interactionLogs.test.mjs @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Comcast Cable Communications Management, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +// InteractionLogs: Tests + +import {jest} from '@jest/globals'; +import { createAndSendInteractionLog } from "../../src/interactionLog.mjs"; +import { logger } from "../../src/logger.mjs"; + +test(`interactionLogs.createAndSendInteractionLog works properly`, () => { + const debugSpy = jest.spyOn(logger, "debug"); + createAndSendInteractionLog('{name: "id"}', "account.id", {}, {send: () => {}}, '12345' ); + expect(debugSpy).toHaveBeenCalled(); +}); + +test(`interactionLogs.createAndSendInteractionLog works properly without ws object`, () => { + const errorSpy = jest.spyOn(logger, "error"); + createAndSendInteractionLog('{name: "id"}', "account.id", {}, undefined ); + expect(errorSpy).toHaveBeenCalled(); +}); \ No newline at end of file diff --git a/server/test/suite/util.test.mjs b/server/test/suite/util.test.mjs index 98caec87..19c328d1 100644 --- a/server/test/suite/util.test.mjs +++ b/server/test/suite/util.test.mjs @@ -23,8 +23,6 @@ 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(); @@ -100,15 +98,3 @@ test(`util.mergeArrayOfStrings works properly`, () => { ); expect(result).toEqual(["test"]); }); - -test(`util.createAndSendInteractionLog works properly`, () => { - const debugSpy = jest.spyOn(logger, "debug"); - util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, {send: () => {}}, '12345' ); - expect(debugSpy).toHaveBeenCalled(); -}); - -test(`util.createAndSendInteractionLog works properly without ws object`, () => { - const errorSpy = jest.spyOn(logger, "error"); - util.createAndSendInteractionLog('{name: "id"}', "account.id", {}, undefined ); - expect(errorSpy).toHaveBeenCalled(); -}); \ No newline at end of file From b5563211d57fcdd55cf535caf32714bb5b7141c1 Mon Sep 17 00:00:00 2001 From: Kummithi Guru Eswar Sainath Reddy Date: Mon, 26 Aug 2024 18:25:25 +0530 Subject: [PATCH 10/10] Unit test fixes --- server/src/events.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/events.mjs b/server/src/events.mjs index 3df1a958..740acbf3 100644 --- a/server/src/events.mjs +++ b/server/src/events.mjs @@ -29,8 +29,8 @@ import { logger } from './logger.mjs'; import * as fireboltOpenRpc from './fireboltOpenRpc.mjs'; import { config } from './config.mjs'; import { updateCallWithResponse } from './sessionManagement.mjs'; +import { createAndSendInteractionLog } from './interactionLog.mjs'; import { createCaseAgnosticMethod } from './util.mjs'; -import { createAndSendInteractionLog } from './interactionService.mjs'; const { dotConfig: { eventConfig } } = config;