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.

6.2 KB
Raw Blame
"""Equivalence tests for WalkSnapshot vs live iter_glob.

The dynamic-hint extractor fleet issues ~40 ``iter_glob`` queries per
``extract_all``; each used to walk the tree from disk. ``WalkSnapshot``
replays one walk for all of them, and these tests pin the replay to the
live walk: same files, same directories, same order, for basename
patterns, ``/``-tail patterns, directory-name matches, and query roots
below the snapshot root.
"""

from __future__ import annotations

from pathlib import Path

import pytest

from repowise.core.fs_walk import PRUNED_DIRS_DERIVED, WalkSnapshot, iter_glob


@pytest.fixture
def tree(tmp_path: Path) -> Path:
    """A tree exercising pruning, nesting, dir matches, and a nested repo."""
    files = [
        "main.go",
        "go.mod",
        "settings.py",
        "pkg/a.go",
        "pkg/settings/local.py",
        "pkg/sub/deep/b.go",
        "tests/conftest.py",
        "tests/test_a.py",
        "tests/sub/test_b.py",
        "node_modules/dep/index.js",          # pruned dir
        "out/gen.go",                         # pruned (derived set)
        "vendored/.git/HEAD",                 # nested git repo
        "vendored/inner.go",
        "META-INF/services/com.example.Impl",
    ]
    for rel in files:
        p = tmp_path / rel
        p.parent.mkdir(parents=True, exist_ok=True)
        p.write_text("x", encoding="utf-8")
    # A directory named "settings" (django matches directories by name).
    (tmp_path / "app" / "settings").mkdir(parents=True)
    (tmp_path / "app" / "settings" / "base.py").write_text("x", encoding="utf-8")
    return tmp_path


QUERIES = [
    "*.go",
    "*.py",
    "settings.py",
    "settings",            # directory-name match
    "conftest.py",
    "go.mod",
    "tsconfig*.json",      # no matches
    "META-INF/services",   # /-tail pattern
    ("*.go", "*.py"),      # multi-pattern, one walk
]


class TestWalkSnapshotEquivalence:
    def test_matches_live_iter_glob_at_root(self, tree: Path) -> None:
        snap = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        for pattern in QUERIES:
            live = list(iter_glob(tree, pattern, prune_dirs=PRUNED_DIRS_DERIVED))
            replay = list(snap.iter_glob(tree, pattern))
            assert replay == live, pattern

    def test_matches_live_iter_glob_at_subroot(self, tree: Path) -> None:
        snap = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        for sub in ("tests", "pkg", "pkg/sub", "app"):
            sub_root = tree / sub
            for pattern in ("*.py", "*.go", "test_*.py", "settings"):
                live = list(iter_glob(sub_root, pattern, prune_dirs=PRUNED_DIRS_DERIVED))
                replay = list(snap.iter_glob(sub_root, pattern))
                assert replay == live, (sub, pattern)

    def test_pruned_and_nested_git_excluded(self, tree: Path) -> None:
        snap = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        hits = {p.name for p in snap.iter_glob(tree, ("*.go", "*.js"))}
        assert "index.js" not in hits   # node_modules pruned
        assert "gen.go" not in hits     # out pruned (derived set)
        assert "inner.go" not in hits   # nested git repo pruned

    def test_out_of_tree_root_falls_back_to_live_walk(self, tree: Path, tmp_path_factory) -> None:
        other = tmp_path_factory.mktemp("elsewhere")
        (other / "lone.go").write_text("x", encoding="utf-8")
        snap = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        assert [p.name for p in snap.iter_glob(other, "*.go")] == ["lone.go"]

    def test_from_files_matches_walk_snapshot(self, tree: Path) -> None:
        """A from_files snapshot over the walk's own file set answers every
        query with the same results (set-wise; entry order is sorted preorder
        rather than walk order, and extract_all sorts its output anyway)."""
        walk_snap = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        rel_files = [
            p.relative_to(tree).as_posix() for p in walk_snap.iter_glob(tree, "*") if p.is_file()
        ]
        list_snap = WalkSnapshot.from_files(tree, rel_files)
        for pattern in QUERIES:
            walked = {p for p in walk_snap.iter_glob(tree, pattern) if p.is_file()}
            listed = {p for p in list_snap.iter_glob(tree, pattern) if p.is_file()}
            assert listed == walked, pattern

    def test_from_files_reconstructs_directories(self, tree: Path) -> None:
        list_snap = WalkSnapshot.from_files(
            tree, ["app/settings/base.py", "pkg/settings/local.py", "pkg/sub/deep/b.go"]
        )
        # Directory-name pattern yields the reconstructed dirs.
        dirs = sorted(
            p.relative_to(tree).as_posix() for p in list_snap.iter_glob(tree, "settings")
        )
        assert dirs == ["app/settings", "pkg/settings"]
        # Subtree-rooted query serves only that subtree.
        sub = [p.name for p in list_snap.iter_glob(tree / "pkg", "*.py")]
        assert sub == ["local.py"]

    def test_from_files_hides_files_not_in_list(self, tree: Path) -> None:
        # settings.py exists on disk but is not in the fed list: invisible.
        list_snap = WalkSnapshot.from_files(tree, ["main.go"])
        assert list(list_snap.iter_glob(tree, "settings.py")) == []
        assert [p.name for p in list_snap.iter_glob(tree, "*.go")] == ["main.go"]

    def test_from_files_is_deterministic(self, tree: Path) -> None:
        rels = ["b/two.py", "a/one.py", "a/sub/three.py"]
        first = list(WalkSnapshot.from_files(tree, rels).iter_glob(tree, "*.py"))
        second = list(WalkSnapshot.from_files(tree, reversed(rels)).iter_glob(tree, "*.py"))
        assert first == second

    def test_extractor_rglob_uses_attached_snapshot(self, tree: Path) -> None:
        from repowise.core.ingestion.dynamic_hints.base import DynamicHintExtractor

        class _Probe(DynamicHintExtractor):
            name = "probe"

            def extract(self, repo_root: Path):  # pragma: no cover - unused
                return []

        probe = _Probe()
        live = list(probe._rglob(tree, "*.go"))
        probe._walk_snapshot = WalkSnapshot(tree, prune_dirs=PRUNED_DIRS_DERIVED)
        try:
            assert list(probe._rglob(tree, "*.go")) == live
        finally:
            probe._walk_snapshot = None