ed93df698b
* fix: add mock reset fixtures and resolve async iterator mock issues - Add pytest_runtest_setup and pytest_runtest_teardown hooks to reset shared module-level mocks between tests - Add module-specific mock reset fixtures for test_qa_fixer and test_qa_reviewer to prevent test interference - Fix async iterator mock for receive_response to properly return an AsyncIteratorMock instance - Update test_qa_fixer.py and test_qa_reviewer.py with proper mock setup for isolated test execution * docs(agents): add CLAUDE.md documentation for agents module Documents the agents module architecture including: - Module components (coder, planner, session, memory_manager, base) - Single-agent architecture without external parallelism - Subagent architecture clarification * Revert "docs(agents): add CLAUDE.md documentation for agents module" This reverts commit bf1ddd7da08f2f34352d11a5d823da981f1a98bb. * chore: update gitignore to allow agents/tests/ * fix(tests): resolve mock isolation and path permission issues - Fix test_tool_concurrency_error_detection by patching where functions are used (qa.fixer) instead of where they're defined - Add Path.exists/is_dir/glob mocks to avoid permission errors on nonexistent directories in test_validation_strategy.py - Add helper function clean_project_index_files() to reduce code duplication in prereqs_validator tests - Add comprehensive tests for spec validation validators (context, prereqs, spec_document) - Fix similar mock/path issues in test_qa_reviewer.py, test_service_orchestrator.py, test_ci_discovery.py, test_prompt_generator.py, test_security_scanner.py All 2103 tests now pass. * fix(tests): remove unused imports and fix double assignment - Remove unused 'patch' import from validator test files - Remove unused 'pytest' import where not needed - Fix double assignment typo in test_error_message_includes_filename * fix(tests): move agents tests to tests/agents/ directory - Move test_agent_architecture.py, test_agent_configs.py, and test_agent_flow.py from apps/backend/agents/tests/ to tests/agents/ - Fix path resolution to work from new location - Remove gitignore exception for agents/tests/ (no longer needed) This resolves the issue where tests were not included in the PR because they were in an untracked location. * fix(tests): simplify conftest.py mock management - Remove redundant pytest_runtest_teardown and pytest_runtest_call hooks (autouse fixtures in test files already handle mock reset) - Add prompts_pkg.project_context to potentially mocked modules list - Remove prompts_pkg from test_qa_fixer entry (not used there) This reduces maintenance burden by having mock reset in one place. * refactor(tests): consolidate duplicate mock setup into shared helper - Create tests/qa_test_helpers.py with shared mock infrastructure: - AsyncIteratorMock and ReceiveResponseMock classes - setup_qa_mocks(), cleanup_qa_mocks(), reset_qa_mocks() functions - Mock response creation helpers - Accessor functions for mock objects - Refactor test_qa_fixer.py to use shared helpers - Reduces ~80 lines of duplicated code per test file - Fixes potential mock binding issues by using accessor functions This addresses code quality issues identified in PR review: - Duplicate mock setup between test_qa_fixer.py and test_qa_reviewer.py - Duplicated _AsyncIteratorMock class across files * refactor(tests): consolidate test_qa_reviewer.py with shared helpers - Refactor test_qa_reviewer.py to use shared qa_test_helpers - Remove ~170 lines of duplicated mock setup and helper functions - Fix unused imports in test_qa_fixer.py (json, sys, MagicMock, etc.) - Fix rate limit error detection tests to patch where functions are used - Consolidate duplicated _create_*_response helper methods to module level Addresses CodeQL warnings about unused imports and reduces code duplication between test_qa_fixer.py and test_qa_reviewer.py. * fix(tests): remove unused Path import from test_qa_reviewer.py * fix(tests): address all PR review findings PR Review Fixes: - Remove unused create_mock_qa_approved_response/rejected_response functions - Guard against overwriting _original_modules on second setup_qa_mocks() call - Clear _original_modules in cleanup_qa_mocks() to prevent stale state - Add prompts_pkg.project_context to test_qa_reviewer preserved_mocks in conftest - Convert asyncio.run() pattern to native async tests in test_agent_flow.py - Remove redundant @pytest.mark.asyncio decorators (asyncio_mode=auto) - Remove unused pytest import from qa_test_helpers.py - Fix structural duplication by keeping fixtures in test files Code Quality: - Removed ~100 lines of duplicated/unused code - Consistent async test patterns across all QA test files - Proper mock state management to prevent test pollution * fix(tests): save original modules individually in setup_qa_mocks The boolean guard `setup_done` prevented saving original modules on subsequent calls with different parameters. When setup_qa_mocks was called first with include_prompts_pkg=False, then with True, the prompts_pkg modules were never saved to _original_modules. During cleanup, these unsaved modules were deleted from sys.modules instead of being restored, causing ModuleNotFoundError in subsequent tests. Now checks each module individually before mocking, ensuring all originals are saved across multiple setup calls. * fix(tests): address all PR review findings including low priority - Fix path in test_no_subtask_worker_config (parent.parent.parent) - Add guard to prevent double setup in setup_qa_mocks() - Don't clear _original_modules in cleanup to fix multi-module cleanup * fix(tests): address PR review follow-up findings - Fix module-level mock setup ordering dependency: now tracks include_prompts_pkg config and allows incremental setup when test_qa_fixer.py (False) is imported before test_qa_reviewer.py (True) - Remove unused asyncio import from test_agent_flow.py - Replace os.chdir() with monkeypatch.chdir() in prereqs validator tests for safe parallel test execution --------- Co-authored-by: StillKnotKnown <stillknotknown@users.noreply.github.com>
498 lines
16 KiB
Python
498 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Tests for QA Fixer Agent Session
|
|
================================
|
|
|
|
Tests the qa/fixer.py module functionality including:
|
|
- load_qa_fixer_prompt function
|
|
- run_qa_fixer_session function
|
|
- QA fixer session execution flow
|
|
- Error handling and edge cases
|
|
- Memory integration hooks
|
|
"""
|
|
|
|
import shutil
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
# =============================================================================
|
|
# MOCK SETUP - Must happen before ANY imports from auto-claude
|
|
# =============================================================================
|
|
|
|
# Import shared mock helpers
|
|
from tests.qa_test_helpers import (
|
|
setup_qa_mocks,
|
|
cleanup_qa_mocks,
|
|
reset_qa_mocks,
|
|
create_mock_response,
|
|
create_mock_fixed_response,
|
|
create_mock_tool_use_response,
|
|
create_mock_client,
|
|
)
|
|
|
|
# Set up mocks (no prompts_pkg needed for fixer)
|
|
setup_qa_mocks(include_prompts_pkg=False)
|
|
|
|
# Import after mocks are set up
|
|
from qa.fixer import load_qa_fixer_prompt, run_qa_fixer_session
|
|
from qa.criteria import save_implementation_plan
|
|
|
|
|
|
# =============================================================================
|
|
# FIXTURES
|
|
# =============================================================================
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def cleanup_mocked_modules():
|
|
"""Restore original modules after all tests in this module complete."""
|
|
yield
|
|
cleanup_qa_mocks()
|
|
|
|
|
|
@pytest.fixture
|
|
def spec_dir(temp_dir):
|
|
"""Create a spec directory with basic structure."""
|
|
spec = temp_dir / "spec"
|
|
spec.mkdir()
|
|
return spec
|
|
|
|
|
|
@pytest.fixture
|
|
def project_dir(temp_dir):
|
|
"""Create a project directory."""
|
|
project = temp_dir / "project"
|
|
project.mkdir()
|
|
return project
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_client():
|
|
"""Create a mock Claude SDK client."""
|
|
return create_mock_client()
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='function')
|
|
def reset_shared_mocks_before_test():
|
|
"""Reset shared module-level mocks before and after each test."""
|
|
reset_qa_mocks()
|
|
yield
|
|
reset_qa_mocks()
|
|
|
|
|
|
# =============================================================================
|
|
# MOCK RESPONSE HELPERS (fixer-specific)
|
|
# =============================================================================
|
|
|
|
def _create_mock_response(text: str = "Fixer session complete."):
|
|
"""Create a standard mock assistant+user message pair."""
|
|
return create_mock_response(text)
|
|
|
|
|
|
def _create_mock_fixed_response():
|
|
"""Create mock response for fixed QA."""
|
|
return create_mock_fixed_response()
|
|
|
|
|
|
def _create_mock_tool_use_response():
|
|
"""Create mock response with tool use blocks."""
|
|
return create_mock_tool_use_response("Edit", {"file_path": "/test/file.py"})
|
|
|
|
|
|
@pytest.fixture
|
|
def fix_request_file(spec_dir):
|
|
"""Create a QA_FIX_REQUEST.md file."""
|
|
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
|
fix_request.write_text("# Fix Request\n\nFix the following issues:\n- Issue 1\n- Issue 2")
|
|
return fix_request
|
|
|
|
|
|
# =============================================================================
|
|
# TEST CLASSES
|
|
# =============================================================================
|
|
|
|
|
|
class TestLoadQAFixerPrompt:
|
|
"""Tests for load_qa_fixer_prompt function."""
|
|
|
|
def test_load_prompt_success(self, spec_dir, monkeypatch):
|
|
"""Test successful prompt loading."""
|
|
# Create prompts directory in temp location
|
|
prompts_dir = spec_dir / "prompts"
|
|
prompts_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
prompt_file = prompts_dir / "qa_fixer.md"
|
|
prompt_content = "# QA Fixer Prompt\n\nFix the issues..."
|
|
prompt_file.write_text(prompt_content)
|
|
|
|
# Patch QA_PROMPTS_DIR to point to temp directory
|
|
import qa.fixer as qa_fixer_module
|
|
monkeypatch.setattr(qa_fixer_module, "QA_PROMPTS_DIR", prompts_dir)
|
|
|
|
result = load_qa_fixer_prompt()
|
|
|
|
assert result == prompt_content
|
|
|
|
def test_load_prompt_file_not_found(self, monkeypatch):
|
|
"""Test FileNotFoundError when prompt file doesn't exist."""
|
|
# Create an empty temp directory with no qa_fixer.md
|
|
empty_dir = Path(tempfile.mkdtemp())
|
|
|
|
try:
|
|
# Patch QA_PROMPTS_DIR to point to empty directory
|
|
import qa.fixer as qa_fixer_module
|
|
monkeypatch.setattr(qa_fixer_module, "QA_PROMPTS_DIR", empty_dir)
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
load_qa_fixer_prompt()
|
|
finally:
|
|
# Clean up temp directory
|
|
shutil.rmtree(empty_dir)
|
|
|
|
|
|
class TestRunQAFixerSessionFixed:
|
|
"""Tests for run_qa_fixer_session returning fixed status."""
|
|
|
|
async def test_fixed_status(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that fixed status is returned when ready_for_qa_revalidation is True."""
|
|
# Setup implementation plan with ready_for_qa_revalidation
|
|
plan = {
|
|
"feature": "Test",
|
|
"qa_signoff": {
|
|
"status": "fixes_applied",
|
|
"ready_for_qa_revalidation": True,
|
|
}
|
|
}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
assert result[0] == "fixed"
|
|
assert len(result[1]) > 0 # Response text
|
|
assert result[2] == {} # No error info
|
|
|
|
async def test_fixed_status_with_project_dir(self, mock_client, spec_dir, project_dir):
|
|
"""Test session with explicit project_dir parameter."""
|
|
# Create fix request file
|
|
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
|
fix_request.write_text("# Fix Request\n\nFix issues")
|
|
|
|
# Setup implementation plan
|
|
plan = {
|
|
"feature": "Test",
|
|
"qa_signoff": {
|
|
"status": "fixes_applied",
|
|
"ready_for_qa_revalidation": True,
|
|
}
|
|
}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False,
|
|
project_dir=project_dir
|
|
)
|
|
|
|
assert result[0] == "fixed"
|
|
|
|
|
|
class TestRunQAFixerSessionError:
|
|
"""Tests for run_qa_fixer_session error handling."""
|
|
|
|
async def test_error_missing_fix_request(self, mock_client, spec_dir):
|
|
"""Test error when QA_FIX_REQUEST.md is missing."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Don't create QA_FIX_REQUEST.md
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
assert result[0] == "error"
|
|
assert "not found" in result[1].lower()
|
|
assert result[2]["type"] == "other"
|
|
assert result[2]["exception_type"] == "FileNotFoundError"
|
|
|
|
async def test_exception_handling(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test exception handling during fixer session."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client to raise exception
|
|
mock_client.query.side_effect = Exception("Test exception")
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
assert result[0] == "error"
|
|
assert "Test exception" in result[1] or "test exception" in result[1].lower()
|
|
assert result[2]["type"] == "other"
|
|
assert result[2]["exception_type"] == "Exception"
|
|
|
|
|
|
class TestRunQAFixerSessionParameters:
|
|
"""Tests for run_qa_fixer_session parameter handling."""
|
|
|
|
async def test_verbose_mode(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test session with verbose mode enabled."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
|
|
|
await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
verbose=True
|
|
)
|
|
|
|
# Verify query was called
|
|
assert mock_client.query.called
|
|
|
|
async def test_fix_session_number(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test session with different fix_session numbers."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
|
|
|
await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
fix_session=3,
|
|
verbose=False
|
|
)
|
|
|
|
# Verify query was called
|
|
assert mock_client.query.called
|
|
|
|
|
|
class TestRunQAFixerSessionIntegration:
|
|
"""Integration tests for QA fixer session."""
|
|
|
|
async def test_full_session_flow(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test complete session flow from start to finish."""
|
|
# Setup implementation plan
|
|
plan = {
|
|
"feature": "Test Feature",
|
|
"qa_signoff": {
|
|
"status": "fixes_applied",
|
|
"ready_for_qa_revalidation": True,
|
|
}
|
|
}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_response("Applying fixes..."))
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
fix_session=1,
|
|
verbose=False
|
|
)
|
|
|
|
assert result[0] == "fixed"
|
|
assert mock_client.query.called
|
|
assert mock_client.receive_response.called
|
|
|
|
|
|
class TestMemoryIntegration:
|
|
"""Tests for memory integration in QA fixer."""
|
|
|
|
async def test_memory_context_retrieval(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that memory context is retrieved during session."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
|
|
|
# Patch where the function is used (in qa.fixer module)
|
|
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock) as mock_get_context:
|
|
mock_get_context.return_value = "Past fix patterns: check imports"
|
|
|
|
await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
# Verify memory context was retrieved
|
|
assert mock_get_context.called
|
|
|
|
async def test_memory_save_on_fixed(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that session memory is saved when fixes are applied."""
|
|
# Setup implementation plan
|
|
plan = {
|
|
"feature": "Test",
|
|
"qa_signoff": {
|
|
"status": "fixes_applied",
|
|
"ready_for_qa_revalidation": True,
|
|
}
|
|
}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
|
|
|
# Patch where the function is used
|
|
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
|
patch('qa.fixer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
|
|
|
await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
# Verify memory was saved
|
|
assert mock_save.called
|
|
|
|
|
|
class TestErrorDetection:
|
|
"""Tests for error type detection in QA fixer."""
|
|
|
|
async def test_rate_limit_error_detection(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that rate limit errors are properly detected."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client to raise exception
|
|
mock_client.query.side_effect = Exception("Rate limit exceeded")
|
|
|
|
# Patch where the functions are used (qa.fixer) not where they're defined
|
|
with patch('qa.fixer.is_rate_limit_error', return_value=True), \
|
|
patch('qa.fixer.is_tool_concurrency_error', return_value=False):
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
assert result[0] == "error"
|
|
assert result[2]["type"] == "rate_limit"
|
|
|
|
async def test_tool_concurrency_error_detection(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that tool concurrency errors are properly detected."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client to raise exception
|
|
mock_client.query.side_effect = Exception("Tool concurrency limit")
|
|
|
|
# Patch where the functions are used (qa.fixer) not where they're defined
|
|
with patch('qa.fixer.is_tool_concurrency_error', return_value=True), \
|
|
patch('qa.fixer.is_rate_limit_error', return_value=False), \
|
|
patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None):
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
assert result[0] == "error"
|
|
assert result[2]["type"] == "tool_concurrency"
|
|
|
|
|
|
class TestStatusNotUpdated:
|
|
"""Tests for when fixer doesn't update status."""
|
|
|
|
async def test_fixed_assumed_when_status_not_updated(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that fixed is assumed even when status not updated."""
|
|
# Setup implementation plan without ready_for_qa_revalidation
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
|
|
|
# Patch where the function is used
|
|
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
|
patch('qa.fixer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
|
|
|
result = await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
# Should still return "fixed" even though status wasn't updated
|
|
assert result[0] == "fixed"
|
|
# Memory should still be saved
|
|
assert mock_save.called
|
|
|
|
|
|
class TestToolUseHandling:
|
|
"""Tests for tool use handling in QA fixer."""
|
|
|
|
async def test_tool_use_blocks(self, mock_client, spec_dir, fix_request_file):
|
|
"""Test that tool use blocks are handled correctly."""
|
|
# Setup implementation plan
|
|
plan = {"feature": "Test"}
|
|
save_implementation_plan(spec_dir, plan)
|
|
|
|
# Mock client responses with tool use
|
|
mock_client.query.return_value = None
|
|
mock_client.receive_response.return_value.set_messages(_create_mock_tool_use_response())
|
|
|
|
await run_qa_fixer_session(
|
|
mock_client,
|
|
spec_dir,
|
|
1,
|
|
False
|
|
)
|
|
|
|
# Verify query was called
|
|
assert mock_client.query.called
|