File size: 8,558 Bytes
4b03eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""Grep tool test case."""
import os
import tempfile
from unittest.async_case import IsolatedAsyncioTestCase

from agentscope.message import ToolResultState
from agentscope.tool import Grep
from agentscope.permission import (
    PermissionContext,
    PermissionBehavior,
    PermissionRule,
)


class GrepToolTest(IsolatedAsyncioTestCase):
    """The grep tool test case."""

    async def asyncSetUp(self) -> None:
        """The async setup method."""
        self.grep_tool = Grep()
        # Create a temporary directory with test files
        self.temp_dir = tempfile.mkdtemp()

        # Create test files
        with open(
            os.path.join(self.temp_dir, "test1.py"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("def hello():\n    print('Hello World')\n")

        with open(
            os.path.join(self.temp_dir, "test2.py"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("def goodbye():\n    print('Goodbye')\n")

        with open(
            os.path.join(self.temp_dir, "test.txt"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("This is a text file\nHello from text\n")

        # Create subdirectory with files for glob pattern testing
        subdir = os.path.join(self.temp_dir, "subdir")
        os.makedirs(subdir)
        with open(
            os.path.join(subdir, "nested.py"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("def nested():\n    print('Nested')\n")

    async def asyncTearDown(self) -> None:
        """Clean up temporary files."""
        import shutil

        if os.path.exists(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    async def test_tool_properties(self) -> None:
        """Test grep tool properties."""
        self.assertEqual(self.grep_tool.name, "Grep")
        self.assertIsInstance(self.grep_tool.description, str)
        self.assertIsInstance(self.grep_tool.input_schema, dict)
        self.assertFalse(self.grep_tool.is_mcp)
        self.assertTrue(self.grep_tool.is_read_only)
        self.assertTrue(self.grep_tool.is_concurrency_safe)

    async def test_check_permissions(self) -> None:
        """Test grep tool permission checking."""
        context = PermissionContext()
        tool_input = {"pattern": "hello"}
        decision = await self.grep_tool.check_permissions(tool_input, context)

        # Read/Glob/Grep are read-only, return PASSTHROUGH
        self.assertEqual(decision.behavior, PermissionBehavior.PASSTHROUGH)

    async def test_simple_search(self) -> None:
        """Test simple grep search."""
        chunk = await self.grep_tool(
            pattern="Hello",
            path=self.temp_dir,
            output_mode="files_with_matches",
        )

        self.assertEqual(chunk.state, ToolResultState.SUCCESS)

        content = chunk.content[0].text
        # Should find files containing "Hello"
        self.assertIn("test1.py", content)
        self.assertIn("test.txt", content)

    async def test_content_mode(self) -> None:
        """Test grep with content output mode."""
        chunk = await self.grep_tool(
            pattern="def",
            path=self.temp_dir,
            output_mode="content",
            type="py",
        )

        content = chunk.content[0].text

        # Should show matching lines
        self.assertIn("def hello", content)
        self.assertIn("def goodbye", content)

    async def test_case_insensitive(self) -> None:
        """Test case-insensitive search."""
        chunk = await self.grep_tool(
            pattern="HELLO",
            path=self.temp_dir,
            case_insensitive=True,
            output_mode="files_with_matches",
        )

        content = chunk.content[0].text
        self.assertIn("test1.py", content)

    async def test_no_matches(self) -> None:
        """Test search with no matches."""
        chunk = await self.grep_tool(
            pattern="NonExistentPattern",
            path=self.temp_dir,
        )

        self.assertIn("No matches found", chunk.content[0].text)

    async def test_type_filter(self) -> None:
        """Test filtering by file type."""
        chunk = await self.grep_tool(
            pattern="Hello",
            path=self.temp_dir,
            type="py",
            output_mode="files_with_matches",
        )

        content = chunk.content[0].text

        # Should only find .py files
        self.assertIn("test1.py", content)
        self.assertNotIn("test.txt", content)

    async def test_invalid_regex(self) -> None:
        """Test grep with invalid regex pattern."""
        chunk = await self.grep_tool(
            pattern="[invalid(regex",
            path=self.temp_dir,
        )

        self.assertEqual(chunk.state, "error")
        # ripgrep returns its own error message for regex parse errors
        self.assertIn("regex parse error", chunk.content[0].text)

    async def test_glob_pattern_with_subdirs(self) -> None:
        """Test glob pattern matching with subdirectories like **/*.py."""
        chunk = await self.grep_tool(
            pattern="def",
            path=self.temp_dir,
            glob="**/*.py",
            output_mode="files_with_matches",
        )

        content = chunk.content[0].text

        # Should find all .py files including in subdirectories
        self.assertIn("test1.py", content)
        self.assertIn("test2.py", content)
        self.assertIn("nested.py", content)
        # Should not find .txt files
        self.assertNotIn("test.txt", content)

    async def test_match_rule_path(self) -> None:
        """Test match_rule with search path patterns."""
        # Test matching explicit path
        self.assertTrue(
            await self.grep_tool.match_rule(
                self.temp_dir,
                {"path": self.temp_dir},
            ),
        )

        # Test wildcard pattern matching path
        parent_dir = os.path.dirname(self.temp_dir)
        self.assertTrue(
            await self.grep_tool.match_rule(
                parent_dir + "/**",
                {"path": self.temp_dir},
            ),
        )

        # Test non-matching path
        self.assertFalse(
            await self.grep_tool.match_rule(
                "/some/other/path/**",
                {"path": self.temp_dir},
            ),
        )

    async def test_match_rule_defaults_to_cwd(self) -> None:
        """Test match_rule defaults to cwd when no path is provided."""
        cwd = os.getcwd()

        # When no path provided, should match against cwd
        self.assertTrue(
            await self.grep_tool.match_rule(
                cwd,
                {"pattern": "hello"},
            ),
        )

        # Should not match a different path
        self.assertFalse(
            await self.grep_tool.match_rule(
                "/some/other/path",
                {"pattern": "hello"},
            ),
        )

    async def test_generate_suggestions_with_path(self) -> None:
        """Test generate_suggestions for grep with explicit path."""

        suggestions = await self.grep_tool.generate_suggestions(
            {"path": self.temp_dir, "pattern": "hello"},
        )

        self.assertIsInstance(suggestions, list)
        self.assertGreater(len(suggestions), 0)
        self.assertIsInstance(suggestions[0], PermissionRule)

        # Should suggest directory pattern
        abs_path = os.path.abspath(self.temp_dir)
        expected_pattern = abs_path.rstrip("/") + "/**"
        suggestion_contents = [s.rule_content for s in suggestions]
        self.assertIn(expected_pattern, suggestion_contents)

    async def test_generate_suggestions_defaults_to_cwd(self) -> None:
        """Test generate_suggestions defaults to cwd when no path provided."""

        suggestions = await self.grep_tool.generate_suggestions(
            {"pattern": "hello"},
        )

        self.assertIsInstance(suggestions, list)
        self.assertGreater(len(suggestions), 0)

        cwd = os.getcwd()
        expected_pattern = os.path.abspath(cwd).rstrip("/") + "/**"
        suggestion_contents = [s.rule_content for s in suggestions]
        self.assertIn(expected_pattern, suggestion_contents)