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
fix(tests): list tests that reach a file only through a hub after the rest (#4333)
Raghav Chamadiyaef021fc5fc38
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.pyindex a46e64489..8d557ef2a 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py@@ -18,7 +18,6 @@ from typing import Any, Literalfrom repowise.core.analysis.function_test_map import MappedTestfrom repowise.core.analysis.health.grading import TARGET_SCORE-from repowise.core.analysis.health.perf.ranking import _percentile_thresholdfrom repowise.core.analysis.health.queue_rules import FilterRulefrom repowise.core.analysis.health.refactoring_summary import STRUCTURAL_TYPESfrom repowise.core.analysis.health.rows import field as row_field@@ -30,6 +29,7 @@ from repowise.core.analysis.health.worth import (worst_after,)from repowise.core.analysis.pr_blast import rank_tests_by_reach+from repowise.core.analysis.test_reach.hubs import hub_filesfrom repowise.core.analysis.test_reachability import (DEFAULT_CALL_DEPTH,MAX_TESTS_PER_TARGET,@@ -371,30 +371,6 @@ class ValidationInputs:evidence: ValidationEvidence = field(default_factory=ValidationEvidence)-# A hub is a module most of the code imports: fan-in above the absolute bar, or-# in the top 1% of the repository's files and at least the floor. Far stricter-# than ``PerfRanker``'s top-quintile "central": that marks a function worth a-# performance claim, while a hub's reach is dropped from validation, so it must-# be a module most tests would pass through. Fan-in counts every importer,-# tests included, as ``graph_metrics.in_degree`` stores it.-HUB_MIN_FAN_IN = 50-HUB_TOP_PERCENTILE = 0.99-HUB_FAN_IN_FLOOR = 10---def hub_files(in_degree: Mapping[str, float], test_files: Collection[str]) -> frozenset[str]:- """The repository's own non-test files that are hubs by stored fan-in."""- own = {- path: int(fan_in or 0)- for path, fan_in in in_degree.items()- if not path.startswith("external:") and path not in test_files- }- bar = max(HUB_FAN_IN_FLOOR, _percentile_threshold(list(own.values()), HUB_TOP_PERCENTILE))- return frozenset(- path for path, fan_in in own.items() if fan_in > HUB_MIN_FAN_IN or fan_in >= bar- )--def _siblings(paths: Collection[str]) -> dict[str, tuple[str, ...]]:by_dir: dict[str, list[str]] = {}for path in paths:diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/hints.py b/packages/core/src/repowise/core/analysis/patch_coverage/hints.pyindex 53b3e891b..b52632b75 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/hints.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/hints.py@@ -339,7 +339,7 @@ async def _walk_tests(symbol, then the import tier for only the files a range still needs itfor. The test-file set is read once for both."""- from ..test_reachability import load_test_files, tests_reaching_by_tier+ from ..test_reachability import cached_hub_files, load_test_files, tests_reaching_by_tiersymbol_ids = sorted({s.symbol_id for syms in placed.values() for s in syms if s})[:MAX_SEEDS]test_files = await load_test_files(session, repository_id)@@ -353,6 +353,7 @@ async def _walk_tests(import_depth=0,symbol_seeds={sid: {sid} for sid in symbol_ids},test_files=test_files,+ hubs=await cached_hub_files(session, repository_id),))unanswered = [diff --git a/packages/core/src/repowise/core/analysis/test_impact.py b/packages/core/src/repowise/core/analysis/test_impact.pyindex 169b3624a..ca223f696 100644--- a/packages/core/src/repowise/core/analysis/test_impact.py+++ b/packages/core/src/repowise/core/analysis/test_impact.py@@ -148,14 +148,22 @@ async def _read_inferred(session: AsyncSession, repository_id: str, changed: list[str]) -> tuple[dict[str, dict[str, Any]], set[str], str | None]:"""The tests the graph walk reaches per changed path, the test files, and the walk error."""- from repowise.core.analysis.test_reachability import load_test_files, tests_reaching_by_tier+ from repowise.core.analysis.test_reachability import (+ cached_hub_files,+ load_test_files,+ tests_reaching_by_tier,+ )inference_error: str | None = Nonetest_files: set[str] = set()try:test_files = await load_test_files(session, repository_id)reached_by_file = await tests_reaching_by_tier(- session, repository_id, changed, test_files=test_files+ session,+ repository_id,+ changed,+ test_files=test_files,+ hubs=await cached_hub_files(session, repository_id),)except Exception as exc:reached_by_file = {}@@ -165,6 +173,7 @@ async def _read_inferred("tests": list(reached.all_tests or reached.tests),"via": reached.via,"reach": reached.reach,+ "transitive": reached.transitive,}for path, reached in reached_by_file.items()}@@ -212,11 +221,12 @@ def assemble_test_impact(to ``{"tests": [...], "via": tier}``, both keys required, with ``tests`` theuncapped list (``ReachedBy.all_tests``, not the display-capped ``tests``), and``narrowed`` true when a reached conftest was already resolved to its tests,- and optional ``reach`` (``ReachedBy.reach``); paths outside *changed_files*- are ignored. That list is used in the walk's order, except where a scope- file expanded (below), and inferred recommendations keep that order.- *coverage_summary* carries ``pair_count``, ``test_count``, ``source_file_count``, ``ingested_at``,- ``source_format`` and ``ingested_commit_sha``. The ``*_error`` arguments+ and optional ``reach`` and ``transitive`` (from ``ReachedBy``); paths+ outside *changed_files* are ignored. That list is used in the walk's order,+ except where a scope file expanded (below), and inferred recommendations+ keep that order. *coverage_summary* carries ``pair_count``, ``test_count``,+ ``source_file_count``, ``ingested_at``, ``source_format`` and+ ``ingested_commit_sha``. The ``*_error`` argumentsare the exception type names of a failed read; each marks the analysis degraded.*changed_files* is filtered by *exclude_spec* here too.@@ -335,7 +345,11 @@ def assemble_test_impact(# A root conftest stands for every test in the repository: rank the# expansion nearest-first and keep the walk's own per-target cap.kept_tests = rank_reaching(- path, kept_tests, reach=reached.get("reach"), changed=changed+ path,+ kept_tests,+ reach=reached.get("reach"),+ changed=changed,+ transitive=reached.get("transitive") or (),)kept_tests = kept_tests[:MAX_TESTS_PER_TARGET]for place, test_id in enumerate(kept_tests):diff --git a/packages/core/src/repowise/core/analysis/test_reach/__init__.py b/packages/core/src/repowise/core/analysis/test_reach/__init__.pyindex 70c68240c..5e351af84 100644--- a/packages/core/src/repowise/core/analysis/test_reach/__init__.py+++ b/packages/core/src/repowise/core/analysis/test_reach/__init__.py@@ -158,6 +158,7 @@ from .db import (always_run_test_files,any_tests_reaching,cached_files_reached_by_tests,+ cached_hub_files,cached_test_files,call_graph_from_db,clear_test_map_cache,@@ -181,12 +182,14 @@ from .forward import (files_reached_by_tests,files_with_paired_tests,)+from .hubs import call_hub_files, hub_filesfrom .inline import go_inline_tests, inline_tests, rust_has_inline_testsfrom .maps import ImportGraph, WalkMaps, build_walk_mapsfrom .model import (DEFAULT_CALL_DEPTH,DEFAULT_MAX_DEPTH,MAX_TESTS_PER_TARGET,+ TRANSITIVE_HOPS,UNRELIABLE_CALL_ORIGINS,ReachDistance,ReachedBy,@@ -204,6 +207,7 @@ __all__ = ["DEFAULT_CALL_DEPTH","DEFAULT_MAX_DEPTH","MAX_TESTS_PER_TARGET",+ "TRANSITIVE_HOPS","UNRELIABLE_CALL_ORIGINS","CallGraphView","ImportGraph",@@ -217,9 +221,11 @@ __all__ = ["any_tests_reaching","build_walk_maps","cached_files_reached_by_tests",+ "cached_hub_files","cached_test_files","call_graph_from_db","call_graph_from_graph",+ "call_hub_files","clear_test_map_cache","dependency_path","direct_dependents",@@ -227,6 +233,7 @@ __all__ = ["files_reached_by_tests","files_with_paired_tests","go_inline_tests",+ "hub_files","imported_names_by_test","inline_tests","is_runnable_test",diff --git a/packages/core/src/repowise/core/analysis/test_reach/db.py b/packages/core/src/repowise/core/analysis/test_reach/db.pyindex 6674d4113..a09db5dc5 100644--- a/packages/core/src/repowise/core/analysis/test_reach/db.py+++ b/packages/core/src/repowise/core/analysis/test_reach/db.py@@ -23,6 +23,7 @@ from repowise.core.persistence.index_cache import IndexStampCachefrom repowise.core.persistence.models import GraphNodefrom .forward import CallGraphView, files_reached_by_tests+from .hubs import call_hub_filesfrom .maps import LEGACY_CONFTEST_HINT, ImportGraph, Need, Walkfrom .model import (DEFAULT_CALL_DEPTH,@@ -144,6 +145,27 @@ async def cached_test_files(session: AsyncSession, repo_id: str) -> frozenset[streturn _cache.remember(key, frozenset(await load_test_files(session, repo_id)))+async def cached_hub_files(session: AsyncSession, repo_id: str) -> frozenset[str]:+ """:func:`~.hubs.call_hub_files` by the stored call edges, read once per index state."""+ key = await _cache.key(session, repo_id, "hubs")+ if key in _cache:+ return _cache.get(key)+ params: dict[str, Any] = {"repo_id": repo_id}+ ets = _in_clause("e", sorted(EXECUTION_EDGE_TYPES), params)+ bad = _in_clause("o", sorted(UNRELIABLE_CALL_ORIGINS), params)+ rows = await session.execute(+ text(+ "SELECT target_node_id, COUNT(DISTINCT source_node_id) FROM graph_edges "+ f"WHERE repository_id = :repo_id AND edge_type IN ({ets}) "+ f"AND (resolution_origin IS NULL OR resolution_origin NOT IN ({bad})) "+ "GROUP BY target_node_id"+ ),+ params,+ )+ test_files = await cached_test_files(session, repo_id)+ return _cache.remember(key, call_hub_files(dict(rows.all()), test_files))++async def cached_files_reached_by_tests(session: AsyncSession, repo_id: str) -> frozenset[str]:""":func:`files_reached_by_tests` over the stored graph, walked once per index state.@@ -332,9 +354,13 @@ async def tests_reaching_by_tier(test_files: set[str] | None = None,avoid: Collection[str] = frozenset(),import_graph: ImportGraph | None = None,+ hubs: Collection[str] = frozenset(),) -> dict[str, ReachedBy]:""":func:`tests_reaching`, also saying which tier answered each target.+ *hubs* (:func:`cached_hub_files`) tags the call-tier tests that reach a+ target only through a hub, and lists them last (``ReachedBy.transitive``).+*test_files* is :func:`load_test_files`'s answer, for a caller that walksmore than once and need not read it twice. A target may be a symbol id(``path::name``); its tests are ranked against the file part.@@ -376,6 +402,7 @@ async def tests_reaching_by_tier(import_depth=import_depth,symbol_seeds=symbol_seeds,avoid=avoid,+ hubs=hubs,)return await drive(walk, session, repo_id, import_graph=import_graph)diff --git a/packages/core/src/repowise/core/analysis/test_reach/hubs.py b/packages/core/src/repowise/core/analysis/test_reach/hubs.pynew file mode 100644index 000000000..b3e404320--- /dev/null+++ b/packages/core/src/repowise/core/analysis/test_reach/hubs.py@@ -0,0 +1,48 @@+"""Hub files: modules so widely imported that reaching a file through one says little."""++from __future__ import annotations++from collections.abc import Collection, Mapping++from repowise.core.analysis.execution_graph import file_of_symbol++# A hub is a module most of the code imports: fan-in above the absolute bar, or+# in the top 1% of the repository's files and at least the floor. Far stricter+# than ``PerfRanker``'s top-quintile "central": that marks a function worth a+# performance claim, while a hub is a module most tests would pass through.+# Fan-in counts every importer, tests included, as ``graph_metrics.in_degree``+# stores it.+HUB_MIN_FAN_IN = 50+HUB_TOP_PERCENTILE = 0.99+HUB_FAN_IN_FLOOR = 10+++def hub_files(in_degree: Mapping[str, float], test_files: Collection[str]) -> frozenset[str]:+ """The repository's own non-test files that are hubs by stored fan-in."""+ # Imported here: the health package imports this one, so a module-level+ # import would be a cycle.+ from repowise.core.analysis.health.perf.ranking import _percentile_threshold++ own = {+ path: int(fan_in or 0)+ for path, fan_in in in_degree.items()+ if not path.startswith("external:") and path not in test_files+ }+ bar = max(HUB_FAN_IN_FLOOR, _percentile_threshold(list(own.values()), HUB_TOP_PERCENTILE))+ return frozenset(+ path for path, fan_in in own.items() if fan_in > HUB_MIN_FAN_IN or fan_in >= bar+ )+++def call_hub_files(+ call_in_degree: Mapping[str, float], test_files: Collection[str]+) -> frozenset[str]:+ """Files declaring a symbol whose call fan-in clears the hub bar.++ *call_in_degree* is each symbol's count of distinct reliable callers, tests+ included (``ExecutionGraphIndex.in_degree``). A call walk passes through+ symbols, so this is the hub that matters to it: an entry point most tests+ call reaches deep into the code for all of them.+ """+ own = {s: n for s, n in call_in_degree.items() if file_of_symbol(s) not in test_files}+ return frozenset(file_of_symbol(s) for s in hub_files(own, test_files))diff --git a/packages/core/src/repowise/core/analysis/test_reach/maps.py b/packages/core/src/repowise/core/analysis/test_reach/maps.pyindex a06b16b5d..68f0dc0a7 100644--- a/packages/core/src/repowise/core/analysis/test_reach/maps.py+++ b/packages/core/src/repowise/core/analysis/test_reach/maps.py@@ -15,6 +15,7 @@ from typing import Any, Literal, NamedTuple, TypeVarfrom repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY_EDGE_TYPES+from .hubs import call_hub_filesfrom .model import UNRELIABLE_CALL_ORIGINSNeedKind = Literal["declared", "callers", "importers", "known"]@@ -99,6 +100,10 @@ class WalkMaps:files: frozenset[str]test_files: frozenset[str]+ def hubs(self) -> frozenset[str]:+ """:func:`~.hubs.call_hub_files` by these maps' call fan-in."""+ return call_hub_files({n: len(c) for n, c in self.callers.items()}, self.test_files)+def answer(self, need: Need) -> Any:if need.kind == "declared":return [(f, s) for f in need.keys for s in self.declares.get(f, ())]diff --git a/packages/core/src/repowise/core/analysis/test_reach/model.py b/packages/core/src/repowise/core/analysis/test_reach/model.pyindex ea3c90615..b3748e2d7 100644--- a/packages/core/src/repowise/core/analysis/test_reach/model.py+++ b/packages/core/src/repowise/core/analysis/test_reach/model.py@@ -26,6 +26,11 @@ UNRELIABLE_CALL_ORIGINS = UNRELIABLE_EXECUTION_ORIGINS# suite cannot produce an unbounded intermediate.MAX_TESTS_PER_TARGET = 50+# A call-tier test this many hops out, from one test function, through a hub+# (:func:`~.hubs.call_hub_files`), reaches the target the way every test does: by+# calling into the hub. Such a row is tagged ``transitive``.+TRANSITIVE_HOPS = 3+# Which tier answered. The CLI prints this so a reader can tell "this test runs# into the file" from the weaker "this test imports it".# ``name-match`` is weaker still: a test named for the file, with no edge.@@ -74,6 +79,10 @@ class ReachedBy:# Import tier only: the files each test was reached through, i.e. the# dependencies of that test on the route to the target.entries: Mapping[str, frozenset[str]] | None = None+ # Call tier, when the walk was told the hubs: tests that reach the target+ # only through a hub, at ``TRANSITIVE_HOPS`` or more from one test function.+ # Listed after every other test; still reaching, so never dropped.+ transitive: frozenset[str] = frozenset()@dataclass(frozen=True)@@ -93,11 +102,22 @@ def capped(direct: Collection[str] = frozenset(),changed: Collection[str] = frozenset(),entries: Mapping[str, frozenset[str]] | None = None,+ transitive: Collection[str] = frozenset(),) -> ReachedBy:"""*tests* ranked by :func:`rank_reaching`, then cut to ``MAX_TESTS_PER_TARGET``."""- ordered = tuple(rank_reaching(target, tests, reach=reach, direct=direct, changed=changed))+ ordered = tuple(+ rank_reaching(+ target, tests, reach=reach, direct=direct, changed=changed, transitive=transitive+ )+ )return ReachedBy(- list(ordered[:MAX_TESTS_PER_TARGET]), via, len(ordered), ordered, reach, entries+ list(ordered[:MAX_TESTS_PER_TARGET]),+ via,+ len(ordered),+ ordered,+ reach,+ entries,+ frozenset(transitive),)@@ -108,10 +128,12 @@ def rank_reaching(reach: Mapping[str, ReachDistance] | None = None,direct: Collection[str] = frozenset(),changed: Collection[str] = frozenset(),+ transitive: Collection[str] = frozenset(),) -> list[str]:"""*tests* reaching *target*, the likeliest to exercise it first. Cap after this.- In turn: a test in *changed* (the change edits it), fewer call hops+ In turn: a test in *changed* (the change edits it), a test not in+ *transitive* (one reaching only through a hub), fewer call hops(*reach*; a test without one sorts after every test with one), a test in*direct* (it imports or calls *target* itself), more of the test'sfunctions at that distance, then by name: the test named for *target*,@@ -124,11 +146,12 @@ def rank_reaching(reach = reach or {}edited = {c.split("::", 1)[0] for c in changed}- def key(test: str) -> tuple[bool, int, bool, int, bool, int, str]:+ def key(test: str) -> tuple[bool, bool, int, bool, int, bool, int, str]:path = test.split("::", 1)[0]distance = reach.get(test) or reach.get(path)return (path not in edited,+ test in transitive or path in transitive,distance.hops if distance else sys.maxsize,test not in direct and path not in direct,-distance.callers if distance else 0,diff --git a/packages/core/src/repowise/core/analysis/test_reach/walk.py b/packages/core/src/repowise/core/analysis/test_reach/walk.pyindex d713869f2..538c5ccb8 100644--- a/packages/core/src/repowise/core/analysis/test_reach/walk.py+++ b/packages/core/src/repowise/core/analysis/test_reach/walk.py@@ -22,6 +22,7 @@ from .maps import Need, Walk, WalkMaps, runfrom .model import (DEFAULT_CALL_DEPTH,DEFAULT_MAX_DEPTH,+ TRANSITIVE_HOPS,ReachDistance,ReachedBy,ReachRow,@@ -248,12 +249,17 @@ def tier_walk(symbol_seeds: Mapping[str, Collection[str]] | None = None,avoid: Collection[str] = frozenset(),helpers: bool = False,+ hubs: Collection[str] = frozenset(),) -> Walk[dict[str, ReachedBy]]:"""The call walk, then the import walk for what it left, then (*helpers*) the helper hop.Each tier is seeded with only the targets the stronger ones leftunanswered, so a weaker tier never speaks over a stronger one. The targetsare usually a change's files, so a test among them leads every list.++ With *hubs*, a call-tier test is tagged ``transitive`` when it reaches the+ target only through a hub (:func:`transitive_tests`). A listing passes+ them; a selection does not, since a tagged test still runs the target."""seeds = sorted({t for t in targets if t})if not seeds or not test_files:@@ -263,8 +269,20 @@ def tier_walk(found = yield from call_walk(seeds, test_files, call_depth, symbol_seeds=symbol_seeds, avoid=avoid)+ tagged: dict[str, set[str]] = {}+ if hubs:+ tagged = yield from transitive_tests(+ found, test_files, call_depth, symbol_seeds=symbol_seeds, avoid=avoid, hubs=hubs+ )for seed, reach in found.items():- out[seed] = capped(seed, reach, "call-graph", reach=reach, changed=seeds)+ out[seed] = capped(+ seed,+ reach,+ "call-graph",+ reach=reach,+ changed=seeds,+ transitive=tagged.get(seed, ()),+ )unanswered = [seed for seed in seeds if seed not in out]if unanswered and import_depth >= 1:found, entries = yield from import_walk(unanswered, test_files, import_depth)@@ -282,6 +300,40 @@ def tier_walk(return out+def transitive_tests(+ found: Mapping[str, Mapping[str, ReachDistance]],+ test_files: Collection[str],+ max_depth: int,+ *,+ symbol_seeds: Mapping[str, Collection[str]] | None = None,+ avoid: Collection[str] = frozenset(),+ hubs: Collection[str],+) -> Walk[dict[str, set[str]]]:+ """``{seed: tests reaching it only through a hub}``, among *found*'s far, single-caller tests.++ A candidate is ``TRANSITIVE_HOPS`` or more calls out from one test+ function. The call walk runs again for the seeds with candidates, with+ *hubs* added to *avoid*; a candidate it no longer finds had no route that+ skips a hub. A target that is a hub itself still carries its own seeds.+ """+ candidates = {+ seed: {t for t, d in reach.items() if d.hops >= TRANSITIVE_HOPS and d.callers == 1}+ for seed, reach in found.items()+ }+ candidates = {seed: tests for seed, tests in candidates.items() if tests}+ if not candidates:+ return {}+ around = yield from call_walk(+ sorted(candidates),+ test_files,+ max_depth,+ symbol_seeds=symbol_seeds,+ avoid={*avoid, *hubs},+ )+ tagged = {seed: tests - set(around.get(seed, ())) for seed, tests in candidates.items()}+ return {seed: tests for seed, tests in tagged.items() if tests}++def reaching_by_tier(maps: WalkMaps,targets: Collection[str],@@ -291,6 +343,7 @@ def reaching_by_tier(symbol_seeds: Mapping[str, Collection[str]] | None = None,avoid: Collection[str] = frozenset(),helpers: bool = True,+ hubs: Collection[str] = frozenset(),) -> dict[str, ReachedBy]:"""Test files reaching each of *targets*, and which tier found them: :func:`tier_walk`.@@ -307,6 +360,7 @@ def reaching_by_tier(symbol_seeds=symbol_seeds,avoid=avoid,helpers=helpers,+ hubs=hubs,),maps,)diff --git a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py b/packages/server/src/repowise/server/routers/code_health/coverage_routes.pyindex 93f8b929b..1dd58e16a 100644--- a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py+++ b/packages/server/src/repowise/server/routers/code_health/coverage_routes.py@@ -22,6 +22,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom repowise.core.analysis.test_reachability import (cached_files_reached_by_tests,+ cached_hub_files,cached_test_files,tests_reaching_by_tier,)@@ -330,7 +331,9 @@ async def health_tests_reaching("truncated": False,}try:- found = await tests_reaching_by_tier(session, repo_id, [file_path])+ # A listing: tests that reach the file only through a hub go last.+ hubs = await cached_hub_files(session, repo_id)+ found = await tests_reaching_by_tier(session, repo_id, [file_path], hubs=hubs)except Exception:return emptyreached = found.get(file_path)diff --git a/tests/unit/analysis/test_test_reach_api.py b/tests/unit/analysis/test_test_reach_api.pyindex 55e302e68..5b661e0b9 100644--- a/tests/unit/analysis/test_test_reach_api.py+++ b/tests/unit/analysis/test_test_reach_api.py@@ -334,3 +334,101 @@ def test_runnable_tests_stand_on_the_stored_flag_and_leave_support_out() -> Noneassert not is_runnable_test("tests/test_a.py", is_test=False)assert not is_runnable_test("tests/conftest.py", is_test=True)assert not is_runnable_test("tests/fixtures/data.py", is_test=True)+++# ---------------------------------------------------------------------------+# Listings: a test reaching only through a hub is tagged and listed last+# ---------------------------------------------------------------------------+++def _hub_graph() -> nx.MultiDiGraph:+ """``src/t.py::f``: one direct test, one 3 calls out through a plain chain,+ one 3 calls out through ``src/hub.py``, and one with two such callers."""+ files = ["src/t.py", "src/x.py", "src/y.py", "src/hub.py"]+ tests = [+ "tests/test_a_hub.py",+ "tests/test_b_chain.py",+ "tests/test_c_two.py",+ "tests/test_t.py",+ ]+ edges = [(p, f"{p}::f", "defines") for p in files]+ edges += [(t, f"{t}::t", "defines") for t in tests]+ edges += [("tests/test_c_two.py", "tests/test_c_two.py::u", "defines")]+ calls = [+ ("tests/test_t.py::t", "src/t.py::f"),+ ("src/hub.py::f", "src/t.py::f"),+ ("src/x.py::f", "src/hub.py::f"),+ ("tests/test_a_hub.py::t", "src/x.py::f"),+ ("src/y.py::f", "src/t.py::f"),+ ("src/y.py::g", "src/y.py::f"),+ ("tests/test_b_chain.py::t", "src/y.py::g"),+ ("tests/test_c_two.py::t", "src/x.py::f"),+ ("tests/test_c_two.py::u", "src/x.py::f"),+ ]+ edges += [("src/y.py", "src/y.py::g", "defines")]+ edges += [(a, b, "calls") for a, b in calls]+ return _graph({**dict.fromkeys(files, False), **dict.fromkeys(tests, True)}, edges)+++def test_a_test_reaching_only_through_a_hub_is_tagged_and_listed_last() -> None:+ maps = build_walk_maps(_hub_graph())+ plain = reaching_by_tier(maps, ["src/t.py"])["src/t.py"]+ assert plain.transitive == frozenset()+ listed = reaching_by_tier(maps, ["src/t.py"], hubs={"src/hub.py"})["src/t.py"]+ assert listed.transitive == {"tests/test_a_hub.py"}+ assert listed.tests == [+ "tests/test_t.py",+ # Through the hub too, but from two test functions: not tagged.+ "tests/test_c_two.py",+ "tests/test_b_chain.py",+ "tests/test_a_hub.py",+ ]+ # Tagged, not dropped: it still runs the target.+ assert listed.total == plain.total == 4+++def test_a_call_hub_is_a_file_declaring_a_symbol_most_callers_call() -> None:+ from repowise.core.analysis.test_reach import call_hub_files++ fan_in = {"src/walk.py::Walker::run": 60, "src/leaf.py::f": 2, "tests/test_x.py::helper": 90}+ assert call_hub_files(fan_in, {"tests/test_x.py"}) == {"src/walk.py"}+ tests = [f"tests/test_{i:02}.py" for i in range(60)]+ edges = [(t, f"{t}::t", "defines") for t in tests]+ edges += [("src/walk.py", "src/walk.py::run", "defines")]+ edges += [(f"{t}::t", "src/walk.py::run", "calls") for t in tests]+ g = _graph({**dict.fromkeys(tests, True), "src/walk.py": False}, edges)+ assert build_walk_maps(g).hubs() == {"src/walk.py"}+++def test_a_target_that_is_a_hub_still_carries_its_own_symbols() -> None:+ """Seeded at ``t.py::f``, a route in through ``t.py::g`` passes through the+ target's own file, which is a hub here; that is not a route through a hub."""+ g = _hub_graph()+ g.add_node("src/t.py::g", node_type="symbol")+ g.add_edge("src/t.py", "src/t.py::g", edge_type="defines")+ g.add_edge("src/t.py::g", "src/t.py::f", edge_type="calls")+ g.add_node("src/z.py", node_type="file", is_test=False)+ g.add_node("src/z.py::f", node_type="symbol")+ g.add_edge("src/z.py", "src/z.py::f", edge_type="defines")+ g.add_edge("src/z.py::f", "src/t.py::g", edge_type="calls")+ g.add_node("tests/test_d_own.py", node_type="file", is_test=True)+ g.add_node("tests/test_d_own.py::t", node_type="symbol")+ g.add_edge("tests/test_d_own.py", "tests/test_d_own.py::t", edge_type="defines")+ g.add_edge("tests/test_d_own.py::t", "src/z.py::f", edge_type="calls")+ maps = build_walk_maps(g)+ sid = "src/t.py::f"+ listed = reaching_by_tier(+ maps, [sid], symbol_seeds={sid: {sid}}, hubs={"src/hub.py", "src/t.py"}+ )[sid]+ assert listed.reach["tests/test_d_own.py"].hops == 3+ assert listed.transitive == {"tests/test_a_hub.py"}+++def test_a_path_level_transitive_entry_ranks_a_test_id_last() -> None:+ from repowise.core.analysis.test_reach import rank_reaching++ tests = ["tests/test_a.py::t", "tests/test_b.py::t"]+ assert rank_reaching("src/x.py", tests, transitive={"tests/test_a.py"}) == [+ "tests/test_b.py::t",+ "tests/test_a.py::t",+ ]diff --git a/tests/unit/persistence/test_test_reachability_walk.py b/tests/unit/persistence/test_test_reachability_walk.pyindex 119ea2718..905bcf1c8 100644--- a/tests/unit/persistence/test_test_reachability_walk.py+++ b/tests/unit/persistence/test_test_reachability_walk.py@@ -922,3 +922,32 @@ async def test_the_stored_walk_and_the_in_memory_walk_return_the_same_rows(tmp_passert in_memory["app/lonely.py"].reached.via == "changed-test"assert in_memory["app/types.py"].state == "type_only"assert {in_memory[p].state for p in in_memory} >= {"reached", "unreached", "new"}+++async def test_stored_call_hubs_match_the_in_memory_ones(async_session):+ """Distinct reliable callers, tests included; a name-guessed call does not count."""+ import networkx as nx++ from repowise.core.analysis.test_reachability import build_walk_maps, cached_hub_files+ from repowise.core.analysis.test_reachability import clear_test_map_cache as clear++ tests = [f"tests/test_{i:02}.py" for i in range(10)]+ edges = [("src/hub.py", "src/hub.py::run", "defines"), ("src/few.py", "src/few.py::f", "defines")]+ for t in tests:+ edges += [(t, f"{t}::t", "defines"), (f"{t}::t", "src/hub.py::run", "calls")]+ edges += [(f"{t}::t", "src/few.py::f", "calls", "global_unique") for t in tests]+ repo = await insert_repo(async_session)+ await _seed(+ async_session,+ repo.id,+ nodes={**dict.fromkeys(tests, True), "src/hub.py": False, "src/few.py": False},+ edges=edges,+ )+ clear()+ g = nx.MultiDiGraph()+ for t in tests:+ g.add_node(t, node_type="file", is_test=True)+ for src, dst, kind, *origin in edges:+ g.add_edge(src, dst, edge_type=kind, resolution_origin=origin[0] if origin else None)+ stored = await cached_hub_files(async_session, repo.id)+ assert stored == build_walk_maps(g).hubs() == {"src/hub.py"}