@@ -0,0 +1,240 @@ |
| 1 | +"""Tests for tool implementations.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from loader.tools import ( |
| 5 | + ReadTool, WriteTool, EditTool, GlobTool, |
| 6 | + BashTool, GrepTool, ConfirmationRequired, |
| 7 | +) |
| 8 | +from loader.tools.base import ToolRegistry, create_default_registry |
| 9 | + |
| 10 | + |
| 11 | +class TestReadTool: |
| 12 | + """Tests for ReadTool.""" |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def tool(self): |
| 16 | + return ReadTool() |
| 17 | + |
| 18 | + @pytest.mark.asyncio |
| 19 | + async def test_read_file(self, tool, sample_file): |
| 20 | + result = await tool.execute(file_path=str(sample_file)) |
| 21 | + assert not result.is_error |
| 22 | + assert "Line 1" in result.output |
| 23 | + assert "Line 2" in result.output |
| 24 | + |
| 25 | + @pytest.mark.asyncio |
| 26 | + async def test_read_nonexistent(self, tool, temp_dir): |
| 27 | + result = await tool.execute(file_path=str(temp_dir / "nonexistent.txt")) |
| 28 | + assert result.is_error |
| 29 | + assert "not found" in result.output.lower() |
| 30 | + |
| 31 | + @pytest.mark.asyncio |
| 32 | + async def test_read_with_offset(self, tool, sample_file): |
| 33 | + result = await tool.execute(file_path=str(sample_file), offset=2, limit=1) |
| 34 | + assert not result.is_error |
| 35 | + assert "Line 2" in result.output |
| 36 | + assert "Line 1" not in result.output |
| 37 | + |
| 38 | + def test_is_not_destructive(self, tool): |
| 39 | + assert not tool.is_destructive |
| 40 | + |
| 41 | + |
| 42 | +class TestWriteTool: |
| 43 | + """Tests for WriteTool.""" |
| 44 | + |
| 45 | + @pytest.fixture |
| 46 | + def tool(self): |
| 47 | + return WriteTool() |
| 48 | + |
| 49 | + @pytest.mark.asyncio |
| 50 | + async def test_write_file(self, tool, temp_dir): |
| 51 | + file_path = temp_dir / "new_file.txt" |
| 52 | + result = await tool.execute(file_path=str(file_path), content="Hello, world!") |
| 53 | + assert not result.is_error |
| 54 | + assert file_path.exists() |
| 55 | + assert file_path.read_text() == "Hello, world!" |
| 56 | + |
| 57 | + @pytest.mark.asyncio |
| 58 | + async def test_write_creates_parents(self, tool, temp_dir): |
| 59 | + file_path = temp_dir / "subdir" / "deep" / "file.txt" |
| 60 | + result = await tool.execute(file_path=str(file_path), content="nested") |
| 61 | + assert not result.is_error |
| 62 | + assert file_path.exists() |
| 63 | + |
| 64 | + def test_is_destructive(self, tool): |
| 65 | + assert tool.is_destructive |
| 66 | + |
| 67 | + def test_requires_confirmation(self, tool): |
| 68 | + with pytest.raises(ConfirmationRequired) as exc_info: |
| 69 | + tool.check_confirmation( |
| 70 | + skip_confirmation=False, |
| 71 | + file_path="/tmp/test.txt", |
| 72 | + content="test", |
| 73 | + ) |
| 74 | + assert "Write to file" in exc_info.value.message |
| 75 | + |
| 76 | + def test_skip_confirmation(self, tool): |
| 77 | + # Should not raise |
| 78 | + tool.check_confirmation( |
| 79 | + skip_confirmation=True, |
| 80 | + file_path="/tmp/test.txt", |
| 81 | + content="test", |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +class TestEditTool: |
| 86 | + """Tests for EditTool.""" |
| 87 | + |
| 88 | + @pytest.fixture |
| 89 | + def tool(self): |
| 90 | + return EditTool() |
| 91 | + |
| 92 | + @pytest.mark.asyncio |
| 93 | + async def test_edit_file(self, tool, sample_file): |
| 94 | + result = await tool.execute( |
| 95 | + file_path=str(sample_file), |
| 96 | + old_string="Line 2", |
| 97 | + new_string="Modified Line 2", |
| 98 | + ) |
| 99 | + assert not result.is_error |
| 100 | + assert "Modified Line 2" in sample_file.read_text() |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_edit_nonexistent(self, tool, temp_dir): |
| 104 | + result = await tool.execute( |
| 105 | + file_path=str(temp_dir / "nonexistent.txt"), |
| 106 | + old_string="foo", |
| 107 | + new_string="bar", |
| 108 | + ) |
| 109 | + assert result.is_error |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_edit_string_not_found(self, tool, sample_file): |
| 113 | + result = await tool.execute( |
| 114 | + file_path=str(sample_file), |
| 115 | + old_string="Not in file", |
| 116 | + new_string="replacement", |
| 117 | + ) |
| 118 | + assert result.is_error |
| 119 | + assert "not found" in result.output.lower() |
| 120 | + |
| 121 | + |
| 122 | +class TestGlobTool: |
| 123 | + """Tests for GlobTool.""" |
| 124 | + |
| 125 | + @pytest.fixture |
| 126 | + def tool(self): |
| 127 | + return GlobTool() |
| 128 | + |
| 129 | + @pytest.mark.asyncio |
| 130 | + async def test_glob_finds_files(self, tool, temp_dir): |
| 131 | + (temp_dir / "file1.py").write_text("# python") |
| 132 | + (temp_dir / "file2.py").write_text("# python") |
| 133 | + (temp_dir / "file3.txt").write_text("text") |
| 134 | + |
| 135 | + result = await tool.execute(pattern="*.py", path=str(temp_dir)) |
| 136 | + assert not result.is_error |
| 137 | + assert "file1.py" in result.output |
| 138 | + assert "file2.py" in result.output |
| 139 | + assert "file3.txt" not in result.output |
| 140 | + |
| 141 | + @pytest.mark.asyncio |
| 142 | + async def test_glob_no_matches(self, tool, temp_dir): |
| 143 | + result = await tool.execute(pattern="*.xyz", path=str(temp_dir)) |
| 144 | + assert not result.is_error |
| 145 | + assert "No files matching" in result.output |
| 146 | + |
| 147 | + |
| 148 | +class TestBashTool: |
| 149 | + """Tests for BashTool.""" |
| 150 | + |
| 151 | + @pytest.fixture |
| 152 | + def tool(self): |
| 153 | + return BashTool() |
| 154 | + |
| 155 | + @pytest.mark.asyncio |
| 156 | + async def test_bash_simple_command(self, tool): |
| 157 | + result = await tool.execute(command="echo 'hello world'") |
| 158 | + assert not result.is_error |
| 159 | + assert "hello world" in result.output |
| 160 | + |
| 161 | + @pytest.mark.asyncio |
| 162 | + async def test_bash_pwd(self, tool): |
| 163 | + result = await tool.execute(command="pwd") |
| 164 | + assert not result.is_error |
| 165 | + assert "/" in result.output |
| 166 | + |
| 167 | + @pytest.mark.asyncio |
| 168 | + async def test_bash_failed_command(self, tool): |
| 169 | + result = await tool.execute(command="exit 1") |
| 170 | + assert result.is_error |
| 171 | + assert "Exit code 1" in result.output |
| 172 | + |
| 173 | + def test_is_destructive(self, tool): |
| 174 | + assert tool.is_destructive |
| 175 | + |
| 176 | + def test_safe_command_no_confirmation(self, tool): |
| 177 | + # ls is safe |
| 178 | + tool.check_confirmation(skip_confirmation=False, command="ls -la") |
| 179 | + # git status is safe |
| 180 | + tool.check_confirmation(skip_confirmation=False, command="git status") |
| 181 | + |
| 182 | + def test_unsafe_command_requires_confirmation(self, tool): |
| 183 | + with pytest.raises(ConfirmationRequired): |
| 184 | + tool.check_confirmation(skip_confirmation=False, command="rm -rf /tmp/test") |
| 185 | + |
| 186 | + |
| 187 | +class TestGrepTool: |
| 188 | + """Tests for GrepTool.""" |
| 189 | + |
| 190 | + @pytest.fixture |
| 191 | + def tool(self): |
| 192 | + return GrepTool() |
| 193 | + |
| 194 | + @pytest.mark.asyncio |
| 195 | + async def test_grep_finds_pattern(self, tool, sample_python_file): |
| 196 | + result = await tool.execute( |
| 197 | + pattern="def.*hello", |
| 198 | + path=str(sample_python_file), |
| 199 | + ) |
| 200 | + assert not result.is_error |
| 201 | + assert "hello" in result.output |
| 202 | + |
| 203 | + @pytest.mark.asyncio |
| 204 | + async def test_grep_no_matches(self, tool, sample_file): |
| 205 | + result = await tool.execute( |
| 206 | + pattern="nonexistent_pattern", |
| 207 | + path=str(sample_file), |
| 208 | + ) |
| 209 | + assert not result.is_error |
| 210 | + assert "No matches" in result.output |
| 211 | + |
| 212 | + |
| 213 | +class TestToolRegistry: |
| 214 | + """Tests for ToolRegistry.""" |
| 215 | + |
| 216 | + def test_create_default_registry(self): |
| 217 | + registry = create_default_registry() |
| 218 | + assert registry.get("read") is not None |
| 219 | + assert registry.get("write") is not None |
| 220 | + assert registry.get("edit") is not None |
| 221 | + assert registry.get("glob") is not None |
| 222 | + assert registry.get("bash") is not None |
| 223 | + assert registry.get("grep") is not None |
| 224 | + |
| 225 | + def test_unknown_tool(self): |
| 226 | + registry = create_default_registry() |
| 227 | + assert registry.get("nonexistent") is None |
| 228 | + |
| 229 | + @pytest.mark.asyncio |
| 230 | + async def test_execute_unknown_tool(self): |
| 231 | + registry = create_default_registry() |
| 232 | + result = await registry.execute("nonexistent") |
| 233 | + assert result.is_error |
| 234 | + assert "Unknown tool" in result.output |
| 235 | + |
| 236 | + def test_skip_confirmation_flag(self): |
| 237 | + registry = create_default_registry() |
| 238 | + assert not registry.skip_confirmation |
| 239 | + registry.skip_confirmation = True |
| 240 | + assert registry.skip_confirmation |