|
| 1 | +import { LLMock } from "@copilotkit/aimock" |
| 2 | + |
| 3 | +import { toolResultContains } from "./fixture-utils" |
| 4 | + |
| 5 | +type SearchFilesFixture = { |
| 6 | + userMessagePattern: string |
| 7 | + toolName: string |
| 8 | + arguments: string |
| 9 | + toolCallId: string |
| 10 | + expected: string[] |
| 11 | + result: string |
| 12 | + id: string |
| 13 | +} |
| 14 | + |
| 15 | +export function addSearchFilesResultFixtures(mock: InstanceType<typeof LLMock>) { |
| 16 | + const fixtures: SearchFilesFixture[] = [ |
| 17 | + { |
| 18 | + userMessagePattern: "JavaScript function declarations", |
| 19 | + toolName: "search_files", |
| 20 | + arguments: '{"path":"search-files-tool-fixture","regex":"function\\\\s+\\\\w+"}', |
| 21 | + toolCallId: "call_search_files_functions_001", |
| 22 | + expected: [ |
| 23 | + "# search-files-tool-fixture/search-fixture.js", |
| 24 | + "function calculateTotal(items) {", |
| 25 | + "function validateUser(user) {", |
| 26 | + ], |
| 27 | + result: "The function search found declarations including `calculateTotal`, `validateUser`, and `formatCurrency`.", |
| 28 | + id: "call_search_files_functions_002", |
| 29 | + }, |
| 30 | + { |
| 31 | + userMessagePattern: "TODO comments using the regex TODO", |
| 32 | + toolName: "search_files", |
| 33 | + arguments: '{"path":"search-files-tool-fixture","regex":"TODO.*"}', |
| 34 | + toolCallId: "call_search_files_todo_001", |
| 35 | + expected: [ |
| 36 | + "# search-files-tool-fixture/search-fixture.js", |
| 37 | + "// TODO: Add more validation functions", |
| 38 | + "// TODO: Implement user fetching", |
| 39 | + ], |
| 40 | + result: "The TODO search found matching TODO entries in the fixture files, including the validation and user-fetching notes.", |
| 41 | + id: "call_search_files_todo_002", |
| 42 | + }, |
| 43 | + { |
| 44 | + userMessagePattern: "TypeScript interfaces you find", |
| 45 | + toolName: "search_files", |
| 46 | + arguments: '{"path":"search-files-tool-fixture","regex":"interface\\\\s+\\\\w+","file_pattern":"*.ts"}', |
| 47 | + toolCallId: "call_search_files_typescript_001", |
| 48 | + expected: ["# search-files-tool-fixture/search-fixture.ts", "interface User {", "interface Product {"], |
| 49 | + result: "The TypeScript-only search found the `User` and `Product` interface definitions.", |
| 50 | + id: "call_search_files_typescript_002", |
| 51 | + }, |
| 52 | + { |
| 53 | + userMessagePattern: "JSON configuration keys", |
| 54 | + toolName: "search_files", |
| 55 | + arguments: '{"path":"search-files-tool-fixture","regex":"\\"\\\\w+\\":\\\\s*","file_pattern":"*.json"}', |
| 56 | + toolCallId: "call_search_files_json_001", |
| 57 | + expected: ["# search-files-tool-fixture/search-config.json", '"name": "test-app",', '"dependencies": {'], |
| 58 | + result: "The JSON search found configuration keys such as `name`, `version`, and `dependencies` in `search-config.json`.", |
| 59 | + id: "call_search_files_json_002", |
| 60 | + }, |
| 61 | + { |
| 62 | + userMessagePattern: "formatCurrency and debounce", |
| 63 | + toolName: "search_files", |
| 64 | + arguments: '{"path":"search-files-tool-fixture","regex":"function\\\\s+(format|debounce)"}', |
| 65 | + toolCallId: "call_search_files_nested_001", |
| 66 | + expected: [ |
| 67 | + "# search-files-tool-fixture/nested/nested-search.js", |
| 68 | + "function formatCurrency(amount) {", |
| 69 | + "function debounce(func, wait) {", |
| 70 | + ], |
| 71 | + result: "The nested-directory search found the utility functions `formatCurrency` and `debounce`.", |
| 72 | + id: "call_search_files_nested_002", |
| 73 | + }, |
| 74 | + { |
| 75 | + userMessagePattern: "import and export statements", |
| 76 | + toolName: "search_files", |
| 77 | + arguments: '{"path":"search-files-tool-fixture","regex":"(import|export).*","file_pattern":"*.{js,ts}"}', |
| 78 | + toolCallId: "call_search_files_complex_regex_001", |
| 79 | + expected: [ |
| 80 | + "# search-files-tool-fixture/search-fixture.js", |
| 81 | + "export { calculateTotal, validateUser }", |
| 82 | + "module.exports = { formatCurrency, debounce }", |
| 83 | + ], |
| 84 | + result: "The import/export search found the `export` statement in the JavaScript fixture module.", |
| 85 | + id: "call_search_files_complex_regex_002", |
| 86 | + }, |
| 87 | + { |
| 88 | + userMessagePattern: "nonExistentPattern12345 and report that there are no matches", |
| 89 | + toolName: "search_files", |
| 90 | + arguments: '{"path":"search-files-tool-fixture","regex":"nonExistentPattern12345"}', |
| 91 | + toolCallId: "call_search_files_no_match_001", |
| 92 | + expected: ["No results found"], |
| 93 | + result: "No matches were found for `nonExistentPattern12345` in the search fixture directory.", |
| 94 | + id: "call_search_files_no_match_002", |
| 95 | + }, |
| 96 | + { |
| 97 | + userMessagePattern: "TypeScript class definitions and async methods", |
| 98 | + toolName: "search_files", |
| 99 | + arguments: |
| 100 | + '{"path":"search-files-tool-fixture","regex":"(class\\\\s+\\\\w+|async\\\\s+\\\\w+)","file_pattern":"*.ts"}', |
| 101 | + toolCallId: "call_search_files_class_method_001", |
| 102 | + expected: [ |
| 103 | + "# search-files-tool-fixture/search-fixture.ts", |
| 104 | + "class UserService {", |
| 105 | + "async getUser(id: number): Promise<User> {", |
| 106 | + ], |
| 107 | + result: "The class-and-method search found `UserService` and its async `getUser` method in the TypeScript fixture.", |
| 108 | + id: "call_search_files_class_method_002", |
| 109 | + }, |
| 110 | + ] |
| 111 | + |
| 112 | + for (const fixture of fixtures) { |
| 113 | + mock.addFixture({ |
| 114 | + match: { |
| 115 | + userMessage: new RegExp(fixture.userMessagePattern), |
| 116 | + }, |
| 117 | + response: { |
| 118 | + toolCalls: [ |
| 119 | + { |
| 120 | + name: fixture.toolName, |
| 121 | + arguments: fixture.arguments, |
| 122 | + id: fixture.toolCallId, |
| 123 | + }, |
| 124 | + ], |
| 125 | + }, |
| 126 | + }) |
| 127 | + |
| 128 | + mock.addFixture({ |
| 129 | + match: { |
| 130 | + toolCallId: fixture.toolCallId, |
| 131 | + predicate: (req) => toolResultContains(req, fixture.toolCallId, fixture.expected), |
| 132 | + }, |
| 133 | + response: { |
| 134 | + toolCalls: [ |
| 135 | + { |
| 136 | + name: "attempt_completion", |
| 137 | + arguments: JSON.stringify({ result: fixture.result }), |
| 138 | + id: fixture.id, |
| 139 | + }, |
| 140 | + ], |
| 141 | + }, |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments