Files
graphify/tests/test_affected_cli.py
T
2026-05-22 13:24:54 +01:00

61 lines
2.1 KiB
Python

from __future__ import annotations
import json
import networkx as nx
from networkx.readwrite import json_graph
import graphify.__main__ as mainmod
def _write_graph(tmp_path):
graph = nx.DiGraph()
graph.add_node("target", label="Foo", source_file="pkg/foo.py", source_location="L1")
graph.add_node("caller", label="X()", source_file="app.py", source_location="L4")
graph.add_node("barrel", label="__init__.py", source_file="pkg/__init__.py", source_location=None)
graph.add_node("consumer", label="app.py", source_file="app.py", source_location=None)
graph.add_edge("caller", "target", relation="calls", context="call", confidence="EXTRACTED")
graph.add_edge("barrel", "target", relation="re_exports", context="export", confidence="EXTRACTED")
graph.add_edge("consumer", "target", relation="imports", context="import", confidence="EXTRACTED")
graph_path = tmp_path / "graph.json"
graph_path.write_text(json.dumps(json_graph.node_link_data(graph, edges="links")), encoding="utf-8")
return graph_path
def test_affected_cli_reverse_traverses_impact_edges(monkeypatch, tmp_path, capsys):
graph_path = _write_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr(
mainmod.sys,
"argv",
["graphify", "affected", "Foo", "--graph", str(graph_path)],
)
mainmod.main()
out = capsys.readouterr().out
assert "Affected nodes for Foo" in out
assert "X()" in out
assert "calls" in out
assert "__init__.py" in out
assert "re_exports" in out
assert "app.py" in out
assert "imports" in out
def test_affected_cli_relation_filter_limits_reverse_traversal(monkeypatch, tmp_path, capsys):
graph_path = _write_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr(
mainmod.sys,
"argv",
["graphify", "affected", "Foo", "--relation", "calls", "--graph", str(graph_path)],
)
mainmod.main()
out = capsys.readouterr().out
assert "Relations: calls" in out
assert "X()" in out
assert "__init__.py" not in out