|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import sinon from 'sinon' |
| 6 | +import { commands, languages } from 'vscode' |
| 7 | +import assert from 'assert' |
| 8 | +import { LanguageClient } from 'vscode-languageclient' |
| 9 | +import { AmazonQInlineCompletionItemProvider, InlineCompletionManager } from '../../../../../src/app/inline/completion' |
| 10 | + |
| 11 | +describe('InlineCompletionManager', () => { |
| 12 | + let manager: InlineCompletionManager |
| 13 | + let languageClient: LanguageClient |
| 14 | + let sendNotificationStub: sinon.SinonStub |
| 15 | + let registerProviderStub: sinon.SinonStub |
| 16 | + let registerCommandStub: sinon.SinonStub |
| 17 | + let executeCommandStub: sinon.SinonStub |
| 18 | + let disposableStub: sinon.SinonStub |
| 19 | + let sandbox: sinon.SinonSandbox |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + sandbox = sinon.createSandbox() |
| 23 | + |
| 24 | + registerProviderStub = sandbox.stub(languages, 'registerInlineCompletionItemProvider') |
| 25 | + registerCommandStub = sandbox.stub(commands, 'registerCommand') |
| 26 | + executeCommandStub = sandbox.stub(commands, 'executeCommand') |
| 27 | + sendNotificationStub = sandbox.stub() |
| 28 | + |
| 29 | + // Create mock disposable |
| 30 | + const mockDisposable = { |
| 31 | + dispose: sandbox.stub(), |
| 32 | + } |
| 33 | + disposableStub = mockDisposable.dispose |
| 34 | + registerProviderStub.returns(mockDisposable) |
| 35 | + |
| 36 | + languageClient = { |
| 37 | + sendNotification: sendNotificationStub, |
| 38 | + } as unknown as LanguageClient |
| 39 | + |
| 40 | + manager = new InlineCompletionManager(languageClient) |
| 41 | + }) |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + sandbox.restore() |
| 45 | + }) |
| 46 | + |
| 47 | + describe('constructor', () => { |
| 48 | + it('should initialize with language client and register provider', () => { |
| 49 | + assert(registerProviderStub.calledOnce) |
| 50 | + assert( |
| 51 | + registerProviderStub.calledWith( |
| 52 | + sinon.match.any, |
| 53 | + sinon.match.instanceOf(AmazonQInlineCompletionItemProvider) |
| 54 | + ) |
| 55 | + ) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('dispose', () => { |
| 60 | + it('should dispose of the disposable', () => { |
| 61 | + manager.dispose() |
| 62 | + assert(disposableStub.calledOnce) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('registerInlineCompletion', () => { |
| 67 | + beforeEach(() => { |
| 68 | + manager.registerInlineCompletion() |
| 69 | + }) |
| 70 | + |
| 71 | + it('should register accept and reject commands', () => { |
| 72 | + assert(registerCommandStub.calledWith('aws.sample-vscode-ext-amazonq.accept')) |
| 73 | + assert(registerCommandStub.calledWith('aws.sample-vscode-ext-amazonq.reject')) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('onInlineAcceptance', () => { |
| 77 | + it('should send notification and re-register provider on acceptance', async () => { |
| 78 | + // Get the acceptance handler |
| 79 | + const acceptanceHandler = registerCommandStub |
| 80 | + .getCalls() |
| 81 | + ?.find((call) => call.args[0] === 'aws.sample-vscode-ext-amazonq.accept')?.args[1] |
| 82 | + |
| 83 | + const sessionId = 'test-session' |
| 84 | + const itemId = 'test-item' |
| 85 | + const requestStartTime = Date.now() - 1000 |
| 86 | + const firstCompletionDisplayLatency = 500 |
| 87 | + |
| 88 | + await acceptanceHandler(sessionId, itemId, requestStartTime, firstCompletionDisplayLatency) |
| 89 | + |
| 90 | + assert(sendNotificationStub.calledOnce) |
| 91 | + assert( |
| 92 | + sendNotificationStub.calledWith( |
| 93 | + sinon.match.any, |
| 94 | + sinon.match({ |
| 95 | + sessionId, |
| 96 | + completionSessionResult: { |
| 97 | + [itemId]: { |
| 98 | + seen: true, |
| 99 | + accepted: true, |
| 100 | + discarded: false, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }) |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + assert(disposableStub.calledOnce) |
| 108 | + assert(registerProviderStub.calledTwice) // Once in constructor, once after acceptance |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + describe('onInlineRejection', () => { |
| 113 | + it('should hide suggestion and send notification on rejection', async () => { |
| 114 | + // Get the rejection handler |
| 115 | + const rejectionHandler = registerCommandStub |
| 116 | + .getCalls() |
| 117 | + .find((call) => call.args[0] === 'aws.sample-vscode-ext-amazonq.reject')?.args[1] |
| 118 | + |
| 119 | + const sessionId = 'test-session' |
| 120 | + const itemId = 'test-item' |
| 121 | + |
| 122 | + await rejectionHandler(sessionId, itemId) |
| 123 | + |
| 124 | + assert(executeCommandStub.calledWith('editor.action.inlineSuggest.hide')) |
| 125 | + assert(sendNotificationStub.calledOnce) |
| 126 | + assert( |
| 127 | + sendNotificationStub.calledWith( |
| 128 | + sinon.match.any, |
| 129 | + sinon.match({ |
| 130 | + sessionId, |
| 131 | + completionSessionResult: { |
| 132 | + [itemId]: { |
| 133 | + seen: true, |
| 134 | + accepted: false, |
| 135 | + discarded: false, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }) |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + assert(disposableStub.calledOnce) |
| 143 | + assert(registerProviderStub.calledTwice) // Once in constructor, once after rejection |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + describe('previous command', () => { |
| 148 | + it('should register and handle previous command correctly', async () => { |
| 149 | + const prevCommandCall = registerCommandStub |
| 150 | + .getCalls() |
| 151 | + .find((call) => call.args[0] === 'editor.action.inlineSuggest.showPrevious') |
| 152 | + |
| 153 | + assert(prevCommandCall, 'Previous command should be registered') |
| 154 | + |
| 155 | + if (prevCommandCall) { |
| 156 | + const handler = prevCommandCall.args[1] |
| 157 | + await handler() |
| 158 | + |
| 159 | + assert(executeCommandStub.calledWith('editor.action.inlineSuggest.hide')) |
| 160 | + assert(disposableStub.calledOnce) |
| 161 | + assert(registerProviderStub.calledTwice) |
| 162 | + assert(executeCommandStub.calledWith('editor.action.inlineSuggest.trigger')) |
| 163 | + } |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + describe('next command', () => { |
| 168 | + it('should register and handle next command correctly', async () => { |
| 169 | + const nextCommandCall = registerCommandStub |
| 170 | + .getCalls() |
| 171 | + .find((call) => call.args[0] === 'editor.action.inlineSuggest.showNext') |
| 172 | + |
| 173 | + assert(nextCommandCall, 'Next command should be registered') |
| 174 | + |
| 175 | + if (nextCommandCall) { |
| 176 | + const handler = nextCommandCall.args[1] |
| 177 | + await handler() |
| 178 | + |
| 179 | + assert(executeCommandStub.calledWith('editor.action.inlineSuggest.hide')) |
| 180 | + assert(disposableStub.calledOnce) |
| 181 | + assert(registerProviderStub.calledTwice) |
| 182 | + assert(executeCommandStub.calledWith('editor.action.inlineSuggest.trigger')) |
| 183 | + } |
| 184 | + }) |
| 185 | + }) |
| 186 | + }) |
| 187 | +}) |
0 commit comments