Skip to content

Commit

Permalink
incorporate event changes in my branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ksentak committed Aug 10, 2023
1 parent 56829ae commit 464a5f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion server/src/events.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ function emitResponse(finalResult, msg, userId, method) {
// If event template config does not exist, just send the raw finalResult
eventMessage = finalResult;
}

//Update the call with event response
updateCallWithResponse(method, eventMessage, "events", userId);
wsArr.forEach((ws) => {
ws.send(eventMessage);
// Check if eventType is included in config
Expand Down
21 changes: 17 additions & 4 deletions server/src/sessionManagement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SessionHandler {
}
}

open(dir) {
open(dir, userId) {
this._determineMode(dir);

if (this.mode === 'websocket') {
Expand All @@ -57,7 +57,7 @@ class SessionHandler {
fs.mkdirSync(dir, { recursive: true });
}

this.stream = fs.createWriteStream(`${dir}/FireboltCalls_live.log`, { flags: 'a' });
this.stream = fs.createWriteStream(`${dir}/FireboltCalls_${userId}_live.log`, { flags: 'a' });
}
}

Expand Down Expand Up @@ -388,6 +388,13 @@ class Session {
}
recordings.push(responseJson)
}
const eventJson = {
type: "event",
timestamp: sessionDataJson.calls[i].timestamp,
method: sessionDataJson.calls[i].methodCall,
events: sessionDataJson.calls[i].response
}
recordings.push(eventJson)
}
//sort by timestamp ascending
recordings.sort(function(a,b) {
Expand Down Expand Up @@ -456,7 +463,7 @@ function getOutputFormat(userId) {

function setOutputDir(dir, userId) {
if (sessionRecording[userId].recordedSession.sessionOutput === "live") {
sessionRecording[userId].recordedSession.sessionHandler.open(dir);
sessionRecording[userId].recordedSession.sessionHandler.open(dir, userId);
}
sessionRecording[userId].recordedSession.sessionOutputPath = dir;
sessionRecording[userId].recordedSession.mockOutputPath = dir;
Expand Down Expand Up @@ -487,14 +494,20 @@ function updateCallWithResponse(method, result, key, userId) {
}
}

// Created to return sessionRecording object for unit testcase
function getMockEventCall(userId){
return sessionRecording[userId].recordedSession.calls;
}

// Utility function for unit tests
const setTestSessionRecording = (mockRecording) => {
function setTestSessionRecording (mockRecording) {
sessionRecording = mockRecording;
}

export const testExports = {
setTestSessionRecording,
setOutputDir,
getMockEventCall,
SessionHandler
}

Expand Down
31 changes: 30 additions & 1 deletion server/test/suite/sessionManagement.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,36 @@ test('verify updateCallWithResponse is working', () => {
sessionManagement.addCall("testing", {});
const result = sessionManagement.updateCallWithResponse("testing", "testing_session", "result", userId);
expect(result).toBeUndefined();
})
});

test('verify updateCallWithResponse is working for recording Events', () => {
const mockSessionRecording = {
'12345': {
recording: true,
recordedSession: {
userId: '12345',
calls: [],
sessionOutput: 'log',
sessionOutputPath: './output/sessions',
mockOutputPath: './output/sessions',
sessionHandler: jest.fn(),
exportSession: jest.fn()
}
}
};
const methodCall = 'method1';
const eventMessage = '{"result":"NEW-DEVICE-NAME-1","id":13,"jsonrpc":"2.0"}';
const key = 'events';
sessionManagement.testExports.setTestSessionRecording(mockSessionRecording);

sessionManagement.startRecording();
sessionManagement.addCall(methodCall, eventMessage, userId);
sessionManagement.updateCallWithResponse(methodCall, eventMessage, key, userId);
const result = sessionManagement.testExports.getMockEventCall(userId);
expect(result).toMatchObject([{"methodCall" : "method1", "response":{"events":"{\"result\":\"NEW-DEVICE-NAME-1\",\"id\":13,\"jsonrpc\":\"2.0\"}" } },
]);
sessionManagement.stopRecording();
});

test('verify a session output directory is created when it does not exist', () => {
const session = new sessionManagement.Session();
Expand Down

0 comments on commit 464a5f6

Please sign in to comment.