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.

22.5 KB
Raw Blame
"""Base-versus-head comparison: identity, attribution, and honesty."""

from __future__ import annotations

import pytest

from repowise.core.analysis.change_health.service import ChangeHealthDeltaService, DeltaRequest

from .conftest import (
    LANGUAGES,
    Repo,
    python_complex,
    python_io_in_loop,
)


def compare(repo: Repo, revspec: str | None = "HEAD", **kwargs):
    service = ChangeHealthDeltaService(repo_path=str(repo.path))
    return service.compare(DeltaRequest(str(repo.path), revspec, **kwargs))


def kinds(delta) -> dict[str, int]:
    out: dict[str, int] = {}
    for finding in delta.findings:
        out[finding.change_kind] = out.get(finding.change_kind, 0) + 1
    return out


# -- the core claim ---------------------------------------------------------


def test_a_new_complex_function_is_introduced(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("stable", 1)})
    repo.commit("add", {"app.py": python_complex("stable", 1) + python_complex("tangle", 14)})

    delta = compare(repo)

    assert delta.status == "available"
    assert delta.introduced_total >= 1
    introduced = [f for f in delta.findings if f.change_kind == "introduced"]
    assert any(f.symbol == "tangle" for f in introduced)
    assert all(f.attribution_basis == "added_lines" for f in introduced)
    assert all(f.attribution_confidence == "high" for f in introduced)


def test_pure_line_movement_introduces_nothing(make_repo):
    """The whole point: moving a finding must not create one."""
    repo = make_repo()
    tangled = python_complex("tangle", 14)
    repo.commit("seed", {"app.py": tangled})
    repo.commit("shift", {"app.py": "# a new header comment\n" * 30 + tangled})

    delta = compare(repo)

    assert delta.introduced_total == 0
    assert delta.worsened_total == 0
    assert delta.unchanged_total >= 1
    assert delta.is_clean


def test_a_function_growing_more_complex_is_worsened_not_introduced(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 6)})
    repo.commit("grow", {"app.py": python_complex("tangle", 26)})

    delta = compare(repo)

    assert delta.introduced_total == 0
    worsened = [f for f in delta.findings if f.change_kind == "worsened"]
    assert worsened, kinds(delta)
    assert worsened[0].symbol == "tangle"
    assert worsened[0].severity_before is not None
    assert worsened[0].severity_before != worsened[0].severity


def test_removing_the_problem_resolves_it_and_surfaces_nothing(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14)})
    repo.commit("simplify", {"app.py": python_complex("tangle", 1)})

    delta = compare(repo)

    assert delta.findings == []
    assert delta.resolved_total >= 1
    assert delta.is_clean


def test_a_rename_carries_the_finding_across(make_repo):
    """A moved file is the same file; its findings are not new."""
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14)})
    repo.move("app.py", "pkg/app.py")
    repo.commit("move")

    delta = compare(repo)

    assert delta.introduced_total == 0
    assert delta.resolved_total == 0
    assert delta.unchanged_total >= 1


def test_deleting_a_file_compares_its_base_side_and_surfaces_nothing(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14), "keep.py": "x = 1\n"})
    repo.remove("app.py")
    repo.commit("delete")

    delta = compare(repo)

    assert delta.introduced_total == 0
    assert "app.py" not in delta.skipped
    assert delta.resolved_total == 0
    assert delta.status == "available"
    assert delta.explanation == "No supported new findings surfaced in the analyzed scope."


def test_a_deleted_file_with_no_health_dialect_is_still_recorded_as_skipped(make_repo):
    repo = make_repo()
    repo.commit("seed", {"page.html": "<p>hi</p>\n", "app.py": python_complex("tangle", 14)})
    repo.remove("page.html")
    repo.commit("delete", {"app.py": python_complex("tangle", 14) + "\n"})

    assert compare(repo).skipped.get("page.html") == "deleted"


# -- multiplicity -----------------------------------------------------------


def test_a_second_hit_of_the_same_marker_is_one_introduced_finding(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("one", 14)})
    repo.commit("add", {"app.py": python_complex("one", 14) + python_complex("two", 14)})

    delta = compare(repo)

    introduced = [f for f in delta.findings if f.change_kind == "introduced"]
    assert [f.symbol for f in introduced].count("two") == 1
    assert not any(f.symbol == "one" for f in introduced)


def test_replacing_one_marker_hit_with_another_is_not_a_regression(make_repo):
    """Same symbol, same marker, rewritten body: one finding, not two."""
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14)})
    repo.commit("rewrite", {"app.py": python_complex("tangle", 15)})

    delta = compare(repo)

    assert delta.introduced_total == 0
    assert delta.resolved_total == 0


# -- symbol identity --------------------------------------------------------


@pytest.mark.parametrize("language", sorted(LANGUAGES))
def test_renaming_a_function_carries_its_finding_across(make_repo, language):
    path, build = LANGUAGES[language]
    repo = make_repo()
    repo.commit("seed", {path: build("tangle", 14)})
    repo.commit("rename", {path: build("knot", 14)})

    delta = compare(repo)

    assert delta.introduced_total == 0
    assert delta.resolved_total == 0
    assert delta.unchanged_total >= 1


def test_a_rename_that_also_grows_the_body_is_still_introduced(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14)})
    repo.commit("rename", {"app.py": python_complex("knot", 15)})

    delta = compare(repo)

    assert any(f.symbol == "knot" and f.change_kind == "introduced" for f in delta.findings)
    assert delta.resolved_total >= 1


def test_a_copy_beside_the_unchanged_original_is_introduced(make_repo):
    """Same shape as an existing finding, but the original is still there."""
    repo = make_repo()
    repo.commit("seed", {"app.py": python_complex("tangle", 14)})
    repo.commit("copy", {"app.py": python_complex("tangle", 14) + python_complex("knot", 14)})

    delta = compare(repo)

    assert any(f.symbol == "knot" and f.change_kind == "introduced" for f in delta.findings)
    assert not any(f.symbol == "tangle" for f in delta.findings)
    assert delta.resolved_total == 0


def test_new_importers_of_a_changed_file_do_not_introduce_its_old_findings(make_repo):
    """Only the changed files are parsed, so a file's dependents are a slice.

    Adding importers inside the change lifts that slice over a centrality gate
    (Brain Method) on head only; the function itself did not change.
    """
    repo = make_repo()
    core = python_complex("tangle", 40)
    repo.commit("seed", {"core.py": core})
    importers = {f"use_{i}.py": "from core import tangle\n\nprint(tangle(1))\n" for i in range(4)}
    repo.commit("use", {"core.py": core + "\n# touched\n", **importers})

    delta = compare(repo)

    assert not any(f.symbol == "tangle" for f in delta.findings)
    assert delta.unchanged_total >= 1


# -- attribution ------------------------------------------------------------


def test_an_untouched_finding_in_an_edited_file_is_not_charged_to_added_lines(make_repo):
    """Editing elsewhere in a file must not claim its pre-existing problems."""
    repo = make_repo()
    original = python_complex("tangle", 14)
    repo.commit("seed", {"app.py": original})
    repo.commit("append", {"app.py": original + "\n\ndef helper():\n    return 1\n"})

    delta = compare(repo)

    assert not any(f.symbol == "tangle" and f.change_kind == "introduced" for f in delta.findings)


def test_a_new_file_is_attributed_as_a_new_file(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"new.py": python_complex("tangle", 14)})

    delta = compare(repo)

    introduced = [f for f in delta.findings if f.change_kind == "introduced"]
    assert introduced
    assert all(f.attribution_basis == "new_file" for f in introduced)


def test_every_surfaced_finding_names_a_basis_and_confidence(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"app.py": python_complex("tangle", 20)})

    delta = compare(repo)

    for finding in delta.findings:
        assert finding.attribution_basis
        assert finding.attribution_confidence in {"high", "medium", "low"}
        assert finding.attribution_detail


# -- cross-language ---------------------------------------------------------


@pytest.mark.parametrize("language", sorted(LANGUAGES))
def test_the_same_change_shape_works_in_every_language(make_repo, language):
    """No language-specific code in the comparison or the response."""
    filename, build = LANGUAGES[language]
    repo = make_repo()
    repo.commit("seed", {filename: build("stable", 1)})
    repo.commit("add", {filename: build("stable", 1) + "\n" + build("tangle", 16)})

    delta = compare(repo)

    assert delta.status == "available"
    assert delta.scope.analyzed == 1
    assert any(f.path == filename for f in delta.findings), (
        f"{language}: {[(f.path, f.biomarker_type) for f in delta.findings]}"
    )


# -- dimensions -------------------------------------------------------------


def test_a_performance_finding_is_ranked_without_defect_impact(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": python_io_in_loop(in_loop=False)})
    repo.commit("loop", {"app.py": python_io_in_loop(in_loop=True)})

    delta = compare(repo)
    perf = [f for f in delta.findings if f.dimension == "performance"]

    assert perf, [(f.dimension, f.biomarker_type) for f in delta.findings]
    assert perf[0].health_impact == 0.0
    assert perf[0].opportunity_id
    assert perf[0].opportunity_rank is not None


def test_findings_are_ordered_by_severity_not_by_dimension(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"app.py": python_complex("tangle", 26)})

    delta = compare(repo)

    severities = [f.severity for f in delta.findings]
    ranks = {"critical": 3, "high": 2, "medium": 1, "low": 0}
    assert severities == sorted(severities, key=lambda s: -ranks[s])


def _finding(**kw):
    from repowise.core.analysis.change_health.models import ChangeFinding

    base = dict(
        change_finding_id="id",
        change_kind="introduced",
        dimension="defect",
        biomarker_type="complexity",
        severity="low",
        path="app.py",
        symbol="f",
        line_start=1,
        line_end=2,
        reason="r",
        attribution_basis="added_lines",
        attribution_confidence="high",
        attribution_detail="d",
        suggestion="",
        follow_up="",
    )
    base.update(kw)
    return ChangeFinding(**base)


def test_what_the_change_wrote_outranks_what_it_only_touched():
    """A capped list must not spend its slots on pre-existing problems."""
    from repowise.core.analysis.change_health.service import _priority

    mine = _finding(severity="low", attribution_basis="added_lines")
    inherited = _finding(
        severity="critical", attribution_basis="file_change", change_kind="worsened"
    )
    scaffolding = _finding(severity="high", path="tests/test_app.py")

    assert sorted([inherited, mine], key=_priority) == [mine, inherited]
    assert sorted([scaffolding, mine], key=_priority) == [mine, scaffolding]
    assert sorted([inherited, scaffolding], key=_priority) == [scaffolding, inherited]


def test_a_performance_finding_on_test_code_is_not_reported(make_repo):
    """Performance findings are about code that serves requests; a test does not."""
    repo = make_repo()
    repo.commit("seed", {"tests/test_app.py": python_io_in_loop(in_loop=False)})
    repo.commit("loop", {"tests/test_app.py": python_io_in_loop(in_loop=True)})

    delta = compare(repo)

    assert not [f for f in delta.findings if f.dimension == "performance"], [
        (f.path, f.biomarker_type) for f in delta.findings
    ]


def test_test_file_findings_sort_below_source_findings(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", "tests/test_app.py": "y = 1\n"})
    repo.commit(
        "add",
        {
            "app.py": python_complex("source", 14),
            "tests/test_app.py": python_complex("scaffolding", 26),
        },
    )

    delta = compare(repo)
    in_tests = [f.path.startswith("tests/") for f in delta.findings]

    assert any(in_tests) and not all(in_tests), "fixture must produce both kinds"
    assert in_tests == sorted(in_tests), [(f.path, f.severity) for f in delta.findings]


# -- honesty ----------------------------------------------------------------


def test_a_docs_only_change_says_there_is_no_code_and_never_looks_clean(make_repo):
    repo = make_repo()
    repo.commit("seed", {"README.md": "# hi\n"})
    repo.commit("edit", {"README.md": "# hi there\n"})

    delta = compare(repo)

    assert delta.findings == []
    assert delta.status == "unavailable"
    assert delta.skipped == {"README.md": "not_code"}
    assert "health analysis does not cover them" in delta.explanation
    assert not delta.is_clean


def test_an_unsupported_code_file_is_skipped_and_never_looks_clean(make_repo):
    repo = make_repo()
    repo.commit("seed", {"lib.ex": "defmodule A do\nend\n"})
    repo.commit("edit", {"lib.ex": "defmodule B do\nend\n"})

    delta = compare(repo)

    assert delta.status == "unavailable"
    assert delta.skipped == {"lib.ex": "not_health_analyzable"}
    assert "nothing was compared" in delta.explanation


def test_a_partial_run_is_reported_as_partial(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit(
        "mixed",
        {"app.py": python_complex("tangle", 16), "lib.ex": "defmodule A do\nend\n"},
    )

    delta = compare(repo)

    assert delta.status == "partial"
    assert delta.skipped
    assert not delta.is_clean


@pytest.mark.parametrize("path", ["page.html", "api.proto", "q.graphql", "x.xaml"])
def test_data_and_markup_with_logic_still_make_the_run_partial(make_repo, path):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", path: "a\n"})
    repo.commit("edit", {"app.py": "x = 2\n", path: "b\n"})

    delta = compare(repo)

    assert delta.status == "partial"
    assert delta.skipped[path] != "not_code"


def test_docs_beside_an_unsupported_code_file_are_still_partial(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", "README.md": "a\n", "lib.ex": "defmodule A do\nend\n"})
    repo.commit("edit", {"app.py": "x = 2\n", "README.md": "b\n", "lib.ex": "defmodule B do\nend\n"})

    delta = compare(repo)

    assert delta.status == "partial"
    assert delta.code_skipped == {"lib.ex": "not_health_analyzable"}
    assert delta.skipped["README.md"] == "not_code"


@pytest.mark.parametrize("path", ["CHANGES.rst", "package.json", "docs/guide.md", ".ci.yml"])
def test_docs_and_config_beside_code_do_not_make_the_run_partial(make_repo, path):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", path: "a\n"})
    repo.commit("edit", {"app.py": "x = 2\n", path: "b\n"})

    delta = compare(repo)

    assert delta.status == "available"
    assert delta.skipped == {path: "not_code"}
    assert delta.code_skipped == {}


def test_a_generated_file_is_excluded_by_reason(make_repo):
    repo = make_repo()
    repo.commit("seed", {"gen.py": "# GENERATED CODE\nx = 1\n"})
    repo.commit("edit", {"gen.py": "# GENERATED CODE\n" + python_complex("tangle", 16)})

    delta = compare(repo)

    assert delta.skipped.get("gen.py") == "generated"
    assert delta.findings == []
    # Nothing was compared, so this is not a partial pass and not a clean one.
    assert delta.status == "unavailable"
    assert not delta.is_clean


def test_a_bad_revspec_is_an_explicit_unsupported_range(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})

    delta = compare(repo, "no-such-ref")

    assert delta.status == "unsupported_range"
    assert delta.findings == []
    assert not delta.is_clean


def test_clean_only_claims_the_analyzed_scope(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("edit", {"app.py": "x = 2\n"})

    delta = compare(repo)

    assert delta.is_clean
    assert "analyzed scope" in delta.explanation


# -- revspec shapes ---------------------------------------------------------


def test_working_tree_changes_are_compared_against_head(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.write("app.py", python_complex("tangle", 16))

    delta = compare(repo, None)

    assert delta.head is not None and delta.head.kind == "working_tree"
    assert delta.introduced_total >= 1


def test_two_dot_and_three_dot_ranges_resolve(make_repo):
    repo = make_repo()
    base = repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"app.py": python_complex("tangle", 16)})

    two_dot = compare(repo, f"{base}..HEAD")
    three_dot = compare(repo, f"{base}...HEAD")

    assert two_dot.introduced_total == three_dot.introduced_total >= 1
    assert two_dot.base is not None and two_dot.base.sha == base


def test_a_historical_commit_is_compared_against_its_own_parent(make_repo):
    """Not against HEAD, and not against whatever the index last saw."""
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    target = repo.commit("add", {"app.py": python_complex("tangle", 16)})
    repo.commit("later", {"other.py": "y = 2\n"})

    delta = compare(repo, target)

    assert delta.head is not None and delta.head.sha == target
    assert delta.introduced_total >= 1
    assert all(f.path == "app.py" for f in delta.findings)


def test_a_root_commit_compares_against_the_empty_tree(make_repo):
    repo = make_repo()
    root = repo.commit("seed", {"app.py": python_complex("tangle", 16)})

    delta = compare(repo, root)

    assert delta.introduced_total >= 1
    assert all(f.attribution_basis == "new_file" for f in delta.findings)


# -- filters and identity ---------------------------------------------------


def test_exclusions_and_extensions_narrow_the_compared_scope(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit(
        "add",
        {"app.py": python_complex("a", 16), "vendor/lib.py": python_complex("b", 16)},
    )

    excluded = compare(repo, "HEAD", exclude_patterns=("vendor/",))

    assert all(not f.path.startswith("vendor/") for f in excluded.findings)
    assert excluded.scope.changed == 1


def test_include_paths_keep_only_the_named_paths(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit(
        "add",
        {"app.py": python_complex("a", 16), "vendor/lib.py": python_complex("b", 16)},
    )

    included = compare(repo, "HEAD", include_paths=("vendor/",))

    assert all(f.path.startswith("vendor/") for f in included.findings)
    assert included.scope.changed == 1


def test_include_paths_match_deleted_and_renamed_files_by_their_path(make_repo):
    repo = make_repo()
    repo.commit(
        "seed",
        {"app.py": "x = 1\n", "vendor/old.py": "y = 1\n", "lib/moved.py": "z = 1\n"},
    )
    repo.remove("vendor/old.py")
    repo.move("lib/moved.py", "vendor/moved.py")
    repo.commit("change", {"app.py": "x = 2\n", "vendor/new.py": "w = 1\n"})

    included = compare(repo, "HEAD", include_paths=("vendor/",))

    # A deletion has no head path; it is kept by its base path, a rename by its new path.
    assert included.scope.changed == 3


def test_change_finding_ids_are_deterministic_across_runs(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"app.py": python_complex("tangle", 16)})

    first = compare(repo)
    second = ChangeHealthDeltaService(repo_path=str(repo.path)).compare(
        DeltaRequest(str(repo.path), "HEAD")
    )

    assert [f.change_finding_id for f in first.findings] == [
        f.change_finding_id for f in second.findings
    ]
    assert all(f.change_finding_id.startswith("chf_") for f in first.findings)


def test_the_analyzer_fingerprint_is_reported_on_both_sides(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n"})
    repo.commit("add", {"app.py": python_complex("tangle", 16)})

    delta = compare(repo)

    assert delta.fingerprint is not None
    assert delta.fingerprint.analyzer_version > 0
    assert delta.fingerprint.performance_model_version > 0
    assert delta.comparison_basis == "both_sides_analyzed"


def test_a_query_file_change_is_data_not_a_gap(make_repo):
    repo = make_repo()
    repo.commit("seed", {"queries/java.scm": "(identifier) @name\n"})
    repo.commit("edit", {"queries/java.scm": "(type_identifier) @name\n"})

    delta = compare(repo)

    assert delta.status == "unavailable"
    assert delta.skipped == {"queries/java.scm": "not_analyzable_data"}
    assert delta.code_skipped == {}
    assert "query" in delta.explanation
    assert not delta.is_clean


def test_a_query_file_beside_code_does_not_make_the_run_partial(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", "queries/java.scm": "(identifier) @a\n"})
    repo.commit("edit", {"app.py": "x = 2\n", "queries/java.scm": "(identifier) @b\n"})

    delta = compare(repo)

    assert delta.status == "available"
    assert delta.code_skipped == {}


def test_a_scheme_file_outside_queries_is_still_a_gap(make_repo):
    repo = make_repo()
    repo.commit("seed", {"src/main.scm": "(define (f x) x)\n"})
    repo.commit("edit", {"src/main.scm": "(define (f x) (+ x 1))\n"})

    delta = compare(repo)

    assert delta.skipped == {"src/main.scm": "unsupported_language"}
    assert delta.code_skipped == {"src/main.scm": "unsupported_language"}


@pytest.mark.parametrize("path", ["templates/page.j2", "views/card.hbs", "tpl/email.tmpl"])
def test_a_template_change_is_data_not_a_gap(make_repo, path):
    repo = make_repo()
    repo.commit("seed", {path: "Hello {{ name }}\n"})
    repo.commit("edit", {path: "Hi {{ name }}\n"})

    delta = compare(repo)

    assert delta.skipped == {path: "not_analyzable_data"}
    assert delta.code_skipped == {}
    assert "template" in delta.explanation


def test_a_template_beside_code_does_not_make_the_run_partial(make_repo):
    repo = make_repo()
    repo.commit("seed", {"app.py": "x = 1\n", "templates/page.j2": "{{ a }}\n"})
    repo.commit("edit", {"app.py": "x = 2\n", "templates/page.j2": "{{ b }}\n"})

    delta = compare(repo)

    assert delta.status == "available"
    assert delta.code_skipped == {}