|
1 | | -/** |
2 | | - * Regression coverage for grep/ripgrep exec display summaries. |
3 | | - * Keeps redirects and `--files` from being mistaken for search targets. |
4 | | - */ |
5 | 1 | import { describe, expect, it } from "vitest"; |
6 | 2 | import { formatToolDetail, resolveToolDisplay } from "./tool-display.js"; |
7 | 3 |
|
8 | | -function detailFor(command: string): string { |
9 | | - return formatToolDetail( |
10 | | - resolveToolDisplay({ name: "exec", args: { command }, detailMode: "explain" }), |
11 | | - ); |
12 | | -} |
| 4 | +const cases = [ |
| 5 | + ["rg --files autonomy .openclaw 2>/dev/null", "list files in .openclaw"], |
| 6 | + ["rg --files -g '*.ts'", "list files"], |
| 7 | + ["rg --files -t ts src", "list files in src"], |
| 8 | + ["rg --files --glob=*.ts src", "list files in src"], |
| 9 | + ["rg -- --files src", 'search "--files" in src'], |
| 10 | + ["rg -e --files src", 'search "--files" in src'], |
| 11 | + ["rg needle src 2>/dev/null", 'search "needle" in src'], |
| 12 | + ["rg needle src 2> /dev/null", 'search "needle" in src'], |
| 13 | + ["rg needle src>/dev/null", 'search "needle" in src'], |
| 14 | + ["rg needle src 2>&1", 'search "needle" in src'], |
| 15 | + ["rg needle src >|out", 'search "needle" in src'], |
| 16 | + ["rg '2>/dev/null' README.md", 'search "2>/dev/null" in README.md'], |
| 17 | + [String.raw`rg 2\>literal README.md`, 'search "2>literal" in README.md'], |
| 18 | + ["rg -e needle src", 'search "needle" in src'], |
| 19 | + ["rg -ne'needle' src", 'search "needle" in src'], |
| 20 | + ["rg -f patterns src", "search text in src"], |
| 21 | + ["grep -E -e needle src", 'search "needle" in src'], |
| 22 | + ["rg -E utf8 needle src", 'search "needle" in src'], |
| 23 | + ["cat >out <<EOF\nsynthetic body\nEOF", "show output"], |
| 24 | + ["head -n 2 source > out", "show first 2 lines of source"], |
| 25 | + ["cp 2>err source destination", "copy source to destination"], |
| 26 | + ["tail source 2>&1", "show source"], |
| 27 | +] as const; |
13 | 28 |
|
14 | | -describe("exec search display", () => { |
15 | | - it("does not treat a shell redirect as the search target", () => { |
16 | | - // `2>/dev/null` is not a positional path. Treating it as one produced summaries |
17 | | - // like `search "autonomy" in 2>/dev/null`, which read as though the agent had |
18 | | - // searched a file named `2>/dev/null`. |
19 | | - for (const [command, expected] of [ |
20 | | - ['rg "foo" src/agents 2>/dev/null', 'search "foo" in src/agents'], |
21 | | - ['rg "foo" src/agents >/dev/null', 'search "foo" in src/agents'], |
22 | | - ['rg "foo" src/agents 2>&1', 'search "foo" in src/agents'], |
23 | | - ]) { |
24 | | - expect(detailFor(command)).toBe(expected); |
25 | | - } |
| 29 | +describe("exec search and redirect display", () => { |
| 30 | + it.each(cases)("summarizes %s", (command, expected) => { |
| 31 | + expect( |
| 32 | + formatToolDetail( |
| 33 | + resolveToolDisplay({ name: "exec", args: { command }, detailMode: "explain" }), |
| 34 | + ), |
| 35 | + ).toBe(expected); |
26 | 36 | }); |
27 | | - |
28 | | - it("treats `rg --files` as a file listing, not a pattern search", () => { |
29 | | - // With --files ripgrep takes no pattern; every positional is a path. Reading the |
30 | | - // first positional as a pattern produced `search "autonomy" in 2>/dev/null`. |
31 | | - for (const [command, expected] of [ |
32 | | - ["rg --files autonomy", "list files in autonomy"], |
33 | | - ["rg --files autonomy .openclaw 2>/dev/null", "list files in .openclaw"], |
34 | | - ["rg --files", "list files"], |
35 | | - ]) { |
36 | | - expect(detailFor(command)).toBe(expected); |
37 | | - } |
38 | | - }); |
39 | | - |
40 | | - it("keeps ordinary search summaries unchanged", () => { |
41 | | - for (const [command, expected] of [ |
42 | | - ['rg "foo|bar" src/agents', 'search "foo|bar" in src/agents'], |
43 | | - // `-e` supplies the pattern, so `somefile` is the only positional and there is |
44 | | - // no second one to use as a target. |
45 | | - ["grep -e pattern somefile", 'search "pattern"'], |
46 | | - ]) { |
47 | | - expect(detailFor(command)).toBe(expected); |
48 | | - } |
| 37 | + it("preserves the original command in raw detail mode", () => { |
| 38 | + const command = "rg needle src 2> /dev/null"; |
| 39 | + const detail = formatToolDetail( |
| 40 | + resolveToolDisplay({ name: "bash", args: { command }, detailMode: "raw" }), |
| 41 | + ); |
| 42 | + expect(detail).toContain('search "needle" in src'); |
| 43 | + expect(detail).toContain(command); |
49 | 44 | }); |
50 | 45 | }); |
0 commit comments