@@ -259,6 +259,42 @@ describe("createReporter", () => {
259259 expect ( sendEvent ) . toHaveBeenCalledTimes ( 0 ) ;
260260 } ) ;
261261
262+ test ( "sends no event if the DO_NOT_TRACK env is set to '1'" , async ( {
263+ expect,
264+ } ) => {
265+ vi . stubEnv ( "DO_NOT_TRACK" , "1" ) ;
266+
267+ const deferred = promiseWithResolvers < string > ( ) ;
268+ const reporter = createReporter ( ) ;
269+ const operation = reporter . collectAsyncMetrics ( {
270+ eventPrefix : "c3 session" ,
271+ props : {
272+ args : {
273+ projectName : "app" ,
274+ } ,
275+ } ,
276+ promise : ( ) => deferred . promise ,
277+ } ) ;
278+
279+ expect ( reporter . isEnabled ) . toBe ( false ) ;
280+
281+ expect ( sendEvent ) . toHaveBeenCalledTimes ( 0 ) ;
282+
283+ deferred . resolve ( "test result" ) ;
284+
285+ await expect ( operation ) . resolves . toBe ( "test result" ) ;
286+ expect ( sendEvent ) . toHaveBeenCalledTimes ( 0 ) ;
287+ } ) ;
288+
289+ test ( "DO_NOT_TRACK='1' takes precedence over CREATE_CLOUDFLARE_TELEMETRY_DISABLED='0'" , ( {
290+ expect,
291+ } ) => {
292+ vi . stubEnv ( "DO_NOT_TRACK" , "1" ) ;
293+ vi . stubEnv ( "CREATE_CLOUDFLARE_TELEMETRY_DISABLED" , "0" ) ;
294+
295+ expect ( createReporter ( ) . isEnabled ) . toBe ( false ) ;
296+ } ) ;
297+
262298 test ( "sends started and cancelled event to sparrow if the promise reject with a CancelError" , async ( {
263299 expect,
264300 } ) => {
@@ -531,6 +567,8 @@ describe("runTelemetryCommand", () => {
531567
532568 afterEach ( ( ) => {
533569 vi . useRealTimers ( ) ;
570+ vi . clearAllMocks ( ) ;
571+ vi . unstubAllEnvs ( ) ;
534572 } ) ;
535573
536574 test ( "run telemetry status when c3permission is disabled" , async ( {
@@ -571,6 +609,36 @@ describe("runTelemetryCommand", () => {
571609 ` ) ;
572610 } ) ;
573611
612+ test ( "run telemetry status when DO_NOT_TRACK is enabled" , ( { expect } ) => {
613+ vi . stubEnv ( "DO_NOT_TRACK" , "1" ) ;
614+
615+ runTelemetryCommand ( "status" ) ;
616+
617+ expect ( readMetricsConfig ) . not . toHaveBeenCalled ( ) ;
618+ expect ( writeMetricsConfig ) . not . toHaveBeenCalled ( ) ;
619+ expect ( normalizeOutput ( std . out ) ) . toMatchInlineSnapshot ( `
620+ "Status: Disabled (set by DO_NOT_TRACK)
621+
622+ "
623+ ` ) ;
624+ } ) ;
625+
626+ test ( "run telemetry status when CREATE_CLOUDFLARE_TELEMETRY_DISABLED is enabled" , ( {
627+ expect,
628+ } ) => {
629+ vi . stubEnv ( "CREATE_CLOUDFLARE_TELEMETRY_DISABLED" , "1" ) ;
630+
631+ runTelemetryCommand ( "status" ) ;
632+
633+ expect ( readMetricsConfig ) . not . toHaveBeenCalled ( ) ;
634+ expect ( writeMetricsConfig ) . not . toHaveBeenCalled ( ) ;
635+ expect ( normalizeOutput ( std . out ) ) . toMatchInlineSnapshot ( `
636+ "Status: Disabled (set by CREATE_CLOUDFLARE_TELEMETRY_DISABLED)
637+
638+ "
639+ ` ) ;
640+ } ) ;
641+
574642 test ( "run telemetry enable when c3permission is disabled" , async ( {
575643 expect,
576644 } ) => {
@@ -618,6 +686,58 @@ describe("runTelemetryCommand", () => {
618686 ` ) ;
619687 } ) ;
620688
689+ test ( "run telemetry enable when DO_NOT_TRACK is enabled" , ( { expect } ) => {
690+ vi . stubEnv ( "DO_NOT_TRACK" , "1" ) ;
691+ vi . mocked ( readMetricsConfig ) . mockReturnValueOnce ( {
692+ c3permission : {
693+ enabled : false ,
694+ date : new Date ( ) ,
695+ } ,
696+ } ) ;
697+
698+ runTelemetryCommand ( "enable" ) ;
699+
700+ expect ( writeMetricsConfig ) . toHaveBeenCalledWith ( {
701+ c3permission : {
702+ enabled : true ,
703+ date : new Date ( ) ,
704+ } ,
705+ } ) ;
706+ expect ( normalizeOutput ( std . out ) ) . toMatchInlineSnapshot ( `
707+ "Status: Disabled (set by DO_NOT_TRACK)
708+
709+ Telemetry has been enabled in Create-Cloudflare's global configuration, but remains disabled while DO_NOT_TRACK is set.
710+ "
711+ ` ) ;
712+ } ) ;
713+
714+ test ( "run telemetry enable when CREATE_CLOUDFLARE_TELEMETRY_DISABLED is enabled" , ( {
715+ expect,
716+ } ) => {
717+ vi . stubEnv ( "CREATE_CLOUDFLARE_TELEMETRY_DISABLED" , "1" ) ;
718+ vi . mocked ( readMetricsConfig ) . mockReturnValueOnce ( {
719+ c3permission : {
720+ enabled : false ,
721+ date : new Date ( ) ,
722+ } ,
723+ } ) ;
724+
725+ runTelemetryCommand ( "enable" ) ;
726+
727+ expect ( writeMetricsConfig ) . toHaveBeenCalledWith ( {
728+ c3permission : {
729+ enabled : true ,
730+ date : new Date ( ) ,
731+ } ,
732+ } ) ;
733+ expect ( normalizeOutput ( std . out ) ) . toMatchInlineSnapshot ( `
734+ "Status: Disabled (set by CREATE_CLOUDFLARE_TELEMETRY_DISABLED)
735+
736+ Telemetry has been enabled in Create-Cloudflare's global configuration, but remains disabled while CREATE_CLOUDFLARE_TELEMETRY_DISABLED is set.
737+ "
738+ ` ) ;
739+ } ) ;
740+
621741 test ( "run telemetry disable when c3permission is enabled" , async ( {
622742 expect,
623743 } ) => {
0 commit comments