forked from safishamsi/graphify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_query_cli.py
More file actions
70 lines (58 loc) · 2.46 KB
/
Copy pathtest_query_cli.py
File metadata and controls
70 lines (58 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Tests for graphify query CLI context filtering."""
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):
G = nx.Graph()
G.add_node("n1", label="extract", source_file="extract.py", source_location="L10", community=0)
G.add_node("n2", label="cluster", source_file="cluster.py", source_location="L5", community=0)
G.add_node("n3", label="build", source_file="build.py", source_location="L1", community=1)
G.add_edge("n1", "n2", relation="calls", confidence="EXTRACTED", context="call")
G.add_edge("n2", "n3", relation="imports", confidence="EXTRACTED", context="import")
graph_path = tmp_path / "graph.json"
graph_path.write_text(json.dumps(json_graph.node_link_data(G, edges="links")))
return graph_path
def test_query_cli_explicit_context_filter(monkeypatch, tmp_path, capsys):
graph_path = _write_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr(
mainmod.sys,
"argv",
["graphify", "query", "extract", "--context", "call", "--graph", str(graph_path)],
)
mainmod.main()
out = capsys.readouterr().out
assert "Context: call (explicit)" in out
assert "cluster" in out
assert "build" not in out
def test_query_cli_heuristic_context_filter(monkeypatch, tmp_path, capsys):
graph_path = _write_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr(
mainmod.sys,
"argv",
["graphify", "query", "who calls extract", "--graph", str(graph_path)],
)
mainmod.main()
out = capsys.readouterr().out
assert "Context: call (heuristic)" in out
assert "cluster" in out
assert "build" not in out
def test_query_cli_rejects_oversized_graph(monkeypatch, tmp_path, capsys):
"""#F4: query CLI must refuse to parse a graph.json that exceeds the cap."""
import pytest
graph_path = _write_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr("graphify.security._MAX_GRAPH_FILE_BYTES", 16)
monkeypatch.setattr(
mainmod.sys,
"argv",
["graphify", "query", "extract", "--graph", str(graph_path)],
)
with pytest.raises(SystemExit):
mainmod.main()
err = capsys.readouterr().err
assert "exceeds" in err
assert "byte cap" in err