"""Unit tests for Chat route helper functions.

Tests verify the helper functions used in chat routes:
- _parse_int_param: Integer parameter parsing with validation

Password retrieval is delegated to web.auth.password_utils.get_user_password
and is covered by tests/web/auth/test_password_utils_gaps.py.
"""


class TestParseIntParam:
    """Tests for the _parse_int_param helper function."""

    def test_parse_int_param_valid_value(self):
        """Test parsing a valid integer string."""
        from src.local_deep_research.chat.routes import _parse_int_param

        result = _parse_int_param("42", default=10)
        assert result == 42

    def test_parse_int_param_default_on_invalid(self):
        """Test that invalid values return the default."""
        from src.local_deep_research.chat.routes import _parse_int_param

        # Non-numeric string
        result = _parse_int_param("abc", default=10)
        assert result == 10

        # Empty string
        result = _parse_int_param("", default=15)
        assert result == 15

    def test_parse_int_param_respects_min_val(self):
        """Test that values below min_val are clamped."""
        from src.local_deep_research.chat.routes import _parse_int_param

        # Value below minimum
        result = _parse_int_param("-5", default=10, min_val=0)
        assert result == 0

        # Value at minimum
        result = _parse_int_param("0", default=10, min_val=0)
        assert result == 0

    def test_parse_int_param_respects_max_val(self):
        """Test that values above max_val are clamped."""
        from src.local_deep_research.chat.routes import _parse_int_param

        # Value above maximum
        result = _parse_int_param("200", default=10, min_val=0, max_val=100)
        assert result == 100

        # Value at maximum
        result = _parse_int_param("100", default=10, min_val=0, max_val=100)
        assert result == 100

    def test_parse_int_param_handles_none(self):
        """Test that None values return the default."""
        from src.local_deep_research.chat.routes import _parse_int_param

        result = _parse_int_param(None, default=25)
        assert result == 25

    def test_parse_int_param_handles_float_string(self):
        """Test that float strings fall back to default."""
        from src.local_deep_research.chat.routes import _parse_int_param

        result = _parse_int_param("3.14", default=10)
        assert result == 10
