# tests/unit/middleware/test_ipv6_host_parsing.py
"""
Unit tests for _strip_port() IPv6-safe host parsing utility.

Ensures the middleware correctly strips ports from:
- IPv4 hosts (localhost:8000)
- IPv6 hosts ([::1]:8000)
- Bare hostnames (example.com)
- Edge cases (empty, malformed brackets)
"""

import pytest

from middleware.platform_context import _strip_port


@pytest.mark.unit
@pytest.mark.middleware
class TestStripPort:
    """Test _strip_port() handles all host formats correctly."""

    def test_ipv4_with_port(self):
        assert _strip_port("127.0.0.1:8000") == "127.0.0.1"

    def test_ipv4_without_port(self):
        assert _strip_port("127.0.0.1") == "127.0.0.1"

    def test_localhost_with_port(self):
        assert _strip_port("localhost:9999") == "localhost"

    def test_localhost_without_port(self):
        assert _strip_port("localhost") == "localhost"

    def test_domain_with_port(self):
        assert _strip_port("example.com:443") == "example.com"

    def test_domain_without_port(self):
        assert _strip_port("example.com") == "example.com"

    def test_ipv6_with_brackets_and_port(self):
        assert _strip_port("[::1]:8000") == "::1"

    def test_ipv6_with_brackets_no_port(self):
        assert _strip_port("[::1]") == "::1"

    def test_ipv6_full_address_with_port(self):
        assert _strip_port("[2001:db8::1]:443") == "2001:db8::1"

    def test_ipv6_full_address_no_port(self):
        assert _strip_port("[2001:db8::1]") == "2001:db8::1"

    def test_empty_string(self):
        assert _strip_port("") == ""

    def test_bare_hostname(self):
        assert _strip_port("myhost") == "myhost"

    def test_subdomain_with_port(self):
        assert _strip_port("store.omsflow.lu:8080") == "store.omsflow.lu"

    def test_malformed_brackets_no_closing(self):
        """Malformed bracket with no closing ] returns as-is."""
        assert _strip_port("[::1") == "[::1"
