Skip to content

Commit adfdf9c

Browse files
committed
Update test and review comments
1 parent 34bde5e commit adfdf9c

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

packages/amazonq/src/lsp/chat/messages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function registerMessageListeners(
9090
await AuthUtil.instance.reauthenticate()
9191
} catch (e) {
9292
languageClient.error(
93-
`[VSCode Client] Failed to re-authenticate after AUTH_FOLLOW_UP_CLICKED: ${e}`
93+
`[VSCode Client] Failed to re-authenticate after AUTH_FOLLOW_UP_CLICKED: ${(e as Error).message}`
9494
)
9595
}
9696
}
@@ -100,7 +100,7 @@ export function registerMessageListeners(
100100
await AuthUtil.instance.secondaryAuth.deleteConnection()
101101
} catch (e) {
102102
languageClient.error(
103-
`[VSCode Client] Failed to authenticate after AUTH_FOLLOW_UP_CLICKED: ${e}`
103+
`[VSCode Client] Failed to authenticate after AUTH_FOLLOW_UP_CLICKED: ${(e as Error).message}`
104104
)
105105
}
106106
}

packages/amazonq/test/unit/amazonq/lsp/chat/messages.test.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { LanguageClient } from 'vscode-languageclient'
88
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
99
import { registerMessageListeners } from '../../../../../src/lsp/chat/messages'
1010
import { 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

1313
describe('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(/Failed to re-authenticate/))
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(/Failed to authenticate/))
117127
})
118128
})
119129
})

0 commit comments

Comments
 (0)