"""Tests for the send message tool."""
import pytest
from unittest.mock import patch, MagicMock
class TestEscapeApplescriptString:
"""Tests for AppleScript string escaping."""
def test_escape_quotes(self):
"""Test escaping double quotes."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _escape_applescript_string
assert _escape_applescript_string('Say "hello"') == 'Say \\"hello\\"'
def test_escape_backslashes(self):
"""Test escaping backslashes."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _escape_applescript_string
assert _escape_applescript_string("path\\to\\file") == "path\\\\to\\\\file"
def test_escape_newlines(self):
"""Test escaping newlines."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _escape_applescript_string
assert _escape_applescript_string("line1\nline2") == "line1\\nline2"
def test_escape_combined(self):
"""Test escaping multiple special characters."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _escape_applescript_string
input_str = 'He said "hello"\nand left'
expected = 'He said \\"hello\\"\\nand left'
assert _escape_applescript_string(input_str) == expected
class TestDetectApplescriptError:
"""Tests for AppleScript error detection."""
def test_detect_not_running(self):
"""Test detecting Messages not running error."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _detect_applescript_error
from jons_mcp_imessage.exceptions import MessagesNotRunningError
error = _detect_applescript_error("Messages got an error: Application not running (-600)")
assert isinstance(error, MessagesNotRunningError)
def test_detect_permission_denied(self):
"""Test detecting automation permission denied."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _detect_applescript_error
from jons_mcp_imessage.exceptions import AutomationDeniedError
error = _detect_applescript_error("Not allowed to send Apple events (-1743)")
assert isinstance(error, AutomationDeniedError)
def test_detect_buddy_not_found(self):
"""Test detecting buddy not found error."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import _detect_applescript_error
from jons_mcp_imessage.exceptions import AppleScriptError
error = _detect_applescript_error("Can't get buddy id +15551234567 (-1728)")
assert isinstance(error, AppleScriptError)
assert "existing conversations" in str(error)
class TestSendMessage:
"""Tests for send_message tool."""
@pytest.mark.asyncio
async def test_send_validates_recipient(self):
"""Test that send_message validates recipient."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import send_message
result = await send_message(recipient="", message="Hello")
assert result["success"] is False
assert "required" in result["error"].lower()
@pytest.mark.asyncio
async def test_send_validates_message(self):
"""Test that send_message validates message."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import send_message
result = await send_message(recipient="+15551234567", message="")
assert result["success"] is False
assert "required" in result["error"].lower()
@pytest.mark.asyncio
async def test_send_validates_service(self):
"""Test that send_message validates service type."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import send_message
result = await send_message(
recipient="+15551234567",
message="Hello",
service="InvalidService"
)
assert result["success"] is False
assert "invalid service" in result["error"].lower()
@pytest.mark.asyncio
async def test_send_success_mock(self):
"""Test successful send with mocked subprocess."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import send_message
with patch('jons_mcp_imessage.tools.send._run_applescript') as mock_run:
mock_run.return_value = (True, "")
result = await send_message(
recipient="+15551234567",
message="Hello!"
)
assert result["success"] is True
assert result["recipient"] == "+15551234567"
assert "warning" in result # Should include delivery warning
@pytest.mark.asyncio
async def test_send_failure_mock(self):
"""Test failed send with mocked subprocess."""
import sys
sys.path.insert(0, 'src')
from jons_mcp_imessage.tools.send import send_message
with patch('jons_mcp_imessage.tools.send._run_applescript') as mock_run:
mock_run.return_value = (False, "Can't get buddy id +15551234567 (-1728)")
result = await send_message(
recipient="+15551234567",
message="Hello!"
)
assert result["success"] is False
assert "error" in result