|
| 1 | +package ai.koog.agents.ext.tool.search |
| 2 | + |
| 3 | +import ai.koog.agents.ext.tool.file.model.FileSystemEntry |
| 4 | +import ai.koog.rag.base.files.FileSystemProvider |
| 5 | +import ai.koog.rag.base.files.JVMFileSystemProvider |
| 6 | +import kotlinx.coroutines.runBlocking |
| 7 | +import java.nio.file.Files |
| 8 | +import java.nio.file.Path |
| 9 | +import kotlin.io.path.createDirectories |
| 10 | +import kotlin.io.path.writeText |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | +class RegexSearchToolTest { |
| 16 | + private fun buildFsWithSampleProject(): Pair<FileSystemProvider.ReadOnly<Path>, Path> { |
| 17 | + val tempDir = Files.createTempDirectory("regex-search-tool-test").toAbsolutePath() |
| 18 | + val fs: FileSystemProvider.ReadOnly<Path> = JVMFileSystemProvider.ReadOnly |
| 19 | + |
| 20 | + // project layout |
| 21 | + val srcMainKotlin = tempDir.resolve("src/main/kotlin").createDirectories() |
| 22 | + val srcMainJava = tempDir.resolve("src/main/java").createDirectories() |
| 23 | + val srcTestKotlin = tempDir.resolve("src/test/kotlin").createDirectories() |
| 24 | + val docs = tempDir.resolve("docs").createDirectories() |
| 25 | + |
| 26 | + // kotlin files |
| 27 | + srcMainKotlin.resolve("File1.kt").writeText("fun main() { println(\"Hello, World!\") }") |
| 28 | + srcMainKotlin.resolve("File2.kt").writeText("class User(val name: String, val age: Int)") |
| 29 | + |
| 30 | + // java files |
| 31 | + srcMainJava.resolve("File1.java").writeText("public class File1 { public static void main(String[] args) { System.out.println(\"Hello, Java!\"); } }") |
| 32 | + srcMainJava.resolve("File2.java").writeText("public class User { private String name; private int age; }") |
| 33 | + |
| 34 | + // test files |
| 35 | + srcTestKotlin.resolve("Test1.kt").writeText("fun testFunction() { assertEquals(expected, actual) }") |
| 36 | + srcTestKotlin.resolve("TestUtils.kt").writeText("fun assertSomething() { assertTrue(condition) }") |
| 37 | + |
| 38 | + // docs |
| 39 | + docs.resolve("readme.txt").writeText("This is a sample project with Kotlin and Java files.") |
| 40 | + docs.resolve("api.txt").writeText("API Documentation: Use the User class to create user instances.") |
| 41 | + docs.resolve("multiline.txt").writeText( |
| 42 | + """line 0 This is a multiline string. |
| 43 | + |Line 1 has some content. |
| 44 | + |Line 2 has different content. |
| 45 | + |Line 3 ends with a number: 42 |
| 46 | + |Line 4 starts with a number: 100 and continues. |
| 47 | + |Line 5 is also there. |
| 48 | + |Line 6 is the last line. |
| 49 | + |""".trimMargin() |
| 50 | + ) |
| 51 | + |
| 52 | + return fs to tempDir |
| 53 | + } |
| 54 | + |
| 55 | + private fun String.norm(): String = replace('\\', '/') |
| 56 | + |
| 57 | + private fun List<String>.containsSuffix(suffix: String): Boolean = any { it.norm().endsWith(suffix) } |
| 58 | + |
| 59 | + private fun List<String>.containsAllSuffixes(vararg suffixes: String): Boolean = suffixes.all { containsSuffix(it) } |
| 60 | + |
| 61 | + private fun List<String>.containsNoneOfSuffixes(vararg suffixes: String): Boolean = suffixes.none { containsSuffix(it) } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun testRegexSearchBasic() = runBlocking { |
| 65 | + val (fs, root) = buildFsWithSampleProject() |
| 66 | + val tool = RegexSearchTool(fs) |
| 67 | + |
| 68 | + val result = tool.execute( |
| 69 | + RegexSearchTool.Args( |
| 70 | + path = fs.toAbsolutePathString(root), |
| 71 | + regex = "class\\s+User", |
| 72 | + limit = 10, |
| 73 | + skip = 0, |
| 74 | + caseSensitive = true |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + assertEquals("class\\s+User", result.original) |
| 79 | + val paths = result.entries.map { it.path } |
| 80 | + assertTrue(paths.containsAllSuffixes("/src/main/java/File2.java", "/src/main/kotlin/File2.kt")) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun testRegexSearchLimit() = runBlocking { |
| 85 | + val (fs, root) = buildFsWithSampleProject() |
| 86 | + val tool = RegexSearchTool(fs) |
| 87 | + |
| 88 | + val result = tool.execute( |
| 89 | + RegexSearchTool.Args( |
| 90 | + path = fs.toAbsolutePathString(root), |
| 91 | + regex = "class", |
| 92 | + limit = 1, |
| 93 | + skip = 0, |
| 94 | + caseSensitive = true |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + assertEquals(1, result.entries.size) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testRegexSearchNoResults() = runBlocking { |
| 103 | + val (fs, root) = buildFsWithSampleProject() |
| 104 | + val tool = RegexSearchTool(fs) |
| 105 | + |
| 106 | + val result = tool.execute( |
| 107 | + RegexSearchTool.Args( |
| 108 | + path = fs.toAbsolutePathString(root), |
| 109 | + regex = "non-existent-pattern", |
| 110 | + limit = 10, |
| 111 | + skip = 0, |
| 112 | + caseSensitive = true |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + assertTrue(result.entries.isEmpty()) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun testRegexSearchCaseInsensitive() = runBlocking { |
| 121 | + val (fs, root) = buildFsWithSampleProject() |
| 122 | + val tool = RegexSearchTool(fs) |
| 123 | + |
| 124 | + val result = tool.execute( |
| 125 | + RegexSearchTool.Args( |
| 126 | + path = fs.toAbsolutePathString(root), |
| 127 | + regex = "CLASS", |
| 128 | + limit = 10, |
| 129 | + skip = 0, |
| 130 | + caseSensitive = false |
| 131 | + ) |
| 132 | + ) |
| 133 | + |
| 134 | + val paths = result.entries.map { it.path } |
| 135 | + assertTrue(paths.containsAllSuffixes( |
| 136 | + "/src/main/kotlin/File2.kt", |
| 137 | + "/src/main/java/File1.java", |
| 138 | + "/src/main/java/File2.java" |
| 139 | + )) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun testRegexSearchSpecificDirectory() = runBlocking { |
| 144 | + val (fs, root) = buildFsWithSampleProject() |
| 145 | + val tool = RegexSearchTool(fs) |
| 146 | + |
| 147 | + val result = tool.execute( |
| 148 | + RegexSearchTool.Args( |
| 149 | + path = root.resolve("src/main/kotlin").toString(), |
| 150 | + regex = "fun|class", |
| 151 | + limit = 10, |
| 152 | + skip = 0, |
| 153 | + caseSensitive = true |
| 154 | + ) |
| 155 | + ) |
| 156 | + |
| 157 | + val paths = result.entries.map { it.path } |
| 158 | + assertTrue(paths.containsAllSuffixes( |
| 159 | + "/src/main/kotlin/File1.kt", |
| 160 | + "/src/main/kotlin/File2.kt" |
| 161 | + )) |
| 162 | + assertTrue(paths.containsNoneOfSuffixes( |
| 163 | + "/src/main/java/File1.java", |
| 164 | + "/src/main/java/File2.java" |
| 165 | + )) |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + fun testRegexSearchComplexPattern() = runBlocking { |
| 170 | + val (fs, root) = buildFsWithSampleProject() |
| 171 | + val tool = RegexSearchTool(fs) |
| 172 | + |
| 173 | + val result = tool.execute( |
| 174 | + RegexSearchTool.Args( |
| 175 | + path = fs.toAbsolutePathString(root), |
| 176 | + regex = "assert\\w+\\(", |
| 177 | + limit = 10, |
| 178 | + skip = 0, |
| 179 | + caseSensitive = true |
| 180 | + ) |
| 181 | + ) |
| 182 | + |
| 183 | + val paths = result.entries.map { it.path } |
| 184 | + assertTrue(paths.containsAllSuffixes( |
| 185 | + "/src/test/kotlin/Test1.kt", |
| 186 | + "/src/test/kotlin/TestUtils.kt" |
| 187 | + )) |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + fun testRegexSearchMultilineMatches() = runBlocking { |
| 192 | + val (fs, root) = buildFsWithSampleProject() |
| 193 | + val tool = RegexSearchTool(fs) |
| 194 | + |
| 195 | + val result = tool.execute( |
| 196 | + RegexSearchTool.Args( |
| 197 | + path = root.resolve("docs").toString(), |
| 198 | + regex = "Line.*?\\d+.*?\\n.*?Line", |
| 199 | + limit = 10, |
| 200 | + skip = 0, |
| 201 | + caseSensitive = true |
| 202 | + ) |
| 203 | + ) |
| 204 | + |
| 205 | + val entry = result.entries.firstOrNull { it.path.norm().endsWith("/docs/multiline.txt") } |
| 206 | + assertTrue(entry != null, "docs/multiline.txt should be in results") |
| 207 | + val content = entry.content |
| 208 | + assertTrue(content is FileSystemEntry.File.Content.Excerpt) |
| 209 | + val hasContext = content.snippets.any { snippet -> |
| 210 | + val t = snippet.text |
| 211 | + t.contains("Line 3 ends with a number: 42") && t.contains("Line 4 starts with a number: 100 and continues.") |
| 212 | + } |
| 213 | + assertTrue(hasContext) |
| 214 | + } |
| 215 | +} |
0 commit comments