1
- """Test insert_text_file_contents function ."""
1
+ """Test insert_text_file functionality using TextEditor ."""
2
2
3
3
from pathlib import Path
4
4
5
5
import pytest
6
6
7
- from mcp_text_editor import get_text_file_contents , insert_text_file_contents
7
+ from mcp_text_editor . text_editor import TextEditor
8
8
9
9
10
10
@pytest .mark .asyncio
@@ -14,20 +14,18 @@ async def test_insert_after_line(tmp_path: Path) -> None:
14
14
test_file = tmp_path / "test.txt"
15
15
test_file .write_text ("line1\n line2\n line3\n " )
16
16
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 }]}]
20
23
)
21
24
file_hash = result [str (test_file )]["file_hash" ]
22
25
23
26
# 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 "
31
29
)
32
30
33
31
assert result ["result" ] == "ok"
@@ -45,20 +43,18 @@ async def test_insert_before_line(tmp_path: Path) -> None:
45
43
test_file = tmp_path / "test.txt"
46
44
test_file .write_text ("line1\n line2\n line3\n " )
47
45
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 }]}]
51
52
)
52
53
file_hash = result [str (test_file )]["file_hash" ]
53
54
54
55
# 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 "
62
58
)
63
59
64
60
assert result ["result" ] == "ok"
@@ -76,20 +72,18 @@ async def test_insert_beyond_file_end(tmp_path: Path) -> None:
76
72
test_file = tmp_path / "test.txt"
77
73
test_file .write_text ("line1\n line2\n line3\n " )
78
74
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 }]}]
82
81
)
83
82
file_hash = result [str (test_file )]["file_hash" ]
84
83
85
84
# 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 "
93
87
)
94
88
95
89
assert result ["result" ] == "error"
@@ -99,14 +93,15 @@ async def test_insert_beyond_file_end(tmp_path: Path) -> None:
99
93
@pytest .mark .asyncio
100
94
async def test_file_not_found (tmp_path : Path ) -> None :
101
95
"""Test inserting text into a non-existent file."""
96
+ # Initialize TextEditor
97
+ editor = TextEditor ()
98
+
102
99
# 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 " ,
110
105
)
111
106
112
107
assert result ["result" ] == "error"
@@ -120,14 +115,15 @@ async def test_hash_mismatch(tmp_path: Path) -> None:
120
115
test_file = tmp_path / "test.txt"
121
116
test_file .write_text ("line1\n line2\n line3\n " )
122
117
118
+ # Initialize TextEditor
119
+ editor = TextEditor ()
120
+
123
121
# 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 " ,
131
127
)
132
128
133
129
assert result ["result" ] == "error"
0 commit comments