"""Tests from the acme-crm-mcp demo's suites. Run: pytest test_automation_practice.py"""
from automation_practice import AUTH_REQUIRED, OK, TOOL_ERROR, eval_gate, release_decision, smoke, suites_for

SMOKE = [
    ("valid token: tools/list", OK),
    ("search_contacts returns results", OK),
    ("no token: authentication required", AUTH_REQUIRED),
    ("unknown tool: tool error", TOOL_ERROR),
    ("invalid email: tool error, nothing saved", TOOL_ERROR),
]


def test_smoke_all_pass():
    code, lines = smoke([(n, e, e) for n, e in SMOKE])
    assert code == 0
    assert lines[0] == "✓ valid token: tools/list" and lines[-1] == "5 passed, 0 failed"


def test_smoke_catches_a_server_that_stopped_checking_tokens():
    checks = [(n, e, e) for n, e in SMOKE]
    checks[2] = ("no token: authentication required", AUTH_REQUIRED, OK)   # it answered without a token
    code, lines = smoke(checks)
    assert code == 1
    assert "✗ no token: authentication required (exit 0, expected 3)" in lines
    assert lines[-1] == "4 passed, 1 failed"


def test_eval_gate_on_the_weakest_model():
    assert eval_gate({"claude-opus-5-5": [True] * 5}) == (0, "gate: weakest model 100% ≥ 95% → PASS")
    # the video's worked example: 91% blocks, whatever the average
    turns = {"a": [True] * 98 + [False] * 2, "b": [True] * 96 + [False] * 4, "c": [True] * 91 + [False] * 9}
    assert eval_gate(turns) == (1, "gate: weakest model 91% < 95% → BLOCK RELEASE")
    assert eval_gate({"claude-opus-5-5": [True] * 5, "claude-haiku-4-5": []}) == \
        (2, "eval failed: no turns for claude-haiku-4-5")


def test_which_suites_when():
    assert suites_for("commit") == ["contract", "scorer"]
    assert suites_for("deploy") == ["smoke"]
    assert suites_for("release") == ["contract", "scorer", "tool-use eval"]
    assert suites_for("nightly") == []


def test_release_decision():
    assert release_decision({"contract": 0, "scorer": 0, "tool-use eval": 0}) == ("ship", [])
    assert release_decision({"contract": 0, "scorer": 0, "tool-use eval": 1}) == \
        ("block", ["tool-use gate: weakest model below threshold"])
    assert release_decision({"contract": 0, "scorer": 1, "tool-use eval": 0}) == \
        ("block", ["scorer tests failed: the eval can't be trusted"])
    # couldn't measure is not a pass
    assert release_decision({"contract": 0, "scorer": 0, "tool-use eval": 2}) == \
        ("block", ["tool-use eval didn't complete: no verdict"])
    assert release_decision({"contract": 1, "scorer": 0}) == \
        ("block", ["tool-use eval didn't run", "contract tests failed: a real bug"])
