# SPDX-License-Identifier: AGPL-3.0-or-later
"""Fixture-fidelity meta-test (report-recurring-failures.md Tier-1 #5).

The meta-class that repeatedly blessed real bugs was a behavioural fixture
that skipped a guard the real boundary enforces. ``tests/fixture_fidelity.yaml``
registers every behavioural fake; this suite:

- fails when a tests/ module defines an unregistered ``*Client`` or
  ``*Executor`` fake (a new fixture lands with a fidelity row or not at all);
- runs each model-client fake through the REAL adapter declaration paths, so a
  fake whose default declarations the real boundary refuses is caught;
- requires executor fakes to declare the trusted-fixture deviations (no
  dispatch lock, no protocol validation) with a remediation batch, and fails a
  stale declaration once the deviation is closed (Phase 1.3 ratchet).
"""
from __future__ import annotations

import ast
import dataclasses
import importlib
import inspect
import math
import os

import pytest
import yaml

from orchestrator import model_adapter

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TESTS_ROOT = os.path.join(PROJECT_ROOT, "tests")
REGISTRY_PATH = os.path.join(TESTS_ROOT, "fixture_fidelity.yaml")


def _registry() -> list:
    with open(REGISTRY_PATH) as handle:
        return yaml.safe_load(handle)["fakes"]


def _test_classes(base_name: str) -> dict:
    """Module-level classes in tests/ whose bases include ``base_name``."""
    found = {}
    for name in sorted(os.listdir(TESTS_ROOT)):
        if not name.endswith(".py"):
            continue
        with open(os.path.join(TESTS_ROOT, name)) as handle:
            source = handle.read()
        tree = ast.parse(source)
        for node in ast.walk(tree):
            if not isinstance(node, ast.ClassDef):
                continue
            for base in node.bases:
                text = ast.unparse(base)
                if base_name in text:
                    found[node.name] = os.path.join("tests", name)
                    break
    return found


def _resolve(entry):
    module = importlib.import_module(entry["module"])
    return getattr(module, entry["qualname"])


def test_every_model_client_fake_is_registered():
    registry = {
        entry["qualname"]: entry
        for entry in _registry()
        if entry.get("kind") == "model_client"
    }
    defined = _test_classes("ModelClient")
    missing = sorted(set(defined) - set(registry))
    stale = sorted(set(registry) - set(defined))
    assert not missing, (
        "unregistered model-client fakes (a fixture lands with a fidelity row "
        f"or not at all): {missing}"
    )
    assert not stale, f"registry fakes that no longer exist: {stale}"


def test_every_executor_fake_is_registered():
    registry = {
        entry["qualname"]: entry
        for entry in _registry()
        if entry.get("kind") == "executor"
    }
    defined = _test_classes("ScriptedExecutor")
    # Registry rows whose module is the real source tree (for example the
    # scripted-executor base, registered as the deviation carrier) are not
    # test-defined fakes and are exempt from the existence scan.
    test_defined = {
        qual: entry
        for qual, entry in registry.items()
        if not registry[qual]["module"].startswith("orchestrator.")
    }
    missing = sorted(set(defined) - set(test_defined))
    stale = sorted(set(test_defined) - set(defined))
    assert not missing, f"unregistered executor fakes: {missing}"
    assert not stale, f"registry fakes that no longer exist: {stale}"


def _canonical_provisional():
    return {
        "node_id": "n1",
        "attempt_id": "provisional",
        "action_id": "provisional",
        "action_type": "REVIEW",
        "resolved_inputs": [],
        "prompt": "probe",
        "prompt_hash": "sha256:" + __import__("hashlib").sha256(b"probe").hexdigest(),
        "budget_commitment": {"dimension": "MODEL_COST", "hard_limit": 5.0},
    }


def test_model_client_fakes_satisfy_the_real_declaration_boundary(tmp_path):
    """Each registered fake's default declarations must be accepted by the
    real adapter paths: a fake that declares what the real boundary refuses
    would bless bugs through malformed fixtures."""
    default_completion = model_adapter.ModelCompletion(
        outputs=(),
        cost=0.01,
        units="COST_UNITS",
        model_id="model-x",
        model_version="1",
        input_tokens=1,
        output_tokens=1,
    )
    for entry in _registry():
        if entry.get("kind") != "model_client":
            continue
        fake_class = _resolve(entry)
        assert issubclass(fake_class, model_adapter.ModelClient), entry["name"]
        kwargs = {
            key: (default_completion if value == "$default_completion" else value)
            for key, value in (entry.get("construct") or {}).items()
        }
        try:
            fake = fake_class(**kwargs)
        except TypeError as error:
            raise AssertionError(
                f"{entry['name']} cannot be constructed with its declared "
                f"fidelity kwargs: {error}"
            )
        adapter = model_adapter.ModelAdapter(fake, model_id="m", model_version="1")
        declared_tokens = adapter.required_max_tokens(_canonical_provisional())
        for value in declared_tokens:
            assert math.isfinite(float(value)) and float(value) >= 0, (
                f"{entry['name']} declares a malformed token ceiling by default"
            )


def test_executor_deviations_are_declared_with_a_remediation():
    """The trusted-fixture deviations (no dispatch lock, no protocol
    validation) must be declared with a remediation batch — and a stale
    declaration fails once the deviation is closed (Phase 1.3 ratchet)."""
    from orchestrator.executor import ScriptedExecutor

    declared = {
        deviation["deviation"]
        for entry in _registry()
        if entry.get("kind") == "executor"
        for deviation in entry.get("deviations") or []
    }
    locked = hasattr(ScriptedExecutor({}), "_dispatch_lock")
    if not locked and "no-dispatch-lock" not in declared:
        pytest.fail(
            "ScriptedExecutor dispatches without the per-attempt single-flight "
            "lock the real executors hold, and no no-dispatch-lock deviation is "
            "declared in fixture_fidelity.yaml (Phase 1.3 closes this)"
        )
    if locked and "no-dispatch-lock" in declared:
        pytest.fail(
            "the no-dispatch-lock deviation is stale: the executor base now "
            "holds the lock — remove the declared deviation (Phase 1.3 ratchet)"
        )
    validated = ScriptedExecutor.requires_response_validation
    if not validated and "no-protocol-validation" not in declared:
        pytest.fail("the no-protocol-validation deviation must stay declared")
