"""
Tests for _extract_domain() in metrics_routes.py.

Covers URL parsing, www. stripping, lowercasing, and error handling.
"""

from local_deep_research.web.routes.metrics_routes import _extract_domain


class TestExtractDomain:
    """Tests for _extract_domain()."""

    def test_normal_https_url(self):
        assert _extract_domain("https://example.com/path") == "example.com"

    def test_strips_www_prefix(self):
        assert _extract_domain("https://www.example.com") == "example.com"

    def test_lowercases_domain(self):
        assert _extract_domain("https://EXAMPLE.COM") == "example.com"

    def test_url_with_port(self):
        assert (
            _extract_domain("https://example.com:8080/path")
            == "example.com:8080"
        )

    def test_empty_string_returns_none(self):
        assert _extract_domain("") is None

    def test_none_input_returns_none(self):
        """None input should be handled defensively like other invalid URLs."""
        assert _extract_domain(None) is None

    def test_path_only_no_netloc_returns_none(self):
        assert _extract_domain("/just/a/path") is None

    def test_subdomain_preserved(self):
        assert _extract_domain("https://api.example.com") == "api.example.com"
