Files
strava-mcp-server/tests/unit/test_map.py
T
matthias 62ab00bb23
CI/CD Pipeline / Lint & Check (push) Successful in 11s
CI/CD Pipeline / Publish to PyPI (push) Successful in 13s
CI/CD Pipeline / Build & Push Docker Image (push) Successful in 1m39s
feat: render activity map directly inline using iframe srcdoc
2026-05-30 17:36:30 +02:00

92 lines
2.5 KiB
Python

from unittest.mock import AsyncMock, MagicMock
import pytest
from mcp.server.fastmcp import Context
from strava_mcp_server.tools.activities import register
class MockMCP:
def __init__(self):
self.tools = {}
def tool(self):
def decorator(func):
self.tools[func.__name__] = func
return func
return decorator
@pytest.mark.asyncio
async def test_get_activity_map_success():
mcp = MockMCP()
strava = MagicMock()
# Mock detailed activity with a valid polyline
strava.get_activity = AsyncMock(
return_value={
"name": "Evening Ride",
"map": {"polyline": "a~l~FqzbwOq}@it@..."},
"sport_type": "Ride",
}
)
strava.get_activity_streams = AsyncMock(
return_value={
"altitude": {"data": [100.0, 101.0, 102.0]},
"distance": {"data": [0.0, 100.0, 200.0]},
"latlng": {"data": [[47.1, 9.1], [47.2, 9.2], [47.3, 9.3]]},
}
)
ctx = AsyncMock(spec=Context)
# Register tools
register(mcp, strava)
# Retrieve and call get_activity_map tool
get_activity_map = mcp.tools["get_activity_map"]
result = await get_activity_map(ctx, 12345)
assert len(result) == 1
assert result[0].type == "text"
iframe_text = result[0].text
assert iframe_text.startswith('<iframe srcdoc="')
assert iframe_text.endswith('"></iframe>')
decoded_html = iframe_text.replace("&quot;", '"')
assert "<!DOCTYPE html>" in decoded_html
assert "unpkg.com/leaflet" in decoded_html
assert "latlngData" in decoded_html
assert "[47.1, 9.1]" in decoded_html
assert 'sportEmoji = "🚴"' in decoded_html
assert "L.control.layers" in decoded_html
assert "satellite" in decoded_html
assert "🟢" in decoded_html
assert "🏁" in decoded_html
@pytest.mark.asyncio
async def test_get_activity_map_no_polyline():
mcp = MockMCP()
strava = MagicMock()
# Mock activity without GPS map info
strava.get_activity = AsyncMock(
return_value={
"name": "Indoor Trainer",
"map": {"polyline": None, "summary_polyline": None},
}
)
ctx = AsyncMock(spec=Context)
# Register tools
register(mcp, strava)
# Retrieve and call get_activity_map tool
get_activity_map = mcp.tools["get_activity_map"]
result = await get_activity_map(ctx, 67890)
assert len(result) == 1
assert result[0].type == "text"
assert "⚠️ Keine GPS-Routen-Daten" in result[0].text