@@ -27,15 +27,18 @@ def test_parse_valid_connection_string(self):
2727
2828 result = _parse_connection_string (connection_string )
2929
30- assert result == "test-key-123"
30+ assert result == {
31+ "InstrumentationKey" : "test-key-123" ,
32+ "IngestionEndpoint" : "https://example.com/" ,
33+ }
3134
3235 def test_parse_connection_string_only_instrumentation_key (self ):
3336 """Test parsing connection string with only InstrumentationKey."""
3437 connection_string = "InstrumentationKey=simple-key"
3538
3639 result = _parse_connection_string (connection_string )
3740
38- assert result == " simple-key"
41+ assert result == { "InstrumentationKey" : " simple-key"}
3942
4043 def test_parse_connection_string_missing_instrumentation_key (self ):
4144 """Test parsing connection string without InstrumentationKey."""
@@ -68,7 +71,7 @@ def test_parse_connection_string_with_special_chars_in_value(self):
6871
6972 result = _parse_connection_string (connection_string )
7073
71- assert result == " key=with=equals"
74+ assert result == { "InstrumentationKey" : " key=with=equals"}
7275
7376
7477class TestAppInsightsEventClient :
@@ -96,14 +99,17 @@ def test_initialize_no_connection_string(self):
9699 assert _AppInsightsEventClient ._initialized is True
97100 assert _AppInsightsEventClient ._client is None
98101
102+ @patch ("uipath.telemetry._track.TelemetryChannel" )
103+ @patch ("uipath.telemetry._track.SynchronousQueue" )
104+ @patch ("uipath.telemetry._track._DiagnosticSender" )
99105 @patch ("uipath.telemetry._track._HAS_APPINSIGHTS" , True )
100106 @patch ("uipath.telemetry._track.AppInsightsTelemetryClient" )
101107 @patch (
102108 "uipath.telemetry._track._CONNECTION_STRING" ,
103109 "InstrumentationKey=builtin-key;IngestionEndpoint=https://example.com/" ,
104110 )
105111 def test_initialize_falls_back_to_builtin_connection_string (
106- self , mock_client_class
112+ self , mock_client_class , mock_sender_class , mock_queue_class , mock_channel_class
107113 ):
108114 """Test initialization uses _CONNECTION_STRING when env var is not set."""
109115 mock_client = MagicMock ()
@@ -116,7 +122,12 @@ def test_initialize_falls_back_to_builtin_connection_string(
116122
117123 assert _AppInsightsEventClient ._initialized is True
118124 assert _AppInsightsEventClient ._client is mock_client
119- mock_client_class .assert_called_once_with ("builtin-key" )
125+ mock_sender_class .assert_called_once_with (
126+ service_endpoint_uri = "https://example.com/v2/track"
127+ )
128+ mock_client_class .assert_called_once_with (
129+ "builtin-key" , telemetry_channel = mock_channel_class .return_value
130+ )
120131
121132 @patch ("uipath.telemetry._track._HAS_APPINSIGHTS" , False )
122133 def test_initialize_no_appinsights_package (self ):
@@ -126,9 +137,14 @@ def test_initialize_no_appinsights_package(self):
126137 assert _AppInsightsEventClient ._initialized is True
127138 assert _AppInsightsEventClient ._client is None
128139
140+ @patch ("uipath.telemetry._track.TelemetryChannel" )
141+ @patch ("uipath.telemetry._track.SynchronousQueue" )
142+ @patch ("uipath.telemetry._track._DiagnosticSender" )
129143 @patch ("uipath.telemetry._track._HAS_APPINSIGHTS" , True )
130144 @patch ("uipath.telemetry._track.AppInsightsTelemetryClient" )
131- def test_initialize_creates_client (self , mock_client_class ):
145+ def test_initialize_creates_client (
146+ self , mock_client_class , mock_sender_class , mock_queue_class , mock_channel_class
147+ ):
132148 """Test that initialization creates Application Insights client."""
133149 mock_client = MagicMock ()
134150 mock_client_class .return_value = mock_client
@@ -145,11 +161,19 @@ def test_initialize_creates_client(self, mock_client_class):
145161
146162 assert _AppInsightsEventClient ._initialized is True
147163 assert _AppInsightsEventClient ._client is mock_client
148- mock_client_class .assert_called_once_with ("test-key" )
164+ mock_sender_class .assert_called_once_with (
165+ service_endpoint_uri = "https://example.com/v2/track"
166+ )
167+ mock_client_class .assert_called_once_with (
168+ "test-key" , telemetry_channel = mock_channel_class .return_value
169+ )
149170
171+ @patch ("uipath.telemetry._track._DiagnosticSender" )
150172 @patch ("uipath.telemetry._track._HAS_APPINSIGHTS" , True )
151173 @patch ("uipath.telemetry._track.AppInsightsTelemetryClient" )
152- def test_initialize_invalid_connection_string (self , mock_client_class ):
174+ def test_initialize_invalid_connection_string (
175+ self , mock_client_class , mock_sender_class
176+ ):
153177 """Test initialization with invalid connection string."""
154178 with patch .dict (
155179 os .environ ,
@@ -276,16 +300,28 @@ def test_track_event_disabled(self, mock_is_enabled):
276300
277301 mock_track .assert_not_called ()
278302
303+ @patch .object (_AppInsightsEventClient , "register_atexit_flush" )
279304 @patch .object (_TelemetryClient , "_is_enabled" , return_value = True )
280305 @patch .object (_AppInsightsEventClient , "track_event" )
281- def test_track_event_enabled (self , mock_track , mock_is_enabled ):
306+ def test_track_event_enabled (self , mock_track , mock_is_enabled , mock_atexit ):
282307 """Test that track_event calls AppInsightsEventClient when enabled."""
283308 properties = {"key" : "value" }
284309
285310 _TelemetryClient .track_event ("test_event" , properties )
286311
287312 mock_track .assert_called_once_with ("test_event" , properties )
288313
314+ @patch .object (_AppInsightsEventClient , "register_atexit_flush" )
315+ @patch .object (_TelemetryClient , "_is_enabled" , return_value = True )
316+ @patch .object (_AppInsightsEventClient , "track_event" )
317+ def test_track_event_registers_atexit_handler (
318+ self , mock_track , mock_is_enabled , mock_atexit
319+ ):
320+ """Test that track_event registers atexit flush handler."""
321+ _TelemetryClient .track_event ("test_event" , {"key" : "value" })
322+
323+ mock_atexit .assert_called_once ()
324+
289325
290326class TestPublicFunctions :
291327 """Test public telemetry functions."""
@@ -488,9 +524,14 @@ def test_flush_handles_exception(self):
488524 # Should not raise exception
489525 _AppInsightsEventClient .flush ()
490526
527+ @patch ("uipath.telemetry._track.TelemetryChannel" )
528+ @patch ("uipath.telemetry._track.SynchronousQueue" )
529+ @patch ("uipath.telemetry._track._DiagnosticSender" )
491530 @patch ("uipath.telemetry._track._HAS_APPINSIGHTS" , True )
492531 @patch ("uipath.telemetry._track.AppInsightsTelemetryClient" )
493- def test_initialize_handles_exception (self , mock_client_class ):
532+ def test_initialize_handles_exception (
533+ self , mock_client_class , mock_sender_class , mock_queue_class , mock_channel_class
534+ ):
494535 """Test that initialization handles exceptions."""
495536 mock_client_class .side_effect = Exception ("Init error" )
496537
0 commit comments