@@ -894,15 +894,91 @@ def mock_open(*args, **kwargs):
894
894
@pytest .mark .asyncio
895
895
async def test_initialization_with_environment_error (monkeypatch ):
896
896
"""Test TextEditor initialization when environment validation fails."""
897
-
897
+
898
898
def mock_validate_environment (self ):
899
899
raise EnvironmentError ("Failed to validate environment" )
900
-
900
+
901
901
# Patch the _validate_environment method
902
902
monkeypatch .setattr (TextEditor , "_validate_environment" , mock_validate_environment )
903
-
903
+
904
904
# Verify that initialization fails with the expected error
905
905
with pytest .raises (EnvironmentError ) as excinfo :
906
906
TextEditor ()
907
-
907
+
908
908
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\n line2\n line3\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\n line2\n line3\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