"""
Tests for utilities/type_utils.py

Tests cover:
- to_bool function with various input types
- String representations
- Default values
- Edge cases
"""


class TestToBool:
    """Tests for to_bool function."""

    def test_bool_true_passthrough(self):
        """Test that True passes through unchanged."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(True) is True

    def test_bool_false_passthrough(self):
        """Test that False passes through unchanged."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(False) is False

    def test_string_true(self):
        """Test string 'true' converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("true") is True

    def test_string_false(self):
        """Test string 'false' converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("false") is False

    def test_string_yes(self):
        """Test string 'yes' converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("yes") is True

    def test_string_no(self):
        """Test string 'no' converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("no") is False

    def test_string_one(self):
        """Test string '1' converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("1") is True

    def test_string_zero(self):
        """Test string '0' converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("0") is False

    def test_string_on(self):
        """Test string 'on' converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("on") is True

    def test_string_off(self):
        """Test string 'off' converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("off") is False

    def test_string_enabled(self):
        """Test string 'enabled' converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("enabled") is True

    def test_string_disabled(self):
        """Test string 'disabled' converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("disabled") is False

    def test_string_case_insensitive(self):
        """Test string conversion is case insensitive."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("TRUE") is True
        assert to_bool("True") is True
        assert to_bool("YES") is True
        assert to_bool("Yes") is True

    def test_integer_one(self):
        """Test integer 1 converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(1) is True

    def test_integer_zero(self):
        """Test integer 0 converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(0) is False

    def test_integer_positive(self):
        """Test positive integers convert to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(5) is True
        assert to_bool(100) is True

    def test_integer_negative(self):
        """Test negative integers convert to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(-1) is True

    def test_none_uses_default_false(self):
        """Test None uses default False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(None) is False

    def test_none_uses_default_true(self):
        """Test None uses custom default True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(None, default=True) is True

    def test_empty_string(self):
        """Test empty string converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("") is False

    def test_whitespace_string(self):
        """Test whitespace string converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("   ") is False

    def test_arbitrary_string(self):
        """Test arbitrary string converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("random") is False
        assert to_bool("anything") is False

    def test_float_zero(self):
        """Test float 0.0 converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(0.0) is False

    def test_float_nonzero(self):
        """Test non-zero float converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(1.0) is True
        assert to_bool(0.5) is True

    def test_empty_list(self):
        """Test empty list converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool([]) is False

    def test_non_empty_list(self):
        """Test non-empty list converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool([1, 2, 3]) is True

    def test_empty_dict(self):
        """Test empty dict converts to False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool({}) is False

    def test_non_empty_dict(self):
        """Test non-empty dict converts to True."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool({"key": "value"}) is True


class TestToBoolWhitespaceHandling:
    """Tests for whitespace handling in to_bool.

    Environment variables often have trailing whitespace from shell parsing,
    config files, or copy-paste errors. The to_bool function should handle
    these gracefully.
    """

    def test_leading_trailing_spaces(self):
        """Test truthy values with leading/trailing spaces."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("  true  ") is True
        assert to_bool("  yes  ") is True
        assert to_bool("  1  ") is True

    def test_tab_characters(self):
        """Test truthy values with tab characters."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("\ttrue\t") is True
        assert to_bool("\tyes\t") is True

    def test_newline_characters(self):
        """Test truthy values with newline characters (common in env vars)."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("true\n") is True
        assert to_bool("\ntrue") is True
        assert to_bool("\ntrue\n") is True

    def test_mixed_whitespace(self):
        """Test truthy values with mixed whitespace."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(" \t yes \n ") is True
        assert to_bool("\t\n  on  \t\n") is True

    def test_whitespace_around_falsy_values(self):
        """Test that whitespace around falsy strings still returns False."""
        from local_deep_research.utilities.type_utils import to_bool

        # These aren't in the truthy set, so should be False
        assert to_bool("  false  ") is False
        assert to_bool("\tno\t") is False
        assert to_bool("  off  ") is False

    def test_only_whitespace_returns_false(self):
        """Test that whitespace-only strings return False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("  ") is False
        assert to_bool("\t\t\t") is False
        assert to_bool("\n\n") is False
        assert to_bool(" \t \n ") is False


class TestToBoolNumericStringEdgeCases:
    """Tests for numeric string edge cases.

    Important distinction: string "2" should be False (not in truthy set),
    but integer 2 should be True (non-zero integer).
    """

    def test_string_two_returns_false(self):
        """Test string '2' returns False (not in truthy set)."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("2") is False

    def test_string_fortytwo_returns_false(self):
        """Test arbitrary numeric string returns False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("42") is False

    def test_string_negative_one_returns_false(self):
        """Test string '-1' returns False (not in truthy set)."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("-1") is False

    def test_string_float_returns_false(self):
        """Test float as string returns False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("3.14") is False
        assert to_bool("0.0") is False
        assert to_bool("1.0") is False

    def test_string_zero_with_leading_zeros(self):
        """Test zero with leading zeros."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("00") is False
        assert to_bool("000") is False

    def test_string_one_with_leading_zeros(self):
        """Test one with leading zeros still returns False (not exact '1')."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("01") is False
        assert to_bool("001") is False

    def test_string_plus_one(self):
        """Test '+1' returns False (not exact '1')."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("+1") is False


class TestToBoolPartialStringMatches:
    """Tests for partial string matches (security concern).

    These values contain truthy keywords but should NOT match because
    we use exact string comparison, not substring matching.
    """

    def test_enable_without_d(self):
        """Test 'enable' (without 'd') returns False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("enable") is False

    def test_truthy_with_suffix(self):
        """Test truthy values with suffixes return False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("enabled_on") is False
        assert to_bool("true_value") is False
        assert to_bool("yes_please") is False

    def test_truthy_with_prefix(self):
        """Test truthy values with prefixes return False."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("x_true") is False
        assert to_bool("is_on") is False
        assert to_bool("say_yes") is False

    def test_contains_on_but_not_word_on(self):
        """Test strings that contain 'on' but aren't the word 'on'."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("honest") is False
        assert to_bool("onset") is False
        assert to_bool("condition") is False
        assert to_bool("python") is False

    def test_contains_yes_but_not_word_yes(self):
        """Test strings that contain 'yes' but aren't the word 'yes'."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("yesterday") is False
        assert to_bool("yes_or_no") is False


class TestToBoolReturnTypeValidation:
    """Tests to ensure to_bool always returns actual bool type."""

    def test_returns_actual_bool_for_string_true(self):
        """Test that string 'true' returns actual bool, not truthy value."""
        from local_deep_research.utilities.type_utils import to_bool

        result = to_bool("true")
        assert result is True
        assert isinstance(result, bool)
        assert type(result) is bool

    def test_returns_actual_bool_for_string_false(self):
        """Test that string 'false' returns actual bool."""
        from local_deep_research.utilities.type_utils import to_bool

        result = to_bool("false")
        assert result is False
        assert isinstance(result, bool)
        assert type(result) is bool

    def test_returns_actual_bool_for_integer(self):
        """Test that integer returns actual bool."""
        from local_deep_research.utilities.type_utils import to_bool

        result = to_bool(1)
        assert result is True
        assert isinstance(result, bool)

        result = to_bool(0)
        assert result is False
        assert isinstance(result, bool)

    def test_returns_actual_bool_for_none(self):
        """Test that None with default returns actual bool."""
        from local_deep_research.utilities.type_utils import to_bool

        result = to_bool(None, default=True)
        assert result is True
        assert isinstance(result, bool)


class TestToBoolDefaultParameterEdgeCases:
    """Tests for default parameter behavior."""

    def test_default_only_used_for_none(self):
        """Test that default is only used for None, not empty string."""
        from local_deep_research.utilities.type_utils import to_bool

        # Empty string should return False, NOT the default
        assert to_bool("", default=True) is False

    def test_default_not_used_for_explicit_false(self):
        """Test that default is not used when value is explicit false."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("false", default=True) is False
        assert to_bool("0", default=True) is False

    def test_default_not_used_for_empty_list(self):
        """Test that default is not used for empty collections."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool([], default=True) is False
        assert to_bool({}, default=True) is False

    def test_default_used_only_for_none(self):
        """Test that default is used for None."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool(None, default=True) is True
        assert to_bool(None, default=False) is False


class TestToBoolSpecialStringValues:
    """Tests for special string values that might appear in config."""

    def test_json_like_values(self):
        """Test JSON-like string values."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("null") is False
        assert to_bool("undefined") is False
        assert to_bool("NaN") is False

    def test_human_like_responses(self):
        """Test human-like response strings."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("absolutely") is False
        assert to_bool("definitely") is False
        assert to_bool("nope") is False
        assert to_bool("maybe") is False

    def test_case_variations_of_truthy(self):
        """Test various case variations of truthy values."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("TRUE") is True
        assert to_bool("True") is True
        assert to_bool("tRuE") is True
        assert to_bool("YES") is True
        assert to_bool("Yes") is True
        assert to_bool("yEs") is True
        assert to_bool("ON") is True
        assert to_bool("On") is True
        assert to_bool("ENABLED") is True
        assert to_bool("Enabled") is True

    def test_case_variations_of_falsy(self):
        """Test various case variations of falsy string values."""
        from local_deep_research.utilities.type_utils import to_bool

        # These aren't in truthy set, so all return False
        assert to_bool("FALSE") is False
        assert to_bool("False") is False
        assert to_bool("NO") is False
        assert to_bool("No") is False
        assert to_bool("OFF") is False
        assert to_bool("Off") is False
        assert to_bool("DISABLED") is False
        assert to_bool("Disabled") is False


class TestToBoolEnvironmentVariableSimulation:
    """Tests simulating real environment variable scenarios."""

    def test_env_var_with_trailing_newline(self):
        """Test env var value with trailing newline (common from shell)."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("true\n") is True
        assert to_bool("false\n") is False

    def test_env_var_with_carriage_return(self):
        """Test env var value with Windows-style line ending."""
        from local_deep_research.utilities.type_utils import to_bool

        assert to_bool("true\r\n") is True
        assert to_bool("true\r") is True

    def test_env_var_quoted_value(self):
        """Test that quoted values are treated as arbitrary strings."""
        from local_deep_research.utilities.type_utils import to_bool

        # If someone accidentally includes quotes in the env var value
        assert to_bool('"true"') is False
        assert to_bool("'true'") is False
