"""Tests for mode-params string parser."""

from app.engine.mode_params import parse_mode_params


class TestParseModeParams:
    """Parse compact parameter strings into structured dicts."""

    def test_standard_params(self):
        """D30M50B.1G.2 => standard Licence G1 params."""
        result = parse_mode_params("D30M50B.1G.2")
        assert result["duration_days"] == 30
        assert result["majority_pct"] == 50
        assert result["base_exponent"] == 0.1
        assert result["gradient_exponent"] == 0.2
        # Optional criteria absent
        assert result["smith_exponent"] is None
        assert result["techcomm_exponent"] is None

    def test_with_smith_exponent(self):
        """D30M50B.1G.2S.1 => standard + smith_exponent=0.1."""
        result = parse_mode_params("D30M50B.1G.2S.1")
        assert result["duration_days"] == 30
        assert result["majority_pct"] == 50
        assert result["base_exponent"] == 0.1
        assert result["gradient_exponent"] == 0.2
        assert result["smith_exponent"] == 0.1
        assert result["techcomm_exponent"] is None

    def test_with_techcomm_exponent(self):
        """D30M50B.1G.2T.1 => standard + techcomm_exponent=0.1."""
        result = parse_mode_params("D30M50B.1G.2T.1")
        assert result["duration_days"] == 30
        assert result["majority_pct"] == 50
        assert result["base_exponent"] == 0.1
        assert result["gradient_exponent"] == 0.2
        assert result["smith_exponent"] is None
        assert result["techcomm_exponent"] == 0.1

    def test_full_params_with_constant(self):
        """D30M50B1G.5C10 => integer base, gradient=0.5, constant=10."""
        result = parse_mode_params("D30M50B1G.5C10")
        assert result["duration_days"] == 30
        assert result["majority_pct"] == 50
        assert result["base_exponent"] == 1.0
        assert result["gradient_exponent"] == 0.5
        assert result["constant_base"] == 10.0

    def test_empty_string_defaults(self):
        """Empty string returns all defaults."""
        result = parse_mode_params("")
        assert result["duration_days"] == 30
        assert result["majority_pct"] == 50
        assert result["base_exponent"] == 0.1
        assert result["gradient_exponent"] == 0.2
        assert result["constant_base"] == 0.0
        assert result["smith_exponent"] is None
        assert result["techcomm_exponent"] is None
        assert result["ratio_multiplier"] is None
        assert result["is_ratio_mode"] is False

    def test_whitespace_only_returns_defaults(self):
        """Whitespace-only string treated as empty."""
        result = parse_mode_params("   ")
        assert result["duration_days"] == 30

    def test_roundtrip_consistency(self):
        """Parsing a standard string then re-checking all keys."""
        result = parse_mode_params("D30M50B.1G.2")
        expected_keys = {
            "duration_days", "majority_pct", "base_exponent",
            "gradient_exponent", "constant_base", "smith_exponent",
            "techcomm_exponent", "ratio_multiplier", "is_ratio_mode",
        }
        assert set(result.keys()) == expected_keys
