"""Tests for v2 State/Warning quadrant hysteresis and basket reseeding keys."""

from app.services.alert_service import (
    _classify_quadrant,
    _parse_quadrant_log_key,
    _quadrant_log_key,
)


def test_fresh_classification_uses_60_60_boundaries():
    assert _classify_quadrant(20, 90, None) == "1"
    assert _classify_quadrant(70, 90, None) == "2"
    assert _classify_quadrant(20, 30, None) == "3"
    assert _classify_quadrant(70, 30, None) == "4"


def test_warning_axis_hysteresis():
    assert _classify_quadrant(20, 62, prev="3") == "3"
    assert _classify_quadrant(20, 66, prev="3") == "1"
    assert _classify_quadrant(20, 58, prev="1") == "1"
    assert _classify_quadrant(20, 54, prev="1") == "3"


def test_state_axis_hysteresis():
    assert _classify_quadrant(63, 30, prev="3") == "3"
    assert _classify_quadrant(66, 30, prev="3") == "4"
    assert _classify_quadrant(57, 30, prev="4") == "4"
    assert _classify_quadrant(54, 30, prev="4") == "3"


def test_boundary_sitting_does_not_flip():
    for quadrant in ("1", "2", "3", "4"):
        assert _classify_quadrant(60, 60, prev=quadrant) == quadrant


def test_quadrant_key_carries_basket_hash_and_parses_legacy_keys():
    key = _quadrant_log_key("3", 32.4, 54.6, "abc123")
    assert _parse_quadrant_log_key(key) == ("abc123", "3", 32.4, 54.6)
    assert _parse_quadrant_log_key("3:32.4:54.6") == (None, "3", 32.4, 54.6)
    assert _parse_quadrant_log_key("3") == (None, "3", None, None)
