"""Tests for phone number and email normalization."""
from src.jons_mcp_imessage.db.queries import (
normalize_for_contact_matching,
normalize_handle,
)
class TestNormalizeHandle:
"""Tests for the legacy normalize_handle() function."""
def test_email_lowercase(self) -> None:
"""Email addresses are lowercased."""
assert normalize_handle("Test@Example.COM") == "test@example.com"
def test_phone_with_formatting(self) -> None:
"""Phone numbers with formatting are stripped to digits."""
assert normalize_handle("(555) 123-4567") == "5551234567"
def test_phone_with_plus(self) -> None:
"""Phone numbers with + prefix preserve it."""
assert normalize_handle("+1 (555) 123-4567") == "+15551234567"
def test_phone_already_normalized(self) -> None:
"""Already normalized phone numbers are unchanged."""
assert normalize_handle("+15551234567") == "+15551234567"
class TestNormalizeForContactMatching:
"""Tests for the phonenumbers-based normalize_for_contact_matching() function."""
def test_email_lowercase(self) -> None:
"""Email addresses are lowercased."""
assert normalize_for_contact_matching("Test@Example.COM") == "test@example.com"
def test_us_phone_to_e164(self) -> None:
"""US phone numbers are normalized to E.164 format."""
# Use a valid US number (202-555-0100 is reserved for fictional use)
assert normalize_for_contact_matching("(202) 555-0100", "US") == "+12025550100"
def test_us_phone_with_country_code(self) -> None:
"""US phone numbers with country code are normalized to E.164."""
assert (
normalize_for_contact_matching("+1 (202) 555-0100", "US") == "+12025550100"
)
def test_international_phone(self) -> None:
"""International phone numbers are normalized to E.164."""
# UK phone number
assert (
normalize_for_contact_matching("+44 20 7946 0958", "GB") == "+442079460958"
)
def test_already_e164(self) -> None:
"""Already E.164 formatted numbers are unchanged."""
assert normalize_for_contact_matching("+12025550100", "US") == "+12025550100"
def test_invalid_fallback_to_digits(self) -> None:
"""Invalid phone numbers fall back to digit stripping."""
# Not enough digits for a valid number
assert normalize_for_contact_matching("123", "US") == "123"
def test_invalid_non_numeric(self) -> None:
"""Completely non-numeric strings fall back to empty string."""
assert normalize_for_contact_matching("not-a-number", "US") == ""
def test_region_defaults_to_us(self) -> None:
"""Region parameter defaults to US."""
# Without country code, assumes US
assert normalize_for_contact_matching("(202) 555-0100") == "+12025550100"
def test_uk_phone_with_region(self) -> None:
"""UK phone numbers work with GB region."""
# UK landline without country code
assert normalize_for_contact_matching("020 7946 0958", "GB") == "+442079460958"
def test_whitespace_stripped(self) -> None:
"""Leading/trailing whitespace is stripped."""
assert (
normalize_for_contact_matching(" (202) 555-0100 ", "US") == "+12025550100"
)
def test_email_with_whitespace(self) -> None:
"""Email whitespace is stripped and lowercased."""
assert (
normalize_for_contact_matching(" Test@Example.COM ") == "test@example.com"
)
class TestBackwardCompatibility:
"""Ensure normalize_handle() still works for existing code."""
def test_both_functions_exist(self) -> None:
"""Both normalize functions are available."""
# This ensures we haven't broken existing code
assert callable(normalize_handle)
assert callable(normalize_for_contact_matching)
def test_basic_phone_normalization_unchanged(self) -> None:
"""Basic phone normalization behavior is unchanged."""
# Legacy function behavior is preserved
assert normalize_handle("+15551234567") == "+15551234567"
assert normalize_handle("5551234567") == "5551234567"