chore(legacy): Remove gui_legacy.py and refactor all tests to use gui_2.py
This commit is contained in:
@@ -4,50 +4,37 @@ import importlib.util
|
||||
import sys
|
||||
import os
|
||||
from typing import Any
|
||||
import dearpygui.dearpygui as dpg
|
||||
|
||||
# Ensure project root is in path for imports
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
# Load gui.py as a module for testing
|
||||
spec = importlib.util.spec_from_file_location("gui_legacy", "gui_legacy.py")
|
||||
gui_legacy = importlib.util.module_from_spec(spec)
|
||||
sys.modules["gui_legacy"] = gui_legacy
|
||||
spec.loader.exec_module(gui_legacy)
|
||||
from gui_legacy import App
|
||||
# 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() -> None:
|
||||
def app_instance() -> Any:
|
||||
"""
|
||||
Fixture to create an instance of the App class for testing.
|
||||
It creates a real DPG context but mocks functions that would
|
||||
render a window or block execution.
|
||||
"""
|
||||
dpg.create_context()
|
||||
# Patch only the functions that would show a window or block,
|
||||
# and the App methods that rebuild UI on init.
|
||||
with patch('dearpygui.dearpygui.create_viewport'), \
|
||||
patch('dearpygui.dearpygui.setup_dearpygui'), \
|
||||
patch('dearpygui.dearpygui.show_viewport'), \
|
||||
patch('dearpygui.dearpygui.start_dearpygui'), \
|
||||
patch('gui_legacy.load_config', return_value={}), \
|
||||
patch.object(App, '_rebuild_files_list'), \
|
||||
patch.object(App, '_rebuild_shots_list'), \
|
||||
patch.object(App, '_rebuild_disc_list'), \
|
||||
patch.object(App, '_rebuild_disc_roles_list'), \
|
||||
patch.object(App, '_rebuild_discussion_selector'), \
|
||||
patch.object(App, '_refresh_project_widgets'):
|
||||
app = App()
|
||||
yield app
|
||||
dpg.destroy_context()
|
||||
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_panel_updates_correctly(app_instance: Any) -> None:
|
||||
def test_telemetry_data_updates_correctly(app_instance: Any) -> None:
|
||||
"""
|
||||
Tests that the _refresh_api_metrics method correctly updates
|
||||
the internal state for display.
|
||||
"""
|
||||
Tests that the _update_performance_diagnostics method correctly updates
|
||||
DPG widgets based on the stats from ai_client.
|
||||
"""
|
||||
# 1. Set the provider to anthropic
|
||||
app_instance.current_provider = "anthropic"
|
||||
app_instance._current_provider = "anthropic"
|
||||
# 2. Define the mock stats
|
||||
mock_stats = {
|
||||
"provider": "anthropic",
|
||||
@@ -56,29 +43,22 @@ def test_telemetry_panel_updates_correctly(app_instance: Any) -> None:
|
||||
"percentage": 75.0,
|
||||
}
|
||||
# 3. Patch the dependencies
|
||||
app_instance._last_bleed_update_time = 0 # Force update
|
||||
with patch('ai_client.get_history_bleed_stats', return_value=mock_stats) as mock_get_stats, \
|
||||
patch('dearpygui.dearpygui.set_value') as mock_set_value, \
|
||||
patch('dearpygui.dearpygui.configure_item') as mock_configure_item, \
|
||||
patch('dearpygui.dearpygui.is_item_shown', return_value=False), \
|
||||
patch('dearpygui.dearpygui.does_item_exist', return_value=True):
|
||||
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()
|
||||
app_instance._refresh_api_metrics({}, md_content="test content")
|
||||
# 5. Assert the results
|
||||
mock_get_stats.assert_called_once()
|
||||
# Assert history bleed widgets were updated
|
||||
mock_set_value.assert_any_call("token_budget_bar", 0.75)
|
||||
mock_set_value.assert_any_call("token_budget_label", "135,000 / 180,000")
|
||||
# Assert Gemini-specific widget was hidden
|
||||
mock_configure_item.assert_any_call("gemini_cache_label", show=False)
|
||||
# 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 _update_performance_diagnostics method correctly updates the
|
||||
GUI with Gemini cache statistics when the provider is set to Gemini.
|
||||
"""
|
||||
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"
|
||||
app_instance._current_provider = "gemini"
|
||||
# 2. Define mock cache stats
|
||||
mock_cache_stats = {
|
||||
'cache_count': 5,
|
||||
@@ -86,20 +66,7 @@ def test_cache_data_display_updates_correctly(app_instance: Any) -> None:
|
||||
}
|
||||
# Expected formatted string
|
||||
expected_text = "Gemini Caches: 5 (12.1 KB)"
|
||||
# 3. Patch dependencies
|
||||
app_instance._last_bleed_update_time = 0 # Force update
|
||||
with patch('ai_client.get_gemini_cache_stats', return_value=mock_cache_stats), \
|
||||
patch('dearpygui.dearpygui.set_value') as mock_set_value, \
|
||||
patch('dearpygui.dearpygui.configure_item') as mock_configure_item, \
|
||||
patch('dearpygui.dearpygui.is_item_shown', return_value=False), \
|
||||
patch('dearpygui.dearpygui.does_item_exist', return_value=True):
|
||||
# We also need to mock get_history_bleed_stats as it's called in the same function
|
||||
with patch('ai_client.get_history_bleed_stats', return_value={}):
|
||||
# 4. Call the method under test with payload
|
||||
app_instance._refresh_api_metrics(payload={'cache_stats': mock_cache_stats})
|
||||
# 5. Assert the results
|
||||
# mock_get_cache_stats.assert_called_once() # No longer called synchronously
|
||||
# Check that the UI item was shown and its value was set
|
||||
mock_configure_item.assert_any_call("gemini_cache_label", show=True)
|
||||
mock_set_value.assert_any_call("gemini_cache_label", expected_text)
|
||||
|
||||
# 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
|
||||
|
||||
Reference in New Issue
Block a user