|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import path from 'path'; |
3 | | -import { afterEach, beforeEach } from 'mocha'; |
| 3 | +import { afterEach, beforeEach, before, after } from 'mocha'; |
4 | 4 | import chai from 'chai'; |
5 | 5 | import type { DataService } from 'mongodb-data-service'; |
6 | 6 | import { config } from 'dotenv'; |
@@ -52,6 +52,32 @@ suite('Telemetry Controller Test Suite', () => { |
52 | 52 | }; |
53 | 53 |
|
54 | 54 | const sandbox = sinon.createSandbox(); |
| 55 | + let originalRequireFunction: any; |
| 56 | + |
| 57 | + before(function () { |
| 58 | + if (!process.env.SEGMENT_KEY) { |
| 59 | + process.env.SEGMENT_KEY = 'test-segment-key'; |
| 60 | + } |
| 61 | + |
| 62 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 63 | + const Module = require('module'); |
| 64 | + const originalRequire = Module.prototype.require; |
| 65 | + Module.prototype.require = function (id: string, ...args: any[]): any { |
| 66 | + if (id === '../../../../constants') { |
| 67 | + return { segmentKey: process.env.SEGMENT_KEY }; |
| 68 | + } |
| 69 | + return originalRequire.apply(this, [id, ...args]); |
| 70 | + }; |
| 71 | + |
| 72 | + // Store the original require for restoration |
| 73 | + originalRequireFunction = originalRequire; |
| 74 | + }); |
| 75 | + |
| 76 | + after(function () { |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 78 | + const Module = require('module'); |
| 79 | + Module.prototype.require = originalRequireFunction; |
| 80 | + }); |
55 | 81 |
|
56 | 82 | beforeEach(() => { |
57 | 83 | const instanceStub = sandbox.stub(); |
@@ -111,33 +137,41 @@ suite('Telemetry Controller Test Suite', () => { |
111 | 137 | '_isTelemetryFeatureEnabled', |
112 | 138 | sandbox.fake.returns(true), |
113 | 139 | ); |
| 140 | + |
| 141 | + // Mock readSegmentKey to return a fake key for local testing |
| 142 | + sandbox.replace( |
| 143 | + testTelemetryService, |
| 144 | + // @ts-expect-error This is a private method |
| 145 | + 'readSegmentKey', |
| 146 | + sandbox.fake.resolves('fake-segment-key-for-testing'), |
| 147 | + ); |
114 | 148 | }); |
115 | 149 |
|
116 | 150 | afterEach(() => { |
117 | 151 | mdbTestExtension.testExtensionController._connectionController.clearAllConnections(); |
118 | 152 | sandbox.restore(); |
119 | 153 | }); |
120 | 154 |
|
121 | | - test('get segment key', () => { |
122 | | - let segmentKey: string | undefined; |
123 | | - |
124 | | - try { |
125 | | - const segmentKeyFileLocation = '../../../../constants'; |
126 | | - // eslint-disable-next-line @typescript-eslint/no-var-requires |
127 | | - segmentKey = require(segmentKeyFileLocation)?.segmentKey; |
128 | | - } catch (error) { |
129 | | - expect(error).to.be.undefined; |
130 | | - } |
131 | | - |
132 | | - expect(segmentKey).to.be.equal(process.env.SEGMENT_KEY); |
133 | | - expect(testTelemetryService._segmentKey).to.be.a('string'); |
134 | | - }); |
135 | | - |
136 | 155 | suite('after setup is complete', () => { |
137 | 156 | beforeEach(async () => { |
138 | 157 | await testTelemetryService.activateSegmentAnalytics(); |
139 | 158 | }); |
140 | 159 |
|
| 160 | + test('get segment key', () => { |
| 161 | + let segmentKey: string | undefined; |
| 162 | + |
| 163 | + try { |
| 164 | + const segmentKeyFileLocation = '../../../../constants'; |
| 165 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 166 | + segmentKey = require(segmentKeyFileLocation)?.segmentKey; |
| 167 | + } catch (error) { |
| 168 | + expect(error).to.be.undefined; |
| 169 | + } |
| 170 | + |
| 171 | + expect(segmentKey).to.be.equal(process.env.SEGMENT_KEY); |
| 172 | + expect(testTelemetryService._segmentKey).to.be.a('string'); |
| 173 | + }); |
| 174 | + |
141 | 175 | test('track command run event', async () => { |
142 | 176 | await vscode.commands.executeCommand('mdb.addConnection'); |
143 | 177 | sandbox.assert.calledWith( |
@@ -670,6 +704,9 @@ suite('Telemetry Controller Test Suite', () => { |
670 | 704 | test('trackTreeViewActivated throttles invocations', async function () { |
671 | 705 | this.timeout(6000); |
672 | 706 |
|
| 707 | + await testTelemetryService.activateSegmentAnalytics(); |
| 708 | + fakeSegmentAnalyticsTrack.resetHistory(); |
| 709 | + |
673 | 710 | const verifyEvent = (call: sinon.SinonSpyCall): void => { |
674 | 711 | const event = call.args[0] as SegmentProperties; |
675 | 712 | expect(event.event).to.equal('Side Panel Opened'); |
|
0 commit comments