"""Tests for phase_key comparison utility."""
from core.phase import is_templated, phase_key


def test_equality_same():
    assert phase_key("1.0") == phase_key("1.0")


def test_inequality():
    assert phase_key("1.0") != phase_key("2.0")


def test_ordering_major():
    assert phase_key("0") < phase_key("1.0")
    assert phase_key("2.0") < phase_key("3.1.0")
    assert phase_key("4.0") < phase_key("5.1")
    assert phase_key("5.2") < phase_key("6")


def test_ordering_minor():
    assert phase_key("1.0") < phase_key("1.1")
    assert phase_key("1.2") < phase_key("1.3")


def test_ordering_sub():
    assert phase_key("3.1.0") < phase_key("3.1.4")
    assert phase_key("3.1.4") < phase_key("3.2.0")


def test_ordering_cross_depth():
    assert phase_key("3") < phase_key("3.0")
    assert phase_key("3.0") < phase_key("3.0.0")


def test_ordering_full_sequence():
    ordered = ["0", "1.0", "1.1", "1.2", "1.3", "2.0", "2.1",
               "3.1.0", "3.1.4", "3.2.0", "4.0", "4.1", "4.2",
               "5.1", "5.2", "6"]
    for i in range(len(ordered) - 1):
        assert phase_key(ordered[i]) < phase_key(ordered[i + 1]), \
            f"{ordered[i]} should be < {ordered[i + 1]}"


def test_gte():
    assert phase_key("3.0") >= phase_key("2.0")
    assert phase_key("2.0") >= phase_key("2.0")


def test_lte():
    assert phase_key("1.3") <= phase_key("2.0")
    assert phase_key("2.0") <= phase_key("2.0")


def test_gt():
    assert phase_key("3.1.4") > phase_key("2.1")


def test_numeric_components_sort_by_integer_value():
    assert phase_key("3.1.4") < phase_key("3.1.10")
    assert phase_key("1.0") < phase_key("1.1")
    assert phase_key("6") > phase_key("4.2")


def test_module_phase_id_does_not_crash():
    key = phase_key("mod.prep.x")
    assert isinstance(key, tuple)
    assert len(key) == 3


def test_numeric_phase_sorts_before_module_phase():
    assert phase_key("3.1.4") < phase_key("mod.prep.x")


def test_is_templated_detects_x_segments():
    assert is_templated("3.x.0") is True
    assert is_templated("3.x.3") is True
    assert is_templated("x") is True


def test_is_templated_rejects_concrete_ids():
    assert is_templated("1.0") is False
    assert is_templated("3.1.2") is False
    assert is_templated("0") is False
    assert is_templated("6") is False


def test_is_templated_empty_string_is_not_templated():
    assert is_templated("") is False


def test_is_templated_ignores_x_as_substring():
    assert is_templated("mod.prep.extra") is False
    assert is_templated("3.11.0") is False
