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.

11.1 KB
Raw Blame
"""The shared per-file dataflow service and the DataflowFacts derivation."""

from __future__ import annotations

import textwrap
from dataclasses import dataclass
from pathlib import Path

import pytest

from repowise.core.analysis.health.complexity import FileComplexity, PerfHit
from repowise.core.analysis.health.dataflow import (
    FileDataflow,
    FileDataflowCache,
    derive_facts,
    function_analysis_at,
)
from repowise.core.analysis.health.perf.promotion import apply_perf_promotions


def _require_python() -> None:
    try:
        from repowise.core.ingestion.parser import _get_language
    except Exception:
        pytest.skip("tree-sitter language pack missing for python")
    if _get_language("python") is None:
        pytest.skip("tree-sitter language pack missing for python")


def _facts(src: str, start_line: int, name: str | None = None, path: str = "x.py"):
    _require_python()
    source = textwrap.dedent(src).encode()
    analysis = function_analysis_at(path, "python", source, start_line, name)
    assert analysis is not None
    return derive_facts(analysis)


# ---------------------------------------------------------------------------
# DataflowFacts derivation
# ---------------------------------------------------------------------------


def test_dead_store_overwritten_before_use():
    facts = _facts(
        """
        def f(x):
            y = first(x)
            y = second(x)
            return y
        """,
        2,
    )
    assert [(d.var, d.line) for d in facts.dead_stores] == [("y", 3)]
    assert "y" in facts.writes
    assert "y" in facts.flows_out


def test_with_branch_assignments_are_both_live():
    facts = _facts(
        """
        def f(ctx, cond):
            with ctx.push():
                if cond:
                    result = 1
                else:
                    result = 2
            return result
        """,
        2,
    )

    dead_result_lines = {
        definition.line for definition in facts.dead_stores if definition.var == "result"
    }
    assert dead_result_lines.isdisjoint({5, 7})


def test_unreachable_lines_after_return():
    facts = _facts(
        """
        def f(x):
            if x:
                return 1
            return 2
            cleanup()
        """,
        2,
    )
    assert 6 in facts.unreachable_lines


def test_params_read_and_unused():
    facts = _facts(
        """
        def f(a, b):
            return a + 1
        """,
        2,
    )
    assert facts.params_read == ("a",)
    assert facts.params_unused == ("b",)


def test_tuple_unpack_partial_use_marks_unused_element_dead():
    facts = _facts(
        """
        def f(pair):
            a, b = pair
            return a
        """,
        2,
    )
    dead_vars = {d.var for d in facts.dead_stores}
    assert "b" in dead_vars
    assert "a" not in dead_vars


def test_reads_are_free_names_only():
    facts = _facts(
        """
        def f(x):
            y = helper(x, CONSTANT)
            return y
        """,
        2,
    )
    assert "helper" in facts.reads or "CONSTANT" in facts.reads
    assert "x" not in facts.reads  # a parameter, not a free name
    assert "y" not in facts.reads  # a local, not a free name


def test_self_referencing_assignment_keeps_prior_def_alive():
    # ``y = y + 1`` reads the PREVIOUS def; neither write is a dead store here.
    facts = _facts(
        """
        def f(x):
            y = x
            y = y + 1
            return y
        """,
        2,
    )
    assert facts.dead_stores == ()


def test_facts_are_deterministic_across_instances():
    src = textwrap.dedent(
        """
        def f(a, b):
            total = 0
            unused = a
            for item in b:
                total = total + item
            return total
        """
    ).encode()
    _require_python()
    one = derive_facts(FileDataflow("x.py", "python", src).analysis_at(2))
    two = derive_facts(FileDataflow("x.py", "python", src).analysis_at(2))
    assert one == two


# ---------------------------------------------------------------------------
# Lookup
# ---------------------------------------------------------------------------


def test_lookup_miss_returns_none():
    _require_python()
    source = b"def f(x):\n    return x\n"
    assert function_analysis_at("x.py", "python", source, 99) is None


def test_lookup_falls_back_to_unique_name_match():
    _require_python()
    source = b"\n\ndef target(x):\n    return x\n"
    analysis = function_analysis_at("x.py", "python", source, 1, name="target")
    assert analysis is not None
    assert analysis.name == "target"
    assert analysis.start_line == 3


def test_lookup_unsupported_language_is_silent():
    assert function_analysis_at("x.zig", "zig", b"fn f() {}", 1) is None


# ---------------------------------------------------------------------------
# The shared cache: one parse per file across both consumers
# ---------------------------------------------------------------------------


@dataclass
class _FileInfo:
    path: str
    abs_path: str
    language: str


@dataclass
class _ParsedFile:
    file_info: _FileInfo


def test_one_parse_when_promotion_and_extract_method_hit_same_file(tmp_path: Path, monkeypatch):
    _require_python()
    from repowise.core.analysis.health.dataflow import facts as facts_mod

    # A function that is BOTH structurally flagged (ccn >= 9) and carries a
    # promotable advisory hit, so the two consumer gates fire on one file.
    branches = "\n".join(f"        if r == {i}:\n            out.append({i})" for i in range(9))
    src = (
        "async def f(items):\n"
        "    out = []\n"
        "    for item in items:\n"
        "        r = await fetch(item.id)  # HIT\n"
        f"{branches}\n"
        "    return out\n"
    )
    p = tmp_path / "both.py"
    p.write_text(src, encoding="utf-8")
    hit_line = next(i for i, ln in enumerate(src.splitlines(), start=1) if "HIT" in ln)

    calls = {"n": 0}
    real = facts_mod.parse_source

    def _counting(*args, **kwargs):
        calls["n"] += 1
        return real(*args, **kwargs)

    monkeypatch.setattr(facts_mod, "parse_source", _counting)

    cache = FileDataflowCache()
    pf = _ParsedFile(_FileInfo(path="both.py", abs_path=str(p), language="python"))
    fcx = FileComplexity(
        functions=[],
        classes=[],
        perf_hits=[PerfHit(kind="serial_await_in_loop", line=hit_line, function="f")],
    )

    # Consumer 1: the promotion pass.
    apply_perf_promotions([(pf, fcx)], dataflow=cache)
    assert fcx.perf_hits[0].promoted is True

    # Consumer 2: the Extract Method view, through the same cache.
    flagged = cache.get(str(p), "python").flagged_analyses()
    assert flagged and flagged[0].name == "f"
    assert flagged[0].ccn >= 9

    assert calls["n"] == 1


def test_flagged_analyses_are_in_source_order():
    _require_python()

    def fn(name: str) -> str:
        body = "".join(f"    if x == {i}:\n        y = {i}\n" for i in range(10))
        return f"def {name}(x):\n    y = 0\n{body}    return y\n\n"

    source = (fn("first") + fn("second") + fn("third")).encode()
    flagged = FileDataflow("a.py", "python", source=source).flagged_analyses()

    assert [a.name for a in flagged] == ["first", "second", "third"]
    assert [a.start_line for a in flagged] == sorted(a.start_line for a in flagged)


def test_cache_construction_does_not_touch_the_file(tmp_path: Path):
    fd = FileDataflow(str(tmp_path / "missing.py"), "python")
    # Nothing read or parsed yet; a consumer call on a missing file is silent.
    assert fd.flagged_analyses() == []
    assert fd.analyses_covering({1}) == []


def test_release_drops_the_entry_and_get_rebuilds_it(tmp_path: Path):
    cache = FileDataflowCache()
    path = str(tmp_path / "a.py")
    first = cache.get(path, "python")
    assert cache.get(path, "python") is first
    cache.release(path)
    cache.release(path)  # releasing an absent entry is a no-op
    assert cache.get(path, "python") is not first


@pytest.mark.parametrize("use_async", [False, True])
def test_health_pass_releases_each_file_after_evaluating_it(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch, use_async: bool
):
    """A repository-wide pass must not hold every file's parse until it ends."""
    import asyncio

    from repowise.core.analysis.health import HealthAnalyzer
    from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder

    _require_python()
    for i in range(3):
        (tmp_path / f"m{i}.py").write_text(f"def f{i}(x):\n    return x\n", encoding="utf-8")
    parser, gb, parsed = ASTParser(), GraphBuilder(), []
    for fi in FileTraverser(tmp_path).traverse():
        pf = parser.parse_file(fi, Path(fi.abs_path).read_bytes())
        gb.add_file(pf)
        parsed.append(pf)
    gb.build()

    released: list[str] = []
    real = FileDataflowCache.release

    def _spy(self, abs_path):
        released.append(abs_path)
        real(self, abs_path)

    monkeypatch.setattr(FileDataflowCache, "release", _spy)
    analyzer = HealthAnalyzer(gb.graph(), parsed_files=parsed)
    if use_async:
        asyncio.run(analyzer.analyze_async())
    else:
        analyzer.analyze()
    assert sorted(released) == sorted(str(pf.file_info.abs_path) for pf in parsed)


# ---------------------------------------------------------------------------
# Exception paths: a protected region reaches its handlers and ``finally``
# from the state before it and from every block inside it.
# ---------------------------------------------------------------------------


def _dead(src: str) -> list[tuple[str, int]]:
    return [(d.var, d.line) for d in _facts(src, 2).dead_stores]


def test_a_write_before_try_is_live_when_the_body_may_raise_first():
    # ``g()`` may raise before ``x = 2``, so the handler reads ``x = 1``.
    dead = _dead(
        """
        def f(g, use):
            x = 1
            try:
                y = g()
                x = 2
            except Exception:
                use(x)
            return x, y
        """
    )
    assert ("x", 3) not in dead


def test_finally_reads_the_write_before_a_body_that_may_raise():
    dead = _dead(
        """
        def f(g, use):
            x = 1
            try:
                x = g()
            finally:
                use(x)
            return x
        """
    )
    assert ("x", 3) not in dead


def test_finally_reads_a_handler_write_before_the_handler_returns():
    dead = _dead(
        """
        def f(g, use):
            x = 0
            try:
                x = g()
            except Exception:
                if g():
                    x = 1
                    return None
                x = 2
            finally:
                use(x)
            return x
        """
    )
    assert ("x", 8) not in dead


def test_finally_reads_an_else_write_before_the_else_returns():
    dead = _dead(
        """
        def f(g, use):
            x = 0
            try:
                y = g()
            except Exception:
                y = None
            else:
                if y:
                    x = 2
                    return y
                x = 3
            finally:
                use(x)
            return x
        """
    )
    assert ("x", 10) not in dead