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