"""Tests for EditTool20250728.""" import pytest from pathlib import Path from nanobot.agent.tools.anthropic.edit import EditTool20250728 from nanobot.agent.tools.anthropic.base import CLIResult @pytest.fixture def edit_tool(): """Create an EditTool20250728 instance.""" return EditTool20250728() @pytest.fixture def temp_file(tmp_path): """Create a temporary file with some content.""" file_path = tmp_path / "test.txt" file_path.write_text("line 1\nline 2\nline 3\n") return file_path @pytest.mark.asyncio async def test_view_command(edit_tool, temp_file): """Test viewing a file with line numbers.""" result = await edit_tool( command="view", path=str(temp_file) ) assert result.output is not None assert "1|line 1" in result.output assert "2|line 2" in result.output assert "3|line 3" in result.output @pytest.mark.asyncio async def test_create_command(edit_tool, tmp_path): """Test creating a new file.""" new_file = tmp_path / "new.txt" result = await edit_tool( command="create", path=str(new_file), file_text="Hello\nWorld\n" ) assert result.exit_code == 0 assert new_file.exists() assert new_file.read_text() == "Hello\nWorld\n" @pytest.mark.asyncio async def test_str_replace_command(edit_tool, temp_file): """Test replacing a unique string.""" result = await edit_tool( command="str_replace", path=str(temp_file), old_str="line 2", new_str="LINE TWO" ) assert result.exit_code == 0 content = temp_file.read_text() assert "LINE TWO" in content assert "line 2" not in content @pytest.mark.asyncio async def test_str_replace_non_unique(edit_tool, temp_file): """Test that str_replace fails on non-unique match.""" # Write content with duplicate "line" temp_file.write_text("line 1\nline 2\nline 3\n") result = await edit_tool( command="str_replace", path=str(temp_file), old_str="line", # This appears 3 times new_str="LINE" ) assert result.exit_code == 1 assert "must match exactly once" in result.error.lower() @pytest.mark.asyncio async def test_insert_command(edit_tool, temp_file): """Test inserting text at a specific line.""" result = await edit_tool( command="insert", path=str(temp_file), insert_line=1, new_str="inserted line\n" ) assert result.exit_code == 0 content = temp_file.read_text() lines = content.splitlines() assert lines[1] == "inserted line" @pytest.mark.asyncio async def test_edit_tool_requires_absolute_path(): """Test edit tool rejects relative paths.""" tool = EditTool20250728() result = await tool( command="view", path="relative/path.txt" ) assert isinstance(result, CLIResult) assert result.exit_code == 1 assert "absolute" in result.error.lower() def test_edit_tool_to_params(): """Test edit tool returns correct params.""" tool = EditTool20250728() params = tool.to_params() assert params["type"] == "text_editor_20250728" assert params["name"] == "str_replace_based_edit_tool"