Skip to content

Commit ae57291

Browse files
authored
feat(cli): filter lines with more than 1000 characters (#8714)
* filter out lines greater than 1000 characters * add tests
1 parent 7bc39db commit ae57291

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

extensions/cli/src/tools/searchCode.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
116
// Since we want to test just the interface and not the internals,
217
// let's create a simplified version of the run function to test the truncation logic
318
describe("searchCodeTool", () => {
@@ -51,4 +66,30 @@ describe("searchCodeTool", () => {
5166
// Check that we have the expected number of lines (header + 50 content lines)
5267
expect(nonEmptyLines.length).toBe(51);
5368
});
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+
});
5495
});

extensions/cli/src/tools/searchCode.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const execPromise = util.promisify(child_process.exec);
1010

1111
// Default maximum number of results to display
1212
const DEFAULT_MAX_RESULTS = 100;
13+
const MAX_LINE_LENGTH = 1000;
1314

1415
export const searchCodeTool: Tool = {
1516
name: "Search",
@@ -84,7 +85,13 @@ export const searchCodeTool: Tool = {
8485
}
8586

8687
// Split the results into lines and limit the number of results
87-
const lines = stdout.split("\n");
88+
const splitLines = stdout.split("\n");
89+
const lines = splitLines.filter((line) => line.length <= MAX_LINE_LENGTH);
90+
if (lines.length === 0) {
91+
return `No matches found for pattern "${args.pattern}"${
92+
args.file_pattern ? ` in files matching "${args.file_pattern}"` : ""
93+
}.`;
94+
}
8895
const truncated = lines.length > DEFAULT_MAX_RESULTS;
8996
const limitedLines = lines.slice(0, DEFAULT_MAX_RESULTS);
9097
const resultText = limitedLines.join("\n");

0 commit comments

Comments
 (0)