diff --git a/gui_2.py b/gui_2.py index 845b75b..9e65bf7 100644 --- a/gui_2.py +++ b/gui_2.py @@ -14,6 +14,7 @@ import session_logger import project_manager import theme_2 as theme import tomllib +import events from imgui_bundle import imgui, hello_imgui, immapp @@ -187,6 +188,15 @@ class App: ai_client.comms_log_callback = self._on_comms_entry ai_client.tool_log_callback = self._on_tool_log + # Subscribe to API lifecycle events + ai_client.events.on("request_start", self._on_api_event) + ai_client.events.on("response_received", self._on_api_event) + ai_client.events.on("tool_execution", self._on_api_event) + + def _on_api_event(self, *args, **kwargs): + """Callback for ai_client events. For now, a placeholder.""" + pass + # ---------------------------------------------------------------- project loading def _load_active_project(self): diff --git a/tests/test_gui2_events.py b/tests/test_gui2_events.py new file mode 100644 index 0000000..51460b0 --- /dev/null +++ b/tests/test_gui2_events.py @@ -0,0 +1,48 @@ +import pytest +from unittest.mock import MagicMock, patch +from gui_2 import App +import ai_client +from events import EventEmitter + +@pytest.fixture +def app_instance(): + """ + Fixture to create an instance of the gui_2.App class for testing. + It mocks functions that would render a window or block execution. + """ + if not hasattr(ai_client, 'events') or ai_client.events is None: + ai_client.events = EventEmitter() + + with ( + patch('gui_2.load_config', return_value={'ai': {}, 'projects': {}}), + patch('gui_2.save_config'), + patch('gui_2.project_manager'), + patch('gui_2.session_logger'), + patch('gui_2.immapp.run'), + patch.object(App, '_load_active_project'), + patch.object(App, '_fetch_models'), + patch.object(App, '_load_fonts'), + patch.object(App, '_post_init') + ): + yield App + +def test_app_subscribes_to_events(app_instance): + """ + This test checks that the App's __init__ method subscribes the necessary + event handlers to the ai_client.events emitter. + This test will fail until the event subscription logic is added to gui_2.App. + """ + with patch.object(ai_client.events, 'on') as mock_on: + app = app_instance() + mock_on.assert_called() + + calls = mock_on.call_args_list + event_names = [call.args[0] for call in calls] + assert "request_start" in event_names + assert "response_received" in event_names + assert "tool_execution" in event_names + + for call in calls: + handler = call.args[1] + assert hasattr(handler, '__self__') + assert handler.__self__ is app