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): never call a file of only types untested (#4337)
Raghav Chamadiya80092d056937
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py b/packages/core/src/repowise/core/analysis/health/biomarkers/base.pyindex 52a637868..88495480a 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py@@ -75,6 +75,9 @@ class FileContext:coverable_lines: set[int] = field(default_factory=set)# Only a docstring, imports and export lists: no code for coverage to measure.reexport_only: bool = False+ # Declares only types (``test_reach.is_type_only``): nothing for a test to run.+ # Read only for a hotspot, the one file a missing test is claimed for.+ type_only: bool = False# Duplication signals (populated when the engine ran the# duplication detector for this analyze() call). ``clones`` is the# list of clone pairs this file participates in; ``duplication_pct``diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.pyindex 3b64d34ff..86bf88678 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py@@ -44,10 +44,11 @@ def _is_hotspot(ctx: FileContext) -> bool:def _has_something_to_test(ctx: FileContext) -> bool:"""Code with at least one function. A config file (``vite.config.ts``, a- ``.d.ts``, a non-code language) or a file with no function (types,- constants, a settings module) has nothing a test could exercise, so the+ ``.d.ts``, a non-code language), a file with no function (types,+ constants, a settings module) or one declaring only types (a ``Protocol``+ whose methods are signatures) has nothing a test could exercise, so theabsence of one is not a gap."""- return is_code_path(ctx.file_path) and bool(ctx.all_functions)+ return is_code_path(ctx.file_path) and bool(ctx.all_functions) and not ctx.type_onlyclass UntestedHotspotDetector:diff --git a/packages/core/src/repowise/core/analysis/health/file_evaluator.py b/packages/core/src/repowise/core/analysis/health/file_evaluator.pyindex f7ee7ca69..39508f14f 100644--- a/packages/core/src/repowise/core/analysis/health/file_evaluator.py+++ b/packages/core/src/repowise/core/analysis/health/file_evaluator.py@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Anyfrom ...ingestion.git_indexer.function_blame import BlameIndexfrom ...ingestion.package_roots import module_for as _module_forfrom ..graph_view import HasEdge+from ..test_reach.type_only import is_type_only, may_be_type_onlyfrom .asserts.lexicon import AssertVocabularyfrom .biomarkers import FileContext, detect_allfrom .biomarkers.base import BiomarkerResult@@ -292,6 +293,8 @@ class FileEvaluator:coverable_lines=set(cov.get("coverable_lines") or ()) if cov else set(),reexport_only=bool(cov)and is_reexport_only(pf, bool(fcx.functions), analyzer.read_source),+ type_only=bool(file_git_meta.get("is_hotspot"))+ and _declares_only_types(file_path, pf, analyzer.read_source),clones=list(clones),duplication_pct=dup_pct,clone_sources=clone_sources,@@ -432,3 +435,13 @@ class FileEvaluator:else FileDataflowCache(self.analyzer.read_source))return cache.get(pf.file_info.abs_path, pf.file_info.language).flagged_analyses()+++def _declares_only_types(file_path: str, pf: Any, read_source: SourceReader) -> bool:+ """:func:`is_type_only` over the file's bytes, read only when its name allows it."""+ if not may_be_type_only(file_path):+ return False+ source = read_source(pf.file_info.abs_path)+ return source is not None and is_type_only(+ file_path, source, is_test=bool(pf.file_info.is_test)+ )diff --git a/packages/core/src/repowise/core/analysis/pr_blast.py b/packages/core/src/repowise/core/analysis/pr_blast.pyindex 47e5887e5..99757b109 100644--- a/packages/core/src/repowise/core/analysis/pr_blast.py+++ b/packages/core/src/repowise/core/analysis/pr_blast.py@@ -78,13 +78,17 @@ async def untested_files(session: AsyncSession, repo_id: str, paths: list[str])also tried with a dispatch-role prefix stripped), an honest "unknown",never asserted as untested.- Test files, and files that are not code (a manifest, a workflow), are never- gaps: they need no test of their own.+ Test files, files that are not code (a manifest, a workflow), and files+ that declare only types (:func:`~repowise.core.analysis.test_reach.is_type_only`,+ read from the repository's checkout) are never gaps: they need no test of+ their own."""from repowise.core.analysis.test_reachability import (load_test_files,+ read_under,tests_matching_by_name,tests_reaching,+ type_only_files,)from repowise.core.analysis.test_selection import is_code_filefrom repowise.core.persistence.crud import covered_source_files@@ -105,7 +109,14 @@ async def untested_files(session: AsyncSession, repo_id: str, paths: list[str])rest = [p for p in candidates if p not in covered and p not in reached]subjects = {p: _subject_names(p) for p in rest}named = tests_matching_by_name(sorted({n for ns in subjects.values() for n in ns}), test_files)- return [p for p in rest if not any(n in named for n in subjects[p])]+ gaps = [p for p in rest if not any(n in named for n in subjects[p])]+ if not gaps:+ return []+ root = (+ await session.execute(select(Repository.local_path).where(Repository.id == repo_id))+ ).scalar_one_or_none()+ types = type_only_files(gaps, read_under(root))+ return [p for p in gaps if p not in types]class PRBlastRadiusAnalyzer:diff --git a/packages/core/src/repowise/core/analysis/test_reach/__init__.py b/packages/core/src/repowise/core/analysis/test_reach/__init__.pyindex 5fb31d027..bb0407e43 100644--- a/packages/core/src/repowise/core/analysis/test_reach/__init__.py+++ b/packages/core/src/repowise/core/analysis/test_reach/__init__.py@@ -201,7 +201,7 @@ from .model import (rank_reaching,tests_matching_by_name,)-from .type_only import is_type_only+from .type_only import is_type_only, read_under, type_only_filesfrom .walk import CLOSURE_SEED_CAP, reach_states, reaching_by_tier__all__ = [@@ -249,6 +249,7 @@ __all__ = ["reach_states","reach_states_from_db","reaching_by_tier",+ "read_under","replaced_files","replaced_modules","rust_has_inline_tests",@@ -256,5 +257,6 @@ __all__ = ["tests_reaching","tests_reaching_by_tier","tests_reaching_through_helpers",+ "type_only_files","unscanned_test_files",]diff --git a/packages/core/src/repowise/core/analysis/test_reach/type_only.py b/packages/core/src/repowise/core/analysis/test_reach/type_only.pyindex 9527af97d..b8002ba56 100644--- a/packages/core/src/repowise/core/analysis/test_reach/type_only.py+++ b/packages/core/src/repowise/core/analysis/test_reach/type_only.py@@ -9,7 +9,8 @@ and says nothing.from __future__ import annotationsimport ast-from pathlib import PurePosixPath+from collections.abc import Callable, Iterable+from pathlib import Path, PurePosixPathfrom repowise.core.test_paths import is_test_related_path@@ -49,6 +50,62 @@ def is_type_only(path: str, content: str | bytes, *, is_test: bool | None = Nonereturn False+def may_be_type_only(path: str) -> bool:+ """Whether *path*'s name allows :func:`is_type_only`: worth reading its bytes."""+ lowered = path.lower()+ return lowered.endswith(_DECLARATION_SUFFIXES) or PurePosixPath(lowered).suffix in (+ _TS_SUFFIXES | {".py", ".pyi"}+ )+++def type_only_files(+ paths: Iterable[str], read: Callable[[str], str | bytes | None]+) -> set[str]:+ """The *paths* that declare only types. *read* returns a file's bytes, or ``None``.++ A declaration file is judged by its name, so it needs no read. Any other+ file *read* cannot open, or that fails to parse, is judged code, so it+ stays a candidate gap: without the checkout only declaration files leave.+ """+ out: set[str] = set()+ for path in paths:+ if not may_be_type_only(path):+ continue+ # A declaration file's verdict does not depend on its bytes.+ content = b"" if path.lower().endswith(_DECLARATION_SUFFIXES) else read(path)+ try:+ if content is not None and is_type_only(path, content):+ out.add(path)+ except Exception: # a parser failure keeps the file a gap, never drops it+ continue+ return out+++# A file of only types is small; past this a read is not worth it.+_MAX_READ_BYTES = 512 * 1024+++def read_under(root: str | Path | None) -> Callable[[str], bytes | None]:+ """A reader of repository-relative paths under *root*; with no root, it reads nothing.++ A path that is absolute or climbs out with ``..`` reads nothing.+ """++ def read(path: str) -> bytes | None:+ parts = PurePosixPath(path.replace("\\", "/")).parts+ if root is None or not parts or parts[0] == "/" or ".." in parts or ":" in parts[0]:+ return None+ try:+ file = Path(root).joinpath(*parts)+ if file.stat().st_size > _MAX_READ_BYTES:+ return None+ return file.read_bytes()+ except (OSError, ValueError):+ return None++ return read++def _ts_type_only(source: bytes, *, tsx: bool) -> bool:from tree_sitter import Parserdiff --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 1dd58e16a..c847e0b5b 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@@ -24,7 +24,9 @@ from repowise.core.analysis.test_reachability import (cached_files_reached_by_tests,cached_hub_files,cached_test_files,+ read_under,tests_reaching_by_tier,+ type_only_files,)from repowise.core.persistence import crudfrom repowise.core.persistence.models import Repository@@ -259,6 +261,15 @@ async def _inferred_coverage(}# Walked once per index state: the edge read is the cost of this tab.reached = await cached_files_reached_by_tests(session, repo_id)+ # A file of only types has nothing a test could run: not a gap.+ repo = await session.get(Repository, repo_id)+ # Reads and parses files: off the event loop.+ types = await asyncio.to_thread(+ type_only_files,+ [m.file_path for m in rows if m.file_path not in reached],+ read_under(repo.local_path if repo else None),+ )+ rows = [m for m in rows if m.file_path not in types]except Exception:return {"basis": "none",diff --git a/tests/unit/analysis/test_test_reach_api.py b/tests/unit/analysis/test_test_reach_api.pyindex 5b661e0b9..d99f9020d 100644--- a/tests/unit/analysis/test_test_reach_api.py+++ b/tests/unit/analysis/test_test_reach_api.py@@ -432,3 +432,35 @@ def test_a_path_level_transitive_entry_ranks_a_test_id_last() -> None:"tests/test_b.py::t","tests/test_a.py::t",]+++def test_type_only_files_reads_only_what_it_must(tmp_path) -> None:+ from repowise.core.analysis.test_reach import read_under, type_only_files++ (tmp_path / "src").mkdir()+ (tmp_path / "src" / "t.ts").write_text("export interface A { a: string }\n", encoding="utf-8")+ (tmp_path / "src" / "c.ts").write_text("export const a = 1;\n", encoding="utf-8")+ asked: list[str] = []+ read = read_under(tmp_path)++ def counting(path: str):+ asked.append(path)+ return read(path)++ paths = ["src/t.ts", "src/c.ts", "src/api.d.ts", "src/gone.py", "src/x.go", "tests/a.d.ts"]+ assert type_only_files(paths, counting) == {"src/t.ts", "src/api.d.ts"}+ assert asked == ["src/t.ts", "src/c.ts", "src/gone.py"]+ assert read_under(None)("src/t.ts") is None+++def test_read_under_stays_inside_the_checkout(tmp_path) -> None:+ from repowise.core.analysis.test_reach import read_under++ (tmp_path / "repo").mkdir()+ (tmp_path / "repo" / "a.ts").write_text("export type A = string;\n", encoding="utf-8")+ (tmp_path / "secret.ts").write_text("export type S = string;\n", encoding="utf-8")+ read = read_under(tmp_path / "repo")+ assert read("a.ts") is not None+ assert read("../secret.ts") is None+ assert read(str(tmp_path / "secret.ts").replace("\\", "/")) is None+ assert read("missing.ts") is Nonediff --git a/tests/unit/health/test_coverage_biomarkers.py b/tests/unit/health/test_coverage_biomarkers.pyindex 0d0ed3718..6e1bfdba6 100644--- a/tests/unit/health/test_coverage_biomarkers.py+++ b/tests/unit/health/test_coverage_biomarkers.py@@ -69,6 +69,12 @@ def test_untested_hotspot_fires_when_low_coverage_and_hotspot() -> None:assert results[0].severity == "critical"+def test_untested_hotspot_silent_on_a_file_of_only_types() -> None:+ ctx = _ctx(git_meta={"is_hotspot": True}, dependents=20)+ ctx.type_only = True+ assert UntestedHotspotDetector().detect(ctx) == []++def test_untested_hotspot_silent_when_not_hotspot() -> None:ctx = _ctx(git_meta={"is_hotspot": False}, dependents=20, line_cov=5.0, total_lines=100)assert UntestedHotspotDetector().detect(ctx) == []diff --git a/tests/unit/persistence/test_test_gap_graph_floor.py b/tests/unit/persistence/test_test_gap_graph_floor.pyindex 7373804a6..b5a8a7e25 100644--- a/tests/unit/persistence/test_test_gap_graph_floor.py+++ b/tests/unit/persistence/test_test_gap_graph_floor.py@@ -153,3 +153,44 @@ async def test_a_stripped_stem_does_not_match_a_longer_word_it_prefixes(async_se)analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)assert await analyzer._find_test_gaps(["src/mcp/tool_repos.py"]) == ["src/mcp/tool_repos.py"]++[email protected]("check", ["pr_blast", "assessment"])+async def test_a_file_of_only_types_is_never_a_gap(async_session, tmp_path, check):+ """Read from the checkout: a ``Protocol`` module and a ``.d.ts`` run nothing."""+ (tmp_path / "src").mkdir()+ (tmp_path / "src" / "ports.py").write_text(+ "from typing import Protocol\n\nclass Store(Protocol):\n def get(self) -> int: ...\n",+ encoding="utf-8",+ )+ (tmp_path / "src" / "lonely.py").write_text("def f():\n return 1\n", encoding="utf-8")+ repo = await insert_repo(async_session, local_path=str(tmp_path))+ for path in ("src/ports.py", "src/api.d.ts", "src/lonely.py"):+ async_session.add(+ GraphNode(repository_id=repo.id, node_id=path, node_type="file", is_test=False)+ )+ async_session.add(+ GraphNode(repository_id=repo.id, node_id="tests/test_x.py", node_type="file", is_test=True)+ )+ await async_session.commit()+ if check == "pr_blast":+ gaps = await PRBlastRadiusAnalyzer(async_session, repo.id)._find_test_gaps(+ ["src/api.d.ts", "src/lonely.py", "src/ports.py"]+ )+ assert gaps == ["src/lonely.py"]+ else:+ assert await _check_test_gap(async_session, repo.id, "src/ports.py") is False+ assert await _check_test_gap(async_session, repo.id, "src/lonely.py") is True+++async def test_without_a_checkout_only_declaration_files_leave_the_gaps(async_session, tmp_path):+ repo = await insert_repo(async_session, local_path=str(tmp_path / "gone"))+ for path, is_test in (("src/ports.py", False), ("src/api.d.ts", False), ("tests/t.py", True)):+ async_session.add(+ GraphNode(repository_id=repo.id, node_id=path, node_type="file", is_test=is_test)+ )+ await async_session.commit()+ gaps = await PRBlastRadiusAnalyzer(async_session, repo.id)._find_test_gaps(+ ["src/api.d.ts", "src/ports.py"]+ )+ assert gaps == ["src/ports.py"]diff --git a/tests/unit/server/test_health_coverage_basis.py b/tests/unit/server/test_health_coverage_basis.pyindex 984877e85..0df31d7d8 100644--- a/tests/unit/server/test_health_coverage_basis.py+++ b/tests/unit/server/test_health_coverage_basis.py@@ -602,3 +602,30 @@ async def test_the_file_endpoint_404s_for_an_unknown_repo(client):"/api/repos/nope/health/tests-reaching", params={"file_path": "src/a.py"})assert resp.status_code == 404+++async def test_a_file_of_only_types_is_not_a_row_nothing_reaches(client, session, tmp_path):+ """A ``.d.ts`` or a module of ``TypedDict`` classes has nothing a test could run."""+ repo = await create_test_repo(client, tmp_path)+ root = tmp_path / "test-repo" / "src"+ root.mkdir()+ (root / "types.py").write_text(+ "from typing import TypedDict\n\nclass Row(TypedDict):\n a: int\n", encoding="utf-8"+ )+ (root / "c.py").write_text("def run():\n return 1\n", encoding="utf-8")+ paths = ["src/a.py", "src/c.py", "src/types.py", "src/api.d.ts"]+ await save_health_metrics(session, repo["id"], [_metric(p) for p in paths])+ await _seed_graph(+ session,+ repo["id"],+ nodes={"tests/test_a.py": True, **{p: False for p in paths}},+ edges=_calls("tests/test_a.py", "src/a.py"),+ )+ await session.commit()++ body = await _get(client, repo["id"])++ reached = {f["file_path"]: f["reached"] for f in body["inferred"]["files"]}+ assert reached == {"src/a.py": True, "src/c.py": False}+ assert body["inferred"]["files_total"] == 2+ assert body["inferred"]["files_not_reached"] == 1