From 337d7d5f1eb43e1bd6f93611f8385605e3a3f356 Mon Sep 17 00:00:00 2001 From: Spencer Lepine Date: Tue, 26 Sep 2023 14:01:12 -0700 Subject: [PATCH] suppress unintended logging when CSM is disabled (#175) issue: https://github.com/amazon-connect/amazon-connect-chatjs/issues/171 --- src/service/csmService.js | 14 +++++ src/service/csmService.spec.js | 99 +++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/service/csmService.js b/src/service/csmService.js index 1a25e5a..85ceb86 100644 --- a/src/service/csmService.js +++ b/src/service/csmService.js @@ -69,6 +69,10 @@ class CsmService { csmConfig.widgetType : this.widgetType; } + _hasCSMFailedToImport() { + return typeof csm === 'undefined'; + } + getDefaultDimensions() { return [ { @@ -79,6 +83,8 @@ class CsmService { } addMetric(metric) { + if (this._hasCSMFailedToImport()) return; + // if csmService is never initialized, store the metrics in an array if (!this.csmInitialized) { if (this.metricsToBePublished) { @@ -101,6 +107,8 @@ class CsmService { } addLatencyMetric(method, timeDifference, category, otherDimensions = []) { + if (this._hasCSMFailedToImport()) return; + try { const latencyMetric = new csm.Metric( method, @@ -135,6 +143,8 @@ class CsmService { } addCountAndErrorMetric(method, category, error, otherDimensions = []) { + if (this._hasCSMFailedToImport()) return; + try { const dimensions = [ ...this.getDefaultDimensions(), @@ -170,6 +180,8 @@ class CsmService { } addCountMetric(method, category, otherDimensions = []) { + if (this._hasCSMFailedToImport()) return; + try { const dimensions = [ ...this.getDefaultDimensions(), @@ -193,6 +205,8 @@ class CsmService { } addAgentCountMetric(metricName, count) { + if (this._hasCSMFailedToImport()) return; + try { const _self = this; if (csm && csm.API.addCount && metricName) { diff --git a/src/service/csmService.spec.js b/src/service/csmService.spec.js index 541916d..fac7b35 100644 --- a/src/service/csmService.spec.js +++ b/src/service/csmService.spec.js @@ -5,6 +5,7 @@ import { } from "../config/csmConfig"; import * as CsmConfig from "../config/csmConfig"; import { GlobalConfig } from "../globalConfig"; +import { ChatSessionObject } from "../core/chatSession"; jest.useFakeTimers(); jest.spyOn(global, 'setTimeout'); @@ -190,4 +191,100 @@ describe("Common csmService tests", () => { expect(csm.API.addCount).toHaveBeenCalledWith("test", 1); }); }); -}); \ No newline at end of file +}); + +describe("Disabling CSM", () => { + let backupGlobalCSM = global.csm; + + const chatSessionCreateInput = { + loggerConfig: { useDefaultLogger: true }, + // disableCSM: false, // default false + chatDetails: { + ContactId: 'abc', + ParticipantId: 'abc', + ParticipantToken: 'abc', + }, + type: "CUSTOMER" // <-- IMPORTANT + }; + + beforeEach(() => { + jest.resetAllMocks(); + jest.spyOn(csmService, 'addLatencyMetricWithStartTime'); + jest.spyOn(csmService, 'addMetric'); + jest.spyOn(csmService.logger, 'error'); + jest.spyOn(csmService.logger, 'info'); + + // Mock these for `lib/connect-csm-worker.js` + global.URL.createObjectURL = jest.fn(() => 'mock_sharedWorkerBlobUrl'); + global.SharedWorker = jest.fn(() => ({ + port: { + start: jest.fn(), + postMessage: jest.fn(), + }, + })); + + // Remove existing "csm" value from other test files + delete global.csm; + }); + + afterEach(() => { + global.csm = backupGlobalCSM; + }); + + it("should not execute addMetric when CSM has been disabled", () => { + let backup_loadCsmScriptAndExecute = csmService.loadCsmScriptAndExecute; + let backup_initializeCSM = csmService.initializeCSM; + + csmService.loadCsmScriptAndExecute = jest.fn(); + csmService.initializeCSM = jest.fn(); + + // Initialize chat session with CSM disabled + ChatSessionObject.create({ + ...chatSessionCreateInput, + disableCSM: true, // <-- IMPORTANT + }); + expect(csmService.loadCsmScriptAndExecute).not.toHaveBeenCalled(); + expect(csmService.initializeCSM).not.toHaveBeenCalled(); + expect(csmService.csmInitialized).toBe(false); + + // Attempt to publish a metric + csmService.addLatencyMetricWithStartTime("test-method", 0, "test-category"); + csmService.addCountAndErrorMetric("test-method", "test-category", "test-error"); + csmService.addCountMetric("test-method", "test-category"); + csmService.addAgentCountMetric("test-name", 0); + + // Assert expected behavior + expect(csmService.addMetric).not.toHaveBeenCalled(); + expect(csmService.logger.error).not.toHaveBeenCalled(); // Should be silent and not output any ERROR logs + expect(csmService.logger.info).not.toHaveBeenCalled(); // Should be silent and not output any INFO logs + + // Cleanup + csmService.loadCsmScriptAndExecute = backup_loadCsmScriptAndExecute; + csmService.initializeCSM = backup_initializeCSM; + }); + + it("should properly execute addMetric when CSM is enabled", () => { + jest.spyOn(CsmConfig, 'getLdasEndpointUrl').mockReturnValue(mockLdasEndpoint); + + // Initialize chat session with CSM disabled + expect(csmService.csmInitialized).toBe(false); + ChatSessionObject.create({ + ...chatSessionCreateInput, + disableCSM: false, // <-- default "true" + }); + expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block + expect(csmService.csmInitialized).toBe(true); + + // Attempt to publish a metric + csmService.addLatencyMetricWithStartTime("test-method", 0, "test-category"); + csmService.addCountAndErrorMetric("test-method", "test-category", "test-error"); + csmService.addCountMetric("test-method", "test-category"); + csmService.addAgentCountMetric("test-name", 0); + + // Assert expected behavior + expect(csmService.addLatencyMetricWithStartTime).toHaveBeenCalled(); + expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block + expect(csmService.addMetric).toHaveBeenCalled(); + expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block + }); +});