Skip to content

Commit a558b5d

Browse files
committed
test: add comprehensive multi-line JSON parsing tests for ripgrep results
Expand test coverage for parseRipgrepResults to simulate realistic ripgrep --json output with multiple JSON objects per line. - Add multi-line JSON test with begin/match/end sequence - Test graceful handling of malformed JSON lines - Add edge cases for empty and whitespace-only input - Better coverage of real-world ripgrep output scenarios
1 parent e049fda commit a558b5d

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/src/core/tlog-search.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,52 @@ describe("TLOG Search Functions", () => {
4545

4646
expect(result).toHaveLength(0);
4747
});
48+
49+
test("parses multi-line ripgrep JSON output", () => {
50+
const multiLineInput = `{"type":"begin"}
51+
{"type":"match","data":{"path":{"text":"/path/file1.ts"},"lines":{"text":" console.log(\\"[TLOG] first match\\");"},"line_number":5,"submatches":[{"start":2}]}}
52+
{"type":"match","data":{"path":{"text":"/path/file2.ts"},"lines":{"text":"console.log(\\"[TLOG] second match\\");"},"line_number":12,"submatches":[{"start":0}]}}
53+
{"type":"end","data":{"elapsed_total":{"secs":0,"nanos":123456}}}`;
54+
55+
const result = parseRipgrepResults(multiLineInput);
56+
57+
expect(result).toHaveLength(2);
58+
expect(result[0]).toEqual({
59+
filePath: "/path/file1.ts",
60+
line: 4, // 0-based
61+
column: 2,
62+
content: 'console.log("[TLOG] first match");',
63+
});
64+
expect(result[1]).toEqual({
65+
filePath: "/path/file2.ts",
66+
line: 11, // 0-based
67+
column: 0,
68+
content: 'console.log("[TLOG] second match");',
69+
});
70+
});
71+
72+
test("handles malformed JSON lines gracefully", () => {
73+
const malformedInput = `{"type":"begin"}
74+
malformed-json-line
75+
{"type":"match","data":{"path":{"text":"/path/file.ts"},"lines":{"text":"console.log(\\"[TLOG] test\\");"},"line_number":10,"submatches":[{"start":5}]}}
76+
{"incomplete": json
77+
{"type":"end"}`;
78+
79+
const result = parseRipgrepResults(malformedInput);
80+
81+
expect(result).toHaveLength(1);
82+
expect(result[0]).toEqual({
83+
filePath: "/path/file.ts",
84+
line: 9,
85+
column: 5,
86+
content: 'console.log("[TLOG] test");',
87+
});
88+
});
89+
90+
test("handles empty and whitespace-only input", () => {
91+
expect(parseRipgrepResults("")).toEqual([]);
92+
expect(parseRipgrepResults(" \n \t ")).toEqual([]);
93+
expect(parseRipgrepResults("\n\n\n")).toEqual([]);
94+
});
4895
});
4996
});

0 commit comments

Comments
 (0)