| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { NEXT_EDIT_MODELS } from "../../llm/constants"; |
| 3 | import { |
| 4 | INSTINCT_USER_PROMPT_PREFIX, |
| 5 | MERCURY_CURRENT_FILE_CONTENT_CLOSE, |
| 6 | MERCURY_CURRENT_FILE_CONTENT_OPEN, |
| 7 | MERCURY_EDIT_DIFF_HISTORY_CLOSE, |
| 8 | MERCURY_EDIT_DIFF_HISTORY_OPEN, |
| 9 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE, |
| 10 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN, |
| 11 | } from "../constants"; |
| 12 | import { |
| 13 | NEXT_EDIT_MODEL_TEMPLATES, |
| 14 | PromptTemplateRenderer, |
| 15 | getTemplateForModel, |
| 16 | } from "./NextEditPromptEngine"; |
| 17 | |
| 18 | describe("NextEditPromptEngine", () => { |
| 19 | describe("NEXT_EDIT_MODEL_TEMPLATES", () => { |
| 20 | it("should contain templates for all supported models", () => { |
| 21 | expect(NEXT_EDIT_MODEL_TEMPLATES).toHaveProperty( |
| 22 | NEXT_EDIT_MODELS.MERCURY_CODER, |
| 23 | ); |
| 24 | expect(NEXT_EDIT_MODEL_TEMPLATES).toHaveProperty( |
| 25 | NEXT_EDIT_MODELS.INSTINCT, |
| 26 | ); |
| 27 | }); |
| 28 | |
| 29 | it("mercury-coder template should contain expected tokens", () => { |
| 30 | const template = |
| 31 | NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.MERCURY_CODER].template; |
| 32 | |
| 33 | expect(template).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN); |
| 34 | expect(template).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE); |
| 35 | expect(template).toContain(MERCURY_CURRENT_FILE_CONTENT_OPEN); |
| 36 | expect(template).toContain(MERCURY_CURRENT_FILE_CONTENT_CLOSE); |
| 37 | expect(template).toContain(MERCURY_EDIT_DIFF_HISTORY_OPEN); |
| 38 | expect(template).toContain(MERCURY_EDIT_DIFF_HISTORY_CLOSE); |
| 39 | expect(template).toContain("{{{recentlyViewedCodeSnippets}}}"); |
| 40 | expect(template).toContain("{{{currentFileContent}}}"); |
| 41 | expect(template).toContain("{{{editDiffHistory}}}"); |
| 42 | }); |
| 43 | |
| 44 | it("instinct template should contain expected tokens", () => { |
| 45 | const template = |
| 46 | NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.INSTINCT].template; |
| 47 | |
| 48 | expect(template).toContain(INSTINCT_USER_PROMPT_PREFIX); |
| 49 | expect(template).toContain("### Context:"); |
| 50 | expect(template).toContain("### User Edits:"); |
| 51 | expect(template).toContain("### User Excerpt:"); |
| 52 | expect(template).toContain("{{{contextSnippets}}}"); |
| 53 | expect(template).toContain("{{{currentFileContent}}}"); |
| 54 | expect(template).toContain("{{{editDiffHistory}}}"); |
| 55 | expect(template).toContain("{{{currentFilePath}}}"); |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | describe("PromptTemplateRenderer", () => { |
| 60 | it("should render a template with provided variables", () => { |
| 61 | const template = "Hello {{{name}}}, you have {{{count}}} messages"; |
| 62 | const renderer = new PromptTemplateRenderer(template); |
| 63 | |
| 64 | const result = renderer.render({ |
| 65 | name: "Alice", |
| 66 | count: "5", |
| 67 | }); |
| 68 | |
| 69 | expect(result).toBe("Hello Alice, you have 5 messages"); |
| 70 | }); |
| 71 | |
| 72 | it("should handle missing variables gracefully", () => { |
| 73 | const template = "Hello {{{name}}}"; |
| 74 | const renderer = new PromptTemplateRenderer(template); |
| 75 | |
| 76 | const result = renderer.render({}); |
| 77 | |
| 78 | expect(result).toBe("Hello "); |
| 79 | }); |
| 80 | |
| 81 | it("should render complex templates correctly", () => { |
| 82 | const template = |
| 83 | NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.MERCURY_CODER].template; |
| 84 | const renderer = new PromptTemplateRenderer(template); |
| 85 | |
| 86 | const result = renderer.render({ |
| 87 | recentlyViewedCodeSnippets: "snippet1", |
| 88 | currentFileContent: "file content", |
| 89 | editDiffHistory: "diff history", |
| 90 | }); |
| 91 | |
| 92 | expect(result).toContain("snippet1"); |
| 93 | expect(result).toContain("file content"); |
| 94 | expect(result).toContain("diff history"); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | describe("getTemplateForModel", () => { |
| 99 | it("should return the correct template for a supported model", () => { |
| 100 | const template = getTemplateForModel(NEXT_EDIT_MODELS.MERCURY_CODER); |
| 101 | expect(template).toBe( |
| 102 | NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.MERCURY_CODER].template, |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | it("should throw an error for an unsupported model", () => { |
| 107 | expect(() => |
| 108 | getTemplateForModel("unsupported" as NEXT_EDIT_MODELS), |
| 109 | ).toThrow("Model unsupported is not supported for next edit."); |
| 110 | }); |
| 111 | }); |
| 112 | }); |