# SPDX-License-Identifier: AGPL-3.0-or-later
"""The five acceptance demonstrations of the fifth roadmap milestone.

Each test is named after and documents one demonstration listed under
"Fifth milestone: conformance hardening — the last gate before the experiment"
in docs/roadmap.md. Nothing here executes anything new; the suite proves the
committed machinery. The whole gate also runs as one command:
``python -m orchestrator.conformance``.
"""

from __future__ import annotations

import pytest

from orchestrator import conformance, oracle
from orchestrator.oracle import OracleManifestInvalid


@pytest.fixture(scope="module")
def suite_report():
    return conformance.run_full_suite()


# 1. Event replay and state-and-ledger equivalence over recorded runs from
# milestones 1-4, with the first differing field reported.


def test_event_replay_state_and_ledger_equivalence_over_the_frozen_corpus(suite_report):
    replay_checks = [
        check for check in suite_report.checks if check.name.startswith("replay-equivalence:")
    ]
    names = {check.name.split(":", 1)[1] for check in replay_checks}
    # The corpus covers the milestone 1-4 flows: decomposed children, conflicts,
    # timers, budget exhaustion, sandbox violations, and model outputs.
    for required in (
        "m1-acceptance",
        "m2-decomposition-conflicts-replan",
        "timers-budget-exhaustion",
        "sandboxed-execution",
        "sandbox-violation",
        "model-outputs",
    ):
        assert required in names, f"corpus is missing the {required} scenario"
    failures = [check for check in replay_checks if not check.passed]
    assert not failures, "; ".join(check.detail for check in failures)
    # The corpus ran under the real sandbox (real command execution inside the
    # recorded runs).
    assert sandbox_available()


def sandbox_available():
    from orchestrator import sandbox

    return sandbox.capabilities().profile_available


# 2. The complete root-state-to-run-outcome mapping exercised from recorded
# runs through the precedence rule.


def test_root_state_to_run_outcome_mapping_exercised_from_recorded_runs(suite_report):
    from orchestrator.reducer import _ROOT_CONDITION_TO_OUTCOME

    mapping_check = next(
        check for check in suite_report.checks if check.name == "root-outcome-mapping"
    )
    assert mapping_check.passed, mapping_check.detail
    # Every terminal root state of the machine maps to exactly one outcome.
    terminal_roots = {"ACCEPTED", "INFEASIBLE", "REJECTED", "CANCELLED",
                      "BUDGET_EXHAUSTED", "SUPERSEDED"}
    assert set(_ROOT_CONDITION_TO_OUTCOME) == terminal_roots
    assert len(set(_ROOT_CONDITION_TO_OUTCOME.values())) == len(terminal_roots)


# 3. Bidirectional hidden-oracle coverage: visible-accepted candidates graded,
# known-bad candidates rejected, both recorded as evidence.


def test_bidirectional_hidden_oracle_coverage_and_calibration(suite_report):
    oracle_check = next(
        check for check in suite_report.checks if check.name == "oracle-coverage-and-grading"
    )
    assert oracle_check.passed, oracle_check.detail


def test_oracle_manifest_bidirectional_coverage_validation():
    criteria = (
        oracle.OracleCriterion("AC-1", mandatory=True, weight=1.0),
        oracle.OracleCriterion("AC-2", mandatory=True, weight=2.0),
        oracle.OracleCriterion("AC-3", mandatory=True, weight=1.0,
                               independent_non_test_oracle=True),
        oracle.OracleCriterion("AC-4", mandatory=False, weight=1.0),
    )
    checks = (
        oracle.OracleCheck("hidden-1", ("AC-1",), 1.0, "PASS", 10.0),
        oracle.OracleCheck("hidden-2", ("AC-2",), 1.0, "PASS", 10.0),
        # An "independent non-test oracle" is only valid when an executable
        # check binding runs it; the declared flag alone is not a binding.
        oracle.OracleCheck("non-test-3", ("AC-3",), 1.0, "PASS", 10.0,
                           probe=lambda tree: oracle.GRADE_PASS),
        oracle.OracleCheck("hidden-4", ("AC-4",), 1.0, "PASS", 10.0, mandatory=False),
    )
    manifest = oracle.OracleManifest("oracle-v1", criteria, checks)
    manifest.validate_bidirectional_coverage(["AC-1", "AC-2", "AC-3", "AC-4"])

    # A bare declared non-test oracle with no executable binding is rejected,
    # and can never yield a vacuous success.
    flag_only = (
        oracle.OracleCriterion("AC-1", mandatory=True, weight=1.0),
        oracle.OracleCriterion("AC-3", mandatory=True, weight=1.0,
                               independent_non_test_oracle=True),
    )
    with pytest.raises(OracleManifestInvalid):
        oracle.OracleManifest("oracle-v1", flag_only, (checks[0],)).validate_bidirectional_coverage(
            ["AC-1", "AC-3"]
        )

    # Direction 1 broken: a mandatory criterion with an empty check set.
    orphan_criteria = (
        oracle.OracleCriterion("AC-1", mandatory=True, weight=1.0),
        oracle.OracleCriterion("AC-9", mandatory=True, weight=1.0),
    )
    with pytest.raises(OracleManifestInvalid):
        oracle.OracleManifest("oracle-v1", orphan_criteria, checks).validate_bidirectional_coverage(
            ["AC-1", "AC-9"]
        )
    # Direction 2 broken: a hidden check mapping to an unknown criterion.
    unknown = (oracle.OracleCheck("hidden-x", ("AC-NOPE",), 1.0, "PASS", 10.0),)
    with pytest.raises(OracleManifestInvalid):
        oracle.OracleManifest("oracle-v1", criteria, unknown).validate_bidirectional_coverage(
            ["AC-1"]
        )


# 4. Oracle grading with indeterminate outcomes and the infrastructure-only
# rerun policy; indeterminate grades never become acceptance evidence.


def test_oracle_grading_with_indeterminate_outcomes_and_infrastructure_only_rerun(suite_report):
    oracle_check = next(
        check for check in suite_report.checks if check.name == "oracle-coverage-and-grading"
    )
    assert oracle_check.passed, oracle_check.detail


# 5. The scripted controller conformance suite: one command covering every
# machine transition, every guarded alternative, the outcome-precedence cases,
# and the crash-injection boundaries.


def test_scripted_controller_conformance_suite_runs_as_one_command(suite_report):
    assert suite_report.passed, "; ".join(suite_report.failures())
    names = {check.name for check in suite_report.checks}
    assert "transition-coverage" in names
    assert "guarded-alternatives" in names
    assert "crash-boundaries" in names
    assert "root-outcome-mapping" in names
