Elffuss BraidHub

kikocis/continue public

94f7b85f6068 · 3.5 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { Position } from "../..";
3import {
4 MERCURY_CODE_TO_EDIT_CLOSE,
5 MERCURY_CODE_TO_EDIT_OPEN,
6 MERCURY_CURSOR,
7} from "../constants";
8import {
9 currentFileContentBlock,
10 editHistoryBlock,
11 recentlyViewedCodeSnippetsBlock,
12} from "./mercuryCoderNextEdit";
13
14describe("mercuryCoderNextEdit", () => {
15 describe("recentlyViewedCodeSnippetsBlock", () => {
16 it("should format a list of code snippets correctly", () => {
17 const snippets = [
18 { filepath: "/path/to/file1.ts", content: "const a = 1;" },
19 {
20 filepath: "/path/to/file2.ts",
21 content: "function test() { return true; }",
22 },
23 ];
24
25 const result = recentlyViewedCodeSnippetsBlock(snippets);
26
27 const expected =
28 "code_snippet_file_path: /path/to/file1.ts\n" +
29 "const a = 1;\n" +
30 "code_snippet_file_path: /path/to/file2.ts\n" +
31 "function test() { return true; }";
32
33 expect(result).toBe(expected);
34 });
35
36 it("should handle empty snippets array", () => {
37 const result = recentlyViewedCodeSnippetsBlock([]);
38 expect(result).toBe("");
39 });
40 });
41
42 describe("currentFileContentBlock", () => {
43 it("should correctly format current file content with editable region and cursor", () => {
44 const fileContent = "line 1\nline 2\nline 3\nline 4\nline 5";
45 const editableRegionStartLine = 1;
46 const editableRegionEndLine = 3;
47 const cursorPosition: Position = { line: 2, character: 3 };
48
49 const result = currentFileContentBlock(
50 fileContent,
51 editableRegionStartLine,
52 editableRegionEndLine,
53 cursorPosition,
54 );
55
56 const expected =
57 "line 1\n" +
58 MERCURY_CODE_TO_EDIT_OPEN +
59 "\n" +
60 "line 2\n" +
61 "lin" +
62 MERCURY_CURSOR +
63 "e 3\n" +
64 "line 4\n" +
65 MERCURY_CODE_TO_EDIT_CLOSE +
66 "\n" +
67 "line 5";
68
69 expect(result).toBe(expected);
70 });
71
72 it("should handle cursor at the beginning of a line", () => {
73 const fileContent = "line 1\nline 2\nline 3";
74 const editableRegionStartLine = 0;
75 const editableRegionEndLine = 2;
76 const cursorPosition: Position = { line: 1, character: 0 };
77
78 const result = currentFileContentBlock(
79 fileContent,
80 editableRegionStartLine,
81 editableRegionEndLine,
82 cursorPosition,
83 );
84
85 expect(result).toContain(MERCURY_CODE_TO_EDIT_OPEN);
86 expect(result).toContain(MERCURY_CURSOR + "line 2");
87 expect(result).toContain(MERCURY_CODE_TO_EDIT_CLOSE);
88 });
89
90 it("should handle cursor at the end of a line", () => {
91 const fileContent = "line 1\nline 2\nline 3";
92 const editableRegionStartLine = 0;
93 const editableRegionEndLine = 2;
94 const cursorPosition: Position = { line: 1, character: 6 };
95
96 const result = currentFileContentBlock(
97 fileContent,
98 editableRegionStartLine,
99 editableRegionEndLine,
100 cursorPosition,
101 );
102
103 expect(result).toContain("line 2" + MERCURY_CURSOR);
104 });
105 });
106
107 describe("editHistoryBlock", () => {
108 it("should return the edit diff history unchanged", () => {
109 const diffHistory =
110 "diff --git a/file.ts b/file.ts\n==============================\n@@ -1,3 +1,4 @@";
111 const result = editHistoryBlock([diffHistory]);
112 expect(result).toBe("@@ -1,3 +1,4 @@");
113 });
114
115 it("should handle empty diff history", () => {
116 const result = editHistoryBlock([]);
117 expect(result).toBe("");
118 });
119 });
120});