#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Mechanical pre-fix verification for the counterexample registry.

"Each fix lands failing-first" is only a claim until it is enforced. For a
covered finding whose mapped regression tests exist today, this script
verifies mechanically that the tests FAIL on the pre-fix revision:

  scripts/check_counterexamples.py --discover            # record prefix revisions
  scripts/check_counterexamples.py --finding R07 --verify

``--discover`` finds each covered finding's prefix revision: the oldest commit
that introduced any of the finding's mapped test names (regression tests land
with their fixes), minus one — the revision the fix landed on top of.
``--verify`` materialises a worktree at the recorded prefix revision, copies
the CURRENT mapped test files over it (the test is a modern regression pin;
the revision carries the unfixed source), and runs the mapped nodes: at least
one must fail, or the "failing-first" claim is false.

The default run (no flags) prints every covered finding's recorded revision
and whether it resolves in git.
"""
from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
REGISTRY_PATH = PROJECT_ROOT / "tests" / "counterexamples.yaml"


def _registry() -> dict:
    import yaml

    with open(REGISTRY_PATH) as handle:
        return yaml.safe_load(handle)


def _git(*args: str, cwd: Path = PROJECT_ROOT) -> str:
    result = subprocess.run(
        ["git", *args], cwd=cwd, capture_output=True, text=True, check=False
    )
    if result.returncode != 0:
        raise SystemExit(f"git {' '.join(args)} failed: {result.stderr}")
    return result.stdout.strip()


def _test_introduced_commit(test_name: str) -> str:
    """The oldest commit whose diff contains ``test_name`` in tests/."""
    log = _git(
        "log", "--reverse", "--format=%H", "-S", test_name, "--", "tests/"
    )
    commits = [line for line in log.splitlines() if line.strip()]
    if not commits:
        raise SystemExit(f"no commit introduced {test_name!r}")
    return commits[0]


def discover() -> dict:
    """Record every covered finding's prefix revision (introduced-commit parent)."""
    registry = _registry()
    findings = registry.get("findings") or {}
    recorded = {}
    for finding_id, entry in findings.items():
        tests = entry.get("tests") or []
        if not tests:
            continue
        if entry.get("prefix_revision"):
            recorded[finding_id] = entry["prefix_revision"]
            continue
        parents = set()
        for node in tests:
            test_name = node.rsplit("::", 1)[1].split("[")[0]
            introduced = _test_introduced_commit(test_name)
            parents.add(_git("rev-parse", f"{introduced}~1"))
        if parents:
            # Ambiguous parents (tests introduced across several commits) resolve
            # to the OLDEST parent: the first revision where any mapped pin was
            # red is the pre-fix revision for the finding as a whole.
            dated = [
                (_git("show", "--no-patch", "--format=%ct", parent), parent)
                for parent in parents
            ]
            recorded[finding_id] = min(dated)[1]
        else:
            recorded[finding_id] = "UNRECORDED"
    print(json.dumps(recorded, indent=2, sort_keys=True))
    print(
        "Record these as prefix_revision in tests/counterexamples.yaml "
        "(covered entries), then run --finding <id> --verify.",
        file=sys.stderr,
    )
    return recorded


def verify(finding_id: str) -> int:
    import yaml

    with open(REGISTRY_PATH) as handle:
        registry = yaml.safe_load(handle)
    entry = (registry.get("findings") or {})[finding_id]
    prefix = entry.get("prefix_revision")
    if not prefix or not _git("cat-file", "-t", prefix) == "commit":
        print(f"{finding_id}: no resolvable prefix_revision recorded", file=sys.stderr)
        return 2
    _git("worktree", "add", "--detach", str(PROJECT_ROOT.parent / "cc-prefix"), prefix)
    worktree = PROJECT_ROOT.parent / "cc-prefix"
    try:
        # The mapped test files are copied from HEAD over the prefix revision:
        # the modern regression pins run against the unfixed source and must
        # fail (failing-first, enforced).
        for node in entry.get("tests") or []:
            test_rel = node.split("::")[0]
            source = PROJECT_ROOT / test_rel
            (worktree / test_rel).write_text(source.read_text())
        nodes = [node for node in entry.get("tests") or []]
        result = subprocess.run(
            [
                sys.executable, "-m", "pytest", *nodes, "-q", "--no-header",
                "-p", "no:cacheprovider", "--timeout", "120",
            ],
            cwd=worktree,
            capture_output=True,
            text=True,
            check=False,
        )
        if result.returncode == 0:
            print(
                f"{finding_id}: the mapped tests PASS at the pre-fix revision "
                f"{prefix} — the failing-first claim is false for this finding"
            )
            return 1
        print(
            f"{finding_id}: OK — the mapped tests fail at the pre-fix revision "
            f"{prefix} (failing-first verified)"
        )
        return 0
    finally:
        _git("worktree", "remove", "--force", str(worktree))


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--discover", action="store_true")
    parser.add_argument("--finding")
    parser.add_argument("--verify", action="store_true")
    args = parser.parse_args()
    if args.discover:
        discover()
        return 0
    if args.finding and args.verify:
        return verify(args.finding)
    registry = _registry()
    for finding_id, entry in sorted((registry.get("findings") or {}).items()):
        if entry.get("tests"):
            print(f"{finding_id}: {entry.get('prefix_revision', 'UNRECORDED')}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
