import pytest
import os
import base64
from unittest.mock import MagicMock
from azure_maps_mcp import get_static_map, GetStaticMapInput

# Read API Key
try:
    with open(".apikey", "r") as f:
        API_KEY = f.read().strip()
except FileNotFoundError:
    API_KEY = "dummy_key"

@pytest.fixture
def mock_context():
    return MagicMock()

@pytest.fixture(autouse=True)
def setup_env():
    os.environ["AZURE_MAPS_SUBSCRIPTION_KEY"] = API_KEY
    yield
    if "AZURE_MAPS_SUBSCRIPTION_KEY" in os.environ:
        del os.environ["AZURE_MAPS_SUBSCRIPTION_KEY"]

@pytest.mark.asyncio
async def test_get_static_map_traffic(mock_context):
    """Test retrieving a map with the Traffic layer."""
    params = GetStaticMapInput(
        centerPoint="47.610,-122.107",
        zoomLevel=12,
        mapLayer="Traffic"
    )
    
    # We can't easily inspect the internal URL construction without mocking httpx,
    # but we can verify that the call succeeds (returns valid image data)
    # which implies the parameters were valid enough for Azure.
    result = await get_static_map(params, mock_context)
    
    if "error" in result:
        pytest.fail(f"Tool returned error: {result['error']}")
    
    assert result["image_data_base64"] is not None
    img_data = base64.b64decode(result["image_data_base64"])
    assert len(img_data) > 0
