Codebase rewritten to:
tangled.org/atpasser.poxiao-labs.work/atpasser
1"""Test cases for the Handle class in atpasser.uri.identifier module."""
2
3import pytest
4from atpasser.uri.identifier import Handle
5from atpasser.uri.exceptions import InvalidHandleError, ResolutionError
6
7
8class TestHandle:
9 """Test cases for the Handle class."""
10
11 def test_valid_handle_simple(self):
12 """Test creating a Handle with a valid simple format."""
13 handle_str = "example.com"
14 handle = Handle(handle_str)
15
16 assert str(handle) == handle_str
17 assert handle.handle == handle_str
18
19 def test_valid_handle_subdomain(self):
20 """Test creating a Handle with a valid subdomain format."""
21 handle_str = "subdomain.example.com"
22 handle = Handle(handle_str)
23
24 assert str(handle) == handle_str
25 assert handle.handle == handle_str
26
27 def test_valid_handle_with_hyphen(self):
28 """Test creating a Handle with a valid format containing hyphens."""
29 handle_str = "my-example.com"
30 handle = Handle(handle_str)
31
32 assert str(handle) == handle_str
33 assert handle.handle == handle_str
34
35 def test_valid_handle_with_numbers(self):
36 """Test creating a Handle with a valid format containing numbers."""
37 handle_str = "example123.com"
38 handle = Handle(handle_str)
39
40 assert str(handle) == handle_str
41 assert handle.handle == handle_str
42
43 def test_valid_handle_long_domain(self):
44 """Test creating a Handle with a valid long domain name."""
45 handle_str = "a" * 63 + "." + "b" * 63 + "." + "c" * 63 + ".com"
46 handle = Handle(handle_str)
47
48 assert str(handle) == handle_str
49 assert handle.handle == handle_str
50
51 def test_invalid_handle_too_long(self):
52 """Test that a Handle that is too long raises InvalidHandleError."""
53 # Create a handle that exceeds the 253 character limit
54 long_handle = "a" * 254
55 handle_str = f"{long_handle}.com"
56
57 with pytest.raises(InvalidHandleError, match="exceeds maximum length"):
58 Handle(handle_str)
59
60 def test_invalid_handle_no_dot_separator(self):
61 """Test that a Handle without a dot separator raises InvalidHandleError."""
62 handle_str = "example"
63
64 with pytest.raises(InvalidHandleError, match="invalid format"):
65 Handle(handle_str)
66
67 def test_invalid_handle_starts_with_dot(self):
68 """Test that a Handle starting with a dot raises InvalidHandleError."""
69 handle_str = ".example.com"
70
71 with pytest.raises(InvalidHandleError, match="invalid format"):
72 Handle(handle_str)
73
74 def test_invalid_handle_ends_with_dot(self):
75 """Test that a Handle ending with a dot raises InvalidHandleError."""
76 handle_str = "example.com."
77
78 with pytest.raises(InvalidHandleError, match="invalid format"):
79 Handle(handle_str)
80
81 def test_invalid_handle_segment_too_long(self):
82 """Test that a Handle with a segment that is too long raises InvalidHandleError."""
83 handle_str = f"{'a' * 64}.com"
84
85 with pytest.raises(InvalidHandleError, match="segment length error"):
86 Handle(handle_str)
87
88 def test_invalid_handle_segment_empty(self):
89 """Test that a Handle with an empty segment raises InvalidHandleError."""
90 handle_str = "example..com"
91
92 with pytest.raises(InvalidHandleError, match="segment length error"):
93 Handle(handle_str)
94
95 def test_invalid_handle_invalid_characters(self):
96 """Test that a Handle with invalid characters raises InvalidHandleError."""
97 handle_str = "ex@mple.com"
98
99 with pytest.raises(InvalidHandleError, match="contains invalid characters"):
100 Handle(handle_str)
101
102 def test_invalid_handle_segment_starts_with_hyphen(self):
103 """Test that a Handle with a segment starting with a hyphen raises InvalidHandleError."""
104 handle_str = "-example.com"
105
106 with pytest.raises(InvalidHandleError, match="invalid format"):
107 Handle(handle_str)
108
109 def test_invalid_handle_segment_ends_with_hyphen(self):
110 """Test that a Handle with a segment ending with a hyphen raises InvalidHandleError."""
111 handle_str = "example-.com"
112
113 with pytest.raises(InvalidHandleError, match="invalid format"):
114 Handle(handle_str)
115
116 def test_invalid_handle_tld_starts_with_digit(self):
117 """Test that a Handle with a TLD starting with a digit raises InvalidHandleError."""
118 handle_str = "example.1com"
119
120 with pytest.raises(InvalidHandleError, match="invalid format"):
121 Handle(handle_str)
122
123 def test_handle_equality(self):
124 """Test Handle equality comparison."""
125 handle_str = "example.com"
126 handle1 = Handle(handle_str)
127 handle2 = Handle(handle_str)
128
129 assert handle1 == handle2
130 assert handle1 != "not a handle object"
131
132 def test_handle_string_representation(self):
133 """Test Handle string representation."""
134 handle_str = "example.com"
135 handle = Handle(handle_str)
136
137 assert str(handle) == handle_str
138
139 def test_handle_case_insensitive_storage(self):
140 """Test that Handle stores the handle in lowercase."""
141 handle_str = "ExAmPlE.CoM"
142 handle = Handle(handle_str)
143
144 # The handle should be stored in lowercase
145 assert handle.handle == "example.com"
146 # The string representation should also return the lowercase form
147 assert str(handle) == "example.com"
148
149 def test_handle_to_tid_dns_resolution(self):
150 """Test resolving a handle to DID using DNS method."""
151 handle_str = "bsky.app"
152 handle = Handle(handle_str)
153
154 # This test may fail if there's no internet connection or if DNS resolution fails
155 try:
156 did = handle.toTID()
157 assert did is not None
158 assert str(did).startswith("did:")
159 except ResolutionError:
160 # If resolution fails, we'll skip this test
161 pytest.skip("Failed to resolve handle via DNS")
162
163 def test_handle_to_tid_http_resolution(self):
164 """Test resolving a handle to DID using HTTP method."""
165 handle_str = "blueskyweb.xyz"
166 handle = Handle(handle_str)
167
168 # This test may fail if there's no internet connection or if HTTP resolution fails
169 try:
170 did = handle.toTID()
171 assert did is not None
172 assert str(did).startswith("did:")
173 except ResolutionError:
174 # If resolution fails, we'll skip this test
175 pytest.skip("Failed to resolve handle via HTTP")
176
177 def test_handle_to_tid_unresolvable(self):
178 """Test resolving an unresolvable handle returns None."""
179 handle_str = "nonexistent-domain-12345.com"
180 handle = Handle(handle_str)
181
182 # This should return None for a non-existent domain
183 did = handle.toTID()
184 assert did is None