Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

10.5 KB
Raw Blame
"""Coverage decay: how much of a measurement still describes the file."""

from __future__ import annotations

import os
import subprocess
from datetime import UTC, datetime, timedelta

import pytest

from repowise.core.analysis.health.coverage import (
    DRIFT_MIN_MEASURED,
    DRIFT_PCT,
    decay_for_file,
    decay_since,
    measurement_commit,
    measurement_ref,
)

# --- the pure split --------------------------------------------------------


def test_nothing_changed_confirms_everything() -> None:
    d = decay_for_file({1, 2, 3}, set())
    assert (d.measured, d.confirmed, d.invalidated) == (3, 3, 0)
    assert d.drift_pct == 0.0


def test_every_covered_line_changed_invalidates_everything() -> None:
    d = decay_for_file({1, 2, 3}, {1, 2, 3})
    assert (d.measured, d.confirmed, d.invalidated) == (3, 0, 3)
    assert d.drift_pct == 100.0


def test_only_covered_lines_count_as_drift() -> None:
    """A change to a line the report never saw covered is not drift.

    It may well be new uncovered code, but this module reports on the
    *measurement*, and a line outside the covered set was never part of it.
    """
    d = decay_for_file({1, 2}, {2, 50, 51, 52})
    assert (d.confirmed, d.invalidated) == (1, 1)
    assert d.drift_pct == 50.0


def test_no_covered_lines_does_not_divide_by_zero() -> None:
    d = decay_for_file(set(), {1, 2, 3})
    assert (d.measured, d.confirmed, d.invalidated) == (0, 0, 0)
    assert d.drift_pct == 0.0
    assert d.is_drifted is False


def test_confirmed_and_invalidated_always_sum_to_measured() -> None:
    d = decay_for_file({1, 4, 9, 16, 25}, {4, 16, 99})
    assert d.confirmed + d.invalidated == d.measured


# --- the staleness convention ----------------------------------------------


def test_small_file_is_never_stale_on_ratio_alone() -> None:
    """A one-line __init__ at 100% drift is noise, not a stale measurement.

    Without the floor this shape led the drift ranking on this repo's own
    index: a version bump is the only covered line, so one edit reads as total
    decay while saying nothing about the file.
    """
    d = decay_for_file({1}, {1})
    assert d.drift_pct == 100.0
    assert d.is_drifted is False


def test_stale_once_the_file_is_big_enough_and_drift_clears_the_bar() -> None:
    covered = set(range(1, DRIFT_MIN_MEASURED + 1))
    changed = set(list(covered)[:3])  # 30% of 10
    d = decay_for_file(covered, changed)
    assert d.measured >= DRIFT_MIN_MEASURED
    assert d.drift_pct > DRIFT_PCT
    assert d.is_drifted is True


def test_big_file_under_the_bar_is_not_stale() -> None:
    covered = set(range(1, 101))
    d = decay_for_file(covered, {1, 2})  # 2%
    assert d.is_drifted is False


# --- end-to-end against a real git repo ------------------------------------


def _git(cwd, *args: str, when: str | None = None) -> str:
    """Run git in *cwd*. *when* pins both dates, since ``rev-list --before``
    filters on the COMMITTER date and ``git commit --date`` only sets the
    author one."""
    env = None
    if when is not None:
        env = {**os.environ, "GIT_COMMITTER_DATE": when, "GIT_AUTHOR_DATE": when}
    return subprocess.run(
        ["git", *args], cwd=cwd, check=True, capture_output=True, text=True, env=env
    ).stdout


@pytest.fixture
def git_repo(tmp_path):
    _git(tmp_path, "init", "-q")
    _git(tmp_path, "config", "user.email", "[email protected]")
    _git(tmp_path, "config", "user.name", "t")
    (tmp_path / "mod.py").write_text("a = 1\nb = 2\nc = 3\nd = 4\n", encoding="utf-8")
    (tmp_path / "quiet.py").write_text("x = 1\ny = 2\n", encoding="utf-8")
    _git(tmp_path, "add", "-A")
    _git(tmp_path, "commit", "-qm", "init")
    return tmp_path


def test_decay_since_splits_an_edited_file_and_leaves_an_untouched_one(git_repo) -> None:
    base = _git(git_repo, "rev-parse", "HEAD").strip()
    (git_repo / "mod.py").write_text("a = 1\nb = 22\nc = 3\nd = 4\n", encoding="utf-8")
    _git(git_repo, "add", "-A")
    _git(git_repo, "commit", "-qm", "edit line 2")

    out = decay_since(str(git_repo), base, {"mod.py": {1, 2, 3}, "quiet.py": {1, 2}})

    # Line 2 was covered and has changed; 1 and 3 still stand.
    assert (out["mod.py"].confirmed, out["mod.py"].invalidated) == (2, 1)
    # A file nothing touched keeps its whole measurement, and is reported
    # rather than omitted, so "nothing moved" is distinguishable from
    # "not checked".
    assert (out["quiet.py"].confirmed, out["quiet.py"].invalidated) == (2, 0)


def test_decay_since_returns_empty_on_an_unknown_ref(git_repo) -> None:
    """An unresolvable ref must not read as zero drift.

    Zero drift is a freshness claim. When the range cannot be diffed the
    honest answer is no answer, so the caller renders the stored figure
    without a decay line rather than one asserting nothing has moved.
    """
    assert decay_since(str(git_repo), "deadbeef" * 5, {"mod.py": {1}}) == {}


def test_decay_since_short_circuits_on_no_input(git_repo) -> None:
    assert decay_since(str(git_repo), "HEAD", {}) == {}


# --- placing the measurement in history ------------------------------------


def test_measurement_ref_prefers_a_recorded_sha(git_repo) -> None:
    head = _git(git_repo, "rev-parse", "HEAD").strip()
    assert measurement_ref(str(git_repo), head, None) == head


def test_measurement_ref_falls_back_to_the_timestamp(git_repo) -> None:
    """The sha is absent on rows written before the repo carried a head commit.

    The second commit is dated an hour ahead so "now" sits strictly between the
    two, which is the shape the fallback exists for: pick the commit that was
    HEAD when the report was taken, not the one that is HEAD today.
    """
    first = _git(git_repo, "rev-parse", "HEAD").strip()
    later = (datetime.now(UTC) + timedelta(hours=1)).isoformat()
    (git_repo / "mod.py").write_text("a = 9\n", encoding="utf-8")
    _git(git_repo, "add", "-A")
    _git(git_repo, "commit", "-qm", "later", when=later)

    assert measurement_ref(str(git_repo), None, datetime.now(UTC)) == first


def test_measurement_ref_is_none_when_the_report_predates_all_history(git_repo) -> None:
    """No commit existed when the measurement was taken, so it cannot be placed.

    ``None`` here is load-bearing: ``decay_since`` refuses to run without a
    ref, so the caller renders the stored figure with no drift claim instead of
    one asserting nothing has moved.
    """
    ancient = datetime(2000, 1, 1, tzinfo=UTC)
    assert measurement_ref(str(git_repo), None, ancient) is None


def test_measurement_ref_ignores_an_unknown_sha_and_falls_back(git_repo) -> None:
    resolved = measurement_ref(str(git_repo), "deadbeef" * 5, datetime.now(UTC))
    assert resolved == _git(git_repo, "rev-parse", "HEAD").strip()


def test_measurement_ref_is_none_when_the_measurement_cannot_be_placed(git_repo) -> None:
    assert measurement_ref(str(git_repo), None, None) is None


def test_naive_timestamp_is_read_as_utc_not_local(git_repo) -> None:
    """A SQLite store hands back a naive datetime; git would read it as local.

    Both spellings of the same instant must resolve to the same commit, which
    they do not if the naive one is left for git to localise.
    """
    now = datetime.now(UTC)
    aware = measurement_ref(str(git_repo), None, now)
    naive = measurement_ref(str(git_repo), None, now.replace(tzinfo=None))
    assert aware == naive


# --- the commit a writer stamps a report with (issue #3901) -----------------


def _commit_all_lines_moved(repo, *, when: str) -> str:
    """Rewrite every line of ``mod.py`` and commit at *when*; returns the new HEAD."""
    (repo / "mod.py").write_text("p = 10\nq = 20\nr = 30\ns = 40\n", encoding="utf-8")
    _git(repo, "add", "-A")
    _git(repo, "commit", "-qm", "rewrite mod.py", when=when)
    return _git(repo, "rev-parse", "HEAD").strip()


def test_measurement_commit_names_the_commit_that_headed_when_the_report_ran(
    git_repo,
) -> None:
    """A report left on disk describes its own date, not the index's HEAD.

    This is the defect: every writer stamped live HEAD, so the forward drift
    read — which diffs the covered lines from the recorded commit to HEAD —
    started at a commit that did not exist when the suite ran. It reported
    zero decay for lines that had all since moved.
    """
    first = _git(git_repo, "rev-parse", "HEAD").strip()
    head = _commit_all_lines_moved(
        git_repo, when=(datetime.now(UTC) + timedelta(hours=1)).isoformat()
    )
    covered = {"mod.py": {1, 2, 3, 4}}

    stamped = measurement_commit(git_repo, datetime.now(UTC), head)
    assert stamped == first

    # Non-vacuous: the right commit exposes decay the wrong one hides.
    from_base = decay_since(str(git_repo), stamped, covered)["mod.py"]
    assert (from_base.confirmed, from_base.invalidated) == (0, 4)
    from_head = decay_since(str(git_repo), head, covered)["mod.py"]
    assert (from_head.confirmed, from_head.invalidated) == (4, 0)


def test_a_report_written_after_the_last_commit_still_stamps_head(git_repo) -> None:
    """The common case keeps its behaviour: ``coverage lcov`` and then index.

    The commit that headed at the report's time is HEAD itself, so a fresh
    report is not pulled back to an ancestor — which is why this fix costs
    nothing for the shape every writer already got right.
    """
    head = _git(git_repo, "rev-parse", "HEAD").strip()
    assert measurement_commit(git_repo, datetime.now(UTC), head) == head


def test_measurement_commit_falls_back_to_head_without_a_time(git_repo) -> None:
    """A writer that did not resolve the reports knows nothing about their date.

    ``None`` *measured_at* covers both that and a report that cannot be
    stat'ed; HEAD is what those writers stamped before, and the honest answer
    is still HEAD when there is no HEAD either.
    """
    head = _git(git_repo, "rev-parse", "HEAD").strip()
    assert measurement_commit(git_repo, None, head) == head
    assert measurement_commit(None, datetime.now(UTC), head) == head
    assert measurement_commit(git_repo, None, None) is None


def test_an_unreachable_history_falls_back_to_head(git_repo, tmp_path) -> None:
    """git cannot place the measurement in either case; the ingest still has a commit.

    A report older than every commit predates the history, and a directory
    that is not a repository has none to search. Returning HEAD matches what
    every writer did before the fix rather than dropping the stamp.
    """
    head = _git(git_repo, "rev-parse", "HEAD").strip()
    ancient = datetime(2000, 1, 1, tzinfo=UTC)
    assert measurement_commit(git_repo, ancient, head) == head
    assert measurement_commit(tmp_path / "not-a-repo", datetime.now(UTC), head) == head