"""Edge-case tests for url_utils.normalize_url and network_utils.is_private_ip."""


class TestNormalizeUrlIPv6:
    """IPv6 handling in normalize_url."""

    def test_bracketed_ipv6_public_gets_https(self):
        """Public IPv6 like [2607:f8b0::] should get https scheme."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("[2607:f8b0::]:8080")
        assert result.startswith("https://")
        assert "[2607:f8b0::]" in result

    def test_bracketed_ipv6_link_local_gets_http(self):
        """Link-local fe80:: should be treated as private -> http."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("[fe80::1]:8080")
        assert result.startswith("http://")


class TestNormalizeUrlMalformed:
    """Malformed URL handling."""

    def test_http_triple_slash_passthrough(self):
        """http:///path is already prefixed with http:// so it passes through."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("http:///path")
        # Starts with http:// so the early return fires
        assert result == "http:///path"

    def test_https_malformed_with_path(self):
        """https:host.com/api/v1 -> https://host.com/api/v1."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("https:host.com/api/v1")
        assert result == "https://host.com/api/v1"


class TestNormalizeUrlPreservation:
    """Query strings and fragments are preserved."""

    def test_url_with_query_parameters(self):
        """Query strings preserved through normalization."""
        from local_deep_research.utilities.url_utils import normalize_url

        result = normalize_url("https://example.com/search?q=test&page=2")
        assert "?q=test&page=2" in result

    def test_url_with_fragment(self):
        """#section fragments preserved."""
        from local_deep_research.utilities.url_utils import normalize_url

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


class TestIsPrivateIpBoundary:
    """Boundary tests for is_private_ip."""

    def test_172_15_boundary_is_public(self):
        """172.15.255.255 is just below the 172.16.0.0/12 private range."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("172.15.255.255") is False

    def test_172_16_is_private(self):
        """172.16.0.0 is the start of the private range."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("172.16.0.0") is True

    def test_169_254_link_local(self):
        """169.254.x.x (APIPA) is link-local and should be private."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("169.254.1.1") is True

    def test_empty_hostname_not_private(self):
        """Empty string edge case — not a valid IP, doesn't end with .local."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("") is False


class TestIsPrivateIpLocalSuffix:
    """Tests for .local mDNS domain detection."""

    def test_local_suffix_is_private(self):
        """printer.local is a valid mDNS name -> private."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("printer.local") is True

    def test_local_substring_is_not_private(self):
        """localnet.com should NOT be detected as private."""
        from local_deep_research.security.network_utils import is_private_ip

        assert is_private_ip("localnet.com") is False
