|
| 1 | +package ai.javaclaw.tools.search; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springaicommunity.tool.search.ToolReference; |
| 5 | +import org.springaicommunity.tool.search.ToolSearchRequest; |
| 6 | +import org.springaicommunity.tool.searcher.LuceneToolSearcher; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +class LuceneToolSearcherTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + void returnsRelevantToolsForQuery() throws Exception { |
| 14 | + try (LuceneToolSearcher searcher = new LuceneToolSearcher(0.0f)) { |
| 15 | + String sessionId = "s1"; |
| 16 | + searcher.indexTool(sessionId, new ToolReference("fileSystem", null, |
| 17 | + "Read, write, and edit local files in the workspace. Use for file operations, patches, and edits.")); |
| 18 | + searcher.indexTool(sessionId, new ToolReference("webFetch", null, |
| 19 | + "Fetch a URL and extract readable content from web pages. Use for scraping and summarization.")); |
| 20 | + searcher.indexTool(sessionId, new ToolReference("shell", null, |
| 21 | + "Execute shell commands to inspect the repository, run builds/tests, and automate development tasks.")); |
| 22 | + |
| 23 | + var response = searcher.search(new ToolSearchRequest(sessionId, "edit a local file", 5, null)); |
| 24 | + |
| 25 | + assertThat(response.toolReferences()).isNotEmpty(); |
| 26 | + assertThat(response.toolReferences().getFirst().toolName()).isEqualTo("fileSystem"); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void ranksMoreRelevantToolHigherBasedOnDescription() throws Exception { |
| 32 | + try (LuceneToolSearcher searcher = new LuceneToolSearcher(0.0f)) { |
| 33 | + String sessionId = "s2"; |
| 34 | + searcher.indexTool(sessionId, new ToolReference("webFetch", null, |
| 35 | + "Fetch a URL and extract page contents. Good for reading articles when you already have a URL.")); |
| 36 | + searcher.indexTool(sessionId, new ToolReference("braveSearch", null, |
| 37 | + "Search the web by keyword query and return results. Use when you do not have a URL yet.")); |
| 38 | + |
| 39 | + var response = searcher.search(new ToolSearchRequest(sessionId, "search the web for spring ai docs", 5, null)); |
| 40 | + |
| 41 | + assertThat(response.toolReferences()).isNotEmpty(); |
| 42 | + assertThat(response.toolReferences().getFirst().toolName()).isEqualTo("braveSearch"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void honorsMaxResults() throws Exception { |
| 48 | + try (LuceneToolSearcher searcher = new LuceneToolSearcher(0.0f)) { |
| 49 | + String sessionId = "s3"; |
| 50 | + for (int i = 0; i < 10; i++) { |
| 51 | + searcher.indexTool(sessionId, new ToolReference("tool-" + i, null, "tool number " + i + " for testing")); |
| 52 | + } |
| 53 | + |
| 54 | + var response = searcher.search(new ToolSearchRequest(sessionId, "tool testing", 3, null)); |
| 55 | + |
| 56 | + assertThat(response.toolReferences().size()).isLessThanOrEqualTo(3); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments