| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | // ---------- Module mocks ---------- |
| 4 | |
| 5 | // Simple Handlebars mock that does naive placeholder substitution |
| 6 | vi.mock("handlebars", () => { |
| 7 | return { |
| 8 | default: { |
| 9 | compile: (template: string) => (ctx: Record<string, string>) => { |
| 10 | return template |
| 11 | .replace(/{{prefix}}/g, ctx.prefix) |
| 12 | .replace(/{{suffix}}/g, ctx.suffix) |
| 13 | .replace(/{{filename}}/g, ctx.filename ?? "") |
| 14 | .replace(/{{reponame}}/g, ctx.reponame ?? "") |
| 15 | .replace(/{{language}}/g, ctx.language ?? ""); |
| 16 | }, |
| 17 | }, |
| 18 | }; |
| 19 | }); |
| 20 | |
| 21 | // Token utilities – we map 1 char = 1 token for simplicity |
| 22 | vi.mock("../../../llm/countTokens", () => { |
| 23 | const countTokens = (str: string) => str.length; |
| 24 | const pruneLinesFromTop = (str: string, allowed: number) => |
| 25 | str.slice(Math.max(0, str.length - allowed)); |
| 26 | const pruneLinesFromBottom = (str: string, allowed: number) => |
| 27 | str.slice(0, allowed); |
| 28 | const getTokenCountingBufferSafety = () => 0; |
| 29 | |
| 30 | return { |
| 31 | countTokens, |
| 32 | pruneLinesFromTop, |
| 33 | pruneLinesFromBottom, |
| 34 | getTokenCountingBufferSafety, |
| 35 | }; |
| 36 | }); |
| 37 | |
| 38 | // Snippet selection – configurable via constant return value |
| 39 | vi.mock("../filtering", () => ({ |
| 40 | getSnippets: () => [], |
| 41 | })); |
| 42 | |
| 43 | // Snippet formatting |
| 44 | const FORMATTED_SNIPPETS = "[FORMATTED_SNIPPETS]"; |
| 45 | vi.mock("../formatting", () => ({ |
| 46 | formatSnippets: () => FORMATTED_SNIPPETS, |
| 47 | })); |
| 48 | |
| 49 | // Stop tokens helper – we expose a variable so each test can override it |
| 50 | let stopTokenReturn: string[] = ["<STOP>"]; |
| 51 | vi.mock("../getStopTokens", () => ({ |
| 52 | getStopTokens: () => stopTokenReturn, |
| 53 | })); |
| 54 | |
| 55 | // AutocompleteTemplate – provide overridable template + compiler + completionOptions |
| 56 | let templateOverride: any = "{{prefix}}|{{suffix}}"; |
| 57 | let compileFnOverride: ((...args: any[]) => [string, string]) | undefined; |
| 58 | let completionOptionsOverride: Record<string, any> | undefined; |
| 59 | vi.mock("../AutocompleteTemplate", () => ({ |
| 60 | getTemplateForModel: () => ({ |
| 61 | template: templateOverride, |
| 62 | compilePrefixSuffix: compileFnOverride, |
| 63 | completionOptions: completionOptionsOverride ?? {}, |
| 64 | }), |
| 65 | })); |
| 66 | |
| 67 | // ---------- Imports after mocks ---------- |
| 68 | import { renderPrompt, renderPromptWithTokenLimit } from ".."; |
| 69 | import { AutocompleteLanguageInfo } from "../../constants/AutocompleteLanguageInfo"; |
| 70 | import { SnippetPayload } from "../../snippets"; |
| 71 | import { HelperVars } from "../../util/HelperVars"; |
| 72 | |
| 73 | // ---------- Helper builders ---------- |
| 74 | |
| 75 | const tsLang: AutocompleteLanguageInfo = { |
| 76 | name: "TypeScript", |
| 77 | topLevelKeywords: [], |
| 78 | singleLineComment: "//", |
| 79 | endOfLine: [";"], |
| 80 | }; |
| 81 | |
| 82 | const emptySnippetPayload: SnippetPayload = { |
| 83 | rootPathSnippets: [], |
| 84 | importDefinitionSnippets: [], |
| 85 | ideSnippets: [], |
| 86 | recentlyEditedRangeSnippets: [], |
| 87 | recentlyVisitedRangesSnippets: [], |
| 88 | diffSnippets: [], |
| 89 | clipboardSnippets: [], |
| 90 | recentlyOpenedFileSnippets: [], |
| 91 | staticSnippet: [], |
| 92 | }; |
| 93 | |
| 94 | function makeHelper(overrides: any = {}) { |
| 95 | return { |
| 96 | input: { |
| 97 | filepath: "file:///test.ts", |
| 98 | pos: { line: 0, character: 0 }, |
| 99 | recentlyEditedRanges: [], |
| 100 | recentlyVisitedRanges: [], |
| 101 | ...overrides.input, |
| 102 | }, |
| 103 | prunedPrefix: "PRUNED_PREFIX", |
| 104 | prunedSuffix: "PRUNED_SUFFIX", |
| 105 | lang: tsLang, |
| 106 | modelName: "test-model", |
| 107 | filepath: "file:///test.ts", |
| 108 | workspaceUris: [], |
| 109 | options: { |
| 110 | // Default template can be overridden per test |
| 111 | template: "{{prefix}}|{{suffix}}", |
| 112 | maxPromptTokens: 2048, |
| 113 | prefixPercentage: 0.5, |
| 114 | maxSuffixPercentage: 0.5, |
| 115 | experimental_includeClipboard: false, |
| 116 | useRecentlyOpened: false, |
| 117 | experimental_includeRecentlyVisitedRanges: false, |
| 118 | experimental_includeRecentlyEditedRanges: false, |
| 119 | experimental_includeDiff: false, |
| 120 | onlyMyCode: false, |
| 121 | ...overrides.options, |
| 122 | }, |
| 123 | ...overrides, |
| 124 | } as unknown as HelperVars; |
| 125 | } |
| 126 | |
| 127 | afterEach(() => { |
| 128 | // reset overridable mocks |
| 129 | templateOverride = "{{prefix}}|{{suffix}}"; |
| 130 | compileFnOverride = undefined; |
| 131 | completionOptionsOverride = undefined; |
| 132 | stopTokenReturn = ["<STOP>"]; |
| 133 | vi.restoreAllMocks(); |
| 134 | }); |
| 135 | |
| 136 | // ---------- Test suite ---------- |
| 137 | |
| 138 | describe("renderPrompt prefix/suffix selection", () => { |
| 139 | it("uses manuallyPassPrefix when provided", () => { |
| 140 | const helper = makeHelper({ |
| 141 | input: { manuallyPassPrefix: "MANUAL" }, |
| 142 | }); |
| 143 | |
| 144 | const { prefix, suffix } = renderPrompt({ |
| 145 | snippetPayload: emptySnippetPayload, |
| 146 | workspaceDirs: ["file:///workspace"], |
| 147 | helper, |
| 148 | }); |
| 149 | |
| 150 | expect(suffix).toBe("\n"); |
| 151 | expect(prefix.endsWith("MANUAL")).toBe(true); |
| 152 | }); |
| 153 | |
| 154 | it("falls back to prunedPrefix when no manual prefix", () => { |
| 155 | const helper = makeHelper({}); |
| 156 | |
| 157 | const { prefix } = renderPrompt({ |
| 158 | snippetPayload: emptySnippetPayload, |
| 159 | workspaceDirs: ["file:///workspace"], |
| 160 | helper, |
| 161 | }); |
| 162 | |
| 163 | expect(prefix.includes("PRUNED_PREFIX")).toBe(true); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | describe("template rendering paths", () => { |
| 168 | it("handles function template", () => { |
| 169 | templateOverride = ( |
| 170 | p: string, |
| 171 | s: string, |
| 172 | _filepath: string, |
| 173 | _reponame: string, |
| 174 | ) => `FUNC:${p}|${s}`; |
| 175 | |
| 176 | const helper = makeHelper({ options: { template: undefined } }); |
| 177 | |
| 178 | const { prompt } = renderPrompt({ |
| 179 | snippetPayload: emptySnippetPayload, |
| 180 | workspaceDirs: ["file:///workspace"], |
| 181 | helper, |
| 182 | }); |
| 183 | |
| 184 | expect(prompt.startsWith("FUNC:")).toBe(true); |
| 185 | expect(prompt.includes("PRUNED_PREFIX")).toBe(true); |
| 186 | }); |
| 187 | }); |
| 188 | |
| 189 | describe("compilePrefixSuffix vs snippet formatting", () => { |
| 190 | it("applies compilePrefixSuffix when provided", () => { |
| 191 | compileFnOverride = (p: string, s: string) => [`COMP_${p}`, `COMP_${s}`]; |
| 192 | templateOverride = "{{prefix}}|{{suffix}}"; |
| 193 | const helper = makeHelper({ options: { template: undefined } }); |
| 194 | |
| 195 | const { prefix: compiledPrefix } = renderPrompt({ |
| 196 | snippetPayload: emptySnippetPayload, |
| 197 | workspaceDirs: ["file:///workspace"], |
| 198 | helper, |
| 199 | }); |
| 200 | |
| 201 | expect(compiledPrefix.startsWith("COMP_PRUNED_PREFIX")).toBe(true); |
| 202 | }); |
| 203 | |
| 204 | it("prepends formatted snippets when no compiler present", () => { |
| 205 | const helper = makeHelper({}); |
| 206 | |
| 207 | const { prefix: compiledPrefix } = renderPrompt({ |
| 208 | snippetPayload: emptySnippetPayload, |
| 209 | workspaceDirs: ["file:///workspace"], |
| 210 | helper, |
| 211 | }); |
| 212 | |
| 213 | expect(compiledPrefix.startsWith(`${FORMATTED_SNIPPETS}\n`)).toBe(true); |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | describe("renderPromptWithTokenLimit parity & pruning", () => { |
| 218 | it("matches renderPrompt when llm is undefined", () => { |
| 219 | const helper = makeHelper({}); |
| 220 | |
| 221 | const res1 = renderPrompt({ |
| 222 | snippetPayload: emptySnippetPayload, |
| 223 | workspaceDirs: ["file:///workspace"], |
| 224 | helper, |
| 225 | }); |
| 226 | |
| 227 | const res2 = renderPromptWithTokenLimit({ |
| 228 | snippetPayload: emptySnippetPayload, |
| 229 | workspaceDirs: ["file:///workspace"], |
| 230 | helper, |
| 231 | llm: undefined, |
| 232 | }); |
| 233 | |
| 234 | expect(res2).toEqual(res1); |
| 235 | }); |
| 236 | |
| 237 | it("prunes prefix/suffix to respect small context length", () => { |
| 238 | const longPrefix = "A".repeat(300); |
| 239 | |
| 240 | const helper = makeHelper({ prunedPrefix: longPrefix }); |
| 241 | |
| 242 | const llmStub = { |
| 243 | contextLength: 120, |
| 244 | completionOptions: { maxTokens: 10 }, |
| 245 | model: "test-model", |
| 246 | } as any; |
| 247 | |
| 248 | const { prefix: compiledPrefix } = renderPromptWithTokenLimit({ |
| 249 | snippetPayload: emptySnippetPayload, |
| 250 | workspaceDirs: ["file:///workspace"], |
| 251 | helper, |
| 252 | llm: llmStub, |
| 253 | }); |
| 254 | |
| 255 | expect(compiledPrefix.length).toBeLessThan(120); |
| 256 | }); |
| 257 | }); |
| 258 | |
| 259 | describe("stop-token merging", () => { |
| 260 | it("returns stop tokens from getStopTokens", () => { |
| 261 | stopTokenReturn = ["LANG_STOP", "TEMPLATE_STOP"]; |
| 262 | completionOptionsOverride = { stop: ["TEMPLATE_STOP"] }; |
| 263 | templateOverride = "{{prefix}}|{{suffix}}"; |
| 264 | |
| 265 | const helper = makeHelper({ options: { template: undefined } }); |
| 266 | |
| 267 | const { completionOptions } = renderPrompt({ |
| 268 | snippetPayload: emptySnippetPayload, |
| 269 | workspaceDirs: ["file:///workspace"], |
| 270 | helper, |
| 271 | }); |
| 272 | |
| 273 | expect(completionOptions?.stop).toEqual(stopTokenReturn); |
| 274 | }); |
| 275 | }); |