|
| 1 | +import * as nodeUtil from "util"; |
| 2 | + |
| 3 | +import { vi } from "vitest"; |
| 4 | + |
| 5 | +const execMock: any = vi.fn(); |
| 6 | +(execMock as any)[(nodeUtil as any).promisify.custom] = (cmd: any) => |
| 7 | + new Promise((resolve, reject) => { |
| 8 | + execMock(cmd, (err: any, stdout: any, stderr: any) => { |
| 9 | + if (err) reject(err); |
| 10 | + else resolve({ stdout, stderr }); |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | +vi.mock("child_process", () => ({ exec: execMock })); |
| 15 | + |
1 | 16 | // Since we want to test just the interface and not the internals, |
2 | 17 | // let's create a simplified version of the run function to test the truncation logic |
3 | 18 | describe("searchCodeTool", () => { |
@@ -51,4 +66,30 @@ describe("searchCodeTool", () => { |
51 | 66 | // Check that we have the expected number of lines (header + 50 content lines) |
52 | 67 | expect(nonEmptyLines.length).toBe(51); |
53 | 68 | }); |
| 69 | + |
| 70 | + describe("searchCodeTool line-length filtering", () => { |
| 71 | + afterEach(() => { |
| 72 | + vi.clearAllMocks(); |
| 73 | + vi.resetModules(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("filters out lines longer than 1000 characters", async () => { |
| 77 | + const childProc = await import("child_process"); |
| 78 | + const long = "a".repeat(1001); |
| 79 | + |
| 80 | + vi.mocked(childProc.exec as any).mockImplementation((...args: any[]) => { |
| 81 | + // exec callback signature: (error, stdout, stderr) |
| 82 | + const cb = args[args.length - 1]; |
| 83 | + cb(null, `path/file.ts:1:${long}\npath/file.ts:2:match`, ""); |
| 84 | + return {} as any; |
| 85 | + }); |
| 86 | + |
| 87 | + const { searchCodeTool } = await import("./searchCode.js"); |
| 88 | + const result = await searchCodeTool.run({ pattern: "match", path: "." }); |
| 89 | + |
| 90 | + expect(result).toContain("path/file.ts:2:match"); |
| 91 | + expect(result).not.toContain(long); |
| 92 | + expect(result).not.toContain("[Results truncated:"); |
| 93 | + }); |
| 94 | + }); |
54 | 95 | }); |
0 commit comments