"""
Tests for utilities/url_utils.py

Tests cover:
- URL normalization
- Scheme handling
- Private IP detection
"""

import pytest
from unittest.mock import patch


class TestNormalizeUrl:
    """Tests for normalize_url function."""

    def test_normalize_url_with_http_scheme(self):
        """Test URL with http:// scheme is returned unchanged."""
        from local_deep_research.utilities.url_utils import normalize_url

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

    def test_normalize_url_with_https_scheme(self):
        """Test URL with https:// scheme is returned unchanged."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("https://example.com")
        assert result == "https://example.com"

    def test_normalize_url_with_http_scheme_and_port(self):
        """Test URL with http:// scheme and port is returned unchanged."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://example.com:8080")
        assert result == "http://example.com:8080"

    def test_normalize_url_empty_raises_error(self):
        """Test that empty URL raises ValueError."""
        from local_deep_research.utilities.url_utils import normalize_url

        with pytest.raises(ValueError) as exc_info:
            normalize_url("")
        assert "empty" in str(exc_info.value).lower()

    def test_normalize_url_strips_whitespace(self):
        """Test that whitespace is stripped."""
        from local_deep_research.utilities.url_utils import normalize_url

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

    def test_normalize_url_malformed_http_colon(self):
        """Test URL with malformed http: (missing //) is fixed."""
        from local_deep_research.utilities.url_utils import normalize_url

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

    def test_normalize_url_malformed_https_colon(self):
        """Test URL with malformed https: (missing //) is fixed."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("https:example.com")
        assert result == "https://example.com"

    def test_normalize_url_localhost_gets_http(self):
        """Test that localhost gets 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("localhost:8080")
            assert result == "http://localhost:8080"

    def test_normalize_url_external_gets_https(self):
        """Test that external hosts get https:// 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:443")
            assert result == "https://example.com:443"

    def test_normalize_url_double_slash_prefix(self):
        """Test URL starting with // has prefix removed."""
        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")
            assert result == "https://example.com"

    def test_normalize_url_127_0_0_1(self):
        """Test that 127.0.0.1 gets 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("127.0.0.1:11434")
            assert result == "http://127.0.0.1:11434"

    def test_normalize_url_preserves_path(self):
        """Test that URL path is preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http://example.com/api/v1/search")
        assert result == "http://example.com/api/v1/search"

    def test_normalize_url_preserves_query_string(self):
        """Test that query string is preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

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