|
| 1 | +"""Call-site rendering in the relationships script. |
| 2 | +
|
| 3 | +The failure mode is silent in both directions: a renderer that drops positions produces output that |
| 4 | +looks perfectly normal, and one that prints a heading for an unknown position tells the agent the |
| 5 | +call happens nowhere. Both are asserted here. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import importlib.util |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 15 | +SCRIPTS_ROOT = REPO_ROOT / "skills" / "codealive-context-engine" / "scripts" |
| 16 | + |
| 17 | + |
| 18 | +def _load_relationships(): |
| 19 | + spec = importlib.util.spec_from_file_location( |
| 20 | + "codealive_relationships_call_sites", SCRIPTS_ROOT / "relationships.py" |
| 21 | + ) |
| 22 | + module = importlib.util.module_from_spec(spec) |
| 23 | + assert spec.loader is not None |
| 24 | + spec.loader.exec_module(module) |
| 25 | + return module |
| 26 | + |
| 27 | + |
| 28 | +relationships = _load_relationships() |
| 29 | + |
| 30 | + |
| 31 | +def _item(**overrides): |
| 32 | + item = { |
| 33 | + "identifier": "my-org/backend::src/db.py::query", |
| 34 | + "filePath": "src/db.py", |
| 35 | + "startLine": 42, |
| 36 | + } |
| 37 | + item.update(overrides) |
| 38 | + return item |
| 39 | + |
| 40 | + |
| 41 | +def test_known_positions_are_rendered_as_call_sites() -> None: |
| 42 | + # Arrange |
| 43 | + item = _item( |
| 44 | + callSites=[{"filePath": "src/svc.py", "line": 17}], |
| 45 | + callSiteCount=1, |
| 46 | + ) |
| 47 | + |
| 48 | + # Act |
| 49 | + lines = relationships._format_call_sites(item) |
| 50 | + |
| 51 | + # Assert |
| 52 | + assert lines == [" ↪ called at src/svc.py:17"] |
| 53 | + |
| 54 | + |
| 55 | +def test_confidence_is_shown_only_when_the_position_is_approximate() -> None: |
| 56 | + # Arrange |
| 57 | + item = _item( |
| 58 | + callSites=[ |
| 59 | + {"filePath": "src/svc.py", "line": 17}, |
| 60 | + {"filePath": "src/svc.py", "line": 88, "confidence": 0.6}, |
| 61 | + ], |
| 62 | + callSiteCount=2, |
| 63 | + ) |
| 64 | + |
| 65 | + # Act |
| 66 | + lines = relationships._format_call_sites(item) |
| 67 | + |
| 68 | + # Assert |
| 69 | + assert lines[0] == " ↪ called at src/svc.py:17" |
| 70 | + assert lines[1] == " ↪ called at src/svc.py:88 (~60% confident)" |
| 71 | + |
| 72 | + |
| 73 | +def test_a_capped_list_says_how_many_were_withheld() -> None: |
| 74 | + # Arrange |
| 75 | + item = _item( |
| 76 | + callSites=[{"filePath": "src/svc.py", "line": 17}], |
| 77 | + callSiteCount=4, |
| 78 | + ) |
| 79 | + |
| 80 | + # Act |
| 81 | + lines = relationships._format_call_sites(item) |
| 82 | + |
| 83 | + # Assert |
| 84 | + assert lines[-1] == " ↪ … 3 more call site(s) not shown" |
| 85 | + |
| 86 | + |
| 87 | +def test_an_unindexed_position_renders_nothing_at_all() -> None: |
| 88 | + # An empty heading here would read as "this call happens nowhere", which is the opposite of |
| 89 | + # what a missing position means. |
| 90 | + # Arrange |
| 91 | + without_key = _item() |
| 92 | + explicit_empty = _item(callSites=[], callSiteCount=0) |
| 93 | + |
| 94 | + # Act |
| 95 | + from_missing = relationships._format_call_sites(without_key) |
| 96 | + from_empty = relationships._format_call_sites(explicit_empty) |
| 97 | + |
| 98 | + # Assert |
| 99 | + assert from_missing == [] |
| 100 | + assert from_empty == [] |
0 commit comments