"""Tests for database utilities."""
import pytest
from datetime import datetime, timezone
class TestCoredataTimestamp:
"""Tests for CoreData timestamp conversion."""
def test_seconds_timestamp(self):
"""Test converting a seconds-based timestamp."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import coredata_to_datetime
# 2024-01-01 00:00:00 UTC in CoreData format
# Unix: 1704067200, CoreData epoch offset: 978307200
# CoreData: 1704067200 - 978307200 = 725760000
ts = 725760000
result = coredata_to_datetime(ts)
assert result is not None
assert result.year == 2024
assert result.month == 1
assert result.day == 1
def test_nanoseconds_timestamp(self):
"""Test converting a nanoseconds-based timestamp."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import coredata_to_datetime
# Same timestamp but in nanoseconds
ts = 725760000 * 1_000_000_000
result = coredata_to_datetime(ts)
assert result is not None
assert result.year == 2024
assert result.month == 1
assert result.day == 1
def test_none_timestamp(self):
"""Test that None returns None."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import coredata_to_datetime
result = coredata_to_datetime(None)
assert result is None
def test_datetime_to_coredata(self):
"""Test converting datetime to CoreData timestamp."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import datetime_to_coredata
dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
result = datetime_to_coredata(dt)
assert result == 725760000
class TestNormalizeHandle:
"""Tests for handle normalization."""
def test_phone_with_formatting(self):
"""Test normalizing phone numbers with formatting."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import normalize_handle
assert normalize_handle("+1 (555) 123-4567") == "+15551234567"
assert normalize_handle("555-123-4567") == "5551234567"
assert normalize_handle("+44 20 7946 0958") == "+442079460958"
def test_email_normalization(self):
"""Test normalizing email addresses."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import normalize_handle
assert normalize_handle("Test@Example.COM") == "test@example.com"
assert normalize_handle(" user@domain.org ") == "user@domain.org"
def test_already_normalized(self):
"""Test that already normalized handles pass through."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import normalize_handle
assert normalize_handle("+15551234567") == "+15551234567"
assert normalize_handle("user@example.com") == "user@example.com"
class TestParseChatGuid:
"""Tests for chat GUID parsing."""
def test_one_to_one_chat(self):
"""Test parsing a 1:1 chat GUID."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import parse_chat_guid
service, is_group, identifier = parse_chat_guid("iMessage;-;+15551234567")
assert service == "iMessage"
assert is_group is False
assert identifier == "+15551234567"
def test_group_chat(self):
"""Test parsing a group chat GUID."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import parse_chat_guid
service, is_group, identifier = parse_chat_guid("iMessage;+;chat123456")
assert service == "iMessage"
assert is_group is True
assert identifier == "chat123456"
def test_sms_chat(self):
"""Test parsing an SMS chat GUID."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.db.queries import parse_chat_guid
service, is_group, identifier = parse_chat_guid("SMS;-;+15551234567")
assert service == "SMS"
assert is_group is False
assert identifier == "+15551234567"