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
"""Unit tests for the Phase 2 coverage-aware biomarkers."""
from __future__ import annotations
import pytest
from repowise.core.analysis.health.biomarkers import FileContext
from repowise.core.analysis.health.biomarkers.coverage_gap import CoverageGapDetector
from repowise.core.analysis.health.biomarkers.coverage_gradient import (
CoverageGradientDetector,
)
from repowise.core.analysis.health.biomarkers.untested_hotspot import (
UntestedHotspotDetector,
)
from repowise.core.analysis.health.complexity.models import FunctionComplexity
from repowise.core.analysis.health.coverage import parse_repowise_json
_ONE_FUNCTION = (
FunctionComplexity(
name="run", start_line=1, end_line=5, ccn=1, max_nesting=0, cognitive=0, nloc=4
),
)
def _ctx(
*,
path: str = "src/example.py",
dependents: int = 0,
has_test_file: bool = False,
reached_by_tests: bool = False,
git_meta: dict | None = None,
line_cov: float | None = None,
branch_cov: float | None = None,
covered_lines: set[int] | None = None,
total_lines: int = 0,
functions: tuple[FunctionComplexity, ...] = _ONE_FUNCTION,
) -> FileContext:
return FileContext(
file_path=path,
language="python",
nloc=100,
has_test_file=has_test_file,
reached_by_tests=reached_by_tests,
module=None,
git_meta=git_meta or {},
dependents_count=dependents,
line_coverage_pct=line_cov,
branch_coverage_pct=branch_cov,
covered_lines=covered_lines or set(),
total_coverable_lines=total_lines,
all_functions=functions,
)
# ---------------------------------------------------------------------------
# untested_hotspot
# ---------------------------------------------------------------------------
def test_untested_hotspot_fires_when_low_coverage_and_hotspot() -> None:
ctx = _ctx(
git_meta={"is_hotspot": True},
dependents=10,
line_cov=12.0,
total_lines=50,
)
results = UntestedHotspotDetector().detect(ctx)
assert len(results) == 1
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) == []
def test_untested_hotspot_silent_when_dependents_low() -> None:
ctx = _ctx(git_meta={"is_hotspot": True}, dependents=1, line_cov=5.0, total_lines=100)
assert UntestedHotspotDetector().detect(ctx) == []
def test_untested_hotspot_silent_when_well_covered() -> None:
ctx = _ctx(git_meta={"is_hotspot": True}, dependents=10, line_cov=85.0, total_lines=100)
assert UntestedHotspotDetector().detect(ctx) == []
def test_untested_hotspot_falls_back_when_no_coverage_data() -> None:
ctx = _ctx(
git_meta={"commit_count_90d": 12, "is_hotspot": True},
dependents=6,
has_test_file=False,
)
results = UntestedHotspotDetector().detect(ctx)
assert len(results) == 1
assert "no coverage data" in results[0].reason
def test_untested_hotspot_needs_the_repo_hotspot_verdict() -> None:
"""A raw commit count is a different bar on a busy repository than on a
quiet one, so the gate is is_hotspot, which is already repo-relative."""
ctx = _ctx(
git_meta={"commit_count_90d": 12, "temporal_hotspot_score": 4.0},
dependents=6,
has_test_file=False,
)
assert UntestedHotspotDetector().detect(ctx) == []
def test_untested_hotspot_skips_when_paired_test_present() -> None:
ctx = _ctx(
git_meta={"commit_count_90d": 12, "is_hotspot": True},
dependents=6,
has_test_file=True,
)
assert UntestedHotspotDetector().detect(ctx) == []
@pytest.mark.parametrize(
("path", "functions"),
[
# Types only: interfaces and aliases, no function to call.
("src/types/router.types.ts", ()),
("src/settings.py", ()),
# Build/tool configuration and declaration files, even with a function in them.
("vite.config.ts", _ONE_FUNCTION),
("src/api.d.ts", _ONE_FUNCTION),
("config/app.yaml", ()),
],
)
def test_untested_hotspot_skips_files_with_nothing_to_test(path, functions) -> None:
ctx = _ctx(
path=path,
git_meta={"is_hotspot": True},
dependents=10,
functions=functions,
)
assert UntestedHotspotDetector().detect(ctx) == []
# ---------------------------------------------------------------------------
# coverage_gap
# ---------------------------------------------------------------------------
def test_coverage_gap_fires_on_regular_gap() -> None:
ctx = _ctx(line_cov=50.0, total_lines=100, covered_lines=set(range(1, 51)))
results = CoverageGapDetector().detect(ctx)
assert len(results) == 1
assert results[0].severity == "low"
assert results[0].details["uncovered_lines"] == 50
def test_coverage_gap_fires_on_deep_gap() -> None:
ctx = _ctx(line_cov=20.0, total_lines=200, covered_lines=set(range(1, 41)))
results = CoverageGapDetector().detect(ctx)
assert len(results) == 1
assert results[0].severity == "high"
def test_coverage_gap_skips_test_files() -> None:
ctx = _ctx(
path="tests/test_thing.py",
line_cov=10.0,
total_lines=200,
)
assert CoverageGapDetector().detect(ctx) == []
def test_coverage_gap_reads_the_stored_flag_over_the_path() -> None:
"""A test-named production module (pytest does not collect it) is still scored."""
ctx = _ctx(path="src/pkg/test_selection.py", line_cov=10.0, total_lines=200)
ctx.is_test = False
assert CoverageGapDetector().detect(ctx)
def test_coverage_gap_skips_when_no_coverage_data() -> None:
ctx = _ctx(line_cov=None, total_lines=0)
assert CoverageGapDetector().detect(ctx) == []
def test_coverage_gap_skips_well_covered() -> None:
ctx = _ctx(line_cov=85.0, total_lines=200, covered_lines=set(range(1, 171)))
assert CoverageGapDetector().detect(ctx) == []
def test_coverage_gap_counts_from_the_percentage_when_no_hit_set_is_reported() -> None:
# A parser may pin a file with a percentage and a total and no per-line set.
# Reading the absent set as "nothing is covered" made 20 real uncovered
# lines report as 40/40 and clear the 25-line gate.
ctx = _ctx(line_cov=50.0, total_lines=40)
assert CoverageGapDetector().detect(ctx) == []
def test_coverage_gap_severity_follows_the_percentage_count_not_the_total() -> None:
ctx = _ctx(line_cov=25.0, total_lines=120)
results = CoverageGapDetector().detect(ctx)
assert len(results) == 1
assert results[0].details["uncovered_lines"] == 90
assert results[0].severity == "medium"
def test_coverage_gap_ranks_the_hit_set_ahead_of_the_percentage() -> None:
# The set is exact evidence; the percentage is the fallback, and rounded.
ctx = _ctx(line_cov=40.0, total_lines=100, covered_lines=set(range(1, 62)))
results = CoverageGapDetector().detect(ctx)
assert len(results) == 1
assert results[0].details["uncovered_lines"] == 39
def test_coverage_gap_reads_a_repowise_json_entry_that_omits_the_hit_set() -> None:
report = parse_repowise_json(
'{"format": "repowise-coverage-v1", "files": {"src/example.py": '
'{"line_coverage_pct": 50.0, "total_coverable_lines": 40}}}'
)
fc = report.files[0]
assert fc.covered_lines == []
ctx = _ctx(line_cov=fc.line_coverage_pct, total_lines=fc.total_coverable_lines)
assert CoverageGapDetector().detect(ctx) == []
# ---------------------------------------------------------------------------
# coverage_gradient
# ---------------------------------------------------------------------------
def test_coverage_gradient_fires_proportionally_to_uncovered() -> None:
# 75% covered → 25% uncovered → deduction 4.0 * 0.25 = 1.0.
ctx = _ctx(line_cov=75.0, total_lines=100)
results = CoverageGradientDetector().detect(ctx)
assert len(results) == 1
r = results[0]
assert r.deduction == 1.0
assert r.details["uncovered_fraction"] == 0.25
assert r.severity == "low"
def test_coverage_gradient_fires_on_well_covered_file() -> None:
# The binary gates stay silent at 92% — the gradient still fires (the point
# of this biomarker): 8% uncovered → deduction 0.32.
ctx = _ctx(line_cov=92.0, total_lines=300)
results = CoverageGradientDetector().detect(ctx)
assert len(results) == 1
assert round(results[0].deduction, 4) == 0.32
assert CoverageGapDetector().detect(ctx) == []
def test_coverage_gradient_severity_bands() -> None:
assert CoverageGradientDetector().detect(_ctx(line_cov=30.0))[0].severity == "high"
assert CoverageGradientDetector().detect(_ctx(line_cov=55.0))[0].severity == "medium"
assert CoverageGradientDetector().detect(_ctx(line_cov=90.0))[0].severity == "low"
def test_coverage_gradient_silent_when_no_coverage_data() -> None:
# Absent coverage is never imputed as uncovered.
assert CoverageGradientDetector().detect(_ctx(line_cov=None)) == []
def test_coverage_gradient_silent_at_full_coverage() -> None:
assert CoverageGradientDetector().detect(_ctx(line_cov=100.0)) == []
def test_coverage_gradient_skips_test_files() -> None:
ctx = _ctx(path="tests/test_thing.py", line_cov=10.0)
assert CoverageGradientDetector().detect(ctx) == []
# ---------------------------------------------------------------------------
# untested_hotspot: the graph-backed half of the no-coverage fallback
# ---------------------------------------------------------------------------
def _uncovered_hotspot(**kw) -> FileContext:
"""A file that fires untested_hotspot on every axis except the test signal."""
return _ctx(git_meta={"is_hotspot": True}, dependents=10, line_cov=None, **kw)
def test_untested_hotspot_fires_when_nothing_says_a_test_touches_it() -> None:
assert len(UntestedHotspotDetector().detect(_uncovered_hotspot())) == 1
def test_a_test_reaching_it_in_the_graph_suppresses_the_finding() -> None:
"""The live false positive: no test is *named* for the file, but six import it.
Suites that name their tests for behaviour satisfy no naming convention, so
``has_test_file`` stays False and this used to fire on files the graph
records several test files importing.
"""
ctx = _uncovered_hotspot(has_test_file=False, reached_by_tests=True)
assert UntestedHotspotDetector().detect(ctx) == []
def test_either_signal_alone_is_enough() -> None:
# Asserting "nothing tests this" is the one accusation this biomarker makes,
# so the bar for making it is no evidence from either source.
for kw in ({"has_test_file": True}, {"reached_by_tests": True}):
assert UntestedHotspotDetector().detect(_uncovered_hotspot(**kw)) == []
def test_reachability_does_not_override_a_real_coverage_number() -> None:
"""Measured coverage decides on its own; the inferred signal never blends in.
Reaching cannot speak to how much of a file runs, so a file the graph
reaches and coverage measures at 12% is still an untested hotspot.
"""
ctx = _ctx(
git_meta={"is_hotspot": True},
dependents=10,
line_cov=12.0,
total_lines=50,
reached_by_tests=True,
)
results = UntestedHotspotDetector().detect(ctx)
assert len(results) == 1
assert results[0].details["reached_by_tests"] is True