"""Test cases for the URI class in atpasser.uri module.""" import pytest from atpasser.uri import URI from atpasser.uri.exceptions import InvalidURIError, ValidationError class TestURI: """Test cases for the URI class.""" def test_valid_uri_with_did(self): """Test creating a URI with a valid DID.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri = URI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" assert uri.query is None assert uri.queryAsText is None assert uri.fragment is None assert uri.fragmentAsText is None def test_valid_uri_with_handle(self): """Test creating a URI with a valid handle.""" uri_str = "at://bnewbold.bsky.team/app.bsky.feed.post/3jwdwj2ctlk26" uri = URI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "bnewbold.bsky.team" assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" def test_valid_uri_with_collection_only(self): """Test creating a URI with only a collection.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" uri = URI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == ["app.bsky.feed.post"] assert uri.pathAsText == "app.bsky.feed.post" def test_valid_uri_with_authority_only(self): """Test creating a URI with only an authority.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur" uri = URI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == [] assert uri.pathAsText == "" def test_valid_uri_with_query(self): """Test creating a URI with query parameters.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post?param1=value1¶m2=value2" uri = URI(uri_str) assert uri.query == {"param1": ["value1"], "param2": ["value2"]} assert uri.queryAsText == "param1%3Dvalue1%26param2%3Dvalue2" def test_valid_uri_with_fragment(self): """Test creating a URI with a fragment.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26#$.some.json.path" uri = URI(uri_str) assert uri.fragment is not None assert uri.fragmentAsText == "%24.some.json.path" def test_invalid_uri_non_ascii_characters(self): """Test that non-ASCII characters in URI raise InvalidURIError.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/💩" with pytest.raises(InvalidURIError, match="contains invalid characters"): URI(uri_str) def test_invalid_uri_too_long(self): """Test that a URI that is too long raises InvalidURIError.""" # Create a URI that exceeds the 8000 character limit long_path = "a" * 8000 uri_str = f"at://did:plc:z72i7hdynmk6r22z27h6tvur/{long_path}" with pytest.raises(InvalidURIError, match="exceeds maximum length"): URI(uri_str) def test_invalid_uri_wrong_scheme(self): """Test that a URI with wrong scheme raises InvalidURIError.""" uri_str = ( "https://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) with pytest.raises(InvalidURIError, match="invalid format"): URI(uri_str) def test_invalid_uri_trailing_slash(self): """Test that a URI with trailing slash raises InvalidURIError.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/" with pytest.raises(InvalidURIError, match="cannot end with a slash"): URI(uri_str) def test_invalid_uri_with_userinfo(self): """Test that a URI with userinfo raises InvalidURIError.""" uri_str = "at://user:pass@did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" with pytest.raises(InvalidURIError, match="does not support user information"): URI(uri_str) def test_uri_equality(self): """Test URI equality comparison.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri1 = URI(uri_str) uri2 = URI(uri_str) assert uri1 == uri2 assert uri1 != "not a uri object" def test_uri_string_representation(self): """Test URI string representation.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri = URI(uri_str) assert str(uri) == uri_str