Skip to content

Commit 2416318

Browse files
committed
test: add tests for TextEditor initialization and file editing functionality to ensure proper error handling and editing capabilities
feat: implement dictionary patching for editing existing and new files in TextEditor to enhance flexibility in file modifications
1 parent 7f62936 commit 2416318

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

tests/test_text_editor.py

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,15 +894,91 @@ def mock_open(*args, **kwargs):
894894
@pytest.mark.asyncio
895895
async def test_initialization_with_environment_error(monkeypatch):
896896
"""Test TextEditor initialization when environment validation fails."""
897-
897+
898898
def mock_validate_environment(self):
899899
raise EnvironmentError("Failed to validate environment")
900-
900+
901901
# Patch the _validate_environment method
902902
monkeypatch.setattr(TextEditor, "_validate_environment", mock_validate_environment)
903-
903+
904904
# Verify that initialization fails with the expected error
905905
with pytest.raises(EnvironmentError) as excinfo:
906906
TextEditor()
907-
907+
908908
assert "Failed to validate environment" in str(excinfo.value)
909+
910+
911+
@pytest.mark.asyncio
912+
async def test_edit_file_using_dict_patch(editor, tmp_path):
913+
"""Test editing file using dictionary patch without EditPatch model."""
914+
test_file = tmp_path / "test.txt"
915+
test_file.write_text("line1\nline2\nline3\n")
916+
917+
# Get first line content and calculate hashes
918+
first_line_content, _, _, file_hash, _, _ = await editor.read_file_contents(
919+
str(test_file), start=1, end=1
920+
)
921+
922+
# Create a patch using dictionary
923+
patch = {
924+
"start": 1,
925+
"end": 1,
926+
"contents": "new line\n",
927+
"range_hash": editor.calculate_hash("line1\n"),
928+
}
929+
930+
result = await editor.edit_file_contents(str(test_file), file_hash, [patch])
931+
assert result["result"] == "ok"
932+
assert test_file.read_text() == "new line\nline2\nline3\n"
933+
934+
935+
@pytest.mark.asyncio
936+
async def test_edit_new_file_with_dict_patch(editor, tmp_path):
937+
"""Test creating and editing a new file using dictionary patch."""
938+
test_file = tmp_path / "new_test.txt" # File does not exist yet
939+
940+
# Create a patch dictionary for the new file
941+
patch = {
942+
"start": 1,
943+
"contents": "new file content\n",
944+
"range_hash": "", # Empty range_hash for new files
945+
}
946+
947+
result = await editor.edit_file_contents(str(test_file), "", [patch])
948+
assert result["result"] == "ok"
949+
assert test_file.read_text() == "new file content\n"
950+
951+
# Test updating the newly created file
952+
content, _, _, file_hash, _, _ = await editor.read_file_contents(str(test_file))
953+
new_patch = {
954+
"start": 1,
955+
"end": 1,
956+
"contents": "updated content\n",
957+
"range_hash": editor.calculate_hash("new file content\n"),
958+
}
959+
960+
update_result = await editor.edit_file_contents(
961+
str(test_file), file_hash, [new_patch]
962+
)
963+
assert update_result["result"] == "ok"
964+
assert test_file.read_text() == "updated content\n"
965+
966+
967+
@pytest.mark.asyncio
968+
async def test_unexpected_file_content_error(editor, tmp_path):
969+
"""Test handling of unexpected file content error."""
970+
# Create a test file that exists but shouldn't
971+
test_file = tmp_path / "test.txt"
972+
test_file.write_text("existing content")
973+
974+
# Try to create a new file with empty hash
975+
result = await editor.edit_file_contents(
976+
str(test_file),
977+
"", # Empty hash indicates new file
978+
[{"start": 1, "contents": "new content\n", "range_hash": ""}],
979+
)
980+
981+
assert result["result"] == "error"
982+
assert "Unexpected error - Cannot treat existing file as new" in result["reason"]
983+
assert result["content"] is None
984+
assert result["file_hash"] is None

0 commit comments

Comments
 (0)