@@ -735,4 +735,217 @@ describe("TelemetryClient", () => {
735735 expect ( fileContent ) . toBe ( "[]" )
736736 } )
737737 } )
738+
739+ describe ( "captureWithRetry" , ( ) => {
740+ it ( "should return true when event is captured successfully" , async ( ) => {
741+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
742+
743+ const providerProperties = {
744+ appName : "roo-code" ,
745+ appVersion : "1.0.0" ,
746+ vscodeVersion : "1.60.0" ,
747+ platform : "darwin" ,
748+ editorName : "vscode" ,
749+ language : "en" ,
750+ mode : "code" ,
751+ }
752+
753+ const mockProvider : TelemetryPropertiesProvider = {
754+ getTelemetryProperties : vi . fn ( ) . mockResolvedValue ( providerProperties ) ,
755+ }
756+
757+ client . setProvider ( mockProvider )
758+
759+ const captureWithRetry = getPrivateProperty <
760+ ( event : { event : TelemetryEventName ; properties ?: Record < string , any > } ) => Promise < boolean >
761+ > ( client , "captureWithRetry" ) . bind ( client )
762+
763+ const result = await captureWithRetry ( {
764+ event : TelemetryEventName . TASK_CREATED ,
765+ properties : { taskId : "test-task-id" } ,
766+ } )
767+
768+ expect ( result ) . toBe ( true )
769+ expect ( mockFetch ) . toHaveBeenCalled ( )
770+ } )
771+
772+ it ( "should return true for invalid events (don't retry invalid events)" , async ( ) => {
773+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
774+
775+ const captureWithRetry = getPrivateProperty <
776+ ( event : { event : TelemetryEventName ; properties ?: Record < string , any > } ) => Promise < boolean >
777+ > ( client , "captureWithRetry" ) . bind ( client )
778+
779+ const result = await captureWithRetry ( {
780+ event : TelemetryEventName . TASK_CREATED ,
781+ properties : { test : "value" } , // Invalid properties
782+ } )
783+
784+ expect ( result ) . toBe ( true ) // Don't retry invalid events
785+ expect ( mockFetch ) . not . toHaveBeenCalled ( )
786+ expect ( console . error ) . toHaveBeenCalledWith ( expect . stringContaining ( "Invalid telemetry event" ) )
787+ } )
788+
789+ it ( "should return false when fetch fails" , async ( ) => {
790+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
791+
792+ mockFetch . mockRejectedValue ( new Error ( "Network error" ) )
793+
794+ const providerProperties = {
795+ appName : "roo-code" ,
796+ appVersion : "1.0.0" ,
797+ vscodeVersion : "1.60.0" ,
798+ platform : "darwin" ,
799+ editorName : "vscode" ,
800+ language : "en" ,
801+ mode : "code" ,
802+ }
803+
804+ const mockProvider : TelemetryPropertiesProvider = {
805+ getTelemetryProperties : vi . fn ( ) . mockResolvedValue ( providerProperties ) ,
806+ }
807+
808+ client . setProvider ( mockProvider )
809+
810+ const captureWithRetry = getPrivateProperty <
811+ ( event : { event : TelemetryEventName ; properties ?: Record < string , any > } ) => Promise < boolean >
812+ > ( client , "captureWithRetry" ) . bind ( client )
813+
814+ const result = await captureWithRetry ( {
815+ event : TelemetryEventName . TASK_CREATED ,
816+ properties : { taskId : "test-task-id" } ,
817+ } )
818+
819+ expect ( result ) . toBe ( false )
820+ } )
821+ } )
822+
823+ describe ( "queue integration" , ( ) => {
824+ it ( "should add event to queue when captureWithRetry fails" , async ( ) => {
825+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
826+
827+ // Create a mock queue
828+ const mockQueue = {
829+ addEvent : vi . fn ( ) ,
830+ } as any
831+
832+ client . setQueue ( mockQueue )
833+
834+ // Make captureWithRetry fail
835+ mockFetch . mockRejectedValue ( new Error ( "Network error" ) )
836+
837+ const providerProperties = {
838+ appName : "roo-code" ,
839+ appVersion : "1.0.0" ,
840+ vscodeVersion : "1.60.0" ,
841+ platform : "darwin" ,
842+ editorName : "vscode" ,
843+ language : "en" ,
844+ mode : "code" ,
845+ }
846+
847+ const mockProvider : TelemetryPropertiesProvider = {
848+ getTelemetryProperties : vi . fn ( ) . mockResolvedValue ( providerProperties ) ,
849+ }
850+
851+ client . setProvider ( mockProvider )
852+
853+ await client . capture ( {
854+ event : TelemetryEventName . TASK_CREATED ,
855+ properties : { taskId : "test-task-id" } ,
856+ } )
857+
858+ expect ( mockQueue . addEvent ) . toHaveBeenCalledWith (
859+ {
860+ event : TelemetryEventName . TASK_CREATED ,
861+ properties : { taskId : "test-task-id" } ,
862+ } ,
863+ "cloud" ,
864+ )
865+ } )
866+
867+ it ( "should not add event to queue when captureWithRetry succeeds" , async ( ) => {
868+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
869+
870+ // Create a mock queue
871+ const mockQueue = {
872+ addEvent : vi . fn ( ) ,
873+ } as any
874+
875+ client . setQueue ( mockQueue )
876+
877+ const providerProperties = {
878+ appName : "roo-code" ,
879+ appVersion : "1.0.0" ,
880+ vscodeVersion : "1.60.0" ,
881+ platform : "darwin" ,
882+ editorName : "vscode" ,
883+ language : "en" ,
884+ mode : "code" ,
885+ }
886+
887+ const mockProvider : TelemetryPropertiesProvider = {
888+ getTelemetryProperties : vi . fn ( ) . mockResolvedValue ( providerProperties ) ,
889+ }
890+
891+ client . setProvider ( mockProvider )
892+
893+ await client . capture ( {
894+ event : TelemetryEventName . TASK_CREATED ,
895+ properties : { taskId : "test-task-id" } ,
896+ } )
897+
898+ expect ( mockQueue . addEvent ) . not . toHaveBeenCalled ( )
899+ } )
900+
901+ it ( "should not fail when queue is not set" , async ( ) => {
902+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
903+
904+ // Make captureWithRetry fail
905+ mockFetch . mockRejectedValue ( new Error ( "Network error" ) )
906+
907+ const providerProperties = {
908+ appName : "roo-code" ,
909+ appVersion : "1.0.0" ,
910+ vscodeVersion : "1.60.0" ,
911+ platform : "darwin" ,
912+ editorName : "vscode" ,
913+ language : "en" ,
914+ mode : "code" ,
915+ }
916+
917+ const mockProvider : TelemetryPropertiesProvider = {
918+ getTelemetryProperties : vi . fn ( ) . mockResolvedValue ( providerProperties ) ,
919+ }
920+
921+ client . setProvider ( mockProvider )
922+
923+ // Should not throw even without queue
924+ await expect (
925+ client . capture ( {
926+ event : TelemetryEventName . TASK_CREATED ,
927+ properties : { taskId : "test-task-id" } ,
928+ } ) ,
929+ ) . resolves . toBeUndefined ( )
930+ } )
931+
932+ it ( "should not add invalid events to queue" , async ( ) => {
933+ const client = new TelemetryClient ( mockAuthService , mockSettingsService )
934+
935+ // Create a mock queue
936+ const mockQueue = {
937+ addEvent : vi . fn ( ) ,
938+ } as any
939+
940+ client . setQueue ( mockQueue )
941+
942+ await client . capture ( {
943+ event : TelemetryEventName . TASK_CREATED ,
944+ properties : { test : "value" } , // Invalid properties
945+ } )
946+
947+ // Should not add invalid events to queue
948+ expect ( mockQueue . addEvent ) . not . toHaveBeenCalled ( )
949+ } )
950+ } )
738951} )
0 commit comments