feat(mma): Decouple UI from API calls using UserRequestEvent and AsyncEventQueue
This commit is contained in:
78
tests/test_gui_async_events.py
Normal file
78
tests/test_gui_async_events.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch, AsyncMock
|
||||
import asyncio
|
||||
from gui_2 import ManualSlopGUI
|
||||
from events import UserRequestEvent
|
||||
|
||||
@pytest.fixture
|
||||
def mock_gui():
|
||||
with patch('gui_2.load_config', return_value={
|
||||
"ai": {"provider": "gemini", "model": "model-1"},
|
||||
"projects": {"paths": [], "active": ""},
|
||||
"gui": {"show_windows": {}}
|
||||
}):
|
||||
with patch('gui_2.project_manager.load_project', return_value={}):
|
||||
with patch('gui_2.project_manager.migrate_from_legacy_config', return_value={}):
|
||||
with patch('gui_2.project_manager.save_project'):
|
||||
with patch('gui_2.session_logger.open_session'):
|
||||
with patch('gui_2.ManualSlopGUI._init_ai_and_hooks'):
|
||||
with patch('gui_2.ManualSlopGUI._fetch_models'):
|
||||
gui = ManualSlopGUI()
|
||||
return gui
|
||||
|
||||
def test_handle_generate_send_pushes_event(mock_gui):
|
||||
# Mock _do_generate to return sample data
|
||||
mock_gui._do_generate = MagicMock(return_value=(
|
||||
"full_md", "path", [], "stable_md", "disc_text"
|
||||
))
|
||||
mock_gui.ui_ai_input = "test prompt"
|
||||
mock_gui.ui_files_base_dir = "."
|
||||
|
||||
# Mock event_queue.put
|
||||
mock_gui.event_queue.put = MagicMock()
|
||||
|
||||
# We need to mock asyncio.run_coroutine_threadsafe to immediately execute
|
||||
with patch('asyncio.run_coroutine_threadsafe') as mock_run:
|
||||
mock_gui._handle_generate_send()
|
||||
|
||||
# Verify run_coroutine_threadsafe was called
|
||||
assert mock_run.called
|
||||
|
||||
# Verify the call to event_queue.put was correct
|
||||
# This is a bit tricky since the first arg to run_coroutine_threadsafe
|
||||
# is the coroutine returned by event_queue.put().
|
||||
# Let's verify that the call to put occurred.
|
||||
mock_gui.event_queue.put.assert_called_once()
|
||||
args, kwargs = mock_gui.event_queue.put.call_args
|
||||
assert args[0] == "user_request"
|
||||
event = args[1]
|
||||
assert isinstance(event, UserRequestEvent)
|
||||
assert event.prompt == "test prompt"
|
||||
assert event.stable_md == "stable_md"
|
||||
assert event.disc_text == "disc_text"
|
||||
assert event.base_dir == "."
|
||||
|
||||
def test_user_request_event_payload():
|
||||
payload = UserRequestEvent(
|
||||
prompt="hello",
|
||||
stable_md="md",
|
||||
file_items=[],
|
||||
disc_text="disc",
|
||||
base_dir="."
|
||||
)
|
||||
|
||||
d = payload.to_dict()
|
||||
assert d["prompt"] == "hello"
|
||||
assert d["stable_md"] == "md"
|
||||
assert d["file_items"] == []
|
||||
assert d["disc_text"] == "disc"
|
||||
assert d["base_dir"] == "."
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_event_queue():
|
||||
from events import AsyncEventQueue
|
||||
q = AsyncEventQueue()
|
||||
await q.put("test_event", {"data": 123})
|
||||
name, payload = await q.get()
|
||||
assert name == "test_event"
|
||||
assert payload["data"] == 123
|
||||
Reference in New Issue
Block a user