Skip to content

Commit f967720

Browse files
committed
test(tests/test_server.py): add tests for error handling in content handlers to ensure robustness
1 parent 339317b commit f967720

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/test_server.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,50 @@ async def test_edit_contents_handler_empty_patches():
288288
edit_results = json.loads(result[0].text)
289289
assert edit_results["test.txt"]["result"] == "error"
290290
assert edit_results["test.txt"]["hash"] is None
291+
292+
293+
@pytest.mark.asyncio
294+
async def test_get_contents_handler_legacy_missing_args():
295+
"""Test GetTextFileContents handler with legacy single file request missing arguments."""
296+
with pytest.raises(RuntimeError) as exc_info:
297+
await get_contents_handler.run_tool({})
298+
assert "Missing required argument: files" in str(exc_info.value)
299+
300+
301+
@pytest.mark.asyncio
302+
async def test_edit_contents_handler_missing_path():
303+
"""Test EditTextFileContents handler with missing path in file operation."""
304+
edit_args = {
305+
"files": [
306+
{
307+
"hash": "any_hash",
308+
"patches": [{"contents": "New content\n"}],
309+
}
310+
]
311+
}
312+
313+
with pytest.raises(RuntimeError) as exc_info:
314+
await edit_contents_handler.run_tool(edit_args)
315+
assert "Missing required field: path" in str(exc_info.value)
316+
317+
318+
@pytest.mark.asyncio
319+
async def test_edit_contents_handler_missing_hash(tmp_path):
320+
"""Test EditTextFileContents handler with missing hash in file operation."""
321+
# Create a test file
322+
test_file = tmp_path / "test.txt"
323+
test_file.write_text("test content")
324+
325+
edit_args = {
326+
"files": [
327+
{
328+
"path": str(test_file),
329+
"patches": [{"contents": "New content\n"}],
330+
}
331+
]
332+
}
333+
334+
result = await edit_contents_handler.run_tool(edit_args)
335+
edit_results = json.loads(result[0].text)
336+
assert edit_results[str(test_file)]["result"] == "error"
337+
assert "Missing required field: hash" in edit_results[str(test_file)]["reason"]

0 commit comments

Comments
 (0)