|
4 | 4 | from typing import List
|
5 | 5 |
|
6 | 6 | import pytest
|
| 7 | +from mcp.server import stdio |
7 | 8 | from mcp.types import TextContent, Tool
|
| 9 | +from pytest_mock import MockerFixture |
8 | 10 |
|
9 | 11 | from mcp_text_editor.server import (
|
| 12 | + app, |
10 | 13 | call_tool,
|
11 | 14 | edit_contents_handler,
|
12 | 15 | get_contents_handler,
|
13 | 16 | list_tools,
|
| 17 | + main, |
14 | 18 | )
|
15 | 19 |
|
16 | 20 |
|
@@ -335,3 +339,33 @@ async def test_edit_contents_handler_missing_hash(tmp_path):
|
335 | 339 | edit_results = json.loads(result[0].text)
|
336 | 340 | assert edit_results[str(test_file)]["result"] == "error"
|
337 | 341 | assert "Missing required field: hash" in edit_results[str(test_file)]["reason"]
|
| 342 | + |
| 343 | + |
| 344 | +@pytest.mark.asyncio |
| 345 | +async def test_main_stdio_server_error(mocker: MockerFixture): |
| 346 | + """Test main function with stdio_server error.""" |
| 347 | + # Mock the stdio_server to raise an exception |
| 348 | + mock_stdio = mocker.patch.object(stdio, "stdio_server") |
| 349 | + mock_stdio.side_effect = Exception("Stdio server error") |
| 350 | + |
| 351 | + with pytest.raises(Exception) as exc_info: |
| 352 | + await main() |
| 353 | + assert "Stdio server error" in str(exc_info.value) |
| 354 | + |
| 355 | + |
| 356 | +@pytest.mark.asyncio |
| 357 | +async def test_main_run_error(mocker: MockerFixture): |
| 358 | + """Test main function with app.run error.""" |
| 359 | + # Mock the stdio_server context manager |
| 360 | + mock_stdio = mocker.patch.object(stdio, "stdio_server") |
| 361 | + mock_context = mocker.MagicMock() |
| 362 | + mock_context.__aenter__.return_value = (mocker.MagicMock(), mocker.MagicMock()) |
| 363 | + mock_stdio.return_value = mock_context |
| 364 | + |
| 365 | + # Mock app.run to raise an exception |
| 366 | + mock_run = mocker.patch.object(app, "run") |
| 367 | + mock_run.side_effect = Exception("App run error") |
| 368 | + |
| 369 | + with pytest.raises(Exception) as exc_info: |
| 370 | + await main() |
| 371 | + assert "App run error" in str(exc_info.value) |
0 commit comments