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