Open History to see the last commit
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 });
30
31 it("reads ripgrep's inline and heading forms", () => {
32 expect(parseGrepOutput("src/a.ts:4:const a = 1;\nC:\\w\\b.ts:9:b: 2", ".")).toEqual([
33 { fileName: "src/a.ts", line: 4, preview: "const a = 1;" },
34 { fileName: "C:\\w\\b.ts", line: 9, preview: "b: 2" },
35 ]);
36 expect(parseGrepOutput("src/a.ts\n4: const a = 1;\n7:a();", ".")).toEqual([
37 { fileName: "src/a.ts", line: 4, preview: " const a = 1;" },
38 { fileName: "src/a.ts", line: 7, preview: "a();" },
39 ]);
40 });
41
42 it("reads Gemini's File and L-numbered form", () => {
43 const output = [
44 'Found 2 matches for pattern "needle" in path "src":',
45 "---",
46 "File: one.ts",
47 "L2: const needle = 1;",
48 "---",
49 "File: two.ts",
50 "L7: needle();",
51 "---",
52 ].join("\n");
53 expect(parseGrepOutput(output, "src")).toEqual([
54 { fileName: "one.ts", line: 2, preview: "const needle = 1;" },
55 { fileName: "two.ts", line: 7, preview: "needle();" },
56 ]);
57 });
58
59 it.each([
60 ["a directory listing", "Contents of /repo:\n [DIR] .git\n [FILE] README.md"],
61 ["an error", "ripgrep failed: invalid regex"],
62 ["a file list", "/repo/a.ts\n/repo/b.ts"],
63 ["no matches", "No files found"],
64 ["ripgrep context lines", "a.ts\n1-before\n2:needle"],
65 ["a line zero", "a.ts:0:needle"],
66 ])("leaves %s raw", (_name, output) => {
67 expect(parseGrepOutput(output, "/repo")).toBeUndefined();
68 });
69
70 it("keeps file and line once the matched text exceeds its budget", () => {
71 const long = "x".repeat(5_000);
72 expect(parseGrepOutput(`a.ts:1:${long}\na.ts:2:${long}\na.ts:3:short`, ".")).toEqual([
73 { fileName: "a.ts", line: 1, preview: long },
74 { fileName: "a.ts", line: 2 },
75 { fileName: "a.ts", line: 3, preview: "short" },
76 ]);
77 });
78
79 it("counts notes against the budget", () => {
80 const results = parseGrepOutput(`a.ts:1:x\n(${"n".repeat(9_000)})`, ".");
81 expect(results?.[0]).toEqual({ fileName: "a.ts", line: 1, preview: "x" });
82 expect(results?.[1]?.preview?.length).toBe(500);
83 });
84});