"""ORCH-076 (ORCH-52c) TC-16 / AC-6: the pipeline invariants are untouched.

ORCH-52c only unifies the MECHANISM of YAML-frontmatter parsing behind a single
API; it must NOT change the composition of the stage machine or the QG registry.
This guard fails first if a future edit drifts either contract.
"""

import os

os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")

from src.qg.checks import QG_CHECKS  # noqa: E402
from src.stages import STAGE_TRANSITIONS  # noqa: E402

_EXPECTED_QGS = {
    "check_analysis_approved",
    "check_analysis_complete",
    "check_architecture_done",
    "check_ci_green",
    "check_review_approved",
    "check_tests_passed",
    "check_reviewer_verdict",
    "check_tests_local",
    "check_deploy_status",
    "check_staging_status",
    "check_branch_mergeable",
    "check_staging_image_fresh",
    "check_security_gate",
    "check_coverage_gate",
}

_EXPECTED_TRANSITIONS = {
    "created":        {"next": "analysis",        "agent": "analyst",   "qg": None},
    "analysis":       {"next": "architecture",    "agent": "architect", "qg": "check_analysis_approved"},
    "architecture":   {"next": "development",     "agent": "developer", "qg": "check_architecture_done"},
    "development":    {"next": "review",          "agent": "reviewer",  "qg": "check_ci_green"},
    "review":         {"next": "testing",         "agent": "tester",    "qg": "check_reviewer_verdict"},
    "testing":        {"next": "deploy-staging",  "agent": "deployer",  "qg": "check_tests_passed"},
    "deploy-staging": {"next": "deploy",          "agent": "deployer",  "qg": "check_staging_status"},
    "deploy":         {"next": "done",            "agent": None,        "qg": "check_deploy_status"},
    "done":           {"next": None,              "agent": None,        "qg": None},
    # ORCH-090 (adr-0026): terminal SINK for a STOP-cancelled task (parallel to
    # `done`; not a new edge — no exit-gate changed).
    "cancelled":      {"next": None,              "agent": None,        "qg": None},
}


def test_tc16_qg_registry_unchanged():
    assert set(QG_CHECKS.keys()) == _EXPECTED_QGS


def test_tc16_qg_callables():
    for name, fn in QG_CHECKS.items():
        assert callable(fn), f"QG {name} is not callable"


def test_tc16_stage_transitions_unchanged():
    assert STAGE_TRANSITIONS == _EXPECTED_TRANSITIONS
