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
@@ -54,6 +53,28 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
5453
5554 const sandbox = sinon . createSandbox ( ) ;
5655
56+ before ( ( ) => {
57+ if ( ! process . env . SEGMENT_KEY ) {
58+ process . env . SEGMENT_KEY = 'test-segment-key' ;
59+ }
60+
61+ // eslint-disable-next-line @typescript-eslint/no-var-requires
62+ const Module = require ( 'module' ) ;
63+ const originalRequire = Module . prototype . require ;
64+ Module . prototype . require = function ( id : string , ...args : any [ ] ) : any {
65+ if ( id === '../../../../constants' ) {
66+ return { segmentKey : process . env . SEGMENT_KEY } ;
67+ }
68+ return originalRequire . apply ( this , [ id , ...args ] ) ;
69+ } ;
70+ } ) ;
71+
72+ after ( ( ) => {
73+ // eslint-disable-next-line @typescript-eslint/no-var-requires
74+ const Module = require ( 'module' ) ;
75+ delete Module . prototype . require ;
76+ } ) ;
77+
5778 beforeEach ( ( ) => {
5879 const instanceStub = sandbox . stub ( ) ;
5980 instanceStub . resolves ( {
@@ -112,33 +133,41 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
112133 '_isTelemetryFeatureEnabled' ,
113134 sandbox . fake . returns ( true ) ,
114135 ) ;
136+
137+ // Mock readSegmentKey to return a fake key for local testing
138+ sandbox . replace (
139+ testTelemetryService ,
140+ // @ts -expect-error This is a private method
141+ 'readSegmentKey' ,
142+ sandbox . fake . resolves ( 'fake-segment-key-for-testing' ) ,
143+ ) ;
115144 } ) ;
116145
117146 afterEach ( ( ) => {
118147 mdbTestExtension . testExtensionController . _connectionController . clearAllConnections ( ) ;
119148 sandbox . restore ( ) ;
120149 } ) ;
121150
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-
137151 suite ( 'after setup is complete' , ( ) => {
138152 beforeEach ( async ( ) => {
139153 await testTelemetryService . activateSegmentAnalytics ( ) ;
140154 } ) ;
141155
156+ test ( 'get segment key' , ( ) => {
157+ let segmentKey : string | undefined ;
158+
159+ try {
160+ const segmentKeyFileLocation = '../../../../constants' ;
161+ // eslint-disable-next-line @typescript-eslint/no-var-requires
162+ segmentKey = require ( segmentKeyFileLocation ) ?. segmentKey ;
163+ } catch ( error ) {
164+ expect ( error ) . to . be . undefined ;
165+ }
166+
167+ expect ( segmentKey ) . to . be . equal ( process . env . SEGMENT_KEY ) ;
168+ expect ( testTelemetryService . _segmentKey ) . to . be . a ( 'string' ) ;
169+ } ) ;
170+
142171 test ( 'track command run event' , async ( ) => {
143172 await vscode . commands . executeCommand ( 'mdb.addConnection' ) ;
144173 sandbox . assert . calledWith (
@@ -671,6 +700,9 @@ suiteIfCI('Telemetry Controller Test Suite', () => {
671700 test ( 'trackTreeViewActivated throttles invocations' , async function ( ) {
672701 this . timeout ( 6000 ) ;
673702
703+ await testTelemetryService . activateSegmentAnalytics ( ) ;
704+ fakeSegmentAnalyticsTrack . resetHistory ( ) ;
705+
674706 const verifyEvent = ( call : sinon . SinonSpyCall ) : void => {
675707 const event = call . args [ 0 ] as SegmentProperties ;
676708 expect ( event . event ) . to . equal ( 'Side Panel Opened' ) ;
0 commit comments