Elffuss BraidHub

kikocis/continue public

main · 13.6 KiBRawPermalink
1// File generated by Continue
2import {
3 dedent,
4 dedentAndGetCommonWhitespace,
5 deduplicateArray,
6 copyOf,
7 getMarkdownLanguageTagForFile,
8 removeCodeBlocksAndTrim,
9 removeQuotesAndEscapes,
10} from "./";
11
12describe("deduplicateArray", () => {
13 it("should return an empty array when given an empty array", () => {
14 const result = deduplicateArray([], (a, b) => a === b);
15 expect(result).toEqual([]);
16 });
17
18 it("should return the same array when there are no duplicates", () => {
19 const input = [1, 2, 3, 4, 5];
20 const result = deduplicateArray(input, (a, b) => a === b);
21 expect(result).toEqual(input);
22 });
23
24 it("should remove duplicates based on the equality function", () => {
25 const input = [1, 2, 2, 3, 4, 4, 5];
26 const result = deduplicateArray(input, (a, b) => a === b);
27 expect(result).toEqual([1, 2, 3, 4, 5]);
28 });
29
30 it("should work with objects using custom equality function", () => {
31 const input = [
32 { id: 1, name: "Alice" },
33 { id: 2, name: "Bob" },
34 { id: 1, name: "Alice" },
35 { id: 3, name: "Charlie" },
36 ];
37 const result = deduplicateArray(input, (a, b) => a.id === b.id);
38 expect(result).toEqual([
39 { id: 1, name: "Alice" },
40 { id: 2, name: "Bob" },
41 { id: 3, name: "Charlie" },
42 ]);
43 });
44
45 it("should preserve the order of items", () => {
46 const input = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
47 const result = deduplicateArray(input, (a, b) => a === b);
48 expect(result).toEqual([3, 1, 4, 5, 9, 2, 6]);
49 });
50
51 it("should work with strings", () => {
52 const input = ["apple", "banana", "apple", "cherry", "banana", "date"];
53 const result = deduplicateArray(input, (a, b) => a === b);
54 expect(result).toEqual(["apple", "banana", "cherry", "date"]);
55 });
56
57 it("should handle arrays with all duplicate elements", () => {
58 const input = [1, 1, 1, 1, 1];
59 const result = deduplicateArray(input, (a, b) => a === b);
60 expect(result).toEqual([1]);
61 });
62
63 it("should work with custom equality function for complex objects", () => {
64 const input = [
65 { x: 1, y: 2 },
66 { x: 2, y: 1 },
67 { x: 1, y: 2 },
68 { x: 3, y: 4 },
69 ];
70 const result = deduplicateArray(
71 input,
72 (a, b) => a.x === b.x && a.y === b.y,
73 );
74 expect(result).toEqual([
75 { x: 1, y: 2 },
76 { x: 2, y: 1 },
77 { x: 3, y: 4 },
78 ]);
79 });
80
81 it("should handle large arrays efficiently", () => {
82 const input = Array(10000)
83 .fill(0)
84 .map((_, i) => i % 100);
85 const start = performance.now();
86 const result = deduplicateArray(input, (a, b) => a === b);
87 const end = performance.now();
88 expect(result).toHaveLength(100);
89 expect(end - start).toBeLessThan(1000); // Ensure it completes in less than 1 second
90 });
91});
92
93describe("dedentAndGetCommonWhitespace", () => {
94 let originalString: string;
95
96 beforeEach(() => {
97 // Setup any global variables or states if needed
98 originalString = " line1\n line2\n line3";
99 });
100
101 afterEach(() => {
102 // Tear down any changes to global variables or states if needed
103 originalString = "";
104 });
105
106 test("should dedent and return common whitespace for a simple case", () => {
107 const input = " line1\n line2\n line3";
108 const output = dedentAndGetCommonWhitespace(input);
109 expect(output).toEqual(["line1\nline2\nline3", " "]);
110 });
111
112 test("should handle empty string", () => {
113 const input = "";
114 const output = dedentAndGetCommonWhitespace(input);
115 expect(output).toEqual(["", ""]);
116 });
117
118 test("should handle string with only whitespace", () => {
119 const input = " ";
120 const output = dedentAndGetCommonWhitespace(input);
121 expect(output).toEqual(["", ""]);
122 });
123
124 test("should handle string with mixed whitespace and content", () => {
125 const input = " line1\n line2\n line3";
126 const output = dedentAndGetCommonWhitespace(input);
127 expect(output).toEqual([" line1\nline2\n line3", " "]);
128 });
129
130 test("should handle string with no common leading whitespace", () => {
131 const input = "line1\n line2\n line3";
132 const output = dedentAndGetCommonWhitespace(input);
133 expect(output).toEqual([input, ""]);
134 });
135
136 test("should handle string with empty lines", () => {
137 const input = " line1\n\n line3";
138 const output = dedentAndGetCommonWhitespace(input);
139 expect(output).toEqual(["line1\n\nline3", " "]);
140 });
141
142 test("should handle string with only empty lines", () => {
143 const input = "\n\n";
144 const output = dedentAndGetCommonWhitespace(input);
145 expect(output).toEqual(["\n\n", ""]);
146 });
147
148 test("should handle string with tabs as whitespace", () => {
149 const input = "\tline1\n\tline2\n\tline3";
150 const output = dedentAndGetCommonWhitespace(input);
151 expect(output).toEqual(["line1\nline2\nline3", "\t"]);
152 });
153
154 test("should handle string with mixed tabs and spaces", () => {
155 const input = "\t line1\n\t line2\n\t line3";
156 const output = dedentAndGetCommonWhitespace(input);
157 expect(output).toEqual(["line1\nline2\nline3", "\t "]);
158 });
159
160 test("should handle string with different leading whitespace lengths", () => {
161 const input = " line1\n line2\n line3";
162 const output = dedentAndGetCommonWhitespace(input);
163 expect(output).toEqual([" line1\nline2\n line3", " "]);
164 });
165});
166
167describe("dedent function", () => {
168 it("should remove common leading whitespace from all lines", () => {
169 const result = dedent`
170 Hello
171 World
172 !
173 `;
174 expect(result).toBe("Hello\n World\n !");
175 });
176
177 it("should handle strings with no indentation", () => {
178 const result = dedent`Hello
179World
180!`;
181 expect(result).toBe("Hello\nWorld\n!");
182 });
183
184 it("should handle strings with mixed indentation", () => {
185 const result = dedent`
186 Hello
187 World
188 !
189 `;
190 expect(result).toBe(" Hello\nWorld\n !");
191 });
192
193 it("should remove leading and trailing empty lines", () => {
194 const result = dedent`
195
196 Hello
197 World
198
199 `;
200 expect(result).toBe("Hello\nWorld");
201 });
202
203 it("should handle empty strings", () => {
204 const result = dedent``;
205 expect(result).toBe("");
206 });
207
208 it("should handle strings with only whitespace", () => {
209 const result = dedent`
210
211 `;
212 expect(result).toBe("");
213 });
214
215 it("should handle interpolated values", () => {
216 const world = "World";
217 const result = dedent`
218 Hello ${world}
219 How are you?
220 `;
221 expect(result).toBe("Hello World\n How are you?");
222 });
223
224 it("should handle multiple interpolated values", () => {
225 const greeting = "Hello";
226 const name = "Alice";
227 const question = "How are you?";
228 const result = dedent`
229 ${greeting} ${name}
230 ${question}
231 `;
232 expect(result).toBe("Hello Alice\n How are you?");
233 });
234
235 it("should handle interpolated values with different indentation", () => {
236 const value1 = "foo";
237 const value2 = "bar";
238 const result = dedent`
239 ${value1}
240 ${value2}
241 `;
242 expect(result).toBe("foo\n bar");
243 });
244
245 it("should handle a single line with indentation", () => {
246 const result = dedent` Hello World!`;
247 expect(result).toBe("Hello World!");
248 });
249
250 it("should handle a string with only one non-empty line", () => {
251 const result = dedent`
252
253 Hello World!
254
255 `;
256 expect(result).toBe("Hello World!");
257 });
258
259 it("should handle a string with Unicode characters", () => {
260 const result = dedent`
261 こんにちは
262 世界
263 `;
264 expect(result).toBe("こんにちは\n 世界");
265 });
266
267 it("should handle a string with emoji", () => {
268 const result = dedent`
269 🌍
270 🌎
271 🌏
272 `;
273 expect(result).toBe("🌍\n 🌎\n 🌏");
274 });
275
276 it.skip("should handle a string with CRLF line endings", () => {
277 const result = dedent`
278 Hello\r
279 World\r
280 `;
281 expect(result).toBe("Hello\r\n World");
282 });
283
284 it.skip("should handle strings with tabs", () => {
285 const result = dedent`
286 \tHello
287 \t\tWorld
288 \t\t\t!
289 `;
290 expect(result).toBe("\tHello\n\t\tWorld\n\t\t\t!");
291 });
292
293 it("should not count empty lines in the minimum indentation", () => {
294 const result = dedent`
295 Hello
296
297 World
298 `;
299
300 expect(result).toBe("Hello\n\nWorld");
301 });
302
303 it("should work with templated strings", () => {
304 const language = "typescript";
305 const code = "console.log('hello');\nconsole.log('world');";
306
307 const result = dedent`
308 This is the prefix of the file:
309 \`\`\`${language}
310 ${code}
311 \`\`\``;
312
313 expect(result).toBe(`\
314This is the prefix of the file:
315\`\`\`${language}
316${code}
317\`\`\``);
318 });
319});
320
321describe("removeQuotesAndEscapes", () => {
322 it("should remove surrounding double quotes and unescape characters", () => {
323 const input = '"Hello \\"World\\""';
324 const output = removeQuotesAndEscapes(input);
325 expect(output).toBe('Hello "World"');
326 });
327
328 it("should remove surrounding single quotes and unescape characters", () => {
329 const input = "'It\\'s a test'";
330 const output = removeQuotesAndEscapes(input);
331 expect(output).toBe("It's a test");
332 });
333
334 it("should handle smart quotes and remove them", () => {
335 const input = "“Smart Quotes”";
336 const output = removeQuotesAndEscapes(input);
337 expect(output).toBe("Smart Quotes");
338 });
339
340 it("should remove backticks if present", () => {
341 const input = "`Some code snippet`";
342 const output = removeQuotesAndEscapes(input);
343 expect(output).toBe("Some code snippet");
344 });
345
346 it("should handle multiple layers of quotes", () => {
347 const input = "\"\"''``Nested``''\"\"";
348 const output = removeQuotesAndEscapes(input);
349 expect(output).toBe("Nested");
350 });
351
352 it("should handle strings without quotes", () => {
353 const input = "No quotes here";
354 const output = removeQuotesAndEscapes(input);
355 expect(output).toBe("No quotes here");
356 });
357
358 it("should handle empty strings", () => {
359 const input = "";
360 const output = removeQuotesAndEscapes(input);
361 expect(output).toBe("");
362 });
363
364 it("should handle strings with only escape sequences", () => {
365 const input = "\\n\\t\\\\";
366 const output = removeQuotesAndEscapes(input);
367 expect(output).toBe("\n\t\\");
368 });
369});
370
371describe("getMarkdownLanguageTagForFile", () => {
372 it("should return correct language tag for known extensions", () => {
373 expect(getMarkdownLanguageTagForFile("test.py")).toBe("python");
374 expect(getMarkdownLanguageTagForFile("test.tsx")).toBe("tsx");
375 expect(getMarkdownLanguageTagForFile("test.java")).toBe("java");
376 expect(getMarkdownLanguageTagForFile("test.class")).toBe("java");
377 expect(getMarkdownLanguageTagForFile("test.md")).toBe("markdown");
378 expect(getMarkdownLanguageTagForFile("test.sh")).toBe("shell");
379 expect(getMarkdownLanguageTagForFile("test.sql")).toBe("sql");
380 });
381
382 it("should sanitize ranges from the extension", () => {
383 expect(getMarkdownLanguageTagForFile("test.java (1-5)")).toBe("java");
384 });
385
386 it("should return the extension if not in the known list", () => {
387 expect(getMarkdownLanguageTagForFile("file.unknownext")).toBe("unknownext");
388 });
389
390 it("should handle filenames without extension", () => {
391 expect(getMarkdownLanguageTagForFile("Makefile")).toBe("Makefile");
392 });
393
394 it("should handle filenames with multiple dots", () => {
395 expect(getMarkdownLanguageTagForFile("archive.tar.gz")).toBe("gz");
396 });
397
398 it("should handle edge case with empty string as filename", () => {
399 expect(getMarkdownLanguageTagForFile("")).toBe("");
400 });
401
402 it("should correctly identify files with no extensions", () => {
403 expect(getMarkdownLanguageTagForFile("README")).toBe("README");
404 });
405});
406
407describe("copyOf", () => {
408 it("should create a deep copy of an object", () => {
409 const original = { a: 1, b: { c: 2 } };
410 const copied = copyOf(original);
411 expect(copied).toEqual(original);
412 expect(copied).not.toBe(original);
413 expect(copied.b).not.toBe(original.b);
414 });
415
416 it("should handle arrays", () => {
417 const original = [1, 2, { a: 3 }];
418 const copied = copyOf(original);
419 expect(copied).toEqual(original);
420 expect(copied).not.toBe(original);
421 expect(copied[2]).not.toBe(original[2]);
422 });
423
424 it("should return null for null input", () => {
425 expect(copyOf(null)).toBeNull();
426 });
427
428 it("should return undefined for undefined input", () => {
429 expect(copyOf(undefined)).toBeUndefined();
430 });
431
432 it("should handle objects with circular references gracefully", () => {
433 // This may not be feasible with JSON.stringify – consider alternative methods like a library for deep cloning.
434 const a: any = {};
435 a.self = a;
436 expect(() => copyOf(a)).toThrow(); // Ideally, handle circular refs before calling JSON.stringify
437 });
438});
439
440describe("removeCodeBlocksAndTrim", () => {
441 it("should remove code blocks from text", () => {
442 const text =
443 "Here is some text.\n```javascript\nconsole.log('Hello');\n```\nMore text.";
444 const output = removeCodeBlocksAndTrim(text);
445 expect(output).toBe("Here is some text.\n\nMore text.");
446 });
447
448 it("should remove multiple code blocks", () => {
449 const text =
450 "Text before.\n```code block 1```\nBetween code blocks.\n```code block 2```\nText after.";
451 const output = removeCodeBlocksAndTrim(text);
452 expect(output).toBe("Text before.\n\nBetween code blocks.\n\nText after.");
453 });
454
455 it("should handle text without code blocks", () => {
456 const text = "No code blocks here.";
457 const output = removeCodeBlocksAndTrim(text);
458 expect(output).toBe("No code blocks here.");
459 });
460
461 it("should trim whitespace from the result", () => {
462 const text = " \n```code```\n ";
463 const output = removeCodeBlocksAndTrim(text);
464 expect(output).toBe("");
465 });
466});