Spaces:
Build error
Build error
File size: 17,348 Bytes
51ff9e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
from pathlib import Path
from unittest.mock import MagicMock, PropertyMock, mock_open, patch
import toml
from openhands.cli.tui import UsageMetrics
from openhands.cli.utils import (
add_local_config_trusted_dir,
extract_model_and_provider,
get_local_config_trusted_dirs,
is_number,
organize_models_and_providers,
read_file,
split_is_actually_version,
update_usage_metrics,
write_to_file,
)
from openhands.events.event import Event
from openhands.llm.metrics import Metrics, TokenUsage
class TestGetLocalConfigTrustedDirs:
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
def test_config_file_does_not_exist(self, mock_config_path):
mock_config_path.exists.return_value = False
result = get_local_config_trusted_dirs()
assert result == []
mock_config_path.exists.assert_called_once()
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch('builtins.open', new_callable=mock_open, read_data='invalid toml')
@patch(
'openhands.cli.utils.toml.load',
side_effect=toml.TomlDecodeError('error', 'doc', 0),
)
def test_config_file_invalid_toml(
self, mock_toml_load, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
result = get_local_config_trusted_dirs()
assert result == []
mock_config_path.exists.assert_called_once()
mock_open_file.assert_called_once_with(mock_config_path, 'r')
mock_toml_load.assert_called_once()
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'sandbox': {'trusted_dirs': ['/path/one']}}),
)
@patch('openhands.cli.utils.toml.load')
def test_config_file_valid(self, mock_toml_load, mock_open_file, mock_config_path):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'sandbox': {'trusted_dirs': ['/path/one']}}
result = get_local_config_trusted_dirs()
assert result == ['/path/one']
mock_config_path.exists.assert_called_once()
mock_open_file.assert_called_once_with(mock_config_path, 'r')
mock_toml_load.assert_called_once()
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'other_section': {}}),
)
@patch('openhands.cli.utils.toml.load')
def test_config_file_missing_sandbox(
self, mock_toml_load, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'other_section': {}}
result = get_local_config_trusted_dirs()
assert result == []
mock_config_path.exists.assert_called_once()
mock_open_file.assert_called_once_with(mock_config_path, 'r')
mock_toml_load.assert_called_once()
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'sandbox': {'other_key': []}}),
)
@patch('openhands.cli.utils.toml.load')
def test_config_file_missing_trusted_dirs(
self, mock_toml_load, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'sandbox': {'other_key': []}}
result = get_local_config_trusted_dirs()
assert result == []
mock_config_path.exists.assert_called_once()
mock_open_file.assert_called_once_with(mock_config_path, 'r')
mock_toml_load.assert_called_once()
class TestAddLocalConfigTrustedDir:
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch('builtins.open', new_callable=mock_open)
@patch('openhands.cli.utils.toml.dump')
@patch('openhands.cli.utils.toml.load')
def test_add_to_non_existent_file(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = False
mock_parent = MagicMock(spec=Path)
mock_config_path.parent = mock_parent
add_local_config_trusted_dir('/new/path')
mock_config_path.exists.assert_called_once()
mock_parent.mkdir.assert_called_once_with(parents=True, exist_ok=True)
mock_open_file.assert_called_once_with(mock_config_path, 'w')
expected_config = {'sandbox': {'trusted_dirs': ['/new/path']}}
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
mock_toml_load.assert_not_called()
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'sandbox': {'trusted_dirs': ['/old/path']}}),
)
@patch('openhands.cli.utils.toml.dump')
@patch('openhands.cli.utils.toml.load')
def test_add_to_existing_file(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'sandbox': {'trusted_dirs': ['/old/path']}}
add_local_config_trusted_dir('/new/path')
mock_config_path.exists.assert_called_once()
assert mock_open_file.call_count == 2 # Once for read, once for write
mock_open_file.assert_any_call(mock_config_path, 'r')
mock_open_file.assert_any_call(mock_config_path, 'w')
mock_toml_load.assert_called_once()
expected_config = {'sandbox': {'trusted_dirs': ['/old/path', '/new/path']}}
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'sandbox': {'trusted_dirs': ['/old/path']}}),
)
@patch('openhands.cli.utils.toml.dump')
@patch('openhands.cli.utils.toml.load')
def test_add_existing_dir(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'sandbox': {'trusted_dirs': ['/old/path']}}
add_local_config_trusted_dir('/old/path')
mock_config_path.exists.assert_called_once()
mock_toml_load.assert_called_once()
expected_config = {
'sandbox': {'trusted_dirs': ['/old/path']}
} # Should not change
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch('builtins.open', new_callable=mock_open, read_data='invalid toml')
@patch('openhands.cli.utils.toml.dump')
@patch(
'openhands.cli.utils.toml.load',
side_effect=toml.TomlDecodeError('error', 'doc', 0),
)
def test_add_to_invalid_toml(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
add_local_config_trusted_dir('/new/path')
mock_config_path.exists.assert_called_once()
mock_toml_load.assert_called_once()
expected_config = {
'sandbox': {'trusted_dirs': ['/new/path']}
} # Should reset to default + new path
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'other_section': {}}),
)
@patch('openhands.cli.utils.toml.dump')
@patch('openhands.cli.utils.toml.load')
def test_add_to_missing_sandbox(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'other_section': {}}
add_local_config_trusted_dir('/new/path')
mock_config_path.exists.assert_called_once()
mock_toml_load.assert_called_once()
expected_config = {
'other_section': {},
'sandbox': {'trusted_dirs': ['/new/path']},
}
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
@patch('openhands.cli.utils._LOCAL_CONFIG_FILE_PATH')
@patch(
'builtins.open',
new_callable=mock_open,
read_data=toml.dumps({'sandbox': {'other_key': []}}),
)
@patch('openhands.cli.utils.toml.dump')
@patch('openhands.cli.utils.toml.load')
def test_add_to_missing_trusted_dirs(
self, mock_toml_load, mock_toml_dump, mock_open_file, mock_config_path
):
mock_config_path.exists.return_value = True
mock_toml_load.return_value = {'sandbox': {'other_key': []}}
add_local_config_trusted_dir('/new/path')
mock_config_path.exists.assert_called_once()
mock_toml_load.assert_called_once()
expected_config = {'sandbox': {'other_key': [], 'trusted_dirs': ['/new/path']}}
mock_toml_dump.assert_called_once_with(expected_config, mock_open_file())
class TestUpdateUsageMetrics:
def test_update_usage_metrics_no_llm_metrics(self):
event = Event()
usage_metrics = UsageMetrics()
# Store original metrics object for comparison
original_metrics = usage_metrics.metrics
update_usage_metrics(event, usage_metrics)
# Metrics should remain unchanged
assert usage_metrics.metrics is original_metrics # Same object reference
assert usage_metrics.metrics.accumulated_cost == 0.0 # Default value
def test_update_usage_metrics_with_cost(self):
event = Event()
# Create a mock Metrics object
metrics = MagicMock(spec=Metrics)
# Mock the accumulated_cost property
type(metrics).accumulated_cost = PropertyMock(return_value=1.25)
event.llm_metrics = metrics
usage_metrics = UsageMetrics()
update_usage_metrics(event, usage_metrics)
# Test that the metrics object was updated to the one from the event
assert usage_metrics.metrics is metrics # Should be the same object reference
# Test that we can access the accumulated_cost through the metrics property
assert usage_metrics.metrics.accumulated_cost == 1.25
def test_update_usage_metrics_with_tokens(self):
event = Event()
# Create mock token usage
token_usage = MagicMock(spec=TokenUsage)
token_usage.prompt_tokens = 100
token_usage.completion_tokens = 50
token_usage.cache_read_tokens = 20
token_usage.cache_write_tokens = 30
# Create mock metrics
metrics = MagicMock(spec=Metrics)
# Set the mock properties
type(metrics).accumulated_cost = PropertyMock(return_value=1.5)
type(metrics).accumulated_token_usage = PropertyMock(return_value=token_usage)
event.llm_metrics = metrics
usage_metrics = UsageMetrics()
update_usage_metrics(event, usage_metrics)
# Test that the metrics object was updated to the one from the event
assert usage_metrics.metrics is metrics # Should be the same object reference
# Test we can access metrics values through the metrics property
assert usage_metrics.metrics.accumulated_cost == 1.5
assert usage_metrics.metrics.accumulated_token_usage is token_usage
assert usage_metrics.metrics.accumulated_token_usage.prompt_tokens == 100
assert usage_metrics.metrics.accumulated_token_usage.completion_tokens == 50
assert usage_metrics.metrics.accumulated_token_usage.cache_read_tokens == 20
assert usage_metrics.metrics.accumulated_token_usage.cache_write_tokens == 30
def test_update_usage_metrics_with_invalid_types(self):
event = Event()
# Create mock token usage with invalid types
token_usage = MagicMock(spec=TokenUsage)
token_usage.prompt_tokens = 'not an int'
token_usage.completion_tokens = 'not an int'
token_usage.cache_read_tokens = 'not an int'
token_usage.cache_write_tokens = 'not an int'
# Create mock metrics
metrics = MagicMock(spec=Metrics)
# Set the mock properties
type(metrics).accumulated_cost = PropertyMock(return_value='not a float')
type(metrics).accumulated_token_usage = PropertyMock(return_value=token_usage)
event.llm_metrics = metrics
usage_metrics = UsageMetrics()
update_usage_metrics(event, usage_metrics)
# Test that the metrics object was still updated to the one from the event
# Even though the values are invalid types, the metrics object reference should be updated
assert usage_metrics.metrics is metrics # Should be the same object reference
# We can verify that we can access the properties through the metrics object
# The invalid types are preserved since our update_usage_metrics function
# simply assigns the metrics object without validation
assert usage_metrics.metrics.accumulated_cost == 'not a float'
assert usage_metrics.metrics.accumulated_token_usage is token_usage
class TestModelAndProviderFunctions:
def test_extract_model_and_provider_slash_format(self):
model = 'openai/gpt-4o'
result = extract_model_and_provider(model)
assert result['provider'] == 'openai'
assert result['model'] == 'gpt-4o'
assert result['separator'] == '/'
def test_extract_model_and_provider_dot_format(self):
model = 'anthropic.claude-3-7'
result = extract_model_and_provider(model)
assert result['provider'] == 'anthropic'
assert result['model'] == 'claude-3-7'
assert result['separator'] == '.'
def test_extract_model_and_provider_openai_implicit(self):
model = 'gpt-4o'
result = extract_model_and_provider(model)
assert result['provider'] == 'openai'
assert result['model'] == 'gpt-4o'
assert result['separator'] == '/'
def test_extract_model_and_provider_anthropic_implicit(self):
model = 'claude-sonnet-4-20250514'
result = extract_model_and_provider(model)
assert result['provider'] == 'anthropic'
assert result['model'] == 'claude-sonnet-4-20250514'
assert result['separator'] == '/'
def test_extract_model_and_provider_versioned(self):
model = 'deepseek.deepseek-coder-1.3b'
result = extract_model_and_provider(model)
assert result['provider'] == 'deepseek'
assert result['model'] == 'deepseek-coder-1.3b'
assert result['separator'] == '.'
def test_extract_model_and_provider_unknown(self):
model = 'unknown-model'
result = extract_model_and_provider(model)
assert result['provider'] == ''
assert result['model'] == 'unknown-model'
assert result['separator'] == ''
def test_organize_models_and_providers(self):
models = [
'openai/gpt-4o',
'anthropic/claude-sonnet-4-20250514',
'o3-mini',
'anthropic.claude-3-5', # Should be ignored as it uses dot separator for anthropic
'unknown-model',
]
result = organize_models_and_providers(models)
assert 'openai' in result
assert 'anthropic' in result
assert 'other' in result
assert len(result['openai']['models']) == 2
assert 'gpt-4o' in result['openai']['models']
assert 'o3-mini' in result['openai']['models']
assert len(result['anthropic']['models']) == 1
assert 'claude-sonnet-4-20250514' in result['anthropic']['models']
assert len(result['other']['models']) == 1
assert 'unknown-model' in result['other']['models']
class TestUtilityFunctions:
def test_is_number_with_digit(self):
assert is_number('1') is True
assert is_number('9') is True
def test_is_number_with_letter(self):
assert is_number('a') is False
assert is_number('Z') is False
def test_is_number_with_special_char(self):
assert is_number('.') is False
assert is_number('-') is False
def test_split_is_actually_version_true(self):
split = ['model', '1.0']
assert split_is_actually_version(split) is True
def test_split_is_actually_version_false(self):
split = ['model', 'version']
assert split_is_actually_version(split) is False
def test_split_is_actually_version_single_item(self):
split = ['model']
assert split_is_actually_version(split) is False
class TestFileOperations:
def test_read_file(self):
mock_content = 'test file content'
with patch('builtins.open', mock_open(read_data=mock_content)):
result = read_file('test.txt')
assert result == mock_content
def test_write_to_file(self):
mock_content = 'test file content'
mock_file = mock_open()
with patch('builtins.open', mock_file):
write_to_file('test.txt', mock_content)
mock_file.assert_called_once_with('test.txt', 'w')
handle = mock_file()
handle.write.assert_called_once_with(mock_content)
|