Elffuss BraidHub

kikocis/continue public

main · 5.5 KiBRawPermalink
1import { expect, test } from "vitest";
2import { formatGrepSearchResults } from "./grepSearch";
3
4// Sample grep output mimicking what would come from ripgrep with the params:
5// ripgrep -i --ignore-file .continueignore --ignore-file .gitignore -C 2 --heading -m 100 -e <query> .
6const sampleGrepOutput = `./program.cs
7 Console.WriteLine("Hello World!");
8 Calculator calc = new Calculator();
9 calc.Add(5).Subtract(3);
10 Console.WriteLine("Result: " + calc.GetResult());
11 }
12--
13 }
14
15 public Calculator Subtract(double number)
16 {
17 result -= number;
18
19./test.kt
20 }
21
22 fun subtract(number: Double): Test {
23 result -= number
24 return this
25
26./test.php
27 }
28
29 public function subtract($number) {
30 $this->result -= $number;
31 return $this;
32--
33
34$calc = new Calculator();
35$calc->add(10)->subtract(5);
36echo "Result: " . $calc->getResult() . "\n";
37
38
39./Calculator.java
40 }
41
42 public Calculator subtract(double number) {
43 result -= number;
44 return this;
45--
46 public static void main(String[] args) {
47 Calculator calc = new Calculator();
48 calc.add(10).subtract(5);
49 System.out.println("Result: " + calc.getResult());
50 }
51
52./calculator_test/Calculator.java
53 }
54
55 public int subtract(int a, int b) {
56 return a - b;
57 }
58
59./test.rb
60 end
61
62 def subtract(number)
63 @result -= number
64 self
65--
66
67calc = Calculator.new
68calc.add(5).subtract(3)
69puts "Result: #{calc.get_result}"
70
71./test.js
72 return this;
73 }
74 subtract(number) {
75 return this;
76 }
77
78./test.py
79 return self
80
81 def subtract(self, number):
82 self.result -= number
83 return self
84
85./test.ts
86 }
87
88 subtract(number: number): Calculator {
89 this.result -= number;
90 return this;`;
91
92test("formats grep search results correctly", () => {
93 const result = formatGrepSearchResults(sampleGrepOutput);
94
95 expect(result.numResults).toBe(13); // 4 file paths in the sample
96 expect(result.truncated).toBe(false);
97
98 // Check that all file paths are preserved
99 expect(result.formatted).toContain("./program.cs");
100 expect(result.formatted).toContain("./test.kt");
101 expect(result.formatted).toContain("./test.php");
102 expect(result.formatted).toContain("./Calculator.java");
103
104 // Check that content is preserved and properly indented
105 expect(result.formatted).toContain('Console.WriteLine("Hello World!");');
106 expect(result.formatted).toContain("fun subtract(number: Double): Test {");
107 expect(result.formatted).toContain("public function subtract($number) {");
108 expect(result.formatted).toContain(
109 "public Calculator subtract(double number) {",
110 );
111});
112
113test("handles empty input", () => {
114 const result = formatGrepSearchResults("");
115
116 expect(result.numResults).toBe(0);
117 expect(result.truncated).toBe(false);
118 expect(result.formatted).toBe("");
119});
120
121test("handles input with only file paths (no content) - lines should be skipped", () => {
122 const input =
123 "./empty-file.ts\n./another-empty.js\n./has-content.ts\n searchMatch";
124 const result = formatGrepSearchResults(input);
125
126 expect(result.numResults).toBe(3);
127 expect(result.formatted).toBe("./has-content.ts\n searchMatch");
128});
129
130test("truncates output when exceeding maxChars", () => {
131 const maxChars = 50;
132 const result = formatGrepSearchResults(sampleGrepOutput, maxChars);
133
134 expect(result.truncated).toBe(true);
135 expect(result.formatted.length).toBe(maxChars);
136});
137
138test("normalizes indentation to 2 spaces", () => {
139 const input = `./file.ts
140 function test() {
141 const x = 1;
142 console.log(x);
143 }`;
144
145 const result = formatGrepSearchResults(input);
146
147 // The function should normalize the indentation to 2 spaces
148 expect(result.formatted).toContain("./file.ts");
149 expect(result.formatted).toContain(" function test() {");
150 expect(result.formatted).toContain(" const x = 1;");
151 expect(result.formatted).toContain(" console.log(x);");
152 expect(result.formatted).toContain(" }");
153});
154
155test("skips leading single-char or empty lines after file paths", () => {
156 const input = `./file.ts
157
158function test() {
159 console.log("test");
160}`;
161
162 const result = formatGrepSearchResults(input);
163
164 expect(result.formatted).toContain("./file.ts");
165 expect(result.formatted).not.toContain("\n \n"); // The single space line should be skipped
166 expect(result.formatted).toContain("function test() {");
167});
168
169test("processes separator lines correctly", () => {
170 const input = `./file1.ts
171function one() {
172 return 1;
173}
174--
175function two() {
176 return 2;
177}
178
179./file2.ts
180class Test {
181 method() {}
182}`;
183
184 const result = formatGrepSearchResults(input);
185
186 expect(result.numResults).toBe(3);
187 expect(result.formatted).toContain("./file1.ts");
188 expect(result.formatted).toContain("--");
189 expect(result.formatted).toContain("function two() {");
190 expect(result.formatted).toContain("./file2.ts");
191});
192
193test("increases indentation when original is less than 2 spaces", () => {
194 const input = `./minimal-indent.ts
195function test() {
196 singleSpaceIndent();
197}`;
198
199 const result = formatGrepSearchResults(input);
200
201 expect(result.formatted).toContain(" function test() {");
202 expect(result.formatted).toContain(" singleSpaceIndent();");
203 expect(result.formatted).toContain(" }");
204});
205
206test("decreases indentation when original is more than 2 spaces", () => {
207 const input = `./excessive-indent.ts
208 function test() {
209 tooMuchIndent();
210 }`;
211
212 const result = formatGrepSearchResults(input);
213
214 expect(result.formatted).toContain(" function test() {");
215 expect(result.formatted).toContain(" tooMuchIndent();");
216 expect(result.formatted).toContain(" }");
217});