Private
Public Access
0
0
Files
manual_slop/tests/test_live_gui_integration_v2.py
T
ed ada9617308 test(ai_client): rename send_result to send in 22 remaining test files
Batch rename of 22 test files. 62 references renamed total.

The full test suite is now GREEN again, matching the pre-rename baseline
from Task 1.1. Pure mechanical rename. No behavior change.

Files affected: test_ai_cache_tracking, test_ai_client_cli,
test_ai_client_result, test_api_events, test_context_pruner,
test_deepseek_provider, test_gemini_cli_* (3 files), test_gui2_mcp,
test_headless_* (2 files), test_live_gui_integration_v2,
test_orchestration_logic, test_phase6_engine, test_rag_integration,
test_run_worker_lifecycle_abort, test_spawn_interception_v2,
test_symbol_parsing, test_tier4_interceptor, test_tiered_aggregation,
test_token_usage.

Note: spec estimated 24 files; actual is 22 (test_deprecation_warnings
no longer exists, and 1 fewer file than spec's list).

Refs: conductor/tracks/send_result_to_send_20260616/
2026-06-17 00:38:29 -04:00

142 lines
4.7 KiB
Python

"""
ANTI-SIMPLIFICATION: These tests verify internal queue synchronization and end-to-end event loops.
They MUST NOT be simplified. They ensure that requests hit the AI client, return to the event queue,
and ultimately end up processed by the GUI render loop.
"""
import pytest
from unittest.mock import patch
import time
from src.gui_2 import App
from src.events import UserRequestEvent
from src.api_hook_client import ApiHookClient
from src.result_types import Result, ErrorInfo, ErrorKind
@pytest.mark.timeout(10)
def test_user_request_integration_flow(mock_app: App) -> None:
"""
Verifies that pushing a UserRequestEvent to the event_queue:
1. Triggers ai_client.send
2. Results in a 'response' event back to the queue
3. Eventually updates the UI state (ai_response, ai_status) after processing GUI tasks.
ANTI-SIMPLIFICATION: This verifies the full cross-thread boundary.
"""
app = mock_app
# Mock all ai_client methods called during _handle_request_event
mock_response = "This is a test AI response"
with (
patch('src.ai_client.send', return_value=Result(data=mock_response)) as mock_send,
patch('src.ai_client.set_custom_system_prompt'),
patch('src.ai_client.set_model_params'),
patch('src.ai_client.set_agent_tools'),
patch('src.app_controller.AppController._update_gcli_adapter')
):
# 1. Create and push a UserRequestEvent
event = UserRequestEvent(
prompt="Hello AI",
stable_md="Context",
file_items=[],
disc_text="History",
base_dir="."
)
while not app.controller.event_queue.empty():
app.controller.event_queue.get()
# 2. Call the handler directly since start_services is mocked (no event loop thread)
# But _handle_request_event itself puts a 'response' event in the queue.
# Our mock_app fixture mocks start_services, so _process_event_queue is NOT running.
# We need to call it manually or not mock start_services.
# Let's call the handler
app.controller._handle_request_event(event)
# 3. Verify ai_client.send was called
assert mock_send.called, "ai_client.send was not called"
# 4. First event should be 'comms' (request logging)
event_name, payload = app.controller.event_queue.get()
assert event_name == "comms"
# 5. Next event should be 'response'
event_name, payload = app.controller.event_queue.get()
assert event_name == "response"
# Manually push it to _pending_gui_tasks as _process_event_queue would
app.controller._pending_gui_tasks.append({
"action": "handle_ai_response",
"payload": payload
})
# 5. Process the GUI tasks
app.controller._process_pending_gui_tasks()
assert app.controller.ai_response == mock_response
assert app.controller.ai_status == "done"
@pytest.mark.timeout(10)
def test_user_request_error_handling(mock_app: App) -> None:
"""
Verifies that if ai_client.send raises an exception, the UI is updated with the error state.
"""
app = mock_app
err = ErrorInfo(kind=ErrorKind.NETWORK, message="API Failure", source="ai_client.test")
with (
patch('src.ai_client.send', return_value=Result(data="", errors=[err])),
patch('src.ai_client.set_custom_system_prompt'),
patch('src.ai_client.set_model_params'),
patch('src.ai_client.set_agent_tools'),
patch('src.app_controller.AppController._update_gcli_adapter')
):
event = UserRequestEvent(
prompt="Trigger Error",
stable_md="",
file_items=[],
disc_text="",
base_dir="."
)
while not app.controller.event_queue.empty():
app.controller.event_queue.get()
app.controller._handle_request_event(event)
# Manually consume from queue
event_name, payload = app.controller.event_queue.get()
assert event_name == "comms"
event_name, payload = app.controller.event_queue.get()
assert event_name == "response"
assert payload["status"] == "error"
# Manually push to GUI tasks
app.controller._pending_gui_tasks.append({
"action": "handle_ai_response",
"payload": payload
})
app.controller._process_pending_gui_tasks()
assert app.controller.ai_status == "error"
assert "API Failure" in app.controller.ai_response
def test_api_gui_state_live(live_gui) -> None:
client = ApiHookClient()
client.set_value('current_provider', 'anthropic')
client.set_value('current_model', 'claude-3-haiku-20240307')
start_time = time.time()
success = False
while time.time() - start_time < 10:
state = client.get_gui_state()
if state and state.get('current_provider') == 'anthropic' and state.get('current_model') == 'claude-3-haiku-20240307':
success = True
break
time.sleep(0.5)
assert success, f"GUI state did not update. Got: {client.get_gui_state()}"
final_state = client.get_gui_state()
assert final_state['current_provider'] == 'anthropic'
assert final_state['current_model'] == 'claude-3-haiku-20240307'