Files
openrabbit/tests/test_ai_review.py
2025-12-21 13:42:30 +01:00

258 lines
8.3 KiB
Python

"""Test Suite for AI Code Review Workflow
Tests for verifying prompt formatting, agent logic, and core functionality.
Run with: pytest tests/ -v
"""
import os
import sys
# Add the tools directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "tools", "ai-review"))
import pytest
class TestPromptFormatting:
"""Test that all prompts can be formatted without errors."""
def get_prompt_path(self, name: str) -> str:
"""Get the full path to a prompt file."""
return os.path.join(
os.path.dirname(__file__),
"..", "tools", "ai-review", "prompts", f"{name}.md"
)
def load_prompt(self, name: str) -> str:
"""Load a prompt file."""
path = self.get_prompt_path(name)
with open(path) as f:
return f.read()
def test_issue_triage_prompt_formatting(self):
"""Test that issue_triage.md can be formatted with placeholders."""
prompt = self.load_prompt("issue_triage")
# This should NOT raise a KeyError
formatted = prompt.format(
title="Test Issue Title",
body="This is the issue body content",
author="testuser",
existing_labels="bug, urgent"
)
assert "Test Issue Title" in formatted
assert "This is the issue body content" in formatted
assert "testuser" in formatted
assert "bug, urgent" in formatted
# JSON example should still be present (curly braces escaped)
assert '"type"' in formatted
assert '"priority"' in formatted
def test_issue_response_prompt_formatting(self):
"""Test that issue_response.md can be formatted with placeholders."""
prompt = self.load_prompt("issue_response")
formatted = prompt.format(
issue_type="bug",
priority="high",
title="Bug Report",
body="Description of the bug",
triage_analysis="This is a high priority bug"
)
assert "bug" in formatted
assert "high" in formatted
assert "Bug Report" in formatted
# JSON example should still be present
assert '"comment"' in formatted
def test_base_prompt_no_placeholders(self):
"""Test that base.md loads correctly (no placeholders needed)."""
prompt = self.load_prompt("base")
# Should contain key elements
assert "security" in prompt.lower()
assert "JSON" in prompt
assert "severity" in prompt.lower()
def test_prompts_have_escaped_json(self):
"""Verify JSON examples use double curly braces."""
for prompt_name in ["issue_triage", "issue_response"]:
prompt = self.load_prompt(prompt_name)
# Check that format() doesn't fail
try:
# Try with minimal placeholders
if prompt_name == "issue_triage":
prompt.format(title="t", body="b", author="a", existing_labels="l")
elif prompt_name == "issue_response":
prompt.format(issue_type="t", priority="p", title="t", body="b", triage_analysis="a")
except KeyError as e:
pytest.fail(f"Prompt {prompt_name} has unescaped curly braces: {e}")
class TestImports:
"""Test that all modules can be imported correctly."""
def test_import_agents(self):
"""Test importing agent classes."""
from agents.base_agent import BaseAgent, AgentContext, AgentResult
from agents.issue_agent import IssueAgent
from agents.pr_agent import PRAgent
from agents.codebase_agent import CodebaseAgent
assert BaseAgent is not None
assert IssueAgent is not None
assert PRAgent is not None
assert CodebaseAgent is not None
def test_import_clients(self):
"""Test importing client classes."""
from clients.gitea_client import GiteaClient
from clients.llm_client import LLMClient
assert GiteaClient is not None
assert LLMClient is not None
def test_import_security(self):
"""Test importing security scanner."""
from security.security_scanner import SecurityScanner
assert SecurityScanner is not None
def test_import_enterprise(self):
"""Test importing enterprise features."""
from enterprise.audit_logger import AuditLogger
from enterprise.metrics import MetricsCollector
assert AuditLogger is not None
assert MetricsCollector is not None
def test_import_dispatcher(self):
"""Test importing dispatcher."""
from dispatcher import Dispatcher
assert Dispatcher is not None
class TestSecurityScanner:
"""Test security scanner pattern detection."""
def test_detects_hardcoded_secret(self):
"""Test detection of hardcoded secrets."""
from security.security_scanner import SecurityScanner
scanner = SecurityScanner()
code = '''
API_KEY = "sk-1234567890abcdef"
'''
findings = list(scanner.scan_content(code, "test.py"))
assert len(findings) >= 1
assert any(f.severity == "HIGH" for f in findings)
def test_detects_eval(self):
"""Test detection of eval usage."""
from security.security_scanner import SecurityScanner
scanner = SecurityScanner()
code = '''
result = eval(user_input)
'''
findings = list(scanner.scan_content(code, "test.py"))
assert len(findings) >= 1
assert any("eval" in f.rule_name.lower() for f in findings)
def test_no_false_positives_on_clean_code(self):
"""Test that clean code doesn't trigger false positives."""
from security.security_scanner import SecurityScanner
scanner = SecurityScanner()
code = '''
def hello():
print("Hello, world!")
return 42
'''
findings = list(scanner.scan_content(code, "test.py"))
# Should have no HIGH severity issues for clean code
high_findings = [f for f in findings if f.severity == "HIGH"]
assert len(high_findings) == 0
class TestAgentContext:
"""Test agent context and result dataclasses."""
def test_agent_context_creation(self):
"""Test creating AgentContext."""
from agents.base_agent import AgentContext
context = AgentContext(
owner="testowner",
repo="testrepo",
event_type="issues",
event_data={"action": "opened"},
config={}
)
assert context.owner == "testowner"
assert context.repo == "testrepo"
assert context.event_type == "issues"
def test_agent_result_creation(self):
"""Test creating AgentResult."""
from agents.base_agent import AgentResult
result = AgentResult(
success=True,
message="Test passed",
data={"key": "value"},
actions_taken=["action1", "action2"]
)
assert result.success is True
assert result.message == "Test passed"
assert len(result.actions_taken) == 2
class TestMetrics:
"""Test metrics collection."""
def test_counter_increment(self):
"""Test counter metrics."""
from enterprise.metrics import Counter
counter = Counter("test_counter")
assert counter.value == 0
counter.inc()
assert counter.value == 1
counter.inc(5)
assert counter.value == 6
def test_histogram_observation(self):
"""Test histogram metrics."""
from enterprise.metrics import Histogram
hist = Histogram("test_histogram")
hist.observe(0.1)
hist.observe(0.5)
hist.observe(1.0)
assert hist.count == 3
assert hist.sum == 1.6
def test_metrics_collector_summary(self):
"""Test metrics collector summary."""
from enterprise.metrics import MetricsCollector
collector = MetricsCollector()
collector.record_request_start("TestAgent")
collector.record_request_end("TestAgent", success=True, duration_seconds=0.5)
summary = collector.get_summary()
assert summary["requests"]["total"] == 1
assert summary["requests"]["success"] == 1
if __name__ == "__main__":
pytest.main([__file__, "-v"])