73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
import pytest
|
|
from unittest.mock import patch
|
|
import importlib.util
|
|
import sys
|
|
import os
|
|
from typing import Any
|
|
|
|
# Ensure project root is in path for imports
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
# Load gui_2.py as a module for testing
|
|
spec = importlib.util.spec_from_file_location("gui_2", "gui_2.py")
|
|
gui_2 = importlib.util.module_from_spec(spec)
|
|
sys.modules["gui_2"] = gui_2
|
|
spec.loader.exec_module(gui_2)
|
|
from gui_2 import App
|
|
|
|
@pytest.fixture
|
|
def app_instance() -> Any:
|
|
"""
|
|
Fixture to create an instance of the App class for testing.
|
|
"""
|
|
with patch('gui_2.load_config', return_value={}):
|
|
# Mock components that start threads or open windows
|
|
with patch('gui_2.PerformanceMonitor'), \
|
|
patch('gui_2.session_logger'), \
|
|
patch.object(App, '_prune_old_logs'):
|
|
app = App()
|
|
yield app
|
|
|
|
def test_telemetry_data_updates_correctly(app_instance: Any) -> None:
|
|
"""
|
|
Tests that the _refresh_api_metrics method correctly updates
|
|
the internal state for display.
|
|
"""
|
|
# 1. Set the provider to anthropic
|
|
app_instance._current_provider = "anthropic"
|
|
# 2. Define the mock stats
|
|
mock_stats = {
|
|
"provider": "anthropic",
|
|
"limit": 180000,
|
|
"current": 135000,
|
|
"percentage": 75.0,
|
|
}
|
|
# 3. Patch the dependencies
|
|
with patch('ai_client.get_token_stats', return_value=mock_stats) as mock_get_stats:
|
|
# 4. Call the method under test
|
|
app_instance._refresh_api_metrics({}, md_content="test content")
|
|
# 5. Assert the results
|
|
mock_get_stats.assert_called_once()
|
|
# Assert token stats were updated
|
|
assert app_instance._token_stats["percentage"] == 75.0
|
|
assert app_instance._token_stats["current"] == 135000
|
|
|
|
def test_cache_data_display_updates_correctly(app_instance: Any) -> None:
|
|
"""
|
|
Tests that the _refresh_api_metrics method correctly updates the
|
|
internal cache text for display.
|
|
"""
|
|
# 1. Set the provider to Gemini
|
|
app_instance._current_provider = "gemini"
|
|
# 2. Define mock cache stats
|
|
mock_cache_stats = {
|
|
'cache_count': 5,
|
|
'total_size_bytes': 12345
|
|
}
|
|
# Expected formatted string
|
|
expected_text = "Gemini Caches: 5 (12.1 KB)"
|
|
# 3. Call the method under test with payload
|
|
app_instance._refresh_api_metrics(payload={'cache_stats': mock_cache_stats})
|
|
# 4. Assert the results
|
|
assert app_instance._gemini_cache_text == expected_text
|