|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as sinon from 'sinon' |
| 7 | +import { LanguageClient } from 'vscode-languageclient' |
| 8 | +import { AuthUtil } from 'aws-core-vscode/codewhisperer' |
| 9 | +import { registerMessageListeners } from '../../../../../src/lsp/chat/messages' |
| 10 | +import { AmazonQChatViewProvider } from '../../../../../src/lsp/chat/webviewProvider' |
| 11 | +import { secondaryAuth, authConnection, AuthFollowUpType } from 'aws-core-vscode/amazonq' |
| 12 | + |
| 13 | +describe('registerMessageListeners', () => { |
| 14 | + let languageClient: LanguageClient |
| 15 | + let provider: AmazonQChatViewProvider |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + let messageHandler: (message: any) => void | Promise<void> |
| 18 | + let errorStub: sinon.SinonStub |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + sandbox = sinon.createSandbox() |
| 22 | + errorStub = sandbox.stub() |
| 23 | + |
| 24 | + languageClient = { |
| 25 | + info: sandbox.stub(), |
| 26 | + error: errorStub, |
| 27 | + sendNotification: sandbox.stub(), |
| 28 | + } as unknown as LanguageClient |
| 29 | + |
| 30 | + provider = { |
| 31 | + webview: { |
| 32 | + onDidReceiveMessage: (callback: (message: any) => void | Promise<void>) => { |
| 33 | + messageHandler = callback |
| 34 | + return { |
| 35 | + dispose: (): void => {}, |
| 36 | + } |
| 37 | + }, |
| 38 | + }, |
| 39 | + } as any |
| 40 | + |
| 41 | + registerMessageListeners(languageClient, provider, Buffer.from('test-key')) |
| 42 | + }) |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + sandbox.restore() |
| 46 | + }) |
| 47 | + |
| 48 | + describe('AUTH_FOLLOW_UP_CLICKED', () => { |
| 49 | + let mockAuthUtil: AuthUtil |
| 50 | + let deleteConnectionStub: sinon.SinonStub |
| 51 | + let reauthenticateStub: sinon.SinonStub |
| 52 | + |
| 53 | + const authFollowUpClickedCommand = 'authFollowUpClicked' |
| 54 | + |
| 55 | + interface TestCase { |
| 56 | + authType: AuthFollowUpType |
| 57 | + stubToReject: sinon.SinonStub |
| 58 | + errorMessage: string |
| 59 | + } |
| 60 | + |
| 61 | + const testFailure = async (testCase: TestCase) => { |
| 62 | + testCase.stubToReject.rejects(new Error()) |
| 63 | + |
| 64 | + await messageHandler({ |
| 65 | + command: authFollowUpClickedCommand, |
| 66 | + params: { |
| 67 | + authFollowupType: testCase.authType, |
| 68 | + }, |
| 69 | + }) |
| 70 | + |
| 71 | + sinon.assert.calledOnce(errorStub) |
| 72 | + sinon.assert.calledWith(errorStub, sinon.match(testCase.errorMessage)) |
| 73 | + } |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + deleteConnectionStub = sandbox.stub().resolves() |
| 77 | + reauthenticateStub = sandbox.stub().resolves() |
| 78 | + |
| 79 | + mockAuthUtil = { |
| 80 | + reauthenticate: reauthenticateStub, |
| 81 | + secondaryAuth: { |
| 82 | + deleteConnection: deleteConnectionStub, |
| 83 | + } as unknown as secondaryAuth.SecondaryAuth<authConnection.Connection>, |
| 84 | + } as unknown as AuthUtil |
| 85 | + |
| 86 | + sandbox.replaceGetter(AuthUtil, 'instance', () => mockAuthUtil) |
| 87 | + }) |
| 88 | + |
| 89 | + it('handles re-authentication request', async () => { |
| 90 | + await messageHandler({ |
| 91 | + command: authFollowUpClickedCommand, |
| 92 | + params: { |
| 93 | + authFollowupType: 're-auth', |
| 94 | + }, |
| 95 | + }) |
| 96 | + |
| 97 | + sinon.assert.calledOnce(reauthenticateStub) |
| 98 | + sinon.assert.notCalled(deleteConnectionStub) |
| 99 | + }) |
| 100 | + |
| 101 | + it('handles full authentication request', async () => { |
| 102 | + await messageHandler({ |
| 103 | + command: authFollowUpClickedCommand, |
| 104 | + params: { |
| 105 | + authFollowupType: 'full-auth', |
| 106 | + }, |
| 107 | + }) |
| 108 | + |
| 109 | + sinon.assert.notCalled(reauthenticateStub) |
| 110 | + sinon.assert.calledOnce(deleteConnectionStub) |
| 111 | + }) |
| 112 | + |
| 113 | + it('logs error if re-authentication fails', async () => { |
| 114 | + await testFailure({ |
| 115 | + authType: 're-auth', |
| 116 | + stubToReject: reauthenticateStub, |
| 117 | + errorMessage: 'Failed to re-authenticate', |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + it('logs error if full authentication fails', async () => { |
| 122 | + await testFailure({ |
| 123 | + authType: 'full-auth', |
| 124 | + stubToReject: deleteConnectionStub, |
| 125 | + errorMessage: 'Failed to authenticate', |
| 126 | + }) |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments