Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.6 KB
Raw Blame
"""A test fixture that exists locally but is not committed is a silent no-op.

`tests/web/test_route_table_parity.py` is the whole reason this exists. It reads
`flask_route_table_snapshot.json`, the extracted record of the 330 routes the
pre-migration Flask app served — the only artefact that can show an old-vs-new
divergence, since nothing else has the old app to compare against. `.gitignore`
carries a blanket `tests/**/*.json`, which swallowed the snapshot when it was
added. The file sat on the author's machine, the test passed there, and in CI it
raised FileNotFoundError from the day it landed. A parity test that has never
run is worse than no parity test: it occupies the slot where one would go.

The rule here is deliberately narrow, because a looser one is useless. Two
earlier attempts at this scan flagged 36 and then 16 candidates, every one a
false positive — synthetic path strings handed to a validator, and
`parents[N]` walks up to the repo root. What actually distinguishes the bug is:

    the file EXISTS on disk, is inside the repo, and git does not track it.

A path that does not exist is a synthetic test input, not a missing fixture. A
tracked file is fine by definition. Only "present locally, absent in a fresh
clone" is the failure, and that is exactly what it asserts.
"""

# allow: no-sut-import — a repo-hygiene guard. Its subject is the relationship
# between the test tree and git's index, not any runtime behaviour of
# local_deep_research, so there is nothing to import and exercise.

import ast
import subprocess
from pathlib import Path

import pytest


REPO_ROOT = Path(__file__).resolve().parents[1]
TESTS_ROOT = REPO_ROOT / "tests"

# Extensions that indicate a data fixture rather than a code module. `.py` is
# excluded: a test referencing a .py path is almost always naming a module for
# an import assertion, not opening a fixture.
FIXTURE_SUFFIXES = {
    ".json",
    ".csv",
    ".yaml",
    ".yml",
    ".toml",
    ".txt",
    ".sql",
    ".gz",
    ".db",
    ".sqlite",
    ".sqlite3",
    ".xml",
    ".html",
}

# Vite creates this release artifact during `npm run build`; it is not a
# source fixture. Its generation and packaging have their own assertions in
# test_vite_asset_pipeline.py. Keep this exemption exact: ignored test data
# (including the route-table snapshot that motivated this guard) must still
# be reported.
GENERATED_ARTIFACTS = {
    "src/local_deep_research/web/static/dist/.vite/manifest.json",
}


def _tracked_files() -> set[str] | None:
    """Every path git tracks, or None if git cannot answer.

    None means "this environment cannot tell me", never "nothing is tracked" —
    a guard that silently passed on a git failure would be the same class of
    bug it exists to catch.
    """
    try:
        completed = subprocess.run(
            ["git", "ls-files"],
            cwd=REPO_ROOT,
            capture_output=True,
            timeout=120,
            check=False,
        )
    except (OSError, subprocess.SubprocessError):
        return None
    if completed.returncode != 0:
        return None
    return {
        line
        for line in completed.stdout.decode("utf-8", "replace").splitlines()
        if line
    }


def _literal_filenames(path: Path):
    """String literals in `path` that look like a data-fixture filename."""
    try:
        tree = ast.parse(path.read_text(encoding="utf-8", errors="replace"))
    except SyntaxError:
        return
    for node in ast.walk(tree):
        if isinstance(node, ast.Constant) and isinstance(node.value, str):
            text = node.value
            if "\n" in text or len(text) > 200:
                continue
            if Path(text).suffix.lower() in FIXTURE_SUFFIXES:
                yield text


def test_every_test_data_file_present_on_disk_is_tracked_by_git():
    tracked = _tracked_files()
    if tracked is None:
        pytest.skip("git cannot be queried here; this guard needs the index")

    assert tracked, "git ls-files returned nothing — refusing to pass vacuously"

    offenders = {}
    for test_file in TESTS_ROOT.rglob("*.py"):
        for name in _literal_filenames(test_file):
            # Resolve against the test's own directory and each ancestor up to
            # the repo root, which covers `parent / x` and `parents[N] / x`.
            base = test_file.parent
            while True:
                candidate = base / name
                if candidate.is_file():
                    try:
                        rel = candidate.resolve().relative_to(REPO_ROOT)
                    except ValueError:
                        break  # outside the repo (tmp dirs etc.) — not ours
                    key = str(rel).replace("\\", "/")
                    if key not in tracked and key not in GENERATED_ARTIFACTS:
                        offenders.setdefault(key, set()).add(
                            str(test_file.relative_to(REPO_ROOT))
                        )
                    break
                if base == REPO_ROOT:
                    break
                base = base.parent

    assert not offenders, (
        "These data files exist on disk, are referenced by a test, and are NOT "
        "tracked by git — so the test using them passes here and fails (or "
        "silently skips) in a fresh clone:\n"
        + "\n".join(
            f"  {path}\n      referenced by: {', '.join(sorted(refs))}"
            for path, refs in sorted(offenders.items())
        )
        + "\n\nFIX: commit the file. If .gitignore excludes it (the repo has a "
        "blanket `tests/**/*.json`), add a negation there AND an entry in "
        ".file-whitelist.txt — both gates apply, and the whitelist needs "
        "maintainer approval."
    )