import pytest from unittest.mock import patch, MagicMock import importlib.util import sys import dearpygui.dearpygui as dpg # Load gui.py as a module for testing spec = importlib.util.spec_from_file_location("gui", "gui.py") gui = importlib.util.module_from_spec(spec) sys.modules["gui"] = gui spec.loader.exec_module(gui) from gui import App @pytest.fixture def app_instance(): """ 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.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() def test_telemetry_panel_updates_correctly(app_instance): """ Tests that the _update_telemetry_panel method correctly updates DPG widgets based on the stats from ai_client. """ # 1. Define the mock stats to be returned by ai_client mock_stats = { "provider": "anthropic", "limit": 180000, "current": 135000, "percentage": 75.0, } # 2. Patch the dependencies 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.does_item_exist', return_value=True) as mock_does_item_exist: # 3. Call the method under test app_instance._update_telemetry_panel() # 4. Assert the results mock_get_stats.assert_called_once() # Check that the code verified the existence of the widgets mock_does_item_exist.assert_any_call("token_budget_bar") mock_does_item_exist.assert_any_call("token_budget_label") # Check that the widgets were updated with the correct values mock_set_value.assert_any_call("token_budget_bar", 0.75) # Note: My implementation formats numbers with commas mock_set_value.assert_any_call("token_budget_label", "135,000 / 180,000")