Skip to content

Commit eee9291

Browse files
committed
fix: add JSON serialization test for delete handler
To prevent potential JSON serialization errors with EditResult objects, add a test case that verifies the handler correctly converts EditResult to a serializable format using to_dict(). - Add test_delete_text_file_contents_handler_success - Add json import to test file - Verify response structure and content
1 parent bd9bcfd commit eee9291

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/mcp_text_editor/handlers/delete_text_file_contents.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,16 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
106106
)
107107

108108
# Execute deletion using the service
109-
result = self.editor.service.delete_text_file_contents(request)
109+
result_dict = self.editor.service.delete_text_file_contents(request)
110110

111-
return [TextContent(type="text", text=json.dumps(result, indent=2))]
111+
# Convert EditResults to dictionaries
112+
serializable_result = {}
113+
for file_path, edit_result in result_dict.items():
114+
serializable_result[file_path] = edit_result.to_dict()
115+
116+
return [
117+
TextContent(type="text", text=json.dumps(serializable_result, indent=2))
118+
]
112119

113120
except Exception as e:
114121
logger.error(f"Error processing request: {str(e)}")

tests/test_delete_file_contents.py

+42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for delete_text_file_contents functionality."""
22

3+
import json
4+
35
import pytest
46

57
from mcp_text_editor.models import DeleteTextFileContentsRequest, FileRange
@@ -301,3 +303,43 @@ def delete_text_file_contents(self, request):
301303
await handler.run_tool(arguments)
302304

303305
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

Comments
 (0)