""" Unit tests for the context resolution system. """ import pytest import tempfile import os from src.pr_reviewer.context import resolve_context from src.pr_reviewer.state import PRReviewState, ContextOverrides def test_resolve_context_with_defaults(): """Test resolving context when default files exist.""" # Create temporary directory structure with tempfile.TemporaryDirectory() as temp_dir: # Create contexts/defaults directory defaults_dir = os.path.join(temp_dir, "contexts", "defaults") os.makedirs(defaults_dir) # Create default context files with open(os.path.join(defaults_dir, "code_review.md"), "w") as f: f.write("# Code Review\n\nFollow PEP8.") with open(os.path.join(defaults_dir, "security_review.md"), "w") as f: f.write("# Security Review\n\nCheck for SQL injection.") with open(os.path.join(defaults_dir, "infra_review.md"), "w") as f: f.write("# Infra Review\n\nValidate Dockerfile.") # Change to temp directory original_cwd = os.getcwd() os.chdir(temp_dir) try: # Create state without overrides state = PRReviewState( pr_id="123", pr_title="Test PR", repo_name="test-repo", repo_url="https://github.com/user/test-repo", branch="main", base_branch="main" ) # Resolve context context = resolve_context(state) # Assertions assert context["code_review"] == "# Code Review\n\nFollow PEP8." assert context["security_review"] == "# Security Review\n\nCheck for SQL injection." assert context["infra_review"] == "# Infra Review\n\nValidate Dockerfile." finally: os.chdir(original_cwd) def test_resolve_context_with_overrides(): """Test resolving context when overrides are provided.""" # Create state with context overrides state = PRReviewState( pr_id="123", pr_title="Test PR", repo_name="test-repo", repo_url="https://github.com/user/test-repo", branch="main", base_branch="main", context_overrides=ContextOverrides( code_review="Custom code review guidelines", security_review="Custom security guidelines" # infra_review is not provided, should use default if file exists ) ) # Since we don't have default files in this test, infra_review should be empty context = resolve_context(state) # Assertions assert context["code_review"] == "Custom code review guidelines" assert context["security_review"] == "Custom security guidelines" assert context["infra_review"] == "" # Empty because no default file and no override def test_resolve_context_empty_state(): """Test resolving context with minimal state.""" state = PRReviewState( pr_id="123", pr_title="Test PR", repo_name="test-repo", repo_url="https://github.com/user/test-repo", branch="main", base_branch="main" ) # Without default files in the current directory, all contexts should be empty # But since we have default files in the project, we need to check what they contain context = resolve_context(state) # The function should return the content of the default files # We're just checking that it returns strings (could be empty or contain default content) assert isinstance(context["code_review"], str) assert isinstance(context["security_review"], str) assert isinstance(context["infra_review"], str)