|
1 | 1 | """Tests for delete_text_file_contents functionality."""
|
2 | 2 |
|
| 3 | +import json |
| 4 | + |
3 | 5 | import pytest
|
4 | 6 |
|
5 | 7 | from mcp_text_editor.models import DeleteTextFileContentsRequest, FileRange
|
@@ -301,3 +303,43 @@ def delete_text_file_contents(self, request):
|
301 | 303 | await handler.run_tool(arguments)
|
302 | 304 |
|
303 | 305 | assert "Error processing request: Mock error during delete" in str(exc_info.value)
|
| 306 | + |
| 307 | + |
| 308 | +@pytest.mark.asyncio |
| 309 | +async def test_delete_text_file_contents_handler_success(tmp_path): |
| 310 | + """Test successful execution of DeleteTextFileContentsHandler including JSON serialization.""" |
| 311 | + from mcp_text_editor.handlers.delete_text_file_contents import ( |
| 312 | + DeleteTextFileContentsHandler, |
| 313 | + ) |
| 314 | + from mcp_text_editor.models import EditResult |
| 315 | + from mcp_text_editor.service import TextEditorService |
| 316 | + from mcp_text_editor.text_editor import TextEditor |
| 317 | + |
| 318 | + class MockService(TextEditorService): |
| 319 | + def delete_text_file_contents(self, request): |
| 320 | + return { |
| 321 | + request.file_path: EditResult(result="ok", hash="new_hash", reason=None) |
| 322 | + } |
| 323 | + |
| 324 | + editor = TextEditor() |
| 325 | + editor.service = MockService() |
| 326 | + handler = DeleteTextFileContentsHandler(editor) |
| 327 | + |
| 328 | + test_file = tmp_path / "test.txt" |
| 329 | + test_file.write_text("test content") |
| 330 | + |
| 331 | + arguments = { |
| 332 | + "file_path": str(test_file), |
| 333 | + "file_hash": "some_hash", |
| 334 | + "ranges": [{"start": 1, "end": 1, "range_hash": "hash1"}], |
| 335 | + } |
| 336 | + |
| 337 | + result = await handler.run_tool(arguments) |
| 338 | + assert len(result) == 1 |
| 339 | + assert result[0].type == "text" |
| 340 | + |
| 341 | + # Check if response is JSON serializable |
| 342 | + response = json.loads(result[0].text) |
| 343 | + assert str(test_file) in response |
| 344 | + assert response[str(test_file)]["result"] == "ok" |
| 345 | + assert response[str(test_file)]["hash"] == "new_hash" |
0 commit comments