| 1 | import * as fs from "fs"; |
| 2 | |
| 3 | import * as diff from "diff"; |
| 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | |
| 6 | // Mock fs module |
| 7 | vi.mock("fs", async () => { |
| 8 | // Use actual fs implementation for non-mocked functions |
| 9 | const actualFs = await vi.importActual("fs"); |
| 10 | return { |
| 11 | ...(actualFs as any), |
| 12 | default: actualFs, |
| 13 | existsSync: vi.fn(), |
| 14 | readFileSync: vi.fn(), |
| 15 | }; |
| 16 | }); |
| 17 | |
| 18 | // Mock telemetry service |
| 19 | vi.mock("./src/telemetry/telemetryService.js"); // Mock diff module |
| 20 | vi.mock("diff", () => ({ |
| 21 | createTwoFilesPatch: vi.fn(), |
| 22 | })); |
| 23 | |
| 24 | // Get mocked functions using vi.mocked |
| 25 | const mockExistsSync = vi.mocked(fs.existsSync); |
| 26 | const mockReadFileSync = vi.mocked(fs.readFileSync); |
| 27 | const mockCreateTwoFilesPatch = vi.mocked(diff.createTwoFilesPatch); |
| 28 | |
| 29 | import { fetchTool } from "./fetch.js"; |
| 30 | import { listFilesTool } from "./listFiles.js"; |
| 31 | import { readFileTool } from "./readFile.js"; |
| 32 | import { runTerminalCommandTool } from "./runTerminalCommand.js"; |
| 33 | import { searchCodeTool } from "./searchCode.js"; |
| 34 | import { viewDiffTool } from "./viewDiff.js"; |
| 35 | import { writeFileTool } from "./writeFile.js"; |
| 36 | |
| 37 | describe.skip("Tool preprocess functions", () => { |
| 38 | beforeEach(() => { |
| 39 | vi.clearAllMocks(); |
| 40 | // The mock functions will be configured in each test as needed |
| 41 | }); |
| 42 | |
| 43 | describe("fetchTool.preprocess", () => { |
| 44 | it("should return preview with URL to fetch", async () => { |
| 45 | const args = { url: "https://example.com" }; |
| 46 | const result = await fetchTool.preprocess!(args); |
| 47 | |
| 48 | expect(result).toEqual({ |
| 49 | preview: [ |
| 50 | { |
| 51 | type: "text", |
| 52 | content: "Will fetch: https://example.com", |
| 53 | }, |
| 54 | ], |
| 55 | args, |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | it("should handle URL with timeout parameter", async () => { |
| 60 | const args = { url: "https://example.com", timeout: 5000 }; |
| 61 | const result = await fetchTool.preprocess!(args); |
| 62 | |
| 63 | expect(result).toEqual({ |
| 64 | preview: [ |
| 65 | { |
| 66 | type: "text", |
| 67 | content: "Will fetch: https://example.com", |
| 68 | }, |
| 69 | ], |
| 70 | args, |
| 71 | }); |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | describe("listFilesTool.preprocess", () => { |
| 76 | it("should return preview with directory path when directory arg present", async () => { |
| 77 | const args = { dirpath: "some/path" }; |
| 78 | const result = await listFilesTool.preprocess!(args); |
| 79 | |
| 80 | expect(result).toEqual({ |
| 81 | args, |
| 82 | preview: [ |
| 83 | { |
| 84 | type: "text", |
| 85 | content: "Will list files in: some/path", |
| 86 | }, |
| 87 | ], |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | it("should show current directory when no directory arg", async () => { |
| 92 | const args = {}; |
| 93 | const result = await listFilesTool.preprocess!(args); |
| 94 | |
| 95 | expect(result).toEqual({ |
| 96 | args, |
| 97 | preview: [ |
| 98 | { |
| 99 | type: "text", |
| 100 | content: "Will list files in current directory", |
| 101 | }, |
| 102 | ], |
| 103 | }); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | describe("readFileTool.preprocess", () => { |
| 108 | it("should return formatted tool call preview", async () => { |
| 109 | const args = { filepath: "path/to/file.txt" }; |
| 110 | const result = await readFileTool.preprocess!(args); |
| 111 | |
| 112 | expect(result).toEqual({ |
| 113 | args, |
| 114 | preview: [ |
| 115 | { |
| 116 | type: "text", |
| 117 | content: "Will read path/to/file.txt", |
| 118 | }, |
| 119 | ], |
| 120 | }); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | describe("runTerminalCommandTool.preprocess", () => { |
| 125 | it("should return preview with full command when short", async () => { |
| 126 | const args = { command: "ls -la" }; |
| 127 | const result = await runTerminalCommandTool.preprocess!(args); |
| 128 | |
| 129 | expect(result).toEqual({ |
| 130 | args, |
| 131 | preview: [ |
| 132 | { |
| 133 | type: "text", |
| 134 | content: "Will run: ls -la", |
| 135 | }, |
| 136 | ], |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | it("should truncate long commands", async () => { |
| 141 | const longCommand = "a".repeat(100); |
| 142 | const args = { command: longCommand }; |
| 143 | const result = await runTerminalCommandTool.preprocess!(args); |
| 144 | |
| 145 | expect(result).toEqual({ |
| 146 | args, |
| 147 | preview: [ |
| 148 | { |
| 149 | type: "text", |
| 150 | content: `Will run: ${"a".repeat(60)}...`, |
| 151 | }, |
| 152 | ], |
| 153 | }); |
| 154 | }); |
| 155 | }); |
| 156 | |
| 157 | describe("searchCodeTool.preprocess", () => { |
| 158 | it("should return preview with full pattern when short", async () => { |
| 159 | const args = { pattern: "searchTerm" }; |
| 160 | const result = await searchCodeTool.preprocess!(args); |
| 161 | |
| 162 | expect(result).toEqual({ |
| 163 | args, |
| 164 | preview: [ |
| 165 | { |
| 166 | type: "text", |
| 167 | content: 'Will search for: "searchTerm"', |
| 168 | }, |
| 169 | ], |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | it("should truncate long patterns", async () => { |
| 174 | const longPattern = "a".repeat(100); |
| 175 | const args = { pattern: longPattern }; |
| 176 | const result = await searchCodeTool.preprocess!(args); |
| 177 | |
| 178 | expect(result).toEqual({ |
| 179 | args, |
| 180 | preview: [ |
| 181 | { |
| 182 | type: "text", |
| 183 | content: `Will search for: "${"a".repeat(50)}..."`, |
| 184 | }, |
| 185 | ], |
| 186 | }); |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | describe("viewDiffTool.preprocess", () => { |
| 191 | it("should return preview indicating git diff will be shown", async () => { |
| 192 | const args = { path: "/repo/path" }; |
| 193 | const result = await viewDiffTool.preprocess!(args); |
| 194 | |
| 195 | expect(result).toEqual({ |
| 196 | args, |
| 197 | preview: [ |
| 198 | { |
| 199 | type: "text", |
| 200 | content: "Will show git diff", |
| 201 | }, |
| 202 | ], |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | it("should handle empty args", async () => { |
| 207 | const args = {}; |
| 208 | const result = await viewDiffTool.preprocess!(args); |
| 209 | |
| 210 | expect(result).toEqual({ |
| 211 | args, |
| 212 | preview: [ |
| 213 | { |
| 214 | type: "text", |
| 215 | content: "Will show git diff", |
| 216 | }, |
| 217 | ], |
| 218 | }); |
| 219 | }); |
| 220 | }); |
| 221 | |
| 222 | describe("writeFileTool.preprocess", () => { |
| 223 | it("should show diff preview when file exists", async () => { |
| 224 | // Setup mocks for this test |
| 225 | mockExistsSync.mockReturnValue(true); |
| 226 | mockReadFileSync.mockReturnValue("old content"); |
| 227 | |
| 228 | const args = { filepath: "path/to/file.txt", content: "new content" }; |
| 229 | const result = await writeFileTool.preprocess!(args); |
| 230 | |
| 231 | expect(mockExistsSync).toHaveBeenCalled(); |
| 232 | expect(mockReadFileSync).toHaveBeenCalled(); |
| 233 | expect(mockCreateTwoFilesPatch).toHaveBeenCalled(); |
| 234 | |
| 235 | expect(result).toEqual({ |
| 236 | args, |
| 237 | preview: [ |
| 238 | { |
| 239 | type: "text", |
| 240 | content: "Preview of changes:", |
| 241 | }, |
| 242 | { |
| 243 | type: "diff", |
| 244 | content: "mock diff content", |
| 245 | }, |
| 246 | ], |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | it("should show new file content preview when file doesn't exist", async () => { |
| 251 | mockExistsSync.mockReturnValue(false); |
| 252 | |
| 253 | const args = { |
| 254 | filepath: "path/to/file.txt", |
| 255 | content: "line 1\nline 2\nline 3\nline 4", |
| 256 | }; |
| 257 | const result = await writeFileTool.preprocess!(args); |
| 258 | |
| 259 | expect(result).toEqual({ |
| 260 | args, |
| 261 | preview: [ |
| 262 | { |
| 263 | type: "text", |
| 264 | content: "New file content:", |
| 265 | }, |
| 266 | { |
| 267 | type: "text", |
| 268 | content: "line 1", |
| 269 | paddingLeft: 2, |
| 270 | }, |
| 271 | { |
| 272 | type: "text", |
| 273 | content: "line 2", |
| 274 | paddingLeft: 2, |
| 275 | }, |
| 276 | { |
| 277 | type: "text", |
| 278 | content: "line 3", |
| 279 | paddingLeft: 2, |
| 280 | }, |
| 281 | { |
| 282 | type: "text", |
| 283 | content: "... (1 more lines)", |
| 284 | }, |
| 285 | ], |
| 286 | }); |
| 287 | }); |
| 288 | |
| 289 | it("should handle empty lines in new file preview", async () => { |
| 290 | mockExistsSync.mockReturnValue(false); |
| 291 | |
| 292 | const args = { |
| 293 | filepath: "path/to/file.txt", |
| 294 | content: "line 1\n\nline 3", |
| 295 | }; |
| 296 | const result = await writeFileTool.preprocess!(args); |
| 297 | |
| 298 | expect(result).toEqual({ |
| 299 | args, |
| 300 | preview: [ |
| 301 | { |
| 302 | type: "text", |
| 303 | content: "New file content:", |
| 304 | }, |
| 305 | { |
| 306 | type: "text", |
| 307 | content: "line 1", |
| 308 | paddingLeft: 2, |
| 309 | }, |
| 310 | { |
| 311 | type: "text", |
| 312 | content: " ", |
| 313 | paddingLeft: 2, |
| 314 | }, |
| 315 | { |
| 316 | type: "text", |
| 317 | content: "line 3", |
| 318 | paddingLeft: 2, |
| 319 | }, |
| 320 | ], |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | it("should handle fs errors gracefully", async () => { |
| 325 | mockExistsSync.mockImplementation(() => { |
| 326 | throw new Error("Permission denied"); |
| 327 | }); |
| 328 | |
| 329 | const args = { filepath: "path/to/file.txt", content: "new content" }; |
| 330 | const result = await writeFileTool.preprocess!(args); |
| 331 | |
| 332 | expect(result).toEqual({ |
| 333 | args, |
| 334 | preview: [ |
| 335 | { |
| 336 | type: "text", |
| 337 | content: "New file content:", |
| 338 | }, |
| 339 | { |
| 340 | type: "text", |
| 341 | content: "new content", |
| 342 | paddingLeft: 2, |
| 343 | }, |
| 344 | ], |
| 345 | }); |
| 346 | }); |
| 347 | }); |
| 348 | }); |