|
| 1 | +"""T6 — impact_analysis MCP tool tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import uuid |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +pytestmark = pytest.mark.anyio |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def anyio_backend() -> str: |
| 16 | + return "asyncio" |
| 17 | + |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Unit tests — no FalkorDB required |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | + |
| 23 | + |
| 24 | +def test_clamp_depth_normalizes(): |
| 25 | + from api.mcp.tools.structural import _clamp_depth, IMPACT_MAX_DEPTH |
| 26 | + |
| 27 | + assert _clamp_depth(1) == 1 |
| 28 | + assert _clamp_depth(5) == 5 |
| 29 | + assert _clamp_depth(IMPACT_MAX_DEPTH) == IMPACT_MAX_DEPTH |
| 30 | + assert _clamp_depth(999) == IMPACT_MAX_DEPTH |
| 31 | + assert _clamp_depth(0) == 1 |
| 32 | + assert _clamp_depth(-3) == 1 |
| 33 | + assert _clamp_depth("4") == 4 |
| 34 | + assert _clamp_depth("999") == IMPACT_MAX_DEPTH |
| 35 | + |
| 36 | + |
| 37 | +def test_clamp_depth_rejects_garbage(): |
| 38 | + from api.mcp.tools.structural import _clamp_depth |
| 39 | + |
| 40 | + with pytest.raises(ValueError, match="depth"): |
| 41 | + _clamp_depth("not-a-number") |
| 42 | + with pytest.raises(ValueError, match="depth"): |
| 43 | + _clamp_depth(True) # bool would silently pass the int check |
| 44 | + with pytest.raises(ValueError, match="depth"): |
| 45 | + _clamp_depth(None) |
| 46 | + |
| 47 | + |
| 48 | +async def test_impact_analysis_rejects_invalid_direction(): |
| 49 | + from api.mcp.tools.structural import impact_analysis |
| 50 | + |
| 51 | + with pytest.raises(ValueError, match="direction"): |
| 52 | + await impact_analysis( |
| 53 | + symbol_id=1, |
| 54 | + project="any", |
| 55 | + direction="BOTH", |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +async def test_impact_analysis_registered_via_app(): |
| 60 | + from api.mcp.server import app |
| 61 | + |
| 62 | + names = {t.name for t in await app.list_tools()} |
| 63 | + assert "impact_analysis" in names |
| 64 | + |
| 65 | + |
| 66 | +# --------------------------------------------------------------------------- |
| 67 | +# Integration — sample_project fixture (T3) |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | + |
| 70 | + |
| 71 | +async def _find_id(indexed_fixture, name: str) -> int: |
| 72 | + from api.mcp.tools.structural import search_code |
| 73 | + |
| 74 | + rows = await search_code( |
| 75 | + prefix=name, |
| 76 | + project=indexed_fixture.project, |
| 77 | + branch=indexed_fixture.branch, |
| 78 | + ) |
| 79 | + for r in rows: |
| 80 | + if r["name"] == name: |
| 81 | + return r["id"] |
| 82 | + raise AssertionError(f"symbol {name!r} not found") |
| 83 | + |
| 84 | + |
| 85 | +async def test_impact_upstream_of_db(indexed_fixture, expected_contract): |
| 86 | + from api.mcp.tools.structural import impact_analysis |
| 87 | + |
| 88 | + db_id = await _find_id(indexed_fixture, "db") |
| 89 | + upstream = await impact_analysis( |
| 90 | + symbol_id=db_id, |
| 91 | + project=indexed_fixture.project, |
| 92 | + branch=indexed_fixture.branch, |
| 93 | + direction="IN", |
| 94 | + depth=5, |
| 95 | + ) |
| 96 | + names = {r["name"] for r in upstream} |
| 97 | + expected = set( |
| 98 | + expected_contract["impact"]["db"]["upstream_includes_any_of"] |
| 99 | + ) |
| 100 | + assert names & expected, ( |
| 101 | + f"db upstream {names} disjoint from expected {expected}" |
| 102 | + ) |
| 103 | + # DISTINCT enforces no duplicate ids. |
| 104 | + ids = [r["id"] for r in upstream] |
| 105 | + assert len(ids) == len(set(ids)) |
| 106 | + for r in upstream: |
| 107 | + assert r["direction"] == "IN" |
| 108 | + |
| 109 | + |
| 110 | +async def test_impact_downstream_of_entrypoint(indexed_fixture, expected_contract): |
| 111 | + from api.mcp.tools.structural import impact_analysis |
| 112 | + |
| 113 | + entry_id = await _find_id(indexed_fixture, "entrypoint") |
| 114 | + downstream = await impact_analysis( |
| 115 | + symbol_id=entry_id, |
| 116 | + project=indexed_fixture.project, |
| 117 | + branch=indexed_fixture.branch, |
| 118 | + direction="OUT", |
| 119 | + depth=5, |
| 120 | + ) |
| 121 | + names = {r["name"] for r in downstream} |
| 122 | + expected = set( |
| 123 | + expected_contract["impact"]["entrypoint"]["downstream_includes_any_of"] |
| 124 | + ) |
| 125 | + assert names & expected, ( |
| 126 | + f"entrypoint downstream {names} disjoint from expected {expected}" |
| 127 | + ) |
| 128 | + for r in downstream: |
| 129 | + assert r["direction"] == "OUT" |
| 130 | + |
| 131 | + |
| 132 | +async def test_impact_depth_one_only_immediate_callers(indexed_fixture): |
| 133 | + """depth=1 returns only direct callers — sufficient for db's caller chain, |
| 134 | + not transitive ancestors like entrypoint.""" |
| 135 | + from api.mcp.tools.structural import impact_analysis |
| 136 | + |
| 137 | + db_id = await _find_id(indexed_fixture, "db") |
| 138 | + upstream = await impact_analysis( |
| 139 | + symbol_id=db_id, |
| 140 | + project=indexed_fixture.project, |
| 141 | + branch=indexed_fixture.branch, |
| 142 | + direction="IN", |
| 143 | + depth=1, |
| 144 | + ) |
| 145 | + names = {r["name"] for r in upstream} |
| 146 | + # entrypoint is 3 hops away — must NOT appear at depth=1. |
| 147 | + assert "entrypoint" not in names |
| 148 | + |
| 149 | + |
| 150 | +async def test_impact_response_serialisable(indexed_fixture): |
| 151 | + from api.mcp.tools.structural import impact_analysis |
| 152 | + |
| 153 | + entry_id = await _find_id(indexed_fixture, "entrypoint") |
| 154 | + rows = await impact_analysis( |
| 155 | + symbol_id=entry_id, |
| 156 | + project=indexed_fixture.project, |
| 157 | + branch=indexed_fixture.branch, |
| 158 | + direction="OUT", |
| 159 | + depth=3, |
| 160 | + ) |
| 161 | + json.dumps(rows) |
| 162 | + |
| 163 | + |
| 164 | +async def test_impact_respects_limit(indexed_fixture): |
| 165 | + """``limit`` bounds the number of impacted symbols returned.""" |
| 166 | + from api.mcp.tools.structural import impact_analysis |
| 167 | + |
| 168 | + entry_id = await _find_id(indexed_fixture, "entrypoint") |
| 169 | + full = await impact_analysis( |
| 170 | + symbol_id=entry_id, |
| 171 | + project=indexed_fixture.project, |
| 172 | + branch=indexed_fixture.branch, |
| 173 | + direction="OUT", |
| 174 | + depth=5, |
| 175 | + ) |
| 176 | + # entrypoint reaches several downstream symbols; cap to 1 and confirm |
| 177 | + # the result is bounded by the limit. |
| 178 | + assert len(full) > 1, "fixture should expose >1 downstream symbol" |
| 179 | + capped = await impact_analysis( |
| 180 | + symbol_id=entry_id, |
| 181 | + project=indexed_fixture.project, |
| 182 | + branch=indexed_fixture.branch, |
| 183 | + direction="OUT", |
| 184 | + depth=5, |
| 185 | + limit=1, |
| 186 | + ) |
| 187 | + assert len(capped) == 1 |
| 188 | + |
| 189 | + |
| 190 | +# --------------------------------------------------------------------------- |
| 191 | +# Cycle safety — small purpose-built graph |
| 192 | +# --------------------------------------------------------------------------- |
| 193 | + |
| 194 | + |
| 195 | +@pytest.fixture |
| 196 | +async def cycle_graph(): |
| 197 | + """Build a tiny graph with a 2-cycle (A↔B) plus an unrelated C → A edge. |
| 198 | +
|
| 199 | + Created with a unique branch so it's isolated from the shared |
| 200 | + sample-project fixture. Not torn down (matches the pattern used by |
| 201 | + ``indexed_fixture``). |
| 202 | + """ |
| 203 | + from api.graph import Graph |
| 204 | + |
| 205 | + project = "impact_cycle_test" |
| 206 | + branch = f"cycle-{uuid.uuid4().hex[:8]}" |
| 207 | + g = Graph(project, branch=branch) |
| 208 | + # CREATE three Function nodes with name + path; add CALLS edges |
| 209 | + # A → B, B → A (cycle), C → A. |
| 210 | + g.g.query( |
| 211 | + """ |
| 212 | + CREATE |
| 213 | + (a:Function:Searchable {name: 'A', path: '/tmp/cycle.py', src_start: 1}), |
| 214 | + (b:Function:Searchable {name: 'B', path: '/tmp/cycle.py', src_start: 2}), |
| 215 | + (c:Function:Searchable {name: 'C', path: '/tmp/cycle.py', src_start: 3}), |
| 216 | + (a)-[:CALLS]->(b), |
| 217 | + (b)-[:CALLS]->(a), |
| 218 | + (c)-[:CALLS]->(a) |
| 219 | + """ |
| 220 | + ) |
| 221 | + yield project, branch |
| 222 | + |
| 223 | + |
| 224 | +async def test_impact_handles_cycles(cycle_graph): |
| 225 | + """Variable-depth Cypher with DISTINCT must return each node once |
| 226 | + even when the graph has a cycle (A↔B). The traversal is depth-bounded |
| 227 | + (``*1..depth``) so it always terminates; without DISTINCT, though, a |
| 228 | + node reachable via multiple paths would be emitted as duplicate rows.""" |
| 229 | + from api.graph import AsyncGraphQuery |
| 230 | + from api.mcp.tools.structural import impact_analysis |
| 231 | + |
| 232 | + project, branch = cycle_graph |
| 233 | + |
| 234 | + # Resolve A's id via Cypher (no Searchable index in this throwaway graph) |
| 235 | + g = AsyncGraphQuery(project, branch=branch) |
| 236 | + try: |
| 237 | + res = await g._query("MATCH (n:Function {name: 'A'}) RETURN ID(n)") |
| 238 | + a_id = res.result_set[0][0] |
| 239 | + finally: |
| 240 | + await g.close() |
| 241 | + |
| 242 | + upstream = await impact_analysis( |
| 243 | + symbol_id=a_id, |
| 244 | + project=project, |
| 245 | + branch=branch, |
| 246 | + direction="IN", |
| 247 | + depth=5, |
| 248 | + ) |
| 249 | + names = [r["name"] for r in upstream] |
| 250 | + # DISTINCT collapses the A->B->A->B->... cycle into single entries. |
| 251 | + # B (direct caller) and C (direct caller) must both be present; |
| 252 | + # A may also appear because A is reachable from itself through the |
| 253 | + # cycle. The crucial guarantee is no duplicates and no infinite loop. |
| 254 | + assert "B" in names and "C" in names |
| 255 | + assert len(names) == len(set(names)), f"duplicates in {names}" |
0 commit comments