"""Unit tests for SSL certificate verification exceptions."""

from __future__ import annotations

import pytest

from rabbitmq_mcp_connection.connection.exceptions import (
    ConnectionError,
    SSLCertificateVerificationError,
)


class TestSSLCertificateVerificationError:
    """Test SSL certificate verification error handling (AC #6)."""

    def test_ssl_error_inherits_from_connection_error(self):
        """Test that SSLCertificateVerificationError inherits from ConnectionError."""
        error = SSLCertificateVerificationError("Test error")
        assert isinstance(error, ConnectionError)
        assert isinstance(error, SSLCertificateVerificationError)

    def test_ssl_error_with_message_only(self):
        """Test error creation with message only."""
        message = "SSL certificate verification failed"
        error = SSLCertificateVerificationError(message)

        assert str(error) == message
        assert error.details is None

    def test_ssl_error_with_message_and_details(self):
        """Test error creation with message and details."""
        message = "SSL certificate verification failed"
        details = "Certificate has expired"
        error = SSLCertificateVerificationError(message, details=details)

        expected = f"{message}: {details}"
        assert str(error) == expected
        assert error.details == details

    def test_ssl_error_expired_certificate_details(self):
        """Test error message for expired certificate (AC #6)."""
        error = SSLCertificateVerificationError(
            "SSL certificate verification failed for example.com",
            details="Certificate has expired",
        )

        assert "expired" in str(error).lower()
        assert error.details == "Certificate has expired"

    def test_ssl_error_hostname_mismatch_details(self):
        """Test error message for hostname mismatch (AC #6)."""
        error = SSLCertificateVerificationError(
            "SSL certificate verification failed for example.com",
            details="Hostname mismatch in certificate",
        )

        assert "hostname" in str(error).lower() or "mismatch" in str(error).lower()
        assert error.details == "Hostname mismatch in certificate"

    def test_ssl_error_untrusted_ca_details(self):
        """Test error message for untrusted CA (AC #6)."""
        error = SSLCertificateVerificationError(
            "SSL certificate verification failed for example.com",
            details="Untrusted or self-signed certificate",
        )

        assert "untrusted" in str(error).lower() or "self-signed" in str(error).lower()
        assert error.details == "Untrusted or self-signed certificate"

    def test_ssl_error_can_be_raised_and_caught(self):
        """Test that error can be raised and caught."""
        with pytest.raises(SSLCertificateVerificationError) as exc_info:
            raise SSLCertificateVerificationError(
                "SSL certificate verification failed", details="Test details"
            )

        assert "SSL certificate verification failed" in str(exc_info.value)
        assert exc_info.value.details == "Test details"

    def test_ssl_error_can_be_caught_as_connection_error(self):
        """Test that SSLCertificateVerificationError can be caught as ConnectionError."""
        with pytest.raises(ConnectionError) as exc_info:
            raise SSLCertificateVerificationError(
                "SSL certificate verification failed", details="Test details"
            )

        assert isinstance(exc_info.value, SSLCertificateVerificationError)
