Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"""Tests for the get_conformance MCP tool + the get_risk conformance directive."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from repowise.core.workspace.config import ConformanceRule
from repowise.core.workspace.conformance import build_conformance_report
from repowise.core.workspace.system_graph import SystemEdge, SystemGraph, SystemNode
from repowise.server.mcp_server import _state
from repowise.server.mcp_server._enrichment import CrossRepoEnricher
from repowise.server.mcp_server.tool_conformance import get_conformance
def _graph() -> SystemGraph:
return SystemGraph(
nodes=[
SystemNode(id="frontend", repo="frontend", service_path=None, name="frontend"),
SystemNode(id="db", repo="db", service_path=None, name="db"),
],
edges=[
SystemEdge(
id="frontend->db:http",
source="frontend",
target="db",
kind="http",
match_type="exact",
confidence=1.0,
weight=1,
structural=True,
),
SystemEdge(
id="db->frontend:http",
source="db",
target="frontend",
kind="http",
match_type="exact",
confidence=1.0,
weight=1,
structural=True,
),
],
)
def _enricher_with_conformance(tmp_path: Path) -> CrossRepoEnricher:
report = build_conformance_report(
_graph(), [ConformanceRule(source="frontend", target="db")], generated_at="t"
)
(tmp_path / "conformance.json").write_text(json.dumps(report.to_dict()), encoding="utf-8")
return CrossRepoEnricher(
tmp_path / "cross_repo_edges.json",
conformance_path=tmp_path / "conformance.json",
)
@pytest.fixture
def workspace_state(tmp_path: Path):
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = _enricher_with_conformance(tmp_path)
try:
yield
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
@pytest.mark.asyncio
async def test_requires_workspace_mode():
prev = _state._registry
_state._registry = None
try:
result = await get_conformance()
finally:
_state._registry = prev
assert "error" in result
assert "workspace mode" in result["error"]
@pytest.mark.asyncio
async def test_reports_violations_and_cycles(workspace_state):
result = await get_conformance()
assert result["violation_count"] == 1
assert result["cycle_count"] == 1
assert result["violations"][0]["source"] == "frontend"
assert "violation" in result["summary"]
@pytest.mark.asyncio
async def test_filter_by_repo(workspace_state):
result = await get_conformance(repo="unrelated")
assert result["violation_count"] == 0
assert result["cycle_count"] == 0
@pytest.mark.asyncio
async def test_no_report_returns_error(tmp_path: Path):
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = CrossRepoEnricher(tmp_path / "cross_repo_edges.json")
try:
result = await get_conformance()
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
assert "error" in result
assert "conformance" in result["error"].lower()
# ---------------------------------------------------------------------------
# get_risk PR-mode conformance directive
# ---------------------------------------------------------------------------
def test_conformance_directive_reports_repo_findings(tmp_path: Path):
from repowise.server.mcp_server.tool_risk import _conformance_directive
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = _enricher_with_conformance(tmp_path)
try:
violations, cycles = _conformance_directive("frontend")
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
assert len(violations) == 1
assert violations[0]["source"] == "frontend"
assert violations[0]["rule"] == "frontend !-> db"
# frontend participates in the frontend<->db cycle.
assert len(cycles) == 1
def test_conformance_directive_empty_for_unrelated_repo(tmp_path: Path):
from repowise.server.mcp_server.tool_risk import _conformance_directive
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = _enricher_with_conformance(tmp_path)
try:
violations, cycles = _conformance_directive("unrelated")
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
assert violations == []
assert cycles == []
def test_conformance_directive_empty_outside_workspace():
from repowise.server.mcp_server.tool_risk import _conformance_directive
prev = _state._registry
_state._registry = None
try:
assert _conformance_directive("frontend") == ([], [])
finally:
_state._registry = prev
@pytest.mark.asyncio
async def test_unstamped_report_is_an_error_not_an_all_clear(tmp_path: Path):
"""A report nothing wrote a result into has zero violations, and saying so
would tell an agent the architecture is clean when nothing checked it."""
report = build_conformance_report(_graph(), [], generated_at="t").to_dict()
report["generated_at"] = ""
report["violations"] = []
report["cycles"] = []
(tmp_path / "conformance.json").write_text(json.dumps(report), encoding="utf-8")
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = CrossRepoEnricher(
tmp_path / "cross_repo_edges.json",
conformance_path=tmp_path / "conformance.json",
)
try:
result = await get_conformance()
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
assert "error" in result
assert "has not run" in result["error"]
assert "violation_count" not in result
@pytest.mark.asyncio
async def test_reports_the_true_cycle_total_and_when_it_checked(workspace_state):
result = await get_conformance()
assert result["total_cycles"] == result["cycle_count"]
assert result["checked_at"] == "t"
def test_conformance_directive_ignores_an_unstamped_report(tmp_path: Path):
"""get_risk's PR directive must not report "no violations" from a report
nothing ever wrote a result into — it would read as an architecture pass."""
from repowise.server.mcp_server.tool_risk import _conformance_directive
report = build_conformance_report(
_graph(), [ConformanceRule(source="frontend", target="db")], generated_at="t"
).to_dict()
report["generated_at"] = ""
(tmp_path / "conformance.json").write_text(json.dumps(report), encoding="utf-8")
enricher = CrossRepoEnricher(
tmp_path / "cross_repo_edges.json",
conformance_path=tmp_path / "conformance.json",
)
assert enricher.get_conformance() is not None, "the report is loaded..."
assert not enricher.has_conformance, "...but it did not run, so it is not evidence"
prev_registry = _state._registry
prev_enricher = _state._cross_repo_enricher
_state._registry = object()
_state._cross_repo_enricher = enricher
try:
assert _conformance_directive("frontend") == ([], [])
finally:
_state._registry = prev_registry
_state._cross_repo_enricher = prev_enricher
@pytest.mark.asyncio
async def test_the_payload_builder_is_the_tool_answer_without_meta(workspace_state):
from repowise.server.mcp_server.tool_conformance import conformance_payload
report = _state._cross_repo_enricher.get_conformance()
for repo in (None, "frontend"):
tool = await get_conformance(repo=repo)
assert {k: v for k, v in tool.items() if k != "_meta"} == conformance_payload(report, repo)
@pytest.mark.asyncio
async def test_the_architecture_payload_is_the_tool_answer_without_meta(workspace_state):
from repowise.server.mcp_server.tool_architecture import architecture_payload, get_architecture
enricher = CrossRepoEnricher.from_data(system_graph={**_graph().to_dict(), "generated_at": "t"})
_state._cross_repo_enricher = enricher
tool = await get_architecture()
assert {k: v for k, v in tool.items() if k != "_meta"} == architecture_payload(
enricher.get_architecture_metrics()
)
assert tool["node_count"] == 2
def test_an_unseen_graph_reads_not_determined_to_an_agent():
"""No edge between services is no score and no "no cycles", with the reason."""
from repowise.core.workspace.architecture_metrics import compute_architecture_metrics
from repowise.server.mcp_server.tool_architecture import architecture_payload
from repowise.server.mcp_server.tool_conformance import conformance_payload
graph = SystemGraph(nodes=[*_graph().nodes], edges=[])
payload = architecture_payload(compute_architecture_metrics(graph).to_dict())
assert payload["score"] is None
assert payload["cycle_count"] is None
assert payload["evidence"]["state"] == "insufficient"
assert payload["summary"].startswith("Architecture not determined for 2 service(s)")
report = conformance_payload(build_conformance_report(graph, []).to_dict())
assert report["cycles_determined"] is False
assert "cycles not determined" in report["summary"]
def test_a_capped_cycle_count_reads_at_least_to_an_agent():
"""When the enumeration budget stopped the count, both tools say "at least"."""
from unittest.mock import patch
import repowise.core.workspace.cycles as cy
from repowise.core.workspace.architecture_metrics import compute_architecture_metrics
from repowise.server.mcp_server.tool_architecture import architecture_payload
from repowise.server.mcp_server.tool_conformance import conformance_payload
names = [f"s{i}" for i in range(5)]
graph = SystemGraph(
nodes=[SystemNode(id=n, repo=n, service_path=None, name=n) for n in names],
edges=[
SystemEdge(
id=f"{s}->{t}:http",
source=s,
target=t,
kind="http",
match_type="exact",
confidence=1.0,
weight=1,
structural=True,
)
for s in names
for t in names
if s != t
],
)
with patch.object(cy, "MAX_ENUMERATED_CYCLES", 30):
report = build_conformance_report(graph, [], generated_at="t").to_dict()
metrics = compute_architecture_metrics(graph).to_dict()
payload = conformance_payload(report)
assert (payload["total_cycles"], payload["total_capped"]) == (30, True)
assert "at least 30 dependency cycle(s)" in payload["summary"]
# A repo-scoped answer counts only what it lists, so it is exact.
assert conformance_payload(report, "s0")["total_capped"] is False
arch = architecture_payload(metrics)
assert (arch["cycle_count"], arch["cycle_count_capped"]) == (30, True)
assert "at least 30 dependency cycle(s)" in arch["summary"]