| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | import { ContinueCompletionProvider } from "../completionProvider"; |
| 6 | |
| 7 | import * as NextEditLoggingServiceModule from "core/nextEdit/NextEditLoggingService"; |
| 8 | import * as PrefetchQueueModule from "core/nextEdit/NextEditPrefetchQueue"; |
| 9 | import * as NextEditProviderModule from "core/nextEdit/NextEditProvider"; |
| 10 | import * as JumpManagerModule from "../../activation/JumpManager"; |
| 11 | |
| 12 | type MockNextEditProvider = ReturnType<typeof createMockNextEditProvider>; |
| 13 | type MockPrefetchQueue = ReturnType<typeof createMockPrefetchQueue>; |
| 14 | type MockJumpManager = ReturnType<typeof createMockJumpManager>; |
| 15 | |
| 16 | const mockOutcome = { |
| 17 | completion: "suggested change", |
| 18 | diffLines: [], |
| 19 | editableRegionStartLine: 0, |
| 20 | editableRegionEndLine: 0, |
| 21 | } as any; |
| 22 | |
| 23 | let mockNextEditProvider: MockNextEditProvider; |
| 24 | let mockPrefetchQueue: MockPrefetchQueue; |
| 25 | let mockJumpManager: MockJumpManager; |
| 26 | |
| 27 | beforeEach(() => { |
| 28 | vi.clearAllMocks(); |
| 29 | |
| 30 | mockNextEditProvider = createMockNextEditProvider(); |
| 31 | (NextEditProviderModule as any).__setMockNextEditProviderInstance( |
| 32 | mockNextEditProvider, |
| 33 | ); |
| 34 | |
| 35 | mockPrefetchQueue = createMockPrefetchQueue(); |
| 36 | (PrefetchQueueModule as any).__setMockPrefetchQueueInstance( |
| 37 | mockPrefetchQueue, |
| 38 | ); |
| 39 | |
| 40 | mockJumpManager = createMockJumpManager(); |
| 41 | (JumpManagerModule as any).__setMockJumpManagerInstance(mockJumpManager); |
| 42 | |
| 43 | const mockLoggingService = createMockLoggingService(); |
| 44 | (NextEditLoggingServiceModule as any).__setMockNextEditLoggingServiceInstance( |
| 45 | mockLoggingService, |
| 46 | ); |
| 47 | |
| 48 | (vscode.window as any).activeTextEditor = null; |
| 49 | }); |
| 50 | |
| 51 | describe("ContinueCompletionProvider triggering logic", () => { |
| 52 | it("starts a new chain when none exists", async () => { |
| 53 | const document = createDocument(); |
| 54 | setActiveEditor(document); |
| 55 | |
| 56 | const provider = buildProvider(); |
| 57 | |
| 58 | await provider.provideInlineCompletionItems( |
| 59 | document, |
| 60 | createPosition(), |
| 61 | createContext(), |
| 62 | createToken(), |
| 63 | ); |
| 64 | |
| 65 | expect(mockNextEditProvider.startChain).toHaveBeenCalledTimes(1); |
| 66 | expect( |
| 67 | mockNextEditProvider.provideInlineCompletionItems, |
| 68 | ).toHaveBeenCalledTimes(1); |
| 69 | expect(mockNextEditProvider.deleteChain).not.toHaveBeenCalled(); |
| 70 | }); |
| 71 | |
| 72 | it("clears an empty chain once in full file diff mode", async () => { |
| 73 | const document = createDocument(); |
| 74 | setActiveEditor(document); |
| 75 | |
| 76 | mockNextEditProvider.chainExists.mockReturnValue(true); |
| 77 | mockPrefetchQueue.__setProcessed([]); |
| 78 | mockPrefetchQueue.__setUnprocessed([]); |
| 79 | |
| 80 | const provider = buildProvider(); |
| 81 | |
| 82 | await provider.provideInlineCompletionItems( |
| 83 | document, |
| 84 | createPosition(), |
| 85 | createContext(), |
| 86 | createToken(), |
| 87 | ); |
| 88 | |
| 89 | expect(mockNextEditProvider.deleteChain).toHaveBeenCalledTimes(1); |
| 90 | expect(mockNextEditProvider.startChain).toHaveBeenCalledTimes(1); |
| 91 | expect( |
| 92 | mockNextEditProvider.provideInlineCompletionItems, |
| 93 | ).toHaveBeenCalledTimes(1); |
| 94 | }); |
| 95 | |
| 96 | it("returns null after clearing empty chain when no outcome is available", async () => { |
| 97 | const document = createDocument(); |
| 98 | setActiveEditor(document); |
| 99 | |
| 100 | mockNextEditProvider.chainExists.mockReturnValue(true); |
| 101 | mockPrefetchQueue.__setProcessed([]); |
| 102 | mockPrefetchQueue.__setUnprocessed([]); |
| 103 | mockNextEditProvider.provideInlineCompletionItems.mockResolvedValueOnce( |
| 104 | undefined, |
| 105 | ); |
| 106 | |
| 107 | const provider = buildProvider(); |
| 108 | |
| 109 | const result = await provider.provideInlineCompletionItems( |
| 110 | document, |
| 111 | createPosition(), |
| 112 | createContext(), |
| 113 | createToken(), |
| 114 | ); |
| 115 | |
| 116 | expect(result).toBeNull(); |
| 117 | expect(mockNextEditProvider.deleteChain).toHaveBeenCalledTimes(1); |
| 118 | expect(mockNextEditProvider.startChain).toHaveBeenCalledTimes(1); |
| 119 | expect( |
| 120 | mockNextEditProvider.provideInlineCompletionItems, |
| 121 | ).toHaveBeenCalledTimes(1); |
| 122 | }); |
| 123 | |
| 124 | it("uses queued outcomes when processed items exist", async () => { |
| 125 | const document = createDocument(); |
| 126 | setActiveEditor(document); |
| 127 | |
| 128 | mockNextEditProvider.chainExists.mockReturnValue(true); |
| 129 | mockPrefetchQueue.__setProcessed([ |
| 130 | { |
| 131 | location: { |
| 132 | range: { |
| 133 | start: { line: 0, character: 0 }, |
| 134 | end: { line: 0, character: 0 }, |
| 135 | }, |
| 136 | }, |
| 137 | outcome: mockOutcome, |
| 138 | }, |
| 139 | ]); |
| 140 | mockJumpManager.suggestJump.mockResolvedValue(true); |
| 141 | |
| 142 | const provider = buildProvider(); |
| 143 | |
| 144 | await provider.provideInlineCompletionItems( |
| 145 | document, |
| 146 | createPosition(), |
| 147 | createContext(), |
| 148 | createToken(), |
| 149 | ); |
| 150 | |
| 151 | expect(mockPrefetchQueue.dequeueProcessed).toHaveBeenCalledTimes(1); |
| 152 | expect(mockJumpManager.setCompletionAfterJump).toHaveBeenCalledTimes(1); |
| 153 | expect(mockNextEditProvider.startChain).not.toHaveBeenCalled(); |
| 154 | expect(mockNextEditProvider.deleteChain).not.toHaveBeenCalled(); |
| 155 | expect( |
| 156 | mockNextEditProvider.provideInlineCompletionItems, |
| 157 | ).not.toHaveBeenCalled(); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | function buildProvider(options: { usingFullFileDiff?: boolean } = {}) { |
| 162 | const usingFullFileDiff = options.usingFullFileDiff ?? true; |
| 163 | const configHandler = { |
| 164 | loadConfig: vi.fn(async () => ({ |
| 165 | config: { selectedModelByRole: { autocomplete: undefined } }, |
| 166 | })), |
| 167 | } as any; |
| 168 | |
| 169 | const ide = { ideUtils: {} } as any; |
| 170 | const webviewProtocol = {} as any; |
| 171 | |
| 172 | const provider = new ContinueCompletionProvider( |
| 173 | configHandler, |
| 174 | ide, |
| 175 | webviewProtocol, |
| 176 | usingFullFileDiff, |
| 177 | ); |
| 178 | provider.activateNextEdit(); |
| 179 | return provider; |
| 180 | } |
| 181 | |
| 182 | function createDocument( |
| 183 | text = "function example() {\n return true;\n}", |
| 184 | ): vscode.TextDocument { |
| 185 | const lines = text.split("\n"); |
| 186 | return { |
| 187 | uri: vscode.Uri.parse("file:///test"), |
| 188 | isUntitled: false, |
| 189 | getText: (range?: any) => { |
| 190 | if (!range) { |
| 191 | return text; |
| 192 | } |
| 193 | const startLine = range.start?.line ?? 0; |
| 194 | const endLine = range.end?.line ?? startLine; |
| 195 | const startChar = range.start?.character ?? 0; |
| 196 | const endChar = range.end?.character ?? lines[endLine]?.length ?? 0; |
| 197 | if (startLine === endLine) { |
| 198 | const lineText = lines[startLine] ?? ""; |
| 199 | return lineText.slice(startChar, endChar); |
| 200 | } |
| 201 | return text; |
| 202 | }, |
| 203 | lineAt: (position: any) => { |
| 204 | const lineNumber = |
| 205 | typeof position === "number" ? position : position.line; |
| 206 | const lineText = lines[lineNumber] ?? ""; |
| 207 | const range = new (vscode.Range as any)( |
| 208 | new (vscode.Position as any)(lineNumber, 0), |
| 209 | new (vscode.Position as any)(lineNumber, lineText.length), |
| 210 | ); |
| 211 | return { |
| 212 | lineNumber, |
| 213 | text: lineText, |
| 214 | range, |
| 215 | rangeIncludingLineBreak: range, |
| 216 | firstNonWhitespaceCharacterIndex: 0, |
| 217 | isEmptyOrWhitespace: lineText.trim().length === 0, |
| 218 | } as unknown as vscode.TextLine; |
| 219 | }, |
| 220 | } as unknown as vscode.TextDocument; |
| 221 | } |
| 222 | |
| 223 | function createContext(): any { |
| 224 | return { |
| 225 | triggerKind: (vscode.InlineCompletionTriggerKind as any).Automatic, |
| 226 | selectedCompletionInfo: undefined, |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | function createPosition(line = 0, character = 0) { |
| 231 | return new (vscode.Position as any)(line, character); |
| 232 | } |
| 233 | |
| 234 | function createToken(): any { |
| 235 | return { |
| 236 | isCancellationRequested: false, |
| 237 | onCancellationRequested: vi.fn(), |
| 238 | }; |
| 239 | } |
| 240 | |
| 241 | function setActiveEditor(document: any, cursor = createPosition()) { |
| 242 | const selection = { active: cursor, anchor: cursor }; |
| 243 | (vscode.window as any).activeTextEditor = { |
| 244 | document, |
| 245 | selection, |
| 246 | selections: [selection], |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | function createMockNextEditProvider() { |
| 251 | return { |
| 252 | chainExists: vi.fn(() => false), |
| 253 | startChain: vi.fn(), |
| 254 | deleteChain: vi.fn(async () => {}), |
| 255 | provideInlineCompletionItems: vi.fn(async () => mockOutcome), |
| 256 | provideInlineCompletionItemsWithChain: vi.fn(async () => mockOutcome), |
| 257 | markDisplayed: vi.fn(), |
| 258 | getChainLength: vi.fn(() => 0), |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | function createMockPrefetchQueue() { |
| 263 | let processedItems: any[] = []; |
| 264 | let unprocessedItems: any[] = []; |
| 265 | |
| 266 | const queue: any = { |
| 267 | initialize: vi.fn(), |
| 268 | process: vi.fn(), |
| 269 | peekThreeProcessed: vi.fn(), |
| 270 | dequeueProcessed: vi.fn(() => processedItems.shift()), |
| 271 | enqueueProcessed: vi.fn((item: any) => { |
| 272 | processedItems.push(item); |
| 273 | }), |
| 274 | __setProcessed(items: any[]) { |
| 275 | processedItems = [...items]; |
| 276 | }, |
| 277 | __setUnprocessed(items: any[]) { |
| 278 | unprocessedItems = [...items]; |
| 279 | }, |
| 280 | }; |
| 281 | |
| 282 | Object.defineProperty(queue, "processedCount", { |
| 283 | get: () => processedItems.length, |
| 284 | }); |
| 285 | |
| 286 | Object.defineProperty(queue, "unprocessedCount", { |
| 287 | get: () => unprocessedItems.length, |
| 288 | }); |
| 289 | |
| 290 | return queue; |
| 291 | } |
| 292 | |
| 293 | function createMockJumpManager() { |
| 294 | return { |
| 295 | isJumpInProgress: vi.fn(() => false), |
| 296 | setJumpInProgress: vi.fn(), |
| 297 | completionAfterJump: undefined, |
| 298 | clearCompletionAfterJump: vi.fn(), |
| 299 | setCompletionAfterJump: vi.fn(), |
| 300 | suggestJump: vi.fn(async () => false), |
| 301 | wasJumpJustAccepted: vi.fn(() => false), |
| 302 | }; |
| 303 | } |
| 304 | |
| 305 | function createMockLoggingService() { |
| 306 | return { |
| 307 | trackPendingCompletion: vi.fn(), |
| 308 | handleAbort: vi.fn(), |
| 309 | markDisplayed: vi.fn(), |
| 310 | cancelRejectionTimeout: vi.fn(), |
| 311 | deleteAbortController: vi.fn(), |
| 312 | cancel: vi.fn(), |
| 313 | }; |
| 314 | } |
| 315 | |
| 316 | vi.mock("vscode", () => { |
| 317 | class Position { |
| 318 | constructor( |
| 319 | public line: number, |
| 320 | public character: number, |
| 321 | ) {} |
| 322 | } |
| 323 | |
| 324 | class Range { |
| 325 | constructor( |
| 326 | public start: Position, |
| 327 | public end: Position, |
| 328 | ) {} |
| 329 | } |
| 330 | |
| 331 | class InlineCompletionItem { |
| 332 | public insertText: string; |
| 333 | public range: Range; |
| 334 | public command?: any; |
| 335 | |
| 336 | constructor(insertText: string, range: Range, command?: any) { |
| 337 | this.insertText = insertText; |
| 338 | this.range = range; |
| 339 | this.command = command; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | const window = { |
| 344 | activeTextEditor: null as any, |
| 345 | showErrorMessage: vi.fn(() => Promise.resolve(undefined)), |
| 346 | }; |
| 347 | |
| 348 | const workspace = { |
| 349 | notebookDocuments: [] as any[], |
| 350 | getConfiguration: vi.fn(() => ({ get: vi.fn() })), |
| 351 | }; |
| 352 | |
| 353 | return { |
| 354 | window, |
| 355 | workspace, |
| 356 | Uri: { parse: (value: string) => ({ toString: () => value }) }, |
| 357 | Position, |
| 358 | Range, |
| 359 | InlineCompletionItem, |
| 360 | InlineCompletionTriggerKind: { Automatic: 0, Invoke: 1 }, |
| 361 | NotebookCellKind: { Markup: 1 }, |
| 362 | }; |
| 363 | }); |
| 364 | |
| 365 | vi.mock("core/autocomplete/CompletionProvider", () => { |
| 366 | return { |
| 367 | CompletionProvider: class { |
| 368 | provideInlineCompletionItems = vi.fn(); |
| 369 | markDisplayed = vi.fn(); |
| 370 | }, |
| 371 | }; |
| 372 | }); |
| 373 | |
| 374 | vi.mock("core/autocomplete/util/processSingleLineCompletion", () => ({ |
| 375 | processSingleLineCompletion: vi.fn((text: string) => ({ |
| 376 | completionText: text, |
| 377 | range: { start: 0, end: text.length }, |
| 378 | })), |
| 379 | })); |
| 380 | |
| 381 | vi.mock("../statusBar", () => { |
| 382 | const StatusBarStatus = { |
| 383 | Enabled: "enabled", |
| 384 | Disabled: "disabled", |
| 385 | } as const; |
| 386 | |
| 387 | return { |
| 388 | StatusBarStatus, |
| 389 | getStatusBarStatus: vi.fn(() => StatusBarStatus.Enabled), |
| 390 | setupStatusBar: vi.fn(), |
| 391 | stopStatusBarLoading: vi.fn(), |
| 392 | }; |
| 393 | }); |
| 394 | |
| 395 | vi.mock("../GhostTextAcceptanceTracker", () => { |
| 396 | const instance = { |
| 397 | setExpectedGhostTextAcceptance: vi.fn(), |
| 398 | }; |
| 399 | return { |
| 400 | GhostTextAcceptanceTracker: { |
| 401 | getInstance: () => instance, |
| 402 | }, |
| 403 | }; |
| 404 | }); |
| 405 | |
| 406 | vi.mock("../lsp", () => ({ |
| 407 | getDefinitionsFromLsp: vi.fn(), |
| 408 | })); |
| 409 | |
| 410 | vi.mock("../recentlyEdited", () => ({ |
| 411 | RecentlyEditedTracker: class { |
| 412 | async getRecentlyEditedRanges() { |
| 413 | return []; |
| 414 | } |
| 415 | }, |
| 416 | })); |
| 417 | |
| 418 | vi.mock("../RecentlyVisitedRangesService", () => ({ |
| 419 | RecentlyVisitedRangesService: class { |
| 420 | getSnippets() { |
| 421 | return []; |
| 422 | } |
| 423 | }, |
| 424 | })); |
| 425 | |
| 426 | vi.mock("../activation/NextEditWindowManager", () => ({ |
| 427 | NextEditWindowManager: { |
| 428 | isInstantiated: vi.fn(() => false), |
| 429 | getInstance: vi.fn(), |
| 430 | }, |
| 431 | })); |
| 432 | |
| 433 | vi.mock("../../activation/JumpManager", () => { |
| 434 | let instance: any = null; |
| 435 | return { |
| 436 | JumpManager: { |
| 437 | getInstance: () => instance, |
| 438 | }, |
| 439 | __setMockJumpManagerInstance(value: any) { |
| 440 | instance = value; |
| 441 | }, |
| 442 | }; |
| 443 | }); |
| 444 | |
| 445 | vi.mock("core/nextEdit/NextEditPrefetchQueue", () => { |
| 446 | let instance: any = null; |
| 447 | return { |
| 448 | PrefetchQueue: { |
| 449 | getInstance: () => instance, |
| 450 | }, |
| 451 | __setMockPrefetchQueueInstance(value: any) { |
| 452 | instance = value; |
| 453 | }, |
| 454 | }; |
| 455 | }); |
| 456 | |
| 457 | vi.mock("core/nextEdit/NextEditProvider", () => { |
| 458 | let instance: any = null; |
| 459 | return { |
| 460 | NextEditProvider: { |
| 461 | initialize: vi.fn(() => instance), |
| 462 | getInstance: vi.fn(() => instance), |
| 463 | }, |
| 464 | __setMockNextEditProviderInstance(value: any) { |
| 465 | instance = value; |
| 466 | }, |
| 467 | }; |
| 468 | }); |
| 469 | |
| 470 | vi.mock("core/nextEdit/NextEditLoggingService", () => { |
| 471 | let instance: any = null; |
| 472 | return { |
| 473 | NextEditLoggingService: { |
| 474 | getInstance: () => instance, |
| 475 | }, |
| 476 | __setMockNextEditLoggingServiceInstance(value: any) { |
| 477 | instance = value; |
| 478 | }, |
| 479 | }; |
| 480 | }); |
| 481 | |
| 482 | vi.mock("core/nextEdit/diff/diff", () => ({ |
| 483 | checkFim: vi.fn(() => ({ isFim: true, fimText: "ghost" })), |
| 484 | })); |
| 485 | |
| 486 | vi.mock("../util/errorHandling", () => ({ |
| 487 | handleLLMError: vi.fn(async () => false), |
| 488 | })); |