conductor(checkpoint): Checkpoint end of Phase 2: Test Suite Migration
This commit is contained in:
@@ -4,131 +4,70 @@ import os
|
||||
import threading
|
||||
import time
|
||||
import json
|
||||
import requests # Import requests for exception types
|
||||
import requests
|
||||
import sys
|
||||
|
||||
# Ensure project root is in path
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
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):
|
||||
def simulate_conductor_phase_completion(client: ApiHookClient):
|
||||
"""
|
||||
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.
|
||||
Simulates the Conductor agent's logic for phase completion using ApiHookClient.
|
||||
"""
|
||||
print(f"Simulating Conductor phase completion. Client base URL: {client_base_url}")
|
||||
client = ApiHookClient(base_url=client_base_url)
|
||||
results = {
|
||||
"verification_successful": False,
|
||||
"verification_message": ""
|
||||
}
|
||||
|
||||
try:
|
||||
status = client.get_status() # Assuming get_status is the verification call
|
||||
print(f"API Hook Client status response: {status}")
|
||||
status = client.get_status()
|
||||
if status.get('status') == 'ok':
|
||||
mock_app.verification_successful = True # Simulate success flag
|
||||
mock_app.verification_message = "Automated verification completed successfully."
|
||||
results["verification_successful"] = True
|
||||
results["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}."
|
||||
results["verification_successful"] = False
|
||||
results["verification_message"] = f"Automated verification failed: {status}"
|
||||
except Exception as e:
|
||||
mock_app.verification_successful = False
|
||||
mock_app.verification_message = f"Automated verification failed: An unexpected error occurred: {e}"
|
||||
results["verification_successful"] = False
|
||||
results["verification_message"] = f"Automated verification failed: {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.
|
||||
return results
|
||||
|
||||
def test_conductor_integrates_api_hook_client_for_verification(hook_server_fixture_for_integration):
|
||||
def test_conductor_integrates_api_hook_client_for_verification(live_gui):
|
||||
"""
|
||||
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.
|
||||
and uses the ApiHookClient for verification against the live GUI.
|
||||
"""
|
||||
client_base_url, mock_app = hook_server_fixture_for_integration
|
||||
client = ApiHookClient()
|
||||
results = simulate_conductor_phase_completion(client)
|
||||
|
||||
dummy_plan_content = """
|
||||
# Implementation Plan: Test Track
|
||||
assert results["verification_successful"] is True
|
||||
assert "successfully" in results["verification_message"]
|
||||
|
||||
## 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):
|
||||
def test_conductor_handles_api_hook_failure(live_gui):
|
||||
"""
|
||||
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.
|
||||
We patch the client's get_status to simulate failure even with live GUI.
|
||||
"""
|
||||
client_base_url, mock_app = hook_server_fixture_for_integration
|
||||
client = ApiHookClient()
|
||||
|
||||
with patch.object(ApiHookClient, 'get_status', autospec=True) as mock_get_status:
|
||||
# Configure mock to simulate a non-'ok' status
|
||||
with patch.object(ApiHookClient, 'get_status') as mock_get_status:
|
||||
mock_get_status.return_value = {'status': 'failed', 'error': 'Something went wrong'}
|
||||
results = simulate_conductor_phase_completion(client)
|
||||
|
||||
mock_app.verification_successful = True # Reset for the test
|
||||
mock_app.verification_message = ""
|
||||
assert results["verification_successful"] is False
|
||||
assert "failed" in results["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):
|
||||
def test_conductor_handles_api_hook_connection_error():
|
||||
"""
|
||||
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.
|
||||
Verify Conductor handles a simulated API hook connection error (server down).
|
||||
"""
|
||||
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")
|
||||
client = ApiHookClient(base_url="http://127.0.0.1:9998", max_retries=0)
|
||||
results = simulate_conductor_phase_completion(client)
|
||||
|
||||
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
|
||||
assert results["verification_successful"] is False
|
||||
# Check for expected error substrings from ApiHookClient
|
||||
msg = results["verification_message"]
|
||||
assert any(term in msg for term in ["Could not connect", "timed out", "Could not reach"])
|
||||
|
||||
Reference in New Issue
Block a user