| 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | import { Position } from "../.."; |
| 3 | import { SnippetPayload } from "../../autocomplete/snippets"; |
| 4 | import { AutocompleteSnippetType } from "../../autocomplete/snippets/types"; |
| 5 | import { HelperVars } from "../../autocomplete/util/HelperVars"; |
| 6 | import { NEXT_EDIT_MODELS } from "../../llm/constants"; |
| 7 | import { |
| 8 | INSTINCT_USER_CURSOR_IS_HERE_TOKEN, |
| 9 | INSTINCT_USER_PROMPT_PREFIX, |
| 10 | MERCURY_CURRENT_FILE_CONTENT_CLOSE, |
| 11 | MERCURY_CURRENT_FILE_CONTENT_OPEN, |
| 12 | MERCURY_CURSOR, |
| 13 | MERCURY_EDIT_DIFF_HISTORY_CLOSE, |
| 14 | MERCURY_EDIT_DIFF_HISTORY_OPEN, |
| 15 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE, |
| 16 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN, |
| 17 | } from "../constants"; |
| 18 | import { |
| 19 | renderDefaultSystemPrompt, |
| 20 | renderDefaultUserPrompt, |
| 21 | renderPrompt, |
| 22 | } from "./NextEditPromptEngine"; |
| 23 | |
| 24 | describe("NextEditPromptEngine", () => { |
| 25 | describe("renderPrompt", () => { |
| 26 | let mercuryHelper: HelperVars; |
| 27 | let instinctHelper: HelperVars; |
| 28 | let testHelper: HelperVars; |
| 29 | let unsupportedHelper: HelperVars; |
| 30 | let ctx: any; |
| 31 | |
| 32 | beforeEach(() => { |
| 33 | mercuryHelper = { |
| 34 | modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS, |
| 35 | fileContents: "function test() {\n const a = 1;\n return a;\n}", |
| 36 | pos: { line: 1, character: 12 } as Position, |
| 37 | lang: { name: "typescript" }, |
| 38 | } as HelperVars; |
| 39 | instinctHelper = { |
| 40 | modelName: "continuedev/instinct" as NEXT_EDIT_MODELS, |
| 41 | fileContents: "function test() {\n const a = 1;\n return a;\n}", |
| 42 | pos: { line: 1, character: 12 } as Position, |
| 43 | lang: { name: "typescript" }, |
| 44 | } as HelperVars; |
| 45 | unsupportedHelper = { |
| 46 | modelName: "mistral/codestral" as NEXT_EDIT_MODELS, |
| 47 | fileContents: "function test() {\n const a = 1;\n return a;\n}", |
| 48 | pos: { line: 1, character: 12 } as Position, |
| 49 | lang: { name: "typescript" }, |
| 50 | } as HelperVars; |
| 51 | |
| 52 | ctx = { |
| 53 | recentlyViewedCodeSnippets: [ |
| 54 | { filepath: "/path/to/file1.ts", content: "const b = 2;" }, |
| 55 | ], |
| 56 | currentFileContent: "function test() {\n const a = 1;\n return a;\n}", |
| 57 | editDiffHistory: ["diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@"], |
| 58 | editableRegionStartLine: 0, |
| 59 | editableRegionEndLine: 3, |
| 60 | userEdits: "Added constant a", |
| 61 | languageShorthand: "ts", |
| 62 | userExcerpts: "const a = 1;", |
| 63 | }; |
| 64 | }); |
| 65 | |
| 66 | it("should render mercury-coder-nextedit prompt correctly", async () => { |
| 67 | const result = await renderPrompt(mercuryHelper, ctx); |
| 68 | |
| 69 | expect(result).toHaveProperty("prompt"); |
| 70 | expect(result.prompt.role).toBe("user"); |
| 71 | |
| 72 | const content = result.prompt.content; |
| 73 | expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN); |
| 74 | expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE); |
| 75 | expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_OPEN); |
| 76 | expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_CLOSE); |
| 77 | expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_OPEN); |
| 78 | expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_CLOSE); |
| 79 | |
| 80 | expect(result.userEdits).toBe(ctx.editDiffHistory); |
| 81 | expect(result.userExcerpts).toContain(MERCURY_CURSOR); |
| 82 | }); |
| 83 | |
| 84 | it("should render instinct prompt correctly", async () => { |
| 85 | // Create proper instinct context structure matching NextEditProvider.ts |
| 86 | const instinctCtx = { |
| 87 | contextSnippets: |
| 88 | "+++++ /path/to/context.ts\nconst contextVar = 'test';", |
| 89 | currentFileContent: instinctHelper.fileContents, |
| 90 | windowStart: 0, // Math.max(0, helper.pos.line - 25) |
| 91 | windowEnd: 3, // Math.min(helper.fileLines.length - 1, helper.pos.line + 25) |
| 92 | editableRegionStartLine: 0, // Math.max(0, helper.pos.line - 1) within window |
| 93 | editableRegionEndLine: 2, // Math.min(helper.pos.line + 5) within window |
| 94 | editDiffHistory: [ |
| 95 | "diff --git a/file.ts b/file.ts\nindex 123..456 789\n--- a/file.ts\n+++ b/file.ts\n@@ -1,3 +1,4 @@\n function test() {\n+ const a = 1;\n return a;\n }", |
| 96 | ], // unified diff |
| 97 | currentFilePath: "/path/to/file.ts", |
| 98 | languageShorthand: "ts", |
| 99 | }; |
| 100 | |
| 101 | const result = await renderPrompt(instinctHelper, instinctCtx); |
| 102 | console.log(result); |
| 103 | |
| 104 | expect(result).toHaveProperty("prompt"); |
| 105 | expect(result.prompt.role).toBe("user"); |
| 106 | expect(result.prompt.content).toContain(INSTINCT_USER_PROMPT_PREFIX); |
| 107 | expect(result.prompt.content).toContain("### Context:"); |
| 108 | expect(result.prompt.content).toContain("### User Edits:"); |
| 109 | expect(result.prompt.content).toContain("### User Excerpt:"); |
| 110 | |
| 111 | expect(result.userEdits).toBe(instinctCtx.editDiffHistory); |
| 112 | expect(result.userExcerpts).toContain(INSTINCT_USER_CURSOR_IS_HERE_TOKEN); |
| 113 | }); |
| 114 | |
| 115 | it("should throw error for unsupported model name", async () => { |
| 116 | await expect(renderPrompt(unsupportedHelper, ctx)).rejects.toThrow( |
| 117 | "mistral/codestral is not yet supported for next edit.", |
| 118 | ); |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | describe("renderDefaultSystemPrompt", () => { |
| 123 | it("should return a system prompt with expected role and content", () => { |
| 124 | const result = renderDefaultSystemPrompt(); |
| 125 | |
| 126 | expect(result.role).toBe("system"); |
| 127 | expect(result.content).toContain("You are an expert polyglot developer"); |
| 128 | expect(result.content).toContain("Do not hallucinate."); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe("renderDefaultUserPrompt", () => { |
| 133 | it("should render a user prompt with the correct structure", () => { |
| 134 | const snippets: SnippetPayload = { |
| 135 | rootPathSnippets: [ |
| 136 | { |
| 137 | type: AutocompleteSnippetType.Code, |
| 138 | filepath: "", |
| 139 | content: "root path", |
| 140 | }, |
| 141 | ], |
| 142 | importDefinitionSnippets: [ |
| 143 | { |
| 144 | type: AutocompleteSnippetType.Code, |
| 145 | filepath: "", |
| 146 | content: "import def", |
| 147 | }, |
| 148 | ], |
| 149 | ideSnippets: [ |
| 150 | { |
| 151 | type: AutocompleteSnippetType.Code, |
| 152 | filepath: "", |
| 153 | content: "ide snippets", |
| 154 | }, |
| 155 | ], |
| 156 | recentlyEditedRangeSnippets: [ |
| 157 | { |
| 158 | type: AutocompleteSnippetType.Code, |
| 159 | filepath: "", |
| 160 | content: "recent edits", |
| 161 | }, |
| 162 | ], |
| 163 | diffSnippets: [ |
| 164 | { |
| 165 | type: AutocompleteSnippetType.Diff, |
| 166 | content: "diff snippets", |
| 167 | }, |
| 168 | ], |
| 169 | clipboardSnippets: [ |
| 170 | { |
| 171 | type: AutocompleteSnippetType.Clipboard, |
| 172 | content: "clipboard", |
| 173 | copiedAt: new Date().toISOString(), |
| 174 | }, |
| 175 | ], |
| 176 | recentlyVisitedRangesSnippets: [ |
| 177 | { |
| 178 | type: AutocompleteSnippetType.Code, |
| 179 | filepath: "", |
| 180 | content: "visited ranges", |
| 181 | }, |
| 182 | ], |
| 183 | recentlyOpenedFileSnippets: [ |
| 184 | { |
| 185 | type: AutocompleteSnippetType.Code, |
| 186 | filepath: "", |
| 187 | content: "recently opened files", |
| 188 | }, |
| 189 | ], |
| 190 | staticSnippet: [ |
| 191 | { |
| 192 | type: AutocompleteSnippetType.Static, |
| 193 | filepath: "", |
| 194 | content: "static content", |
| 195 | }, |
| 196 | ], |
| 197 | }; |
| 198 | |
| 199 | const helper: HelperVars = { |
| 200 | lang: { name: "typescript" }, |
| 201 | } as HelperVars; |
| 202 | |
| 203 | const result = renderDefaultUserPrompt(snippets, helper); |
| 204 | |
| 205 | expect(result.role).toBe("user"); |
| 206 | expect(result.content).toContain("Your junior made the following edit:"); |
| 207 | expect(result.content).toContain("typescript"); |
| 208 | expect(result.content).toContain("root path"); |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | describe("insertTokens", () => { |
| 213 | it("should correctly insert cursor and editable region tokens", () => { |
| 214 | const mercuryHelper = { |
| 215 | modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS, |
| 216 | fileContents: "function test() {\n const a = 1;\n return a;\n}", |
| 217 | pos: { line: 1, character: 12 } as Position, |
| 218 | lang: { name: "typescript" }, |
| 219 | } as HelperVars; |
| 220 | |
| 221 | const ctx = { |
| 222 | recentlyViewedCodeSnippets: [ |
| 223 | { filepath: "/path/to/file1.ts", content: "const b = 2;" }, |
| 224 | ], |
| 225 | currentFileContent: "function test() {\n const a = 1;\n return a;\n}", |
| 226 | editDiffHistory: ["diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@"], |
| 227 | editableRegionStartLine: 0, |
| 228 | editableRegionEndLine: 3, |
| 229 | userEdits: "Added constant a", |
| 230 | languageShorthand: "ts", |
| 231 | userExcerpts: "const a = 1;", |
| 232 | }; |
| 233 | |
| 234 | return renderPrompt(mercuryHelper, ctx).then((result) => { |
| 235 | expect(result.userExcerpts).toContain(MERCURY_CURSOR); |
| 236 | }); |
| 237 | }); |
| 238 | }); |
| 239 | }); |