from __future__ import annotations

from supply_infra.pipeline.dag import PIPELINE_STEPS
from supply_infra.pipeline.gates import evaluate_step_gate


def test_pipeline_has_eleven_strictly_ordered_steps() -> None:
    assert len(PIPELINE_STEPS) == 11
    assert PIPELINE_STEPS[0].key == "global_tree_sync"
    assert PIPELINE_STEPS[-1].key == "video_discovery"
    assert "aigc_write_record" not in {step.key for step in PIPELINE_STEPS}
    for previous, current in zip(PIPELINE_STEPS, PIPELINE_STEPS[1:]):
        assert current.dependencies == (previous.key,)
        assert current.critical is True


def test_explicit_step_failure_fails_closed() -> None:
    decision = evaluate_step_gate(
        "global_tree_sync",
        {"success": False, "error": "source missing"},
    )
    assert decision.passed is False
    assert decision.error_code == "step_reported_failure"


def test_video_discovery_gate_rejects_business_failures() -> None:
    decision = evaluate_step_gate(
        "video_discovery",
        {"success": True, "failed": 2},
    )
    assert decision.passed is False
    assert decision.error_code == "business_failures"
