|
| 1 | +"""Tests for response formatters. |
| 2 | +
|
| 3 | +Formatters are pure functions (dict -> str) so they're straightforward to test |
| 4 | +without any mocking or network calls. |
| 5 | +""" |
| 6 | +import pytest |
| 7 | +import sys |
| 8 | +import os |
| 9 | + |
| 10 | +# Add parent directory to path so we can import the modules |
| 11 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 12 | + |
| 13 | +from formatters import ( |
| 14 | + format_search_results, |
| 15 | + format_repositories, |
| 16 | + format_dependency_graph, |
| 17 | + format_code_style, |
| 18 | + format_impact_analysis, |
| 19 | + format_repository_insights, |
| 20 | + format_codebase_dna, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +# -- Search results (v2 format) -- |
| 25 | + |
| 26 | +class TestFormatSearchResults: |
| 27 | + def test_empty_results(self): |
| 28 | + result = {"total": 0, "results": [], "cached": False, "search_version": "v2"} |
| 29 | + output = format_search_results(result) |
| 30 | + assert "Found 0 results" in output |
| 31 | + assert "No results found" in output |
| 32 | + |
| 33 | + def test_basic_result(self): |
| 34 | + result = { |
| 35 | + "total": 1, |
| 36 | + "cached": False, |
| 37 | + "search_version": "v2", |
| 38 | + "results": [{ |
| 39 | + "name": "authenticate", |
| 40 | + "qualified_name": "auth.service.authenticate", |
| 41 | + "file_path": "backend/auth.py", |
| 42 | + "code": "def authenticate(token): ...", |
| 43 | + "signature": "def authenticate(token: str) -> User", |
| 44 | + "language": "python", |
| 45 | + "score": 0.95, |
| 46 | + "line_start": 10, |
| 47 | + "line_end": 20, |
| 48 | + "match_reason": "Semantic match on authentication logic", |
| 49 | + }], |
| 50 | + } |
| 51 | + output = format_search_results(result) |
| 52 | + assert "authenticate" in output |
| 53 | + assert "95% match" in output |
| 54 | + assert "backend/auth.py" in output |
| 55 | + assert "auth.service.authenticate" in output |
| 56 | + assert "Signature" in output |
| 57 | + assert "Why:" in output |
| 58 | + assert "(v2)" in output |
| 59 | + |
| 60 | + def test_cached_flag(self): |
| 61 | + result = {"total": 0, "results": [], "cached": True, "search_version": "v2"} |
| 62 | + output = format_search_results(result) |
| 63 | + assert "(cached)" in output |
| 64 | + |
| 65 | + def test_v1_fallback(self): |
| 66 | + """Formatter handles v1-style response with 'count' field.""" |
| 67 | + result = {"count": 0, "results": []} |
| 68 | + output = format_search_results(result) |
| 69 | + assert "Found 0 results" in output |
| 70 | + assert "(v1)" in output |
| 71 | + |
| 72 | + def test_no_emoji_in_output(self): |
| 73 | + """CLAUDE.md violation check: no emojis anywhere in formatted output.""" |
| 74 | + result = {"total": 1, "cached": True, "search_version": "v2", "results": [{ |
| 75 | + "name": "test", "file_path": "test.py", "code": "pass", |
| 76 | + "language": "python", "score": 0.5, "line_start": 1, "line_end": 1, |
| 77 | + }]} |
| 78 | + output = format_search_results(result) |
| 79 | + # Lightning bolt was the specific emoji found in OPE-91 audit |
| 80 | + assert "\u26a1" not in output |
| 81 | + |
| 82 | + |
| 83 | +# -- Repositories -- |
| 84 | + |
| 85 | +class TestFormatRepositories: |
| 86 | + def test_no_repos(self): |
| 87 | + output = format_repositories({"repositories": []}) |
| 88 | + assert "No repositories indexed" in output |
| 89 | + |
| 90 | + def test_repo_listing(self): |
| 91 | + output = format_repositories({ |
| 92 | + "repositories": [{ |
| 93 | + "id": "abc-123", |
| 94 | + "name": "my-project", |
| 95 | + "status": "indexed", |
| 96 | + "file_count": 1500, |
| 97 | + "branch": "main", |
| 98 | + }] |
| 99 | + }) |
| 100 | + assert "my-project" in output |
| 101 | + assert "abc-123" in output |
| 102 | + assert "indexed" in output |
| 103 | + assert "1,500" in output |
| 104 | + |
| 105 | + |
| 106 | +# -- Dependency graph -- |
| 107 | + |
| 108 | +class TestFormatDependencyGraph: |
| 109 | + def test_empty_graph(self): |
| 110 | + output = format_dependency_graph({"nodes": [], "edges": [], "metrics": {}}) |
| 111 | + assert "Total Files:** 0" in output |
| 112 | + |
| 113 | + def test_critical_files_ranked(self): |
| 114 | + output = format_dependency_graph({ |
| 115 | + "nodes": [{"id": "a.py"}, {"id": "b.py"}], |
| 116 | + "edges": [ |
| 117 | + {"source": "b.py", "target": "a.py"}, |
| 118 | + {"source": "c.py", "target": "a.py"}, |
| 119 | + ], |
| 120 | + "metrics": {"total_edges": 2, "avg_dependencies": 1.0}, |
| 121 | + }) |
| 122 | + assert "a.py" in output |
| 123 | + assert "2 dependents" in output |
| 124 | + |
| 125 | + |
| 126 | +# -- Code style -- |
| 127 | + |
| 128 | +class TestFormatCodeStyle: |
| 129 | + def test_basic_summary(self): |
| 130 | + output = format_code_style({ |
| 131 | + "summary": { |
| 132 | + "total_files_analyzed": 50, |
| 133 | + "total_functions": 200, |
| 134 | + "async_adoption": "35%", |
| 135 | + "type_hints_usage": "80%", |
| 136 | + }, |
| 137 | + }) |
| 138 | + assert "50" in output |
| 139 | + assert "200" in output |
| 140 | + assert "35%" in output |
| 141 | + assert "80%" in output |
| 142 | + |
| 143 | + |
| 144 | +# -- Impact analysis -- |
| 145 | + |
| 146 | +class TestFormatImpactAnalysis: |
| 147 | + def test_high_risk(self): |
| 148 | + output = format_impact_analysis({ |
| 149 | + "file": "core/engine.py", |
| 150 | + "risk_level": "high", |
| 151 | + "impact_summary": "Central dependency", |
| 152 | + "direct_dependencies": ["utils.py"], |
| 153 | + "all_dependents": ["api.py", "cli.py"], |
| 154 | + "test_files": ["test_engine.py"], |
| 155 | + }) |
| 156 | + assert "core/engine.py" in output |
| 157 | + assert "HIGH" in output |
| 158 | + assert "utils.py" in output |
| 159 | + assert "test_engine.py" in output |
| 160 | + |
| 161 | + |
| 162 | +# -- Repository insights -- |
| 163 | + |
| 164 | +class TestFormatRepositoryInsights: |
| 165 | + def test_basic_insights(self): |
| 166 | + output = format_repository_insights({ |
| 167 | + "name": "opencodeintel", |
| 168 | + "status": "indexed", |
| 169 | + "functions_indexed": 500, |
| 170 | + "total_files": 80, |
| 171 | + "total_dependencies": 120, |
| 172 | + }) |
| 173 | + assert "opencodeintel" in output |
| 174 | + assert "500" in output |
| 175 | + |
| 176 | + |
| 177 | +# -- Codebase DNA -- |
| 178 | + |
| 179 | +class TestFormatCodebaseDna: |
| 180 | + def test_dna_output(self): |
| 181 | + output = format_codebase_dna({ |
| 182 | + "dna": "## Patterns\n- Uses FastAPI\n- SQLAlchemy ORM", |
| 183 | + "cached": False, |
| 184 | + }) |
| 185 | + assert "Codebase DNA" in output |
| 186 | + assert "FastAPI" in output |
| 187 | + assert "Follow the auth patterns" in output |
| 188 | + |
| 189 | + def test_dna_cached(self): |
| 190 | + output = format_codebase_dna({"dna": "test", "cached": True}) |
| 191 | + assert "(cached)" in output |
| 192 | + |
| 193 | + def test_no_emoji_in_dna(self): |
| 194 | + output = format_codebase_dna({"dna": "test", "cached": True}) |
| 195 | + assert "\u26a1" not in output |
0 commit comments