feat(gui2): Integrate core event system
Integrates the ai_client.events emitter into the gui_2.py App class. Adds a new test file to verify that the App subscribes to API lifecycle events upon initialization. This is the first step in aligning gui_2.py with the project's event-driven architecture.
This commit is contained in:
10
gui_2.py
10
gui_2.py
@@ -14,6 +14,7 @@ import session_logger
|
|||||||
import project_manager
|
import project_manager
|
||||||
import theme_2 as theme
|
import theme_2 as theme
|
||||||
import tomllib
|
import tomllib
|
||||||
|
import events
|
||||||
|
|
||||||
from imgui_bundle import imgui, hello_imgui, immapp
|
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.comms_log_callback = self._on_comms_entry
|
||||||
ai_client.tool_log_callback = self._on_tool_log
|
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
|
# ---------------------------------------------------------------- project loading
|
||||||
|
|
||||||
def _load_active_project(self):
|
def _load_active_project(self):
|
||||||
|
|||||||
48
tests/test_gui2_events.py
Normal file
48
tests/test_gui2_events.py
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user