Skip to content

Commit 2e424b3

Browse files
authored
test(amazonq): Add e2e tests for general amazon q chat panel (#6279)
## Problem - We want tests for the general amazon q panel - We want other teams to quickly be able to write tests for their agents ## Solution - Add general tests for the amazon q chat panel - Add a template that other teams can use to write their tests - Fix an issue that occurred only in tests where tab id's weren't defined for the help message. The problem was that in the framework everything is instant which meant a tab id was defined before that code ran and was never passed through to processQuickActionCommand - By default don't show the welcome page for now tests --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0d7ea7d commit 2e424b3

File tree

7 files changed

+172
-12
lines changed

7 files changed

+172
-12
lines changed

packages/amazonq/test/e2e/amazonq/assert.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ export function assertQuickActions(tab: Messenger, commands: string[]) {
2828
assert.fail(`Could not find commands: ${missingCommands.join(', ')} for ${tab.tabID}`)
2929
}
3030
}
31+
32+
export function assertContextCommands(tab: Messenger, contextCommands: string[]) {
33+
assert.deepStrictEqual(
34+
tab
35+
.getStore()
36+
.contextCommands?.map((x) => x.commands)
37+
.flat()
38+
.map((x) => x.command),
39+
contextCommands
40+
)
41+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from 'assert'
7+
import { qTestingFramework } from './framework/framework'
8+
import sinon from 'sinon'
9+
import { Messenger } from './framework/messenger'
10+
import { MynahUIDataModel } from '@aws/mynah-ui'
11+
import { assertContextCommands, assertQuickActions } from './assert'
12+
import { registerAuthHook, using } from 'aws-core-vscode/test'
13+
import { loginToIdC } from './utils/setup'
14+
import { webviewConstants } from 'aws-core-vscode/amazonq'
15+
16+
describe('Amazon Q Chat', function () {
17+
let framework: qTestingFramework
18+
let tab: Messenger
19+
let store: MynahUIDataModel
20+
21+
const availableCommands: string[] = ['/dev', '/test', '/review', '/doc', '/transform']
22+
23+
before(async function () {
24+
/**
25+
* Login to the amazonq-test-account. When running in CI this has unlimited
26+
* calls to the backend api
27+
*/
28+
await using(registerAuthHook('amazonq-test-account'), async () => {
29+
await loginToIdC()
30+
})
31+
})
32+
33+
// jscpd:ignore-start
34+
beforeEach(() => {
35+
// Make sure you're logged in before every test
36+
registerAuthHook('amazonq-test-account')
37+
framework = new qTestingFramework('cwc', true, [])
38+
tab = framework.createTab()
39+
store = tab.getStore()
40+
})
41+
42+
afterEach(() => {
43+
framework.removeTab(tab.tabID)
44+
framework.dispose()
45+
sinon.restore()
46+
})
47+
48+
it(`Shows quick actions: ${availableCommands.join(', ')}`, async () => {
49+
assertQuickActions(tab, availableCommands)
50+
})
51+
52+
it('Shows @workspace', () => {
53+
assertContextCommands(tab, ['@workspace'])
54+
})
55+
56+
// jscpd:ignore-end
57+
58+
it('Shows title', () => {
59+
assert.deepStrictEqual(store.tabTitle, 'Chat')
60+
})
61+
62+
it('Shows placeholder', () => {
63+
assert.deepStrictEqual(store.promptInputPlaceholder, 'Ask a question or enter "/" for quick actions')
64+
})
65+
66+
it('Sends message', async () => {
67+
tab.addChatMessage({
68+
prompt: 'What is a lambda',
69+
})
70+
await tab.waitForChatFinishesLoading()
71+
const chatItems = tab.getChatItems()
72+
// the last item should be an answer
73+
assert.deepStrictEqual(chatItems[4].type, 'answer')
74+
})
75+
76+
describe('Clicks examples', () => {
77+
it('Click help', async () => {
78+
tab.clickButton('help')
79+
await tab.waitForText(webviewConstants.helpMessage)
80+
const chatItems = tab.getChatItems()
81+
assert.deepStrictEqual(chatItems[4].type, 'answer')
82+
assert.deepStrictEqual(chatItems[4].body, webviewConstants.helpMessage)
83+
})
84+
})
85+
})

packages/amazonq/test/e2e/amazonq/framework/framework.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class qTestingFramework {
2929
featureName: TabType,
3030
amazonQEnabled: boolean,
3131
featureConfigsSerialized: [string, FeatureContext][],
32-
welcomeCount = 0
32+
welcomeCount = Number.MAX_VALUE // by default don't show the welcome page
3333
) {
3434
/**
3535
* Instantiate the UI and override the postMessage to publish using the app message
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
// jscpd:ignore-start
7+
import assert from 'assert'
8+
import { qTestingFramework } from './framework/framework'
9+
import sinon from 'sinon'
10+
import { Messenger } from './framework/messenger'
11+
import { MynahUIDataModel } from '@aws/mynah-ui'
12+
import { assertQuickActions } from './assert'
13+
import { registerAuthHook, using } from 'aws-core-vscode/test'
14+
import { loginToIdC } from './utils/setup'
15+
16+
describe.skip('Amazon Q Test Template', function () {
17+
let framework: qTestingFramework
18+
let tab: Messenger
19+
let store: MynahUIDataModel
20+
21+
const availableCommands: string[] = []
22+
23+
before(async function () {
24+
/**
25+
* Login to the amazonq-test-account. When running in CI this has unlimited
26+
* calls to the backend api
27+
*/
28+
await using(registerAuthHook('amazonq-test-account'), async () => {
29+
await loginToIdC()
30+
})
31+
})
32+
33+
beforeEach(() => {
34+
// Make sure you're logged in before every test
35+
registerAuthHook('amazonq-test-account')
36+
37+
// TODO change unknown to the tab type you want to test
38+
framework = new qTestingFramework('unknown', true, [])
39+
tab = framework.getTabs()[0] // use the default tab that gets created
40+
framework.createTab() // alternatively you can create a new tab
41+
store = tab.getStore()
42+
})
43+
44+
afterEach(() => {
45+
framework.removeTab(tab.tabID)
46+
framework.dispose()
47+
sinon.restore()
48+
})
49+
50+
it(`Shows quick actions: ${availableCommands.join(', ')}`, async () => {
51+
assertQuickActions(tab, availableCommands)
52+
})
53+
54+
it('Shows title', () => {
55+
assert.deepStrictEqual(store.tabTitle, '')
56+
})
57+
58+
it('Shows placeholder', () => {
59+
assert.deepStrictEqual(store.promptInputPlaceholder, '')
60+
})
61+
62+
describe('clicks examples', () => {})
63+
64+
describe('sends message', async () => {})
65+
})
66+
67+
// jscpd:ignore-end

packages/amazonq/test/e2e/amazonq/welcome.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { qTestingFramework } from './framework/framework'
88
import sinon from 'sinon'
99
import { Messenger } from './framework/messenger'
1010
import { MynahUIDataModel } from '@aws/mynah-ui'
11-
import { assertQuickActions } from './assert'
1211
import { FeatureContext } from 'aws-core-vscode/shared'
12+
import { assertContextCommands, assertQuickActions } from './assert'
1313

1414
describe('Amazon Q Welcome page', function () {
1515
let framework: qTestingFramework
@@ -42,13 +42,7 @@ describe('Amazon Q Welcome page', function () {
4242
})
4343

4444
it('Shows context commands', async () => {
45-
assert.deepStrictEqual(
46-
store.contextCommands
47-
?.map((x) => x.commands)
48-
.flat()
49-
.map((x) => x.command),
50-
['@workspace', '@highlight']
51-
)
45+
assertContextCommands(tab, ['@workspace', '@highlight'])
5246
})
5347

5448
describe('shows 3 times', async () => {

packages/core/src/amazonq/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export { init as gumbyChatAppInit } from '../amazonqGumby/app'
2525
export { init as testChatAppInit } from '../amazonqTest/app'
2626
export { init as docChatAppInit } from '../amazonqDoc/app'
2727
export { amazonQHelpUrl } from '../shared/constants'
28+
export * as webviewConstants from './webview/ui/texts/constants'
2829
export { listCodeWhispererCommandsWalkthrough } from '../codewhisperer/ui/statusBarMenu'
2930
export { focusAmazonQPanel, focusAmazonQPanelKeybinding } from '../codewhispererChat/commands/registerCommands'
3031
export { TryChatCodeLensProvider, tryChatCodeLensCommand } from '../codewhispererChat/editor/codelens'

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,19 @@ export class ChatController {
231231
this.openLinkInExternalBrowser(click)
232232
}
233233

234-
private processQuickActionCommand(quickActionCommand: ChatPromptCommandType) {
234+
private processQuickActionCommand(message: PromptMessage) {
235235
this.editorContextExtractor
236236
.extractContextForTrigger('QuickAction')
237237
.then((context) => {
238238
const triggerID = randomUUID()
239239

240+
const quickActionCommand = message.command as ChatPromptCommandType
241+
240242
this.messenger.sendQuickActionMessage(quickActionCommand, triggerID)
241243

242244
this.triggerEventsStorage.addTriggerEvent({
243245
id: triggerID,
244-
tabID: undefined,
246+
tabID: message.tabID,
245247
message: undefined,
246248
type: 'quick_action',
247249
quickAction: quickActionCommand,
@@ -484,7 +486,7 @@ export class ChatController {
484486
recordTelemetryChatRunCommand('clear')
485487
return
486488
default:
487-
this.processQuickActionCommand(message.command)
489+
this.processQuickActionCommand(message)
488490
}
489491
}
490492

0 commit comments

Comments
 (0)