|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import assert from 'assert' |
| 8 | +import * as sinon from 'sinon' |
| 9 | +import { |
| 10 | + InlineCompletionService, |
| 11 | + ReferenceInlineProvider, |
| 12 | + RecommendationHandler, |
| 13 | + ConfigurationEntry, |
| 14 | + CWInlineCompletionItemProvider, |
| 15 | + session, |
| 16 | + DefaultCodeWhispererClient, |
| 17 | +} from 'aws-core-vscode/codewhisperer' |
| 18 | +import { createMockTextEditor, resetCodeWhispererGlobalVariables, createMockDocument } from 'aws-core-vscode/test' |
| 19 | + |
| 20 | +describe('inlineCompletionService', function () { |
| 21 | + beforeEach(async function () { |
| 22 | + await resetCodeWhispererGlobalVariables() |
| 23 | + }) |
| 24 | + |
| 25 | + describe('getPaginatedRecommendation', function () { |
| 26 | + const config: ConfigurationEntry = { |
| 27 | + isShowMethodsEnabled: true, |
| 28 | + isManualTriggerEnabled: true, |
| 29 | + isAutomatedTriggerEnabled: true, |
| 30 | + isSuggestionsWithCodeReferencesEnabled: true, |
| 31 | + } |
| 32 | + |
| 33 | + let mockClient: DefaultCodeWhispererClient |
| 34 | + |
| 35 | + beforeEach(async function () { |
| 36 | + mockClient = new DefaultCodeWhispererClient() |
| 37 | + await resetCodeWhispererGlobalVariables() |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(function () { |
| 41 | + sinon.restore() |
| 42 | + }) |
| 43 | + |
| 44 | + it('should call checkAndResetCancellationTokens before showing inline and next token to be null', async function () { |
| 45 | + const mockEditor = createMockTextEditor() |
| 46 | + sinon.stub(RecommendationHandler.instance, 'getRecommendations').resolves({ |
| 47 | + result: 'Succeeded', |
| 48 | + errorMessage: undefined, |
| 49 | + recommendationCount: 1, |
| 50 | + }) |
| 51 | + const checkAndResetCancellationTokensStub = sinon.stub( |
| 52 | + RecommendationHandler.instance, |
| 53 | + 'checkAndResetCancellationTokens' |
| 54 | + ) |
| 55 | + session.recommendations = [{ content: "\n\t\tconsole.log('Hello world!');\n\t}" }, { content: '' }] |
| 56 | + await InlineCompletionService.instance.getPaginatedRecommendation( |
| 57 | + mockClient, |
| 58 | + mockEditor, |
| 59 | + 'OnDemand', |
| 60 | + config |
| 61 | + ) |
| 62 | + assert.ok(checkAndResetCancellationTokensStub.called) |
| 63 | + assert.strictEqual(RecommendationHandler.instance.hasNextToken(), false) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe('clearInlineCompletionStates', function () { |
| 68 | + it('should remove inline reference and recommendations', async function () { |
| 69 | + const fakeReferences = [ |
| 70 | + { |
| 71 | + message: '', |
| 72 | + licenseName: 'MIT', |
| 73 | + repository: 'http://github.com/fake', |
| 74 | + recommendationContentSpan: { |
| 75 | + start: 0, |
| 76 | + end: 10, |
| 77 | + }, |
| 78 | + }, |
| 79 | + ] |
| 80 | + ReferenceInlineProvider.instance.setInlineReference(1, 'test', fakeReferences) |
| 81 | + session.recommendations = [{ content: "\n\t\tconsole.log('Hello world!');\n\t}" }, { content: '' }] |
| 82 | + session.language = 'python' |
| 83 | + |
| 84 | + assert.ok(session.recommendations.length > 0) |
| 85 | + await RecommendationHandler.instance.clearInlineCompletionStates() |
| 86 | + assert.strictEqual(ReferenceInlineProvider.instance.refs.length, 0) |
| 87 | + assert.strictEqual(session.recommendations.length, 0) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + describe('truncateOverlapWithRightContext', function () { |
| 92 | + const fileName = 'test.py' |
| 93 | + const language = 'python' |
| 94 | + const rightContext = 'return target\n' |
| 95 | + const doc = `import math\ndef two_sum(nums, target):\n` |
| 96 | + const provider = new CWInlineCompletionItemProvider(0, 0, [], '', new vscode.Position(0, 0), '') |
| 97 | + |
| 98 | + it('removes overlap with right context from suggestion', async function () { |
| 99 | + const mockSuggestion = 'return target\n' |
| 100 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 101 | + const cursorPosition = new vscode.Position(2, 0) |
| 102 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 103 | + assert.strictEqual(result, '') |
| 104 | + }) |
| 105 | + |
| 106 | + it('only removes the overlap part from suggestion', async function () { |
| 107 | + const mockSuggestion = 'print(nums)\nreturn target\n' |
| 108 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 109 | + const cursorPosition = new vscode.Position(2, 0) |
| 110 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 111 | + assert.strictEqual(result, 'print(nums)\n') |
| 112 | + }) |
| 113 | + |
| 114 | + it('only removes the last overlap pattern from suggestion', async function () { |
| 115 | + const mockSuggestion = 'return target\nprint(nums)\nreturn target\n' |
| 116 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 117 | + const cursorPosition = new vscode.Position(2, 0) |
| 118 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 119 | + assert.strictEqual(result, 'return target\nprint(nums)\n') |
| 120 | + }) |
| 121 | + |
| 122 | + it('returns empty string if the remaining suggestion only contains white space', async function () { |
| 123 | + const mockSuggestion = 'return target\n ' |
| 124 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 125 | + const cursorPosition = new vscode.Position(2, 0) |
| 126 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 127 | + assert.strictEqual(result, '') |
| 128 | + }) |
| 129 | + |
| 130 | + it('returns the original suggestion if no match found', async function () { |
| 131 | + const mockSuggestion = 'import numpy\n' |
| 132 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 133 | + const cursorPosition = new vscode.Position(2, 0) |
| 134 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 135 | + assert.strictEqual(result, 'import numpy\n') |
| 136 | + }) |
| 137 | + |
| 138 | + it('ignores the space at the end of recommendation', async function () { |
| 139 | + const mockSuggestion = 'return target\n\n\n\n\n' |
| 140 | + const mockEditor = createMockTextEditor(`${doc}${rightContext}`, fileName, language) |
| 141 | + const cursorPosition = new vscode.Position(2, 0) |
| 142 | + const result = provider.truncateOverlapWithRightContext(mockEditor.document, mockSuggestion, cursorPosition) |
| 143 | + assert.strictEqual(result, '') |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
| 147 | + |
| 148 | +describe('CWInlineCompletionProvider', function () { |
| 149 | + beforeEach(async function () { |
| 150 | + await resetCodeWhispererGlobalVariables() |
| 151 | + }) |
| 152 | + |
| 153 | + describe('provideInlineCompletionItems', function () { |
| 154 | + beforeEach(async function () { |
| 155 | + await resetCodeWhispererGlobalVariables() |
| 156 | + }) |
| 157 | + |
| 158 | + afterEach(function () { |
| 159 | + sinon.restore() |
| 160 | + }) |
| 161 | + |
| 162 | + it('should return undefined if position is before RecommendationHandler start pos', async function () { |
| 163 | + const position = new vscode.Position(0, 0) |
| 164 | + const document = createMockDocument() |
| 165 | + const fakeContext = { triggerKind: 0, selectedCompletionInfo: undefined } |
| 166 | + const token = new vscode.CancellationTokenSource().token |
| 167 | + const provider = new CWInlineCompletionItemProvider(0, 0, [], '', new vscode.Position(1, 1), '') |
| 168 | + const result = await provider.provideInlineCompletionItems(document, position, fakeContext, token) |
| 169 | + |
| 170 | + assert.ok(result === undefined) |
| 171 | + }) |
| 172 | + }) |
| 173 | +}) |
0 commit comments