"""
Tests for the Playwright MCP Proxy server
"""
import json
from unittest.mock import AsyncMock, Mock, patch
import pytest
from playwright_proxy_mcp.server import _call_playwright_tool, mcp
def test_server_name():
"""Test that the server has the correct name"""
assert mcp.name == "Playwright MCP Proxy"
def test_server_instructions():
"""Test that the server has instructions"""
assert mcp.instructions is not None
assert "playwright" in mcp.instructions.lower()
assert "blob" in mcp.instructions.lower()
@pytest.mark.asyncio
async def test_call_playwright_tool_no_client():
"""Test calling playwright tool when client is not initialized."""
with patch("playwright_proxy_mcp.server.proxy_client", None):
with pytest.raises(RuntimeError, match="Playwright subprocess not running"):
await _call_playwright_tool("navigate", {"url": "https://example.com"})
@pytest.mark.asyncio
async def test_call_playwright_tool_unhealthy():
"""Test calling playwright tool when client is unhealthy."""
mock_client = Mock()
mock_client.is_healthy.return_value = False
with patch("playwright_proxy_mcp.server.proxy_client", mock_client):
with pytest.raises(RuntimeError, match="Playwright subprocess not running"):
await _call_playwright_tool("navigate", {"url": "https://example.com"})
@pytest.mark.asyncio
async def test_call_playwright_tool_no_process():
"""Test calling playwright tool when process is not initialized."""
mock_client = Mock()
mock_client.is_healthy.return_value = True
mock_client.call_tool = AsyncMock(
side_effect=RuntimeError("Playwright subprocess not properly initialized")
)
with patch("playwright_proxy_mcp.server.proxy_client", mock_client):
with pytest.raises(RuntimeError, match="not properly initialized"):
await _call_playwright_tool("navigate", {"url": "https://example.com"})
@pytest.mark.asyncio
async def test_call_playwright_tool_success():
"""Test successful playwright tool call."""
mock_client = Mock()
mock_client.is_healthy.return_value = True
mock_client.call_tool = AsyncMock(
return_value={"status": "success", "data": "transformed"}
)
with patch("playwright_proxy_mcp.server.proxy_client", mock_client):
# Use playwright_ prefix to test the mapping
result = await _call_playwright_tool("playwright_navigate", {"url": "https://example.com"})
assert result == {"status": "success", "data": "transformed"}
# Verify call_tool was called with the correct tool name
mock_client.call_tool.assert_called_once_with(
"browser_navigate", {"url": "https://example.com"}
)
@pytest.mark.asyncio
async def test_call_playwright_tool_strips_prefix():
"""Test that playwright_ prefix is mapped to browser_ prefix."""
mock_client = Mock()
mock_client.is_healthy.return_value = True
mock_client.call_tool = AsyncMock(return_value={})
with patch("playwright_proxy_mcp.server.proxy_client", mock_client):
await _call_playwright_tool("playwright_navigate", {"url": "https://example.com"})
# Tool name should be mapped to browser_navigate
mock_client.call_tool.assert_called_once_with(
"browser_navigate", {"url": "https://example.com"}
)
@pytest.mark.asyncio
async def test_call_playwright_tool_error_response():
"""Test handling of error response from playwright."""
mock_client = Mock()
mock_client.is_healthy.return_value = True
mock_client.call_tool = AsyncMock(
side_effect=RuntimeError("MCP error: {'code': -1, 'message': 'Navigation failed'}")
)
with patch("playwright_proxy_mcp.server.proxy_client", mock_client):
with pytest.raises(RuntimeError, match="MCP error"):
await _call_playwright_tool("navigate", {"url": "https://example.com"})