Skip to content

Commit 9d498ef

Browse files
authored
tests(CodeWhisperer): add accountless unit test case (#2731)
## Problem There are no unit tests for `showAccessTokenPrompt` `showAccessTokenPrompt` is implemented as a combination of a quick pick and input box. It's meant to be temporary. ## Solution Add basic test cases
1 parent 198c371 commit 9d498ef

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*!
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as assert from 'assert'
7+
import * as sinon from 'sinon'
8+
import * as vscode from 'vscode'
9+
import { DefaultCodeWhispererClient } from '../../../codewhisperer/client/codewhisperer'
10+
import { exposeEmitters } from '../../shared/vscode/testUtils'
11+
import { showAccessTokenPrompt } from '../../../codewhisperer/util/showAccessTokenPrompt'
12+
import { sleep } from '../../../shared/utilities/timeoutUtils'
13+
14+
function stubQuickInputs() {
15+
const picker = exposeEmitters(vscode.window.createQuickPick(), ['onDidAccept', 'onDidChangeValue'])
16+
17+
const inputBox = exposeEmitters(vscode.window.createInputBox(), ['onDidAccept', 'onDidChangeValue'])
18+
sinon.stub(vscode.window, 'createQuickPick').returns(picker)
19+
sinon.stub(vscode.window, 'createInputBox').returns(inputBox)
20+
21+
return { picker, inputBox }
22+
}
23+
24+
describe('showAccessTokenPrompt', function () {
25+
afterEach(function () {
26+
sinon.restore()
27+
})
28+
29+
it('shows an error message when the backend rejects the user input', async function () {
30+
const mockClient: DefaultCodeWhispererClient = new DefaultCodeWhispererClient()
31+
const { picker, inputBox } = stubQuickInputs()
32+
sinon.stub(mockClient, 'getAccessToken').rejects(new Error('Invalid access code. Please re-enter.'))
33+
34+
showAccessTokenPrompt(mockClient, () => {})
35+
36+
picker.value = 'bad input'
37+
picker.fireOnDidAccept()
38+
39+
await sleep()
40+
assert.strictEqual(inputBox.validationMessage, 'Invalid access code. Please re-enter.')
41+
})
42+
43+
it('shows an error message for empty user input', async function () {
44+
const mockClient: DefaultCodeWhispererClient = new DefaultCodeWhispererClient()
45+
const { picker, inputBox } = stubQuickInputs()
46+
sinon.stub(mockClient, 'getAccessToken').rejects(new Error('Invalid access code. Please re-enter.'))
47+
48+
showAccessTokenPrompt(mockClient, () => {})
49+
50+
picker.value = ''
51+
picker.fireOnDidAccept()
52+
53+
await sleep()
54+
assert.strictEqual(inputBox.validationMessage, 'Invalid access code. Please re-enter.')
55+
})
56+
57+
it('executes the callback parameter for valid user input', async function () {
58+
let out
59+
const setToken = (token: string) => {
60+
out = token
61+
}
62+
const mockClient: DefaultCodeWhispererClient = new DefaultCodeWhispererClient()
63+
const mockServerResult = {
64+
accessToken: 'token',
65+
}
66+
const { picker } = stubQuickInputs()
67+
sinon.stub(mockClient, 'getAccessToken').resolves(mockServerResult)
68+
showAccessTokenPrompt(mockClient, setToken)
69+
picker.value = 'token'
70+
picker.fireOnDidAccept()
71+
72+
await sleep()
73+
assert.strictEqual(picker.value, out)
74+
})
75+
})

0 commit comments

Comments
 (0)