Skip to content

Commit baaf290

Browse files
committed
test(patch): add test for end=None case and update schema
1 parent f02326f commit baaf290

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/mcp_text_editor/handlers/patch_text_file_contents.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_tool_description(self) -> Tool:
4646
"description": "Starting line number (1-based).it should match the range hash.",
4747
},
4848
"end": {
49-
"type": ["integer", "null"],
49+
"type": "integer",
5050
"description": "Ending line number (null for end of file).it should match the range hash.",
5151
},
5252
"contents": {
@@ -58,7 +58,7 @@ def get_tool_description(self) -> Tool:
5858
"description": "Hash of the content being replaced. it should get from get_text_file_contents tool with the same start and end.",
5959
},
6060
},
61-
"required": ["start", "contents", "range_hash"],
61+
"required": ["start", "end", "contents", "range_hash"],
6262
},
6363
},
6464
"encoding": {
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test module for patch_text_file with end=None."""
2+
3+
import os
4+
5+
import pytest
6+
7+
from mcp_text_editor.text_editor import TextEditor
8+
9+
10+
@pytest.mark.asyncio
11+
async def test_patch_text_file_end_none(tmp_path):
12+
"""Test patching text file with end=None."""
13+
# Create a test file
14+
file_path = os.path.join(tmp_path, "test.txt")
15+
editor = TextEditor()
16+
17+
# Create initial content
18+
content = "line1\nline2\nline3\nline4\nline5\n"
19+
with open(file_path, "w", encoding="utf-8") as f:
20+
f.write(content)
21+
22+
# Get file hash and range hash
23+
file_info = await editor.read_multiple_ranges(
24+
[
25+
{
26+
"file_path": str(file_path),
27+
"ranges": [{"start": 2, "end": None}], # Test with end=None
28+
}
29+
]
30+
)
31+
32+
# Extract file and range hashes
33+
file_content = file_info[str(file_path)]
34+
file_hash = file_content["file_hash"]
35+
range_hash = file_content["ranges"][0]["range_hash"]
36+
37+
# Patch the file
38+
new_content = "new line2\nnew line3\nnew line4\nnew line5\n"
39+
patch = {
40+
"start": 2,
41+
"end": None, # Test with end=None
42+
"contents": new_content,
43+
"range_hash": range_hash,
44+
}
45+
result = await editor.edit_file_contents(
46+
str(file_path),
47+
file_hash,
48+
[patch],
49+
)
50+
51+
# Verify the patch was successful
52+
assert result["result"] == "ok"
53+
with open(file_path, "r", encoding="utf-8") as f:
54+
updated_content = f.read()
55+
assert updated_content == "line1\nnew line2\nnew line3\nnew line4\nnew line5\n"

0 commit comments

Comments
 (0)