"""Test cases for the Handle class in atpasser.uri.identifier module.""" import pytest from atpasser.uri.identifier import Handle from atpasser.uri.exceptions import InvalidHandleError, ResolutionError class TestHandle: """Test cases for the Handle class.""" def test_valid_handle_simple(self): """Test creating a Handle with a valid simple format.""" handle_str = "example.com" handle = Handle(handle_str) assert str(handle) == handle_str assert handle.handle == handle_str def test_valid_handle_subdomain(self): """Test creating a Handle with a valid subdomain format.""" handle_str = "subdomain.example.com" handle = Handle(handle_str) assert str(handle) == handle_str assert handle.handle == handle_str def test_valid_handle_with_hyphen(self): """Test creating a Handle with a valid format containing hyphens.""" handle_str = "my-example.com" handle = Handle(handle_str) assert str(handle) == handle_str assert handle.handle == handle_str def test_valid_handle_with_numbers(self): """Test creating a Handle with a valid format containing numbers.""" handle_str = "example123.com" handle = Handle(handle_str) assert str(handle) == handle_str assert handle.handle == handle_str def test_valid_handle_long_domain(self): """Test creating a Handle with a valid long domain name.""" handle_str = "a" * 63 + "." + "b" * 63 + "." + "c" * 63 + ".com" handle = Handle(handle_str) assert str(handle) == handle_str assert handle.handle == handle_str def test_invalid_handle_too_long(self): """Test that a Handle that is too long raises InvalidHandleError.""" # Create a handle that exceeds the 253 character limit long_handle = "a" * 254 handle_str = f"{long_handle}.com" with pytest.raises(InvalidHandleError, match="exceeds maximum length"): Handle(handle_str) def test_invalid_handle_no_dot_separator(self): """Test that a Handle without a dot separator raises InvalidHandleError.""" handle_str = "example" with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_invalid_handle_starts_with_dot(self): """Test that a Handle starting with a dot raises InvalidHandleError.""" handle_str = ".example.com" with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_invalid_handle_ends_with_dot(self): """Test that a Handle ending with a dot raises InvalidHandleError.""" handle_str = "example.com." with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_invalid_handle_segment_too_long(self): """Test that a Handle with a segment that is too long raises InvalidHandleError.""" handle_str = f"{'a' * 64}.com" with pytest.raises(InvalidHandleError, match="segment length error"): Handle(handle_str) def test_invalid_handle_segment_empty(self): """Test that a Handle with an empty segment raises InvalidHandleError.""" handle_str = "example..com" with pytest.raises(InvalidHandleError, match="segment length error"): Handle(handle_str) def test_invalid_handle_invalid_characters(self): """Test that a Handle with invalid characters raises InvalidHandleError.""" handle_str = "ex@mple.com" with pytest.raises(InvalidHandleError, match="contains invalid characters"): Handle(handle_str) def test_invalid_handle_segment_starts_with_hyphen(self): """Test that a Handle with a segment starting with a hyphen raises InvalidHandleError.""" handle_str = "-example.com" with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_invalid_handle_segment_ends_with_hyphen(self): """Test that a Handle with a segment ending with a hyphen raises InvalidHandleError.""" handle_str = "example-.com" with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_invalid_handle_tld_starts_with_digit(self): """Test that a Handle with a TLD starting with a digit raises InvalidHandleError.""" handle_str = "example.1com" with pytest.raises(InvalidHandleError, match="invalid format"): Handle(handle_str) def test_handle_equality(self): """Test Handle equality comparison.""" handle_str = "example.com" handle1 = Handle(handle_str) handle2 = Handle(handle_str) assert handle1 == handle2 assert handle1 != "not a handle object" def test_handle_string_representation(self): """Test Handle string representation.""" handle_str = "example.com" handle = Handle(handle_str) assert str(handle) == handle_str def test_handle_case_insensitive_storage(self): """Test that Handle stores the handle in lowercase.""" handle_str = "ExAmPlE.CoM" handle = Handle(handle_str) # The handle should be stored in lowercase assert handle.handle == "example.com" # The string representation should also return the lowercase form assert str(handle) == "example.com" def test_handle_to_tid_dns_resolution(self): """Test resolving a handle to DID using DNS method.""" handle_str = "bsky.app" handle = Handle(handle_str) # This test may fail if there's no internet connection or if DNS resolution fails try: did = handle.toTID() assert did is not None assert str(did).startswith("did:") except ResolutionError: # If resolution fails, we'll skip this test pytest.skip("Failed to resolve handle via DNS") def test_handle_to_tid_http_resolution(self): """Test resolving a handle to DID using HTTP method.""" handle_str = "blueskyweb.xyz" handle = Handle(handle_str) # This test may fail if there's no internet connection or if HTTP resolution fails try: did = handle.toTID() assert did is not None assert str(did).startswith("did:") except ResolutionError: # If resolution fails, we'll skip this test pytest.skip("Failed to resolve handle via HTTP") def test_handle_to_tid_unresolvable(self): """Test resolving an unresolvable handle returns None.""" handle_str = "nonexistent-domain-12345.com" handle = Handle(handle_str) # This should return None for a non-existent domain did = handle.toTID() assert did is None