@@ -8,7 +8,7 @@ import { LanguageClient } from 'vscode-languageclient'
88import { AuthUtil } from 'aws-core-vscode/codewhisperer'
99import { registerMessageListeners } from '../../../../../src/lsp/chat/messages'
1010import { AmazonQChatViewProvider } from '../../../../../src/lsp/chat/webviewProvider'
11- import { SecondaryAuth , Connection } from 'aws-core-vscode/amazonq'
11+ import { SecondaryAuth , Connection , AuthFollowUpType } from 'aws-core-vscode/amazonq'
1212
1313describe ( 'registerMessageListeners' , ( ) => {
1414 let languageClient : LanguageClient
@@ -50,6 +50,28 @@ describe('registerMessageListeners', () => {
5050 let deleteConnectionStub : sinon . SinonStub
5151 let reauthenticateStub : sinon . SinonStub
5252
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+
5375 beforeEach ( ( ) => {
5476 deleteConnectionStub = sandbox . stub ( ) . resolves ( )
5577 reauthenticateStub = sandbox . stub ( ) . resolves ( )
@@ -64,9 +86,9 @@ describe('registerMessageListeners', () => {
6486 sandbox . replaceGetter ( AuthUtil , 'instance' , ( ) => mockAuthUtil )
6587 } )
6688
67- it ( 'should handle re-authentication request' , async ( ) => {
89+ it ( 'handles re-authentication request' , async ( ) => {
6890 await messageHandler ( {
69- command : 'authFollowUpClicked' ,
91+ command : authFollowUpClickedCommand ,
7092 params : {
7193 authFollowupType : 're-auth' ,
7294 } ,
@@ -76,9 +98,9 @@ describe('registerMessageListeners', () => {
7698 sinon . assert . notCalled ( deleteConnectionStub )
7799 } )
78100
79- it ( 'should handle full authentication request' , async ( ) => {
101+ it ( 'handles full authentication request' , async ( ) => {
80102 await messageHandler ( {
81- command : 'authFollowUpClicked' ,
103+ command : authFollowUpClickedCommand ,
82104 params : {
83105 authFollowupType : 'full-auth' ,
84106 } ,
@@ -88,32 +110,20 @@ describe('registerMessageListeners', () => {
88110 sinon . assert . calledOnce ( deleteConnectionStub )
89111 } )
90112
91- it ( 'should log error if re-authentication fails' , async ( ) => {
92- reauthenticateStub . rejects ( new Error ( ) )
93-
94- await messageHandler ( {
95- command : 'authFollowUpClicked' ,
96- params : {
97- authFollowupType : 're-auth' ,
98- } ,
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' ,
99118 } )
100-
101- sinon . assert . calledOnce ( errorStub )
102- sinon . assert . calledWith ( errorStub , sinon . match ( / F a i l e d t o r e - a u t h e n t i c a t e / ) )
103119 } )
104120
105- it ( 'should log error if full authentication fails' , async ( ) => {
106- deleteConnectionStub . rejects ( new Error ( ) )
107-
108- await messageHandler ( {
109- command : 'authFollowUpClicked' ,
110- params : {
111- authFollowupType : 'full-auth' ,
112- } ,
121+ it ( 'logs error if full authentication fails' , async ( ) => {
122+ await testFailure ( {
123+ authType : 'full-auth' ,
124+ stubToReject : deleteConnectionStub ,
125+ errorMessage : 'Failed to authenticate' ,
113126 } )
114-
115- sinon . assert . calledOnce ( errorStub )
116- sinon . assert . calledWith ( errorStub , sinon . match ( / F a i l e d t o a u t h e n t i c a t e / ) )
117127 } )
118128 } )
119129} )
0 commit comments