# SPDX-License-Identifier: AGPL-3.0-or-later
"""Meta-test for the counterexample registry.

``tests/counterexamples.yaml`` maps every finding in
``docs/report-milestone8-readiness.md`` to the permanent regression test(s)
that pin its corrected behaviour, or to an explicit pending remediation batch.
This test makes the registry authoritative: a mapping cannot be deleted, a
mapped test cannot disappear, and a covered finding cannot be silently demoted
to pending.
"""
from __future__ import annotations

import os
import re
import subprocess
import sys

import yaml

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
REGISTRY_PATH = os.path.join(PROJECT_ROOT, "tests", "counterexamples.yaml")
REPORT_PATH = os.path.join(PROJECT_ROOT, "docs", "report-milestone8-readiness.md")

FINDING_ID = re.compile(r"^\|\s*(R\d{2})\s*\|", re.MULTILINE)


def _load_registry() -> dict:
    with open(REGISTRY_PATH) as handle:
        return yaml.safe_load(handle)


def _report_finding_ids() -> set[str]:
    with open(REPORT_PATH) as handle:
        return set(FINDING_ID.findall(handle.read()))


def _collected_node_ids() -> set[str]:
    result = subprocess.run(
        [sys.executable, "-m", "pytest", "--collect-only", "-q", "-p", "no:cacheprovider"],
        cwd=PROJECT_ROOT,
        capture_output=True,
        text=True,
        check=False,
    )
    assert result.returncode == 0, f"pytest --collect-only failed:\n{result.stdout}\n{result.stderr}"
    return {
        line.strip()
        for line in result.stdout.splitlines()
        if line.startswith("tests/") and "::" in line
    }


def test_registry_covers_every_reported_finding_exactly():
    registry = _load_registry()
    registered = set(registry["findings"])
    reported = _report_finding_ids()
    assert reported, "no findings parsed from the readiness report"
    assert registered == reported, (
        f"registry/report mismatch: missing={sorted(reported - registered)} "
        f"extra={sorted(registered - reported)}"
    )


def test_registry_entries_are_well_formed():
    registry = _load_registry()
    for finding_id, entry in registry["findings"].items():
        assert entry.get("title"), f"{finding_id} has no title"
        tests = entry.get("tests")
        if tests:
            assert isinstance(tests, list), f"{finding_id}.tests must be a list"
            assert not entry.get("remediation_batch"), (
                f"{finding_id} is covered and must not name a remediation batch"
            )
        else:
            assert entry.get("remediation_batch"), (
                f"{finding_id} is pending and must name a remediation_batch"
            )


def test_registry_min_covered_floor_is_declared_once():
    """YAML's default duplicate-key handling is last-wins; a duplicated
    `min_covered` silently changes the effective floor and must be visible."""
    with open(REGISTRY_PATH, encoding="utf-8") as handle:
        lines = handle.readlines()
    declarations = [
        line for line in lines if line.startswith("min_covered:")
    ]
    assert len(declarations) == 1, (
        f"min_covered must be declared exactly once, got {declarations!r}"
    )


def test_every_mapped_test_exists():
    registry = _load_registry()
    collected = _collected_node_ids()
    assert collected, "pytest collected no test node ids"
    missing = {
        finding_id: [node for node in entry.get("tests", []) if node not in collected]
        for finding_id, entry in registry["findings"].items()
        if entry.get("tests")
    }
    missing = {finding_id: nodes for finding_id, nodes in missing.items() if nodes}
    assert not missing, f"mapped tests no longer collect: {missing}"


def test_covered_findings_do_not_fall_below_the_floor():
    registry = _load_registry()
    covered = [
        finding_id
        for finding_id, entry in registry["findings"].items()
        if entry.get("tests")
    ]
    assert len(covered) >= registry["min_covered"], (
        f"only {len(covered)} findings are covered; floor is {registry['min_covered']}"
    )


def test_covered_findings_record_a_prefix_revision():
    """Failing-first, enforced: every covered finding names the revision its
    regression tests were red on. scripts/check_counterexamples.py --verify
    runs the mechanical red-check against it on demand."""
    registry = _load_registry()
    unrecorded = [
        finding_id
        for finding_id, entry in registry["findings"].items()
        if entry.get("tests") and not entry.get("prefix_revision")
    ]
    assert not unrecorded, (
        f"covered findings without a recorded prefix revision: {unrecorded}"
    )
    bad = {
        finding_id: entry["prefix_revision"]
        for finding_id, entry in registry["findings"].items()
        if entry.get("prefix_revision", "").startswith(("AMBIGUOUS", "UNRECORDED"))
    }
    assert not bad, f"unresolved prefix revisions: {bad}"
