import pytest from unittest.mock import MagicMock, patch import os import threading import time import json import requests # Import requests for exception types from api_hooks import HookServer from api_hook_client import ApiHookClient @pytest.fixture(scope="module") def hook_server_fixture_for_integration(): # Mock the 'app' object that HookServer expects mock_app = MagicMock() mock_app.test_hooks_enabled = True # Essential for the server to start mock_app.project = {'name': 'test_project'} mock_app.disc_entries = [{'role': 'user', 'content': 'hello'}] mock_app._pending_gui_tasks = [] mock_app._pending_gui_tasks_lock = threading.Lock() # Use an ephemeral port (0) to avoid conflicts server = HookServer(mock_app, port=0) server.start() time.sleep(0.1) # Wait a moment for the server thread to start and bind actual_port = server.server.server_address[1] client_base_url = f"http://127.0.0.1:{actual_port}" yield client_base_url, mock_app server.stop() def simulate_conductor_phase_completion(client_base_url: str, mock_app: MagicMock, plan_content: str): """ Simulates the Conductor agent's logic for phase completion. This function, in the *actual* implementation, will be *my* (the agent's) code. Now includes basic result handling and simulated user feedback. """ print(f"Simulating Conductor phase completion. Client base URL: {client_base_url}") client = ApiHookClient(base_url=client_base_url) try: status = client.get_status() # Assuming get_status is the verification call print(f"API Hook Client status response: {status}") if status.get('status') == 'ok': mock_app.verification_successful = True # Simulate success flag mock_app.verification_message = "Automated verification completed successfully." else: mock_app.verification_successful = False mock_app.verification_message = f"Automated verification failed: {status}" except requests.exceptions.Timeout: mock_app.verification_successful = False mock_app.verification_message = "Automated verification failed: Request timed out." except requests.exceptions.ConnectionError: mock_app.verification_successful = False mock_app.verification_message = "Automated verification failed: Could not connect to API hook server." except requests.exceptions.HTTPError as e: mock_app.verification_successful = False mock_app.verification_message = f"Automated verification failed: HTTP error {e.response.status_code}." except Exception as e: mock_app.verification_successful = False mock_app.verification_message = f"Automated verification failed: An unexpected error occurred: {e}" print(mock_app.verification_message) # In a real scenario, the agent would then ask the user if they want to proceed # if verification_successful is True, or if they want to debug/fix if False. def test_conductor_integrates_api_hook_client_for_verification(hook_server_fixture_for_integration): """ Verify that Conductor's simulated phase completion logic properly integrates and uses the ApiHookClient for verification. This test *should* pass (Green Phase) if the integration in `simulate_conductor_phase_completion` is correct. """ client_base_url, mock_app = hook_server_fixture_for_integration dummy_plan_content = """ # Implementation Plan: Test Track ## Phase 1: Initial Setup [checkpoint: abcdefg] - [x] Task: Dummy Task 1 [1234567] - [ ] Task: Conductor - User Manual Verification 'Phase 1: Initial Setup' (Protocol in workflow.md) """ # Reset mock_app's success flag for this test run mock_app.verification_successful = False mock_app.verification_message = "" simulate_conductor_phase_completion(client_base_url, mock_app, dummy_plan_content) # Assert that the verification was considered successful by the simulated Conductor assert mock_app.verification_successful is True assert "successfully" in mock_app.verification_message def test_conductor_handles_api_hook_failure(hook_server_fixture_for_integration): """ Verify Conductor handles a simulated API hook verification failure. This test will be 'Red' until simulate_conductor_phase_completion correctly sets verification_successful to False and provides a failure message. """ client_base_url, mock_app = hook_server_fixture_for_integration with patch.object(ApiHookClient, 'get_status', autospec=True) as mock_get_status: # Configure mock to simulate a non-'ok' status mock_get_status.return_value = {'status': 'failed', 'error': 'Something went wrong'} mock_app.verification_successful = True # Reset for the test mock_app.verification_message = "" simulate_conductor_phase_completion(client_base_url, mock_app, "") assert mock_app.verification_successful is False assert "failed" in mock_app.verification_message def test_conductor_handles_api_hook_connection_error(hook_server_fixture_for_integration): """ Verify Conductor handles a simulated API hook connection error. This test will be 'Red' until simulate_conductor_phase_completion correctly sets verification_successful to False and provides a connection error message. """ client_base_url, mock_app = hook_server_fixture_for_integration with patch.object(ApiHookClient, 'get_status', autospec=True) as mock_get_status: # Configure mock to raise a ConnectionError mock_get_status.side_effect = requests.exceptions.ConnectionError("Mocked connection error") mock_app.verification_successful = True # Reset for the test mock_app.verification_message = "" simulate_conductor_phase_completion(client_base_url, mock_app, "") assert mock_app.verification_successful is False assert "Could not connect" in mock_app.verification_message