From 21b3dfd8b2cdb0ecea1cd0e7fcfd48faafba5ebd Mon Sep 17 00:00:00 2001 From: synapz-org Date: Sun, 26 Oct 2025 09:37:08 -0400 Subject: [PATCH] feat: add validator error cases and twitter.com support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added comprehensive test coverage for URL validation: - Test for invalid URL formats (missing /i/, missing space ID, malformed URLs) - Test for twitter.com domain support (in addition to x.com) Updated validator implementation: - Extended regex pattern to accept both x.com and twitter.com domains - Updated group capture index to account for domain alternation All validator tests passing (3/3). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/validator.py | 6 +++--- tests/test_validator.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/validator.py b/src/validator.py index 8ad40e9..c55c655 100644 --- a/src/validator.py +++ b/src/validator.py @@ -12,12 +12,12 @@ def validate_space_url(url: str) -> Tuple[bool, Optional[str], Optional[str]]: Returns: Tuple of (is_valid, space_id, error_message) """ - # Pattern for X Space URLs - pattern = r"https://x\.com/i/spaces/([a-zA-Z0-9]+)" + # Pattern for X Space URLs (supports both x.com and twitter.com) + pattern = r"https://(x|twitter)\.com/i/spaces/([a-zA-Z0-9]+)" match = re.match(pattern, url) if not match: return False, None, "Invalid URL format. Expected: https://x.com/i/spaces/{SPACE_ID}" - space_id = match.group(1) + space_id = match.group(2) # Changed from group(1) to group(2) return True, space_id, None diff --git a/tests/test_validator.py b/tests/test_validator.py index 75dfd67..e15ef55 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -10,3 +10,29 @@ def test_validate_space_url_valid(): assert is_valid is True assert space_id == "1ZkKzZWnVRRKv" assert error is None + + +def test_validate_space_url_invalid_format(): + """Test that invalid URLs are rejected.""" + invalid_urls = [ + "https://x.com/spaces/1ZkKzZWnVRRKv", # Missing /i/ + "https://x.com/i/spaces/", # Missing space ID + "not a url", # Completely invalid + "", # Empty string + ] + + for url in invalid_urls: + is_valid, space_id, error = validate_space_url(url) + assert is_valid is False + assert space_id is None + assert "Invalid URL format" in error + + +def test_validate_space_url_twitter_domain(): + """Test that twitter.com URLs are also accepted.""" + url = "https://twitter.com/i/spaces/1ZkKzZWnVRRKv" + is_valid, space_id, error = validate_space_url(url) + + assert is_valid is True + assert space_id == "1ZkKzZWnVRRKv" + assert error is None