Skip to content

Commit 7d460a7

Browse files
committed
Refactor MCP Text Editor: Remove unused async functions and update tests to utilize TextEditor directly. This change simplifies the codebase by eliminating the get_text_file_contents and insert_text_file_contents functions from the main module, enhancing test clarity and maintainability.
1 parent 1da3740 commit 7d460a7

File tree

2 files changed

+42
-69
lines changed

2 files changed

+42
-69
lines changed

src/mcp_text_editor/__init__.py

-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""MCP Text Editor Server package."""
22

33
import asyncio
4-
from typing import Any, Dict, List
54

65
from .server import main
76
from .text_editor import TextEditor
@@ -13,25 +12,3 @@
1312
def run() -> None:
1413
"""Run the MCP Text Editor Server."""
1514
asyncio.run(main())
16-
17-
18-
# Export functions
19-
async def get_text_file_contents(
20-
request: Dict[str, List[Dict[str, Any]]]
21-
) -> Dict[str, Any]:
22-
"""Get text file contents with line range specification."""
23-
return await _text_editor.read_multiple_ranges(
24-
ranges=request["files"],
25-
encoding="utf-8",
26-
)
27-
28-
29-
async def insert_text_file_contents(request: Dict[str, Any]) -> Dict[str, Any]:
30-
"""Insert text content before or after a specific line in a file."""
31-
return await _text_editor.insert_text_file_contents(
32-
file_path=request["file_path"],
33-
file_hash=request["file_hash"],
34-
after=request.get("after"),
35-
before=request.get("before"),
36-
contents=request["contents"],
37-
)

tests/test_insert_text_file.py

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"""Test insert_text_file_contents function."""
1+
"""Test insert_text_file functionality using TextEditor."""
22

33
from pathlib import Path
44

55
import pytest
66

7-
from mcp_text_editor import get_text_file_contents, insert_text_file_contents
7+
from mcp_text_editor.text_editor import TextEditor
88

99

1010
@pytest.mark.asyncio
@@ -14,20 +14,18 @@ async def test_insert_after_line(tmp_path: Path) -> None:
1414
test_file = tmp_path / "test.txt"
1515
test_file.write_text("line1\nline2\nline3\n")
1616

17-
# Get the current hash
18-
result = await get_text_file_contents(
19-
{"files": [{"file_path": str(test_file), "ranges": [{"start": 1}]}]}
17+
# Initialize TextEditor
18+
editor = TextEditor()
19+
20+
# Read file to get hash
21+
result = await editor.read_multiple_ranges(
22+
[{"file_path": str(test_file), "ranges": [{"start": 1}]}]
2023
)
2124
file_hash = result[str(test_file)]["file_hash"]
2225

2326
# Insert text after line 2
24-
result = await insert_text_file_contents(
25-
{
26-
"file_path": str(test_file),
27-
"file_hash": file_hash,
28-
"after": 2,
29-
"contents": "new_line\n",
30-
}
27+
result = await editor.insert_text_file_contents(
28+
file_path=str(test_file), file_hash=file_hash, after=2, contents="new_line\n"
3129
)
3230

3331
assert result["result"] == "ok"
@@ -45,20 +43,18 @@ async def test_insert_before_line(tmp_path: Path) -> None:
4543
test_file = tmp_path / "test.txt"
4644
test_file.write_text("line1\nline2\nline3\n")
4745

48-
# Get the current hash
49-
result = await get_text_file_contents(
50-
{"files": [{"file_path": str(test_file), "ranges": [{"start": 1}]}]}
46+
# Initialize TextEditor
47+
editor = TextEditor()
48+
49+
# Read file to get hash
50+
result = await editor.read_multiple_ranges(
51+
[{"file_path": str(test_file), "ranges": [{"start": 1}]}]
5152
)
5253
file_hash = result[str(test_file)]["file_hash"]
5354

5455
# Insert text before line 2
55-
result = await insert_text_file_contents(
56-
{
57-
"file_path": str(test_file),
58-
"file_hash": file_hash,
59-
"before": 2,
60-
"contents": "new_line\n",
61-
}
56+
result = await editor.insert_text_file_contents(
57+
file_path=str(test_file), file_hash=file_hash, before=2, contents="new_line\n"
6258
)
6359

6460
assert result["result"] == "ok"
@@ -76,20 +72,18 @@ async def test_insert_beyond_file_end(tmp_path: Path) -> None:
7672
test_file = tmp_path / "test.txt"
7773
test_file.write_text("line1\nline2\nline3\n")
7874

79-
# Get the current hash
80-
result = await get_text_file_contents(
81-
{"files": [{"file_path": str(test_file), "ranges": [{"start": 1}]}]}
75+
# Initialize TextEditor
76+
editor = TextEditor()
77+
78+
# Read file to get hash
79+
result = await editor.read_multiple_ranges(
80+
[{"file_path": str(test_file), "ranges": [{"start": 1}]}]
8281
)
8382
file_hash = result[str(test_file)]["file_hash"]
8483

8584
# Try to insert text after line 10 (file has only 3 lines)
86-
result = await insert_text_file_contents(
87-
{
88-
"file_path": str(test_file),
89-
"file_hash": file_hash,
90-
"after": 10,
91-
"contents": "new_line\n",
92-
}
85+
result = await editor.insert_text_file_contents(
86+
file_path=str(test_file), file_hash=file_hash, after=10, contents="new_line\n"
9387
)
9488

9589
assert result["result"] == "error"
@@ -99,14 +93,15 @@ async def test_insert_beyond_file_end(tmp_path: Path) -> None:
9993
@pytest.mark.asyncio
10094
async def test_file_not_found(tmp_path: Path) -> None:
10195
"""Test inserting text into a non-existent file."""
96+
# Initialize TextEditor
97+
editor = TextEditor()
98+
10299
# Try to insert text into a non-existent file
103-
result = await insert_text_file_contents(
104-
{
105-
"file_path": str(tmp_path / "nonexistent.txt"),
106-
"file_hash": "any_hash",
107-
"after": 1,
108-
"contents": "new_line\n",
109-
}
100+
result = await editor.insert_text_file_contents(
101+
file_path=str(tmp_path / "nonexistent.txt"),
102+
file_hash="any_hash",
103+
after=1,
104+
contents="new_line\n",
110105
)
111106

112107
assert result["result"] == "error"
@@ -120,14 +115,15 @@ async def test_hash_mismatch(tmp_path: Path) -> None:
120115
test_file = tmp_path / "test.txt"
121116
test_file.write_text("line1\nline2\nline3\n")
122117

118+
# Initialize TextEditor
119+
editor = TextEditor()
120+
123121
# Try to insert text with incorrect hash
124-
result = await insert_text_file_contents(
125-
{
126-
"file_path": str(test_file),
127-
"file_hash": "incorrect_hash",
128-
"after": 1,
129-
"contents": "new_line\n",
130-
}
122+
result = await editor.insert_text_file_contents(
123+
file_path=str(test_file),
124+
file_hash="incorrect_hash",
125+
after=1,
126+
contents="new_line\n",
131127
)
132128

133129
assert result["result"] == "error"

0 commit comments

Comments
 (0)