|
| 1 | +/* |
| 2 | + * Copyright (c) 1998-2026 KX Systems Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as telemetryModule from "@vscode/extension-telemetry"; |
| 15 | +import * as sinon from "sinon"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | + |
| 18 | +import { ExtensionTelemetry } from "../../../src/utils/telemetryClient"; |
| 19 | + |
| 20 | +describe("ExtensionTelemetry", () => { |
| 21 | + let telemetryReporterMock: sinon.SinonStubbedInstance<telemetryModule.TelemetryReporter>; |
| 22 | + let outputChannelMock: any; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + telemetryReporterMock = sinon.createStubInstance( |
| 26 | + telemetryModule.TelemetryReporter, |
| 27 | + ); |
| 28 | + sinon |
| 29 | + .stub(telemetryModule, "TelemetryReporter") |
| 30 | + .returns(telemetryReporterMock as any); |
| 31 | + |
| 32 | + outputChannelMock = { |
| 33 | + appendLine: sinon.spy(), |
| 34 | + dispose: sinon.stub().resolves(), |
| 35 | + }; |
| 36 | + sinon.stub(vscode.window, "createOutputChannel").returns(outputChannelMock); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + sinon.restore(); |
| 41 | + delete process.env.CODE_TEST; |
| 42 | + }); |
| 43 | + |
| 44 | + it("should verify sendTelemetryEvent was called correctly", () => { |
| 45 | + process.env.CODE_TEST = ""; // Production mode |
| 46 | + const ext = new ExtensionTelemetry("conn-string", true); |
| 47 | + const props = { category: "testing" }; |
| 48 | + const measurements = { duration: 100 }; |
| 49 | + |
| 50 | + ext.sendEvent("testEvent", props, measurements); |
| 51 | + |
| 52 | + sinon.assert.calledOnce(telemetryReporterMock.sendTelemetryEvent); |
| 53 | + sinon.assert.calledWith( |
| 54 | + telemetryReporterMock.sendTelemetryEvent, |
| 55 | + "testEvent", |
| 56 | + sinon.match(props), |
| 57 | + sinon.match(measurements), |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should verify sendTelemetryErrorEvent was called correctly", () => { |
| 62 | + process.env.CODE_TEST = ""; |
| 63 | + const ext = new ExtensionTelemetry("conn-string", true); |
| 64 | + const testError = new Error("critical failure"); |
| 65 | + testError.name = "DatabaseError"; |
| 66 | + |
| 67 | + ext.sendError(testError, { userRole: "admin" }); |
| 68 | + |
| 69 | + sinon.assert.calledOnce(telemetryReporterMock.sendTelemetryErrorEvent); |
| 70 | + sinon.assert.calledWith( |
| 71 | + telemetryReporterMock.sendTelemetryErrorEvent, |
| 72 | + "DatabaseError", |
| 73 | + sinon.match({ |
| 74 | + name: "DatabaseError", |
| 75 | + message: "critical failure", |
| 76 | + userRole: "admin", |
| 77 | + }), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should verify output channel logging in test mode", () => { |
| 82 | + process.env.CODE_TEST = "true"; // Test mode |
| 83 | + const ext = new ExtensionTelemetry("conn-string", true); |
| 84 | + |
| 85 | + ext.sendEvent("clickAction", { button: "left" }); |
| 86 | + |
| 87 | + sinon.assert.calledOnce(outputChannelMock.appendLine); |
| 88 | + sinon.assert.calledWithMatch( |
| 89 | + outputChannelMock.appendLine, |
| 90 | + /telemetry\/clickAction/, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should verify nothing is called when telemetry is disabled", () => { |
| 95 | + const ext = new ExtensionTelemetry("conn-string", false); |
| 96 | + |
| 97 | + ext.sendEvent("unusedEvent"); |
| 98 | + |
| 99 | + sinon.assert.notCalled(telemetryReporterMock.sendTelemetryEvent); |
| 100 | + sinon.assert.notCalled(outputChannelMock.appendLine); |
| 101 | + }); |
| 102 | +}); |
0 commit comments