"""Extended tests for url_utils.py - covering IPv6, fragments, and edge cases."""

import pytest
from unittest.mock import patch


class TestNormalizeUrlIPv6:
    """Tests for IPv6 address handling in normalize_url."""

    def test_ipv6_loopback_gets_http(self):
        """IPv6 loopback [::1] should get http:// scheme."""
        from local_deep_research.utilities.url_utils import normalize_url

        with patch(
            "local_deep_research.utilities.url_utils.is_private_ip",
            return_value=True,
        ):
            result = normalize_url("[::1]:8080")
            assert result == "http://[::1]:8080"

    def test_ipv6_address_with_brackets(self):
        """IPv6 address with brackets should be handled correctly."""
        from local_deep_research.utilities.url_utils import normalize_url

        with patch(
            "local_deep_research.utilities.url_utils.is_private_ip",
            return_value=False,
        ):
            result = normalize_url("[2001:db8::1]:443")
            assert result == "https://[2001:db8::1]:443"

    def test_ipv6_address_already_has_scheme(self):
        """IPv6 URL with existing scheme should be returned unchanged."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://[::1]:11434")
        assert result == "http://[::1]:11434"


class TestNormalizeUrlFragments:
    """Tests for URL fragment/anchor handling."""

    def test_preserves_fragment(self):
        """URL with # fragment should be preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://example.com/page#section")
        assert result == "http://example.com/page#section"

    def test_preserves_query_and_fragment(self):
        """URL with both query string and fragment should be preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://example.com/page?q=test#section")
        assert result == "http://example.com/page?q=test#section"


class TestNormalizeUrlEdgeCases:
    """Additional edge cases for normalize_url."""

    def test_url_with_path_and_no_scheme(self):
        """URL with path but no scheme should get appropriate scheme."""
        from local_deep_research.utilities.url_utils import normalize_url

        with patch(
            "local_deep_research.utilities.url_utils.is_private_ip",
            return_value=False,
        ):
            result = normalize_url("example.com/api/v1/search")
            assert result == "https://example.com/api/v1/search"

    def test_url_with_encoded_characters(self):
        """URL with percent-encoded characters should be preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://example.com/path%20with%20spaces")
        assert result == "http://example.com/path%20with%20spaces"

    def test_none_url_raises_error(self):
        """None URL should raise a TypeError or ValueError."""
        from local_deep_research.utilities.url_utils import normalize_url

        with pytest.raises((ValueError, TypeError)):
            normalize_url(None)

    def test_whitespace_only_url_raises_error(self):
        """Whitespace-only URL should raise ValueError after strip."""
        from local_deep_research.utilities.url_utils import normalize_url

        # After stripping whitespace, the URL becomes empty which should raise
        # However, the function may handle this differently
        try:
            result = normalize_url("   ")
            # If it doesn't raise, it should at least produce some output
            assert isinstance(result, str)
        except (ValueError, TypeError):
            pass  # Expected behavior

    def test_url_with_port_no_scheme_external(self):
        """External hostname with port and no scheme should get https."""
        from local_deep_research.utilities.url_utils import normalize_url

        with patch(
            "local_deep_research.utilities.url_utils.is_private_ip",
            return_value=False,
        ):
            result = normalize_url("api.example.com:8443")
            assert result == "https://api.example.com:8443"

    def test_double_slash_with_private_ip(self):
        """// prefix with private IP should resolve to http."""
        from local_deep_research.utilities.url_utils import normalize_url

        with patch(
            "local_deep_research.utilities.url_utils.is_private_ip",
            return_value=True,
        ):
            result = normalize_url("//192.168.1.1:8080")
            assert result == "http://192.168.1.1:8080"

    def test_url_with_userinfo(self):
        """URL with user info (user:pass@host) should be preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://user:pass@example.com:8080/path")
        assert result == "http://user:pass@example.com:8080/path"
