diff --git a/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts b/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts new file mode 100644 index 000000000..2849082b7 --- /dev/null +++ b/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts @@ -0,0 +1,115 @@ +import { describe, expect, it } from "vitest"; +import { Position } from "../.."; +import { CODE_TO_EDIT_CLOSE, CODE_TO_EDIT_OPEN, CURSOR } from "../constants"; +import { + currentFileContentBlock, + editHistoryBlock, + recentlyViewedCodeSnippetsBlock, +} from "./mercuryCoderNextEdit"; + +describe("mercuryCoderNextEdit", () => { + describe("recentlyViewedCodeSnippetsBlock", () => { + it("should format a list of code snippets correctly", () => { + const snippets = [ + { filepath: "/path/to/file1.ts", content: "const a = 1;" }, + { + filepath: "/path/to/file2.ts", + content: "function test() { return true; }", + }, + ]; + + const result = recentlyViewedCodeSnippetsBlock(snippets); + + const expected = + "code_snippet_file_path: /path/to/file1.ts\n" + + "const a = 1;\n" + + "code_snippet_file_path: /path/to/file2.ts\n" + + "function test() { return true; }"; + + expect(result).toBe(expected); + }); + + it("should handle empty snippets array", () => { + const result = recentlyViewedCodeSnippetsBlock([]); + expect(result).toBe(""); + }); + }); + + describe("currentFileContentBlock", () => { + it("should correctly format current file content with editable region and cursor", () => { + const fileContent = "line 1\nline 2\nline 3\nline 4\nline 5"; + const editableRegionStartLine = 1; + const editableRegionEndLine = 3; + const cursorPosition: Position = { line: 2, character: 3 }; + + const result = currentFileContentBlock( + fileContent, + editableRegionStartLine, + editableRegionEndLine, + cursorPosition, + ); + + const expected = + "line 1\n" + + CODE_TO_EDIT_OPEN + + "\n" + + "line 2\n" + + "lin" + + CURSOR + + "e 3\n" + + "line 4\n" + + CODE_TO_EDIT_CLOSE + + "\n" + + "line 5"; + + expect(result).toBe(expected); + }); + + it("should handle cursor at the beginning of a line", () => { + const fileContent = "line 1\nline 2\nline 3"; + const editableRegionStartLine = 0; + const editableRegionEndLine = 2; + const cursorPosition: Position = { line: 1, character: 0 }; + + const result = currentFileContentBlock( + fileContent, + editableRegionStartLine, + editableRegionEndLine, + cursorPosition, + ); + + expect(result).toContain(CODE_TO_EDIT_OPEN); + expect(result).toContain(CURSOR + "line 2"); + expect(result).toContain(CODE_TO_EDIT_CLOSE); + }); + + it("should handle cursor at the end of a line", () => { + const fileContent = "line 1\nline 2\nline 3"; + const editableRegionStartLine = 0; + const editableRegionEndLine = 2; + const cursorPosition: Position = { line: 1, character: 6 }; + + const result = currentFileContentBlock( + fileContent, + editableRegionStartLine, + editableRegionEndLine, + cursorPosition, + ); + + expect(result).toContain("line 2" + CURSOR); + }); + }); + + describe("editHistoryBlock", () => { + it("should return the edit diff history unchanged", () => { + const diffHistory = "diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@"; + const result = editHistoryBlock(diffHistory); + expect(result).toBe(diffHistory); + }); + + it("should handle empty diff history", () => { + const result = editHistoryBlock(""); + expect(result).toBe(""); + }); + }); +});