1
1
import * as assert from 'assert'
2
+ import * as posthogModule from 'posthog-node'
2
3
import * as sinon from 'sinon'
3
4
import * as vscode from 'vscode'
5
+ import { TELEMETRY_EVENTS } from '../../constants/telemetry'
4
6
import { TelemetryService } from '../telemetry.service'
5
7
6
8
suite ( 'TelemetryService Tests' , ( ) => {
7
9
let sandbox : sinon . SinonSandbox
10
+ let posthogCaptureStub : sinon . SinonStub
11
+ let telemetryServiceInstance : TelemetryService
8
12
9
13
setup ( ( ) => {
10
14
sandbox = sinon . createSandbox ( )
@@ -16,8 +20,31 @@ suite('TelemetryService Tests', () => {
16
20
get : configGetStub
17
21
} as any )
18
22
19
- // Stub console.log to avoid test output pollution
23
+ // Stub console methods to avoid test output pollution
20
24
sandbox . stub ( console , 'log' )
25
+ sandbox . stub ( console , 'error' )
26
+
27
+ // Create a fresh instance and clear any existing ones
28
+ // @ts -ignore: Accessing private static instance
29
+ TelemetryService . instance = undefined
30
+
31
+ // Set up PostHog stub
32
+ posthogCaptureStub = sandbox . stub ( )
33
+ const posthogStub = {
34
+ capture : posthogCaptureStub
35
+ }
36
+
37
+ // Stub the PostHog constructor
38
+ sandbox . stub ( posthogModule , 'PostHog' ) . returns ( posthogStub as any )
39
+
40
+ // Get the instance after stubbing
41
+ telemetryServiceInstance = TelemetryService . getInstance ( )
42
+
43
+ // Set the service as ready and enabled
44
+ // @ts -ignore: Accessing private properties
45
+ telemetryServiceInstance . ready = true
46
+ // @ts -ignore: Accessing private properties
47
+ telemetryServiceInstance . enabled = true
21
48
} )
22
49
23
50
teardown ( ( ) => {
@@ -32,16 +59,96 @@ suite('TelemetryService Tests', () => {
32
59
} )
33
60
34
61
test ( 'isEnabled should return the boolean state' , ( ) => {
35
- const telemetryService = TelemetryService . getInstance ( )
36
- const result = telemetryService . isEnabled ( )
62
+ const result = telemetryServiceInstance . isEnabled ( )
37
63
38
64
// Just check that it returns a boolean value, as the actual
39
65
// implementation details might change
40
66
assert . strictEqual ( typeof result , 'boolean' )
41
67
} )
42
68
43
69
test ( 'isReady should return ready state' , ( ) => {
44
- const telemetryService = TelemetryService . getInstance ( )
45
- assert . strictEqual ( typeof telemetryService . isReady ( ) , 'boolean' )
70
+ assert . strictEqual ( typeof telemetryServiceInstance . isReady ( ) , 'boolean' )
71
+ } )
72
+
73
+ test ( 'trackEvent should track file count and folder count for ADD_FILE event' , ( ) => {
74
+ // Track event with file count and folder count
75
+ telemetryServiceInstance . trackEvent ( TELEMETRY_EVENTS . FILES . ADD_FILE , {
76
+ fileCount : 5 ,
77
+ folderCount : 1
78
+ } )
79
+
80
+ // Verify capture was called with correct parameters
81
+ sinon . assert . calledOnce ( posthogCaptureStub )
82
+ const captureCall = posthogCaptureStub . getCall ( 0 ) . args [ 0 ]
83
+
84
+ assert . strictEqual ( captureCall . event , TELEMETRY_EVENTS . FILES . ADD_FILE )
85
+ assert . strictEqual ( captureCall . properties . fileCount , 5 )
86
+ assert . strictEqual ( captureCall . properties . folderCount , 1 )
87
+ } )
88
+
89
+ test ( 'trackEvent should track file count and folder count for ADD_FOLDER event' , ( ) => {
90
+ // Track event with file count, folder count, and recursive flag
91
+ telemetryServiceInstance . trackEvent ( TELEMETRY_EVENTS . FILES . ADD_FOLDER , {
92
+ fileCount : 10 ,
93
+ folderCount : 3 ,
94
+ recursive : true
95
+ } )
96
+
97
+ // Verify capture was called with correct parameters
98
+ sinon . assert . calledOnce ( posthogCaptureStub )
99
+ const captureCall = posthogCaptureStub . getCall ( 0 ) . args [ 0 ]
100
+
101
+ assert . strictEqual ( captureCall . event , TELEMETRY_EVENTS . FILES . ADD_FOLDER )
102
+ assert . strictEqual ( captureCall . properties . fileCount , 10 )
103
+ assert . strictEqual ( captureCall . properties . folderCount , 3 )
104
+ assert . strictEqual ( captureCall . properties . recursive , true )
105
+ } )
106
+
107
+ test ( 'trackEvent should track file count and folder count for ADD_SELECTION event' , ( ) => {
108
+ // Track event with file count, folder count, and recursive flag
109
+ telemetryServiceInstance . trackEvent ( TELEMETRY_EVENTS . FILES . ADD_SELECTION , {
110
+ fileCount : 7 ,
111
+ folderCount : 2 ,
112
+ recursive : false
113
+ } )
114
+
115
+ // Verify capture was called with correct parameters
116
+ sinon . assert . calledOnce ( posthogCaptureStub )
117
+ const captureCall = posthogCaptureStub . getCall ( 0 ) . args [ 0 ]
118
+
119
+ assert . strictEqual ( captureCall . event , TELEMETRY_EVENTS . FILES . ADD_SELECTION )
120
+ assert . strictEqual ( captureCall . properties . fileCount , 7 )
121
+ assert . strictEqual ( captureCall . properties . folderCount , 2 )
122
+ assert . strictEqual ( captureCall . properties . recursive , false )
123
+ } )
124
+
125
+ test ( 'trackEvent should track file count and folder count for ADD_SMART_SELECTION event' , ( ) => {
126
+ // Track event with file count and folder count
127
+ telemetryServiceInstance . trackEvent ( TELEMETRY_EVENTS . FILES . ADD_SMART_SELECTION , {
128
+ fileCount : 15 ,
129
+ folderCount : 4
130
+ } )
131
+
132
+ // Verify capture was called with correct parameters
133
+ sinon . assert . calledOnce ( posthogCaptureStub )
134
+ const captureCall = posthogCaptureStub . getCall ( 0 ) . args [ 0 ]
135
+
136
+ assert . strictEqual ( captureCall . event , TELEMETRY_EVENTS . FILES . ADD_SMART_SELECTION )
137
+ assert . strictEqual ( captureCall . properties . fileCount , 15 )
138
+ assert . strictEqual ( captureCall . properties . folderCount , 4 )
139
+ } )
140
+
141
+ test ( 'trackEvent should not capture when telemetry is disabled' , ( ) => {
142
+ // Mock isEnabled to return false by setting the private enabled field
143
+ // @ts -ignore: Accessing private properties
144
+ telemetryServiceInstance . enabled = false
145
+
146
+ telemetryServiceInstance . trackEvent ( TELEMETRY_EVENTS . FILES . ADD_FILE , {
147
+ fileCount : 5 ,
148
+ folderCount : 1
149
+ } )
150
+
151
+ // Verify capture was not called
152
+ sinon . assert . notCalled ( posthogCaptureStub )
46
153
} )
47
154
} )
0 commit comments