|
1 | 1 |
|
| 2 | +import asyncio |
| 3 | +import json |
| 4 | + |
2 | 5 | from freezegun import freeze_time |
3 | 6 | from mcp.shared.exceptions import MCPError |
| 7 | +from mcp.types import CallToolRequestParams, INVALID_PARAMS |
4 | 8 | import pytest |
5 | 9 | from unittest.mock import patch |
6 | 10 | from zoneinfo import ZoneInfo |
7 | 11 |
|
8 | | -from mcp_server_time.server import TimeServer, get_local_tz |
| 12 | +from mcp_server_time.server import TimeServer, create_tool_handlers, get_local_tz |
9 | 13 |
|
10 | 14 |
|
11 | 15 | @pytest.mark.parametrize( |
@@ -526,3 +530,59 @@ def test_get_local_tz_various_timezones(mock_get_localzone, timezone_name): |
526 | 530 | result = get_local_tz() |
527 | 531 | assert str(result) == timezone_name |
528 | 532 | assert isinstance(result, ZoneInfo) |
| 533 | + |
| 534 | + |
| 535 | +def test_list_tools_returns_expected_tools(): |
| 536 | + list_tools, _ = create_tool_handlers("UTC") |
| 537 | + |
| 538 | + async def _run(): |
| 539 | + return await list_tools(None, None) # type: ignore[arg-type] |
| 540 | + |
| 541 | + result = asyncio.run(_run()) |
| 542 | + assert [tool.name for tool in result.tools] == [ |
| 543 | + "get_current_time", |
| 544 | + "convert_time", |
| 545 | + ] |
| 546 | + |
| 547 | + |
| 548 | +def test_call_tool_get_current_time(): |
| 549 | + _, call_tool = create_tool_handlers("UTC") |
| 550 | + params = CallToolRequestParams( |
| 551 | + name="get_current_time", |
| 552 | + arguments={"timezone": "Europe/Warsaw"}, |
| 553 | + ) |
| 554 | + |
| 555 | + async def _run(): |
| 556 | + with freeze_time("2024-01-01 12:00:00+00:00"): |
| 557 | + return await call_tool(None, params) # type: ignore[arg-type] |
| 558 | + |
| 559 | + result = asyncio.run(_run()) |
| 560 | + payload = json.loads(result.content[0].text) |
| 561 | + assert payload["timezone"] == "Europe/Warsaw" |
| 562 | + assert payload["datetime"] == "2024-01-01T13:00:00+01:00" |
| 563 | + |
| 564 | + |
| 565 | +def test_call_tool_invalid_timezone_raises_mcp_error(): |
| 566 | + _, call_tool = create_tool_handlers("UTC") |
| 567 | + params = CallToolRequestParams( |
| 568 | + name="get_current_time", |
| 569 | + arguments={"timezone": "Invalid/Timezone"}, |
| 570 | + ) |
| 571 | + |
| 572 | + async def _run(): |
| 573 | + await call_tool(None, params) # type: ignore[arg-type] |
| 574 | + |
| 575 | + with pytest.raises(MCPError) as exc_info: |
| 576 | + asyncio.run(_run()) |
| 577 | + assert exc_info.value.code == INVALID_PARAMS |
| 578 | + |
| 579 | + |
| 580 | +def test_call_tool_unknown_tool_raises_value_error(): |
| 581 | + _, call_tool = create_tool_handlers("UTC") |
| 582 | + params = CallToolRequestParams(name="unknown_tool", arguments={}) |
| 583 | + |
| 584 | + async def _run(): |
| 585 | + await call_tool(None, params) # type: ignore[arg-type] |
| 586 | + |
| 587 | + with pytest.raises(ValueError, match="Error processing mcp-server-time query"): |
| 588 | + asyncio.run(_run()) |
0 commit comments