@@ -32,7 +32,8 @@ describe('Telemetry Config', () => {
3232 enabled : true ,
3333 anonymousId : 'test-uuid' ,
3434 endpoint : 'https://example.com' ,
35- trackingId : 'G-TEST123'
35+ trackingId : 'G-TEST123' ,
36+ apiSecret : ''
3637 } ,
3738 hasShownTelemetryNotice : true
3839 } ;
@@ -51,7 +52,8 @@ describe('Telemetry Config', () => {
5152 enabled : true ,
5253 anonymousId : 'test-uuid' ,
5354 endpoint : 'https://example.com' ,
54- trackingId : 'G-TEST123'
55+ trackingId : 'G-TEST123' ,
56+ apiSecret : ''
5557 } ,
5658 hasShownTelemetryNotice : true
5759 } ;
@@ -92,7 +94,8 @@ describe('Telemetry Config', () => {
9294 enabled : true ,
9395 anonymousId : 'test-uuid' ,
9496 endpoint : 'https://example.com' ,
95- trackingId : 'G-TEST123'
97+ trackingId : 'G-TEST123' ,
98+ apiSecret : ''
9699 } ,
97100 hasShownTelemetryNotice : true
98101 } ;
@@ -120,7 +123,8 @@ describe('Telemetry Config', () => {
120123 enabled : true ,
121124 anonymousId : 'test-uuid' ,
122125 endpoint : 'https://example.com' ,
123- trackingId : 'G-TEST123'
126+ trackingId : 'G-TEST123' ,
127+ apiSecret : ''
124128 } ,
125129 hasShownTelemetryNotice : true
126130 } ;
@@ -138,6 +142,160 @@ describe('Telemetry Config', () => {
138142 expect ( config . endpoint ) . toBe ( 'https://example.com' ) ; // From global
139143 expect ( config . trackingId ) . toBe ( 'G-TEST123' ) ; // From global
140144 } ) ;
145+
146+ it ( 'should apply CODEGEN_TELEMETRY_ENDPOINT environment variable override' , async ( ) => {
147+ process . env . CODEGEN_TELEMETRY_ENDPOINT = 'https://custom-endpoint.com' ;
148+
149+ const mockConfig = {
150+ version : '1.0.0' ,
151+ telemetry : {
152+ enabled : true ,
153+ anonymousId : 'test-uuid' ,
154+ endpoint : 'https://example.com' ,
155+ trackingId : 'G-TEST123' ,
156+ apiSecret : ''
157+ } ,
158+ hasShownTelemetryNotice : true
159+ } ;
160+
161+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
162+
163+ const config = await getTelemetryConfig ( ) ;
164+
165+ expect ( config . endpoint ) . toBe ( 'https://custom-endpoint.com' ) ;
166+ expect ( config . enabled ) . toBe ( true ) ; // Should not affect other properties
167+ expect ( config . trackingId ) . toBe ( 'G-TEST123' ) ;
168+ } ) ;
169+
170+ it ( 'should apply CODEGEN_TELEMETRY_ID environment variable override' , async ( ) => {
171+ process . env . CODEGEN_TELEMETRY_ID = 'G-CUSTOM123' ;
172+
173+ const mockConfig = {
174+ version : '1.0.0' ,
175+ telemetry : {
176+ enabled : true ,
177+ anonymousId : 'test-uuid' ,
178+ endpoint : 'https://example.com' ,
179+ trackingId : 'G-TEST123' ,
180+ apiSecret : ''
181+ } ,
182+ hasShownTelemetryNotice : true
183+ } ;
184+
185+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
186+
187+ const config = await getTelemetryConfig ( ) ;
188+
189+ expect ( config . trackingId ) . toBe ( 'G-CUSTOM123' ) ;
190+ expect ( config . enabled ) . toBe ( true ) ; // Should not affect other properties
191+ expect ( config . endpoint ) . toBe ( 'https://example.com' ) ;
192+ } ) ;
193+
194+ it ( 'should apply CODEGEN_TELEMETRY_API_SECRET environment variable override' , async ( ) => {
195+ process . env . CODEGEN_TELEMETRY_API_SECRET = 'custom-secret-123' ;
196+
197+ const mockConfig = {
198+ version : '1.0.0' ,
199+ telemetry : {
200+ enabled : true ,
201+ anonymousId : 'test-uuid' ,
202+ endpoint : 'https://example.com' ,
203+ trackingId : 'G-TEST123' ,
204+ apiSecret : 'original-secret'
205+ } ,
206+ hasShownTelemetryNotice : true
207+ } ;
208+
209+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
210+
211+ const config = await getTelemetryConfig ( ) ;
212+
213+ expect ( config . apiSecret ) . toBe ( 'custom-secret-123' ) ;
214+ expect ( config . enabled ) . toBe ( true ) ; // Should not affect other properties
215+ expect ( config . endpoint ) . toBe ( 'https://example.com' ) ;
216+ } ) ;
217+
218+ it ( 'should apply multiple environment variable overrides together' , async ( ) => {
219+ process . env . CODEGEN_TELEMETRY_ENDPOINT = 'https://org-analytics.com' ;
220+ process . env . CODEGEN_TELEMETRY_ID = 'G-ORG123' ;
221+ process . env . CODEGEN_TELEMETRY_API_SECRET = 'org-secret' ;
222+
223+ const mockConfig = {
224+ version : '1.0.0' ,
225+ telemetry : {
226+ enabled : true ,
227+ anonymousId : 'test-uuid' ,
228+ endpoint : 'https://example.com' ,
229+ trackingId : 'G-TEST123' ,
230+ apiSecret : 'original-secret'
231+ } ,
232+ hasShownTelemetryNotice : true
233+ } ;
234+
235+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
236+
237+ const config = await getTelemetryConfig ( ) ;
238+
239+ expect ( config . endpoint ) . toBe ( 'https://org-analytics.com' ) ;
240+ expect ( config . trackingId ) . toBe ( 'G-ORG123' ) ;
241+ expect ( config . apiSecret ) . toBe ( 'org-secret' ) ;
242+ expect ( config . enabled ) . toBe ( true ) ;
243+ expect ( config . anonymousId ) . toBe ( 'test-uuid' ) ; // Should keep from global
244+ } ) ;
245+
246+ it ( 'should prioritize environment variables over project config' , async ( ) => {
247+ process . env . CODEGEN_TELEMETRY_ENDPOINT = 'https://env-endpoint.com' ;
248+
249+ const mockConfig = {
250+ version : '1.0.0' ,
251+ telemetry : {
252+ enabled : true ,
253+ anonymousId : 'test-uuid' ,
254+ endpoint : 'https://global-endpoint.com' ,
255+ trackingId : 'G-TEST123' ,
256+ apiSecret : ''
257+ } ,
258+ hasShownTelemetryNotice : true
259+ } ;
260+
261+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
262+
263+ const projectConfig = {
264+ endpoint : 'https://project-endpoint.com'
265+ } ;
266+
267+ const config = await getTelemetryConfig ( projectConfig ) ;
268+
269+ // Environment variable should override both project and global config
270+ expect ( config . endpoint ) . toBe ( 'https://env-endpoint.com' ) ;
271+ } ) ;
272+
273+ it ( 'should disable telemetry via environment variable even if project config enables it' , async ( ) => {
274+ process . env . CODEGEN_TELEMETRY_DISABLED = '1' ;
275+
276+ const mockConfig = {
277+ version : '1.0.0' ,
278+ telemetry : {
279+ enabled : false ,
280+ anonymousId : 'test-uuid' ,
281+ endpoint : 'https://example.com' ,
282+ trackingId : 'G-TEST123' ,
283+ apiSecret : ''
284+ } ,
285+ hasShownTelemetryNotice : true
286+ } ;
287+
288+ mockGetGlobalConfig . mockResolvedValue ( mockConfig ) ;
289+
290+ const projectConfig = {
291+ enabled : true // Try to enable in project config
292+ } ;
293+
294+ const config = await getTelemetryConfig ( projectConfig ) ;
295+
296+ // Environment variable should have highest priority
297+ expect ( config . enabled ) . toBe ( false ) ;
298+ } ) ;
141299 } ) ;
142300
143301 describe ( 'setTelemetryEnabled' , ( ) => {
@@ -148,7 +306,8 @@ describe('Telemetry Config', () => {
148306 enabled : false ,
149307 anonymousId : 'test-uuid' ,
150308 endpoint : 'https://example.com' ,
151- trackingId : 'G-TEST123'
309+ trackingId : 'G-TEST123' ,
310+ apiSecret : ''
152311 } ,
153312 hasShownTelemetryNotice : true
154313 } ;
@@ -174,7 +333,8 @@ describe('Telemetry Config', () => {
174333 enabled : true ,
175334 anonymousId : 'test-uuid' ,
176335 endpoint : 'https://example.com' ,
177- trackingId : 'G-TEST123'
336+ trackingId : 'G-TEST123' ,
337+ apiSecret : ''
178338 } ,
179339 hasShownTelemetryNotice : true
180340 } ;
@@ -216,7 +376,8 @@ describe('Telemetry Config', () => {
216376 enabled : true ,
217377 anonymousId : 'test-uuid' ,
218378 endpoint : 'https://example.com' ,
219- trackingId : 'G-TEST123'
379+ trackingId : 'G-TEST123' ,
380+ apiSecret : ''
220381 } ,
221382 hasShownTelemetryNotice : true
222383 } ;
0 commit comments