11import os
22import unittest
3- from agentstack .utils import get_telemetry_opt_out
3+ import uuid
4+ from unittest .mock import patch , mock_open
45
6+ from agentstack .telemetry import _get_cli_user_guid
7+ from agentstack .utils import get_telemetry_opt_out
58
69class TelemetryTest (unittest .TestCase ):
710 def test_telemetry_opt_out_env_var_set (self ):
@@ -10,3 +13,70 @@ def test_telemetry_opt_out_env_var_set(self):
1013
1114 def test_telemetry_opt_out_set_in_test_environment (self ):
1215 assert get_telemetry_opt_out ()
16+
17+ @patch ('pathlib.Path.exists' )
18+ @patch ('builtins.open' , new_callable = mock_open , read_data = 'existing-guid' )
19+ def test_existing_guid_file (self , mock_file , mock_exists ):
20+ """Test when GUID file exists and can be read successfully"""
21+ mock_exists .return_value = True
22+
23+ result = _get_cli_user_guid ()
24+
25+ self .assertEqual (result , 'existing-guid' )
26+ mock_exists .assert_called_once_with ()
27+
28+ @patch ('pathlib.Path.exists' )
29+ @patch ('pathlib.Path.mkdir' )
30+ @patch ('uuid.uuid4' )
31+ @patch ('builtins.open' , new_callable = mock_open )
32+ def test_create_new_guid (self , mock_file , mock_uuid , mock_mkdir , mock_exists ):
33+ """Test creation of new GUID when file doesn't exist"""
34+ mock_exists .return_value = False
35+ mock_uuid .return_value = uuid .UUID ('12345678-1234-5678-1234-567812345678' )
36+
37+ result = _get_cli_user_guid ()
38+
39+ self .assertEqual (result , '12345678-1234-5678-1234-567812345678' )
40+ mock_exists .assert_called_once_with ()
41+ mock_mkdir .assert_called_once_with (parents = True , exist_ok = True )
42+ handle = mock_file ()
43+ handle .write .assert_called_once_with ('12345678-1234-5678-1234-567812345678' )
44+
45+ @patch ('pathlib.Path.exists' )
46+ @patch ('builtins.open' )
47+ def test_permission_error_on_read (self , mock_file , mock_exists ):
48+ """Test handling of PermissionError when reading file"""
49+ mock_exists .return_value = True
50+ mock_file .side_effect = PermissionError ()
51+
52+ result = _get_cli_user_guid ()
53+
54+ self .assertEqual (result , 'unknown' )
55+ mock_exists .assert_called_once_with ()
56+
57+ @patch ('pathlib.Path.exists' )
58+ @patch ('pathlib.Path.mkdir' )
59+ @patch ('builtins.open' )
60+ def test_permission_error_on_write (self , mock_file , mock_mkdir , mock_exists ):
61+ """Test handling of PermissionError when writing new file"""
62+ mock_exists .return_value = False
63+ mock_file .side_effect = PermissionError ()
64+
65+ result = _get_cli_user_guid ()
66+
67+ self .assertEqual (result , 'unknown' )
68+ mock_exists .assert_called_once_with ()
69+ mock_mkdir .assert_called_once_with (parents = True , exist_ok = True )
70+
71+ @patch ('pathlib.Path.exists' )
72+ @patch ('pathlib.Path.mkdir' )
73+ def test_os_error_on_mkdir (self , mock_mkdir , mock_exists ):
74+ """Test handling of OSError when creating directory"""
75+ mock_exists .return_value = False
76+ mock_mkdir .side_effect = OSError ()
77+
78+ result = _get_cli_user_guid ()
79+
80+ self .assertEqual (result , 'unknown' )
81+ mock_exists .assert_called_once_with ()
82+ mock_mkdir .assert_called_once_with (parents = True , exist_ok = True )
0 commit comments