|
| 1 | +import { getToolDescriptionsForMode } from "../tools/index" |
| 2 | +import { defaultModeSlug } from "../../../shared/modes" |
| 3 | +import * as fs from "fs/promises" |
| 4 | +import { toPosix } from "./utils" |
| 5 | + |
| 6 | +// Mock the fs/promises module |
| 7 | +jest.mock("fs/promises", () => ({ |
| 8 | + readFile: jest.fn(), |
| 9 | + mkdir: jest.fn().mockResolvedValue(undefined), |
| 10 | + access: jest.fn().mockResolvedValue(undefined), |
| 11 | +})) |
| 12 | + |
| 13 | +// Get the mocked fs module |
| 14 | +const mockedFs = fs as jest.Mocked<typeof fs> |
| 15 | + |
| 16 | +describe("Tool Override System", () => { |
| 17 | + beforeEach(() => { |
| 18 | + // Reset mocks before each test |
| 19 | + jest.clearAllMocks() |
| 20 | + |
| 21 | + // Default behavior: file doesn't exist |
| 22 | + mockedFs.readFile.mockRejectedValue({ code: "ENOENT" }) |
| 23 | + }) |
| 24 | + |
| 25 | + it("should return default read_file tool description when no override file exists", async () => { |
| 26 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 27 | + defaultModeSlug, |
| 28 | + "test/workspace", |
| 29 | + false, // supportsComputerUse |
| 30 | + undefined, // codeIndexManager |
| 31 | + undefined, // diffStrategy |
| 32 | + undefined, // browserViewportSize |
| 33 | + undefined, // mcpHub |
| 34 | + undefined, // customModes |
| 35 | + undefined, // experiments |
| 36 | + ) |
| 37 | + |
| 38 | + // Should contain the default read_file description |
| 39 | + expect(toolDescriptions).toContain("## read_file") |
| 40 | + expect(toolDescriptions).toContain("Request to read the contents of one or more files") |
| 41 | + expect(toolDescriptions).toContain("relative to the current workspace directory test/workspace") |
| 42 | + expect(toolDescriptions).toContain("<read_file>") |
| 43 | + expect(toolDescriptions).toContain("Examples:") |
| 44 | + }) |
| 45 | + |
| 46 | + it("should use custom read_file tool description when override file exists", async () => { |
| 47 | + // Mock the readFile to return content from an override file |
| 48 | + const customReadFileDescription = `## read_file |
| 49 | +Description: Custom read file description for testing |
| 50 | +Parameters: |
| 51 | +- path: (required) Custom path description for \${args.cwd} |
| 52 | +- start_line: (optional) Custom start line description |
| 53 | +- end_line: (optional) Custom end line description |
| 54 | +Usage: |
| 55 | +<read_file> |
| 56 | +<path>Custom file path</path> |
| 57 | +</read_file> |
| 58 | +Custom example: |
| 59 | +<read_file> |
| 60 | +<path>custom-example.txt</path> |
| 61 | +</read_file>` |
| 62 | + |
| 63 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 64 | + if (toPosix(filePath).includes(".roo/tools/read_file.md") && options === "utf-8") { |
| 65 | + return Promise.resolve(customReadFileDescription) |
| 66 | + } |
| 67 | + return Promise.reject({ code: "ENOENT" }) |
| 68 | + }) |
| 69 | + |
| 70 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 71 | + defaultModeSlug, |
| 72 | + "test/workspace", |
| 73 | + false, // supportsComputerUse |
| 74 | + undefined, // codeIndexManager |
| 75 | + undefined, // diffStrategy |
| 76 | + undefined, // browserViewportSize |
| 77 | + undefined, // mcpHub |
| 78 | + undefined, // customModes |
| 79 | + undefined, // experiments |
| 80 | + ) |
| 81 | + |
| 82 | + // Should contain the custom read_file description |
| 83 | + expect(toolDescriptions).toContain("Custom read file description for testing") |
| 84 | + expect(toolDescriptions).toContain("Custom path description for test/workspace") |
| 85 | + expect(toolDescriptions).toContain("Custom example:") |
| 86 | + expect(toolDescriptions).toContain("custom-example.txt") |
| 87 | + |
| 88 | + // Should not contain the default description text |
| 89 | + expect(toolDescriptions).not.toContain("Request to read the contents of one or more files") |
| 90 | + expect(toolDescriptions).not.toContain("3. Reading lines 500-1000 of a CSV file:") |
| 91 | + }) |
| 92 | + |
| 93 | + it("should interpolate args properties in override content", async () => { |
| 94 | + // Mock the readFile to return content with args interpolation |
| 95 | + const customReadFileDescription = `## read_file |
| 96 | +Description: Custom read file description with interpolated args |
| 97 | +Parameters: |
| 98 | +- path: (required) File path relative to workspace \${args.cwd} |
| 99 | +- workspace: Current workspace is \${args.cwd} |
| 100 | +Usage: |
| 101 | +<read_file> |
| 102 | +<path>File relative to \${args.cwd}</path> |
| 103 | +</read_file>` |
| 104 | + |
| 105 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 106 | + if (toPosix(filePath).includes(".roo/tools/read_file.md") && options === "utf-8") { |
| 107 | + return Promise.resolve(customReadFileDescription) |
| 108 | + } |
| 109 | + return Promise.reject({ code: "ENOENT" }) |
| 110 | + }) |
| 111 | + |
| 112 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 113 | + defaultModeSlug, |
| 114 | + "test/workspace", |
| 115 | + false, // supportsComputerUse |
| 116 | + undefined, // codeIndexManager |
| 117 | + undefined, // diffStrategy |
| 118 | + undefined, // browserViewportSize |
| 119 | + undefined, // mcpHub |
| 120 | + undefined, // customModes |
| 121 | + undefined, // experiments |
| 122 | + ) |
| 123 | + |
| 124 | + // Should contain interpolated values |
| 125 | + expect(toolDescriptions).toContain("File path relative to workspace test/workspace") |
| 126 | + expect(toolDescriptions).toContain("Current workspace is test/workspace") |
| 127 | + expect(toolDescriptions).toContain("File relative to test/workspace") |
| 128 | + |
| 129 | + // Should not contain the placeholder text |
| 130 | + expect(toolDescriptions).not.toContain("\${args.cwd}") |
| 131 | + }) |
| 132 | + |
| 133 | + it("should return multiple tool descriptions including read_file", async () => { |
| 134 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 135 | + defaultModeSlug, |
| 136 | + "test/workspace", |
| 137 | + false, // supportsComputerUse |
| 138 | + undefined, // codeIndexManager |
| 139 | + undefined, // diffStrategy |
| 140 | + undefined, // browserViewportSize |
| 141 | + undefined, // mcpHub |
| 142 | + undefined, // customModes |
| 143 | + undefined, // experiments |
| 144 | + ) |
| 145 | + |
| 146 | + // Should contain multiple tools from the default mode |
| 147 | + expect(toolDescriptions).toContain("# Tools") |
| 148 | + expect(toolDescriptions).toContain("## read_file") |
| 149 | + expect(toolDescriptions).toContain("## write_to_file") |
| 150 | + expect(toolDescriptions).toContain("## list_files") |
| 151 | + expect(toolDescriptions).toContain("## search_files") |
| 152 | + |
| 153 | + // Tools should be separated by double newlines |
| 154 | + const toolSections = toolDescriptions.split("\n\n") |
| 155 | + expect(toolSections.length).toBeGreaterThan(1) |
| 156 | + }) |
| 157 | + |
| 158 | + it("should handle empty override file gracefully", async () => { |
| 159 | + // Mock the readFile to return empty content |
| 160 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 161 | + if (toPosix(filePath).includes(".roo/tools/read_file.md") && options === "utf-8") { |
| 162 | + return Promise.resolve("") |
| 163 | + } |
| 164 | + return Promise.reject({ code: "ENOENT" }) |
| 165 | + }) |
| 166 | + |
| 167 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 168 | + defaultModeSlug, |
| 169 | + "test/workspace", |
| 170 | + false, // supportsComputerUse |
| 171 | + undefined, // codeIndexManager |
| 172 | + undefined, // diffStrategy |
| 173 | + undefined, // browserViewportSize |
| 174 | + undefined, // mcpHub |
| 175 | + undefined, // customModes |
| 176 | + undefined, // experiments |
| 177 | + ) |
| 178 | + |
| 179 | + // Should fall back to default description when override file is empty |
| 180 | + expect(toolDescriptions).toContain("## read_file") |
| 181 | + expect(toolDescriptions).toContain("Request to read the contents of one or more files") |
| 182 | + }) |
| 183 | + |
| 184 | + it("should handle whitespace-only override file gracefully", async () => { |
| 185 | + // Mock the readFile to return whitespace-only content |
| 186 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 187 | + if (toPosix(filePath).includes(".roo/tools/read_file.md") && options === "utf-8") { |
| 188 | + return Promise.resolve(" \n \t \n ") |
| 189 | + } |
| 190 | + return Promise.reject({ code: "ENOENT" }) |
| 191 | + }) |
| 192 | + |
| 193 | + const toolDescriptions = await getToolDescriptionsForMode( |
| 194 | + defaultModeSlug, |
| 195 | + "test/workspace", |
| 196 | + false, // supportsComputerUse |
| 197 | + undefined, // codeIndexManager |
| 198 | + undefined, // diffStrategy |
| 199 | + undefined, // browserViewportSize |
| 200 | + undefined, // mcpHub |
| 201 | + undefined, // customModes |
| 202 | + undefined, // experiments |
| 203 | + ) |
| 204 | + |
| 205 | + // Should fall back to default description when override file contains only whitespace |
| 206 | + expect(toolDescriptions).toContain("## read_file") |
| 207 | + expect(toolDescriptions).toContain("Request to read the contents of one or more files") |
| 208 | + }) |
| 209 | +}) |
0 commit comments