"""Tests for the MCP server tools.""" import json from finn_eiendom.mcp_server import ( finn_decode_unit_vector, mcp, ) def test_mcp_server_has_correct_tools(): """Assert that the MCP server has all expected tools.""" import asyncio async def check_tools(): tools = await mcp.list_tools() tool_names = {tool.name for tool in tools} expected_tools = { "finn_analyze_search", "finn_get_ad", "finn_resolve_eiendom_unit", "finn_get_eiendom_unit", "finn_get_similar_units", "finn_build_unit_vector", "finn_decode_unit_vector", } assert expected_tools.issubset(tool_names), f"Missing tools: {expected_tools - tool_names}" asyncio.run(check_tools()) def test_finn_decode_unit_vector_returns_json(): """Test that finn_decode_unit_vector returns valid JSON with expected keys.""" from unittest.mock import patch test_vector = { "lon": 10.7, "lat": 59.9, "ptype": "APARTMENT", "floor": 3, "rooms": 3, "built": 2000, "area": 80, "price": 5000000, } with patch("finn_eiendom.mcp_server.decode_unit_vector", return_value=test_vector): result = finn_decode_unit_vector("dGVzdA==") data = json.loads(result) assert "lon" in data assert "lat" in data assert "ptype" in data assert data["lat"] == 59.9 assert data["lon"] == 10.7 def test_finn_decode_unit_vector_error_handling(): """Test that finn_decode_unit_vector handles errors gracefully.""" from unittest.mock import patch with patch( "finn_eiendom.mcp_server.decode_unit_vector", side_effect=Exception("decode failed") ): result = finn_decode_unit_vector("invalid") data = json.loads(result) assert data.get("error") is True assert "message" in data