|
| 1 | +# Tests for mcp-server-time's entry points (#4855): `main()` in `__init__.py`, |
| 2 | +# `python -m mcp_server_time` (`__main__.py`), and the `mcp-server-time` |
| 3 | +# console script. |
| 4 | +# |
| 5 | +# `main()` and both `__main__` paths run in-process with `serve` patched out, |
| 6 | +# so coverage measures them and nothing reads real stdin. The console script |
| 7 | +# gets the one thin subprocess smoke test: it is the only way to prove the |
| 8 | +# installed entry point boots and speaks MCP over real stdio. |
| 9 | + |
| 10 | +import os |
| 11 | +import runpy |
| 12 | +import shutil |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 16 | + |
| 17 | +import pytest |
| 18 | +from mcp import ClientSession, StdioServerParameters |
| 19 | +from mcp.client.stdio import stdio_client |
| 20 | + |
| 21 | +import mcp_server_time |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize( |
| 25 | + "argv,expected_local_timezone", |
| 26 | + [ |
| 27 | + ([], None), |
| 28 | + (["--local-timezone", "Europe/Paris"], "Europe/Paris"), |
| 29 | + (["--local-timezone=America/Chicago"], "America/Chicago"), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_main_passes_local_timezone_to_serve( |
| 33 | + argv: list[str], expected_local_timezone: str | None |
| 34 | +) -> None: |
| 35 | + serve = AsyncMock() |
| 36 | + with ( |
| 37 | + patch.object(mcp_server_time, "serve", serve), |
| 38 | + patch.object(sys, "argv", ["mcp-server-time", *argv]), |
| 39 | + ): |
| 40 | + mcp_server_time.main() |
| 41 | + serve.assert_awaited_once_with(expected_local_timezone) |
| 42 | + |
| 43 | + |
| 44 | +def test_main_help_describes_the_server(capsys: pytest.CaptureFixture[str]) -> None: |
| 45 | + serve = AsyncMock() |
| 46 | + with ( |
| 47 | + patch.object(mcp_server_time, "serve", serve), |
| 48 | + patch.object(sys, "argv", ["mcp-server-time", "--help"]), |
| 49 | + pytest.raises(SystemExit) as exit_info, |
| 50 | + ): |
| 51 | + mcp_server_time.main() |
| 52 | + assert exit_info.value.code == 0 |
| 53 | + out = capsys.readouterr().out |
| 54 | + assert ( |
| 55 | + "give a model the ability to handle time queries and timezone conversions" |
| 56 | + in out |
| 57 | + ) |
| 58 | + assert "--local-timezone" in out |
| 59 | + serve.assert_not_awaited() |
| 60 | + |
| 61 | + |
| 62 | +def test_main_rejects_unknown_arguments(capsys: pytest.CaptureFixture[str]) -> None: |
| 63 | + serve = AsyncMock() |
| 64 | + with ( |
| 65 | + patch.object(mcp_server_time, "serve", serve), |
| 66 | + patch.object(sys, "argv", ["mcp-server-time", "--bogus"]), |
| 67 | + pytest.raises(SystemExit) as exit_info, |
| 68 | + ): |
| 69 | + mcp_server_time.main() |
| 70 | + assert exit_info.value.code == 2 |
| 71 | + assert "unrecognized arguments: --bogus" in capsys.readouterr().err |
| 72 | + serve.assert_not_awaited() |
| 73 | + |
| 74 | + |
| 75 | +def test_python_dash_m_calls_main() -> None: |
| 76 | + # What `python -m mcp_server_time` executes. |
| 77 | + main = MagicMock() |
| 78 | + with patch.object(mcp_server_time, "main", main): |
| 79 | + runpy.run_module("mcp_server_time", run_name="__main__") |
| 80 | + main.assert_called_once_with() |
| 81 | + |
| 82 | + |
| 83 | +def test_init_module_run_as_main_calls_main() -> None: |
| 84 | + # `__init__.py` carries its own `if __name__ == "__main__"` guard. Running |
| 85 | + # it as a module re-executes it in a fresh namespace, which imports |
| 86 | + # `serve` from `.server` again, so the patch goes there. |
| 87 | + serve = AsyncMock() |
| 88 | + with ( |
| 89 | + patch("mcp_server_time.server.serve", serve), |
| 90 | + patch.object(sys, "argv", ["mcp-server-time", "--local-timezone", "UTC"]), |
| 91 | + ): |
| 92 | + runpy.run_module("mcp_server_time.__init__", run_name="__main__") |
| 93 | + serve.assert_awaited_once_with("UTC") |
| 94 | + |
| 95 | + |
| 96 | +async def test_console_script_boots_over_stdio() -> None: |
| 97 | + script = shutil.which("mcp-server-time", path=str(Path(sys.executable).parent)) |
| 98 | + assert script is not None, "the console script is not installed in this venv" |
| 99 | + params = StdioServerParameters( |
| 100 | + command=script, |
| 101 | + args=["--local-timezone", "UTC"], |
| 102 | + env={**os.environ}, |
| 103 | + ) |
| 104 | + with open(os.devnull, "w") as errlog: |
| 105 | + async with stdio_client(params, errlog=errlog) as (read, write): |
| 106 | + async with ClientSession(read, write) as session: |
| 107 | + init = await session.initialize() |
| 108 | + tools = await session.list_tools() |
| 109 | + result = await session.call_tool( |
| 110 | + "get_current_time", {"timezone": "UTC"} |
| 111 | + ) |
| 112 | + assert init.model_dump(by_alias=True, mode="json")["serverInfo"]["name"] == ( |
| 113 | + "mcp-time" |
| 114 | + ) |
| 115 | + assert [tool.name for tool in tools.tools] == ["get_current_time", "convert_time"] |
| 116 | + assert result.model_dump(by_alias=True, mode="json")["isError"] is False |
0 commit comments