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 service-granular system graph — node derivation, typed edges, persistence."""
from __future__ import annotations
from pathlib import Path
from repowise.core.workspace.contracts import Contract, ContractLink
from repowise.core.workspace.cross_repo import (
CrossRepoCoChange,
CrossRepoOverlay,
CrossRepoPackageDep,
)
from repowise.core.workspace.extractors.service_boundary import ServiceBoundary
from repowise.core.workspace.system_graph import (
SYSTEM_GRAPH_FILENAME,
SystemGraph,
build_system_graph,
edge_kind_for_contract_type,
load_system_graph,
save_system_graph,
)
def _provider(repo, cid, ctype="http", file="src/handler.py") -> Contract:
return Contract(
repo=repo,
contract_id=cid,
contract_type=ctype,
role="provider",
file_path=file,
symbol_name="handler",
confidence=0.9,
)
def _consumer(repo, cid, ctype="http", file="src/client.py") -> Contract:
return Contract(
repo=repo,
contract_id=cid,
contract_type=ctype,
role="consumer",
file_path=file,
symbol_name="call",
confidence=0.8,
)
def _link(
cid, p_repo, p_file, c_repo, c_file, ctype="http", match="exact", conf=0.72, strategy=None
) -> ContractLink:
return ContractLink(
contract_id=cid,
contract_type=ctype,
match_type=match,
confidence=conf,
provider_repo=p_repo,
provider_file=p_file,
provider_symbol="handler",
provider_service=None,
consumer_repo=c_repo,
consumer_file=c_file,
consumer_symbol="call",
consumer_service=None,
strategy=strategy,
)
def _nodes_by_id(graph: SystemGraph):
return {n.id: n for n in graph.nodes}
def _edges_by_key(graph: SystemGraph):
return {(e.source, e.target, e.kind): e for e in graph.edges}
# ---------------------------------------------------------------------------
# Node derivation (service granularity — D3)
# ---------------------------------------------------------------------------
def test_empty_graph():
graph = build_system_graph([], [], CrossRepoOverlay(), {})
assert graph.nodes == []
assert graph.edges == []
assert graph.diagnostics.total_providers == 0
def test_repo_without_boundary_collapses_to_one_node():
contracts = [_provider("api", "http::GET::/users", file="app/routes.py")]
graph = build_system_graph(contracts, [], CrossRepoOverlay(), {"api": []})
nodes = _nodes_by_id(graph)
assert set(nodes) == {"api"}
assert nodes["api"].service_path is None
assert nodes["api"].provider_count == 1
assert nodes["api"].name == "api"
def test_monorepo_yields_multiple_service_nodes():
boundaries = {
"mono": [
ServiceBoundary(service_path="services/auth", service_name="auth"),
ServiceBoundary(service_path="services/billing", service_name="billing"),
]
}
contracts = [
_provider("mono", "http::GET::/login", file="services/auth/handler.py"),
_provider("mono", "http::POST::/charge", file="services/billing/handler.py"),
]
graph = build_system_graph(contracts, [], CrossRepoOverlay(), boundaries)
nodes = _nodes_by_id(graph)
assert set(nodes) == {"mono::services/auth", "mono::services/billing"}
assert nodes["mono::services/auth"].name == "auth"
assert nodes["mono::services/auth"].service_path == "services/auth"
assert nodes["mono::services/billing"].name == "billing"
# ---------------------------------------------------------------------------
# Edge kind + direction + match_type
# ---------------------------------------------------------------------------
def test_contract_type_to_edge_kind():
assert edge_kind_for_contract_type("http") == "http"
assert edge_kind_for_contract_type("grpc") == "grpc"
assert edge_kind_for_contract_type("socket") == "socket"
assert edge_kind_for_contract_type("topic") == "event"
assert edge_kind_for_contract_type("unknown") == "unknown"
def test_contract_edge_direction_is_consumer_to_provider():
contracts = [
_provider("api", "http::GET::/users", file="api/h.py"),
_consumer("web", "http::GET::/users", file="web/c.py"),
]
links = [_link("http::GET::/users", "api", "api/h.py", "web", "web/c.py")]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
assert len(graph.edges) == 1
edge = graph.edges[0]
assert edge.source == "web" # consumer depends on / calls...
assert edge.target == "api" # ...the provider
assert edge.kind == "http"
assert edge.match_type == "exact"
assert edge.structural is True
assert edge.contract_refs == ["http::GET::/users"]
def test_topic_contract_becomes_event_edge():
contracts = [
_provider("a", "topic::orders", ctype="topic", file="a/pub.py"),
_consumer("b", "topic::orders", ctype="topic", file="b/sub.py"),
]
links = [_link("topic::orders", "a", "a/pub.py", "b", "b/sub.py", ctype="topic")]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
assert graph.edges[0].kind == "event"
def test_socket_contract_becomes_socket_edge():
contracts = [
_provider("api", "socket::/hubs/game", ctype="socket", file="api/hub.cs"),
_consumer("unity", "socket::/hubs/game", ctype="socket", file="unity/net.cs"),
]
links = [
_link("socket::/hubs/game", "api", "api/hub.cs", "unity", "unity/net.cs", ctype="socket")
]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
assert graph.edges[0].kind == "socket"
def test_parallel_links_aggregate_into_one_weighted_edge():
contracts = [
_provider("api", "http::GET::/a", file="api/h.py"),
_provider("api", "http::GET::/b", file="api/h.py"),
_consumer("web", "http::GET::/a", file="web/c.py"),
_consumer("web", "http::GET::/b", file="web/c.py"),
]
links = [
_link("http::GET::/b", "api", "api/h.py", "web", "web/c.py", match="candidate", conf=0.5,
strategy="prefix_stripped"),
_link("http::GET::/a", "api", "api/h.py", "web", "web/c.py", match="exact", conf=0.9,
strategy="exact_id"),
]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
edges = _edges_by_key(graph)
edge = edges[("web", "api", "http")]
assert edge.weight == 2
assert edge.confidence == 0.9 # max of contributors
assert edge.match_type == "exact" # most authoritative wins
assert edge.strategy == "exact_id" # the strategy behind the edge's confidence
assert set(edge.contract_refs) == {"http::GET::/a", "http::GET::/b"}
# ---------------------------------------------------------------------------
# Package + co-change edges
# ---------------------------------------------------------------------------
def test_package_dep_edge_points_dependent_to_dependency():
overlay = CrossRepoOverlay(
package_deps=[
CrossRepoPackageDep(
source_repo="web",
target_repo="shared",
source_manifest="package.json",
kind="npm_local_path",
)
]
)
graph = build_system_graph([], [], overlay, {})
edges = _edges_by_key(graph)
edge = edges[("web", "shared", "package")]
assert edge.structural is True
assert edge.match_type == "exact"
assert edge.confidence == 1.0
def test_maven_package_edge_keeps_coordinate_evidence():
overlay = CrossRepoOverlay(
package_deps=[
CrossRepoPackageDep(
source_repo="consumer",
target_repo="producer",
source_manifest="app/pom.xml",
target_manifest="shared/pom.xml",
target_package="com.acme:shared",
requested_version="1.2.0",
scope="compile",
resolution_basis="unique_workspace_coordinate",
kind="maven_coordinate",
)
]
)
graph = build_system_graph([], [], overlay, {})
edge = _edges_by_key(graph)[("consumer", "producer", "package")]
assert edge.contract_refs == ["maven_coordinate:com.acme:shared:app/pom.xml"]
def test_cochange_edge_is_behavioral_and_undirected():
overlay = CrossRepoOverlay(
co_changes=[
CrossRepoCoChange(
source_repo="z",
source_file="z/a.py",
target_repo="a",
target_file="a/b.py",
strength=0.6,
frequency=4,
last_date="2026-06-01",
)
]
)
graph = build_system_graph([], [], overlay, {})
assert len(graph.edges) == 1
edge = graph.edges[0]
assert edge.kind == "co_change"
assert edge.structural is False
# Direction canonicalized to (min, max) of node ids.
assert edge.source == "a"
assert edge.target == "z"
# ---------------------------------------------------------------------------
# Orphan + isolation flags
# ---------------------------------------------------------------------------
def test_orphan_provider_and_isolation_flags():
contracts = [_provider("api", "http::GET::/unused", file="api/h.py")]
graph = build_system_graph(contracts, [], CrossRepoOverlay(), {})
node = _nodes_by_id(graph)["api"]
assert node.is_orphan_provider is True
assert node.is_isolated is True
def test_connected_provider_is_not_orphan():
contracts = [
_provider("api", "http::GET::/users", file="api/h.py"),
_consumer("web", "http::GET::/users", file="web/c.py"),
]
links = [_link("http::GET::/users", "api", "api/h.py", "web", "web/c.py")]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
nodes = _nodes_by_id(graph)
assert nodes["api"].is_orphan_provider is False
assert nodes["api"].is_isolated is False
assert nodes["web"].is_orphan_consumer is False
# ---------------------------------------------------------------------------
# Schema shape + persistence (snapshot guard)
# ---------------------------------------------------------------------------
def test_system_graph_json_shape_is_locked():
contracts = [
_provider("api", "http::GET::/users", file="api/h.py"),
_consumer("web", "http::GET::/users", file="web/c.py"),
]
links = [_link("http::GET::/users", "api", "api/h.py", "web", "web/c.py")]
graph = build_system_graph(
contracts, links, CrossRepoOverlay(), {}, version=1, generated_at="t"
)
data = graph.to_dict()
assert set(data) == {
"version",
"generated_at",
"repo_provenance",
"nodes",
"edges",
"diagnostics",
}
assert set(data["nodes"][0]) == {
"id",
"repo",
"service_path",
"name",
"kind",
"provider_count",
"consumer_count",
"contract_types",
"is_orphan_provider",
"is_orphan_consumer",
"is_isolated",
}
assert set(data["edges"][0]) == {
"id",
"source",
"target",
"kind",
"match_type",
"confidence",
"weight",
"structural",
"contract_refs",
"strategy",
"family",
}
assert set(data["diagnostics"]) == {
"total_providers",
"total_consumers",
"total_links",
"weak_link_count",
"repo_breakdown",
"unmatched_consumers",
"unmatched_by_reason",
"orphan_providers",
"in_service_consumers",
"providers_by_layer",
"consumers_by_layer",
"http_consumers_unresolved",
"http_consumer_coverage",
"http_consumers_linked",
"http_link_coverage",
"symbol_identity",
"schema_coverage",
"openapi",
"code_api",
"unrecognised_call_files",
"unrecognised_call_file_count",
"topic_names_unresolved",
"unrecognised_message_files",
"unrecognised_message_file_count",
}
def test_every_edge_kind_has_one_family():
from repowise.core.workspace.system_graph import (
EDGE_FAMILIES,
EDGE_KIND_FAMILY,
EDGE_KINDS,
edge_family,
)
assert set(EDGE_KIND_FAMILY) == set(EDGE_KINDS)
assert set(EDGE_KIND_FAMILY.values()) == set(EDGE_FAMILIES)
assert [edge_family(k) for k in ("http", "db", "package", "co_change")] == [
"runtime",
"data",
"build",
"history",
]
# An unmapped kind is non-structural in the builder, so it is history here.
assert edge_family("carrier_pigeon") == "history"
def test_edges_serialize_their_family():
contracts = [
_provider("api", "http::GET::/users", file="api/h.py"),
_consumer("web", "http::GET::/users", file="web/c.py"),
]
links = [_link("http::GET::/users", "api", "api/h.py", "web", "web/c.py")]
graph = build_system_graph(contracts, links, CrossRepoOverlay(), {})
assert [e.to_dict()["family"] for e in graph.edges] == ["runtime"]
def test_round_trip_serialization():
contracts = [_provider("api", "http::GET::/users", file="api/h.py")]
graph = build_system_graph(contracts, [], CrossRepoOverlay(), {}, generated_at="t")
restored = SystemGraph.from_dict(graph.to_dict())
assert restored.to_dict() == graph.to_dict()
def test_save_and_load(tmp_path: Path):
contracts = [_provider("api", "http::GET::/users", file="api/h.py")]
graph = build_system_graph(contracts, [], CrossRepoOverlay(), {}, generated_at="t")
out = save_system_graph(graph, tmp_path)
assert out.name == SYSTEM_GRAPH_FILENAME
loaded = load_system_graph(tmp_path)
assert loaded is not None
assert loaded.to_dict() == graph.to_dict()
def test_load_missing_returns_none(tmp_path: Path):
assert load_system_graph(tmp_path) is None
def test_adjacency_is_the_one_view_every_walk_reads():
from repowise.core.workspace.system_graph import SystemEdge
def edge(src, tgt, kind, structural=True):
return SystemEdge(
id=f"{src}->{tgt}:{kind}", source=src, target=tgt, kind=kind,
match_type="exact", confidence=1.0, weight=1, structural=structural,
)
http, event, co = edge("a", "b", "http"), edge("a", "b", "event"), edge("b", "c", "co_change", False)
graph = SystemGraph(edges=[http, event, co])
assert graph.adjacency() == {"a": {"b": [http, event]}}
assert graph.adjacency(impact=True) == {"b": {"a": [http, event]}}
assert graph.adjacency(behavioral=True, impact=True) == {
"b": {"a": [http, event], "c": [co]},
"c": {"b": [co]},
}