11import * as vscode from 'vscode' ;
22import path from 'path' ;
3- import { afterEach , beforeEach } from 'mocha' ;
3+ import { afterEach , beforeEach , before , after } from 'mocha' ;
44import chai from 'chai' ;
55import type { DataService } from 'mongodb-data-service' ;
66import { config } from 'dotenv' ;
@@ -25,7 +25,6 @@ import {
2525 SavedConnectionsLoadedTelemetryEvent ,
2626} from '../../../telemetry' ;
2727import type { SegmentProperties } from '../../../telemetry/telemetryService' ;
28- import { suiteIfCI } from '../helpers' ;
2928
3029// eslint-disable-next-line @typescript-eslint/no-var-requires
3130const { version } = require ( '../../../../package.json' ) ;
@@ -36,7 +35,7 @@ chai.use(sinonChai);
3635
3736config ( { path : resolve ( __dirname , '../../../../.env' ) } ) ;
3837
39- suiteIfCI ( 'Telemetry Controller Test Suite' , ( ) => {
38+ suite ( 'Telemetry Controller Test Suite' , ( ) => {
4039 const testTelemetryService =
4140 mdbTestExtension . testExtensionController . _telemetryService ;
4241
@@ -53,6 +52,32 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
5352 } ;
5453
5554 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+ } ) ;
5681
5782 beforeEach ( ( ) => {
5883 const instanceStub = sandbox . stub ( ) ;
@@ -112,33 +137,41 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
112137 '_isTelemetryFeatureEnabled' ,
113138 sandbox . fake . returns ( true ) ,
114139 ) ;
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+ ) ;
115148 } ) ;
116149
117150 afterEach ( ( ) => {
118151 mdbTestExtension . testExtensionController . _connectionController . clearAllConnections ( ) ;
119152 sandbox . restore ( ) ;
120153 } ) ;
121154
122- test ( 'get segment key' , ( ) => {
123- let segmentKey : string | undefined ;
124-
125- try {
126- const segmentKeyFileLocation = '../../../../constants' ;
127- // eslint-disable-next-line @typescript-eslint/no-var-requires
128- segmentKey = require ( segmentKeyFileLocation ) ?. segmentKey ;
129- } catch ( error ) {
130- expect ( error ) . to . be . undefined ;
131- }
132-
133- expect ( segmentKey ) . to . be . equal ( process . env . SEGMENT_KEY ) ;
134- expect ( testTelemetryService . _segmentKey ) . to . be . a ( 'string' ) ;
135- } ) ;
136-
137155 suite ( 'after setup is complete' , ( ) => {
138156 beforeEach ( async ( ) => {
139157 await testTelemetryService . activateSegmentAnalytics ( ) ;
140158 } ) ;
141159
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+
142175 test ( 'track command run event' , async ( ) => {
143176 await vscode . commands . executeCommand ( 'mdb.addConnection' ) ;
144177 sandbox . assert . calledWith (
@@ -671,6 +704,9 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
671704 test ( 'trackTreeViewActivated throttles invocations' , async function ( ) {
672705 this . timeout ( 6000 ) ;
673706
707+ await testTelemetryService . activateSegmentAnalytics ( ) ;
708+ fakeSegmentAnalyticsTrack . resetHistory ( ) ;
709+
674710 const verifyEvent = ( call : sinon . SinonSpyCall ) : void => {
675711 const event = call . args [ 0 ] as SegmentProperties ;
676712 expect ( event . event ) . to . equal ( 'Side Panel Opened' ) ;
0 commit comments