Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

grepOutput.test.ts

145 lines5.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { parseGrepOutput } from "./grepOutput.ts";
4​
5describe("parseGrepOutput", () => {
6 it("reads OpenCode's grep output", () => {
7 const output = [
8 "Found 3 matches",
9 "/repo/src/one.ts:",
10 ' Line 1: export const heading = "intro";',
11 "",
12 ' Line 3: export const footer = "outro";',
13 "",
14 "",
15 "/repo/src/two.ts:",
16 " Line 1: export const second = 2;",
17 "",
18 "(Results are truncated. Consider using a more specific path or pattern.)",
19 ].join("\n");
20 expect(parseGrepOutput(output, "/repo/src")).toEqual([
21 { fileName: "/repo/src/one.ts", line: 1, preview: 'export const heading = "intro";' },
22 { fileName: "/repo/src/one.ts", line: 3, preview: 'export const footer = "outro";' },
23 { fileName: "/repo/src/two.ts", line: 1, preview: "export const second = 2;" },
24 {
25 fileName: "/repo/src",
26 preview: "(Results are truncated. Consider using a more specific path or pattern.)",
27 },
28 ]);
29 expect(
30 parseGrepOutput("Found 1 matches\n/tmp/a:12:b.txt:\n Line 2: needle second\n", "/tmp"),
31 ).toEqual([{ fileName: "/tmp/a:12:b.txt", line: 2, preview: "needle second" }]);
32 });
33​
34 it("reads ripgrep's inline and heading forms", () => {
35 expect(parseGrepOutput("src/a.ts:4:const a = 1;\nC:\\w\\b.ts:9:b: 2", ".")).toEqual([
36 { fileName: "src/a.ts", line: 4, preview: "const a = 1;" },
37 { fileName: "C:\\w\\b.ts", line: 9, preview: "b: 2" },
38 ]);
39 expect(parseGrepOutput("/tmp/a:12:b.txt:", "/tmp")).toEqual([
40 { fileName: "/tmp/a", line: 12, preview: "b.txt:" },
41 ]);
42 expect(parseGrepOutput("a.ts:1:needle:\nLine 2:3:needle", ".")).toEqual([
43 { fileName: "a.ts", line: 1, preview: "needle:" },
44 { fileName: "Line 2", line: 3, preview: "needle" },
45 ]);
46 expect(parseGrepOutput("src/a.ts\n4: const a = 1;\n7:a();", ".")).toEqual([
47 { fileName: "src/a.ts", line: 4, preview: " const a = 1;" },
48 { fileName: "src/a.ts", line: 7, preview: "a();" },
49 ]);
50 expect(
51 parseGrepOutput(
52 "docs/Release notes.md \n2:match\n\ndocs/Release notes.md\n6:another match",
53 ".",
54 ),
55 ).toEqual([
56 { fileName: "docs/Release notes.md ", line: 2, preview: "match" },
57 { fileName: "docs/Release notes.md", line: 6, preview: "another match" },
58 ]);
59 expect(
60 parseGrepOutput(
61 "README.md\n1:needle first\n\ndocs/Release notes:12:draft.md\n2:needle second\n",
62 ".",
63 ),
64 ).toEqual([
65 { fileName: "README.md", line: 1, preview: "needle first" },
66 { fileName: "docs/Release notes:12:draft.md", line: 2, preview: "needle second" },
67 ]);
68 expect(
69 parseGrepOutput("README.md\n1:needle first\n\n2026:12:draft.md\n2:at 10:30:00 needle", "."),
70 ).toEqual([
71 { fileName: "README.md", line: 1, preview: "needle first" },
72 { fileName: "2026:12:draft.md", line: 2, preview: "at 10:30:00 needle" },
73 ]);
74 expect(parseGrepOutput("(notes:12:draft.md)\n2:needle", ".")).toEqual([
75 { fileName: "(notes:12:draft.md)", line: 2, preview: "needle" },
76 ]);
77 expect(parseGrepOutput(" notes.md\n2:needle", ".")).toEqual([
78 { fileName: " notes.md", line: 2, preview: "needle" },
79 ]);
80 expect(parseGrepOutput("a.ts:1:first\na.ts:2:second\n2026:notes.md:3:third", ".")).toEqual([
81 { fileName: "a.ts", line: 1, preview: "first" },
82 { fileName: "a.ts", line: 2, preview: "second" },
83 { fileName: "2026:notes.md", line: 3, preview: "third" },
84 ]);
85 });
86​
87 it("reads Gemini's File and L-numbered form", () => {
88 const output = [
89 'Found 2 matches for pattern "needle" in path "src":',
90 "---",
91 "File: one.ts",
92 "L2: const needle = 1;",
93 "---",
94 "File: two.ts",
95 "L7: needle();",
96 "---",
97 ].join("\n");
98 expect(parseGrepOutput(output, "src")).toEqual([
99 { fileName: "one.ts", line: 2, preview: "const needle = 1;" },
100 { fileName: "two.ts", line: 7, preview: "needle();" },
101 ]);
102 });
103​
104 it.each([
105 ["a directory listing", "Contents of /repo:\n [DIR] .git\n [FILE] README.md"],
106 ["an error", "ripgrep failed: invalid regex"],
107 ["prose before a tool-specific numbered line", "Text output\n Line 2: not a grep match"],
108 ["a file list", "/repo/a.ts\n/repo/b.ts"],
109 ["no matches", "No files found"],
110 ["ripgrep context lines", "a.ts\n1-before\n2:needle"],
111 ["a line zero", "a.ts:0:needle"],
112 ["grouped text starts with digits and a colon", "docs/notes:12:draft.md\n2:10:needle"],
113 ["grouped text contains a timestamp", "docs/notes:12:draft.md\n2:at 10:30:00 needle"],
114 ["the second inline filename is all digits", "a.ts:1:first\n2026:3:second"],
115 ["the second inline filename has a numeric prefix", "a.ts:1:first\n2026:notes.md:3:second"],
116 ["two inline rows with a leading-space filename", " notes.md:1:first\n2026:3:second"],
117 ])("leaves %s raw", (_name, output) => {
118 expect(parseGrepOutput(output, "/repo")).toBeUndefined();
119 });
120​
121 it("keeps file and line once the matched text exceeds its budget", () => {
122 const long = "x".repeat(5_000);
123 expect(parseGrepOutput(`a.ts:1:${long}\na.ts:2:${long}\na.ts:3:short`, ".")).toEqual([
124 { fileName: "a.ts", line: 1, preview: long },
125 { fileName: "a.ts", line: 2 },
126 { fileName: "a.ts", line: 3, preview: "short" },
127 ]);
128 });
129​
130 it("counts notes against the budget", () => {
131 const results = parseGrepOutput(`a.ts:1:x\n(${"n".repeat(9_000)})`, ".");
132 expect(results?.[0]).toEqual({ fileName: "a.ts", line: 1, preview: "x" });
133 expect(results?.[1]?.preview?.length).toBe(500);
134 });
135​
136 it.each([
137 [498, `(${"x".repeat(498)}`],
138 [497, `(${"x".repeat(497)}🚀`],
139 ])("keeps whole characters when truncating a note with %i padding units", (padding, preview) => {
140 expect(parseGrepOutput(`a.ts:1:hit\n(${"x".repeat(padding)}🚀)`, ".")).toEqual([
141 { fileName: "a.ts", line: 1, preview: "hit" },
142 { fileName: ".", preview },
143 ]);
144 });
145});