|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 fort |
| 4 | + */ |
| 5 | + |
| 6 | +import { RemoteFetcher } from '../../notifications/controller' |
| 7 | +import { NotificationsNode } from '../../notifications/panelNode' |
| 8 | +import { activate, deactivate } from '../../notifications/activation' |
| 9 | +import globals from '../../shared/extensionGlobals' |
| 10 | +import assert from 'assert' |
| 11 | +import sinon from 'sinon' |
| 12 | +import { createTestAuth } from '../../test/credentials/testUtil' |
| 13 | +import { AuthUtil } from '../../codewhisperer/util/authUtil' |
| 14 | +import { Auth } from '../../auth/auth' |
| 15 | +import { ShownMessage } from '../../test/shared/vscode/message' |
| 16 | +import { getTestWindow } from '../../test/shared/vscode/window' |
| 17 | +import { AuthUserState } from '../../shared/telemetry/telemetry.gen' |
| 18 | +import { globalKey } from '../../shared/globalState' |
| 19 | +import { waitUntil } from '../../shared/utilities/timeoutUtils' |
| 20 | +import { assertTelemetry, assertTextEditorContains } from '../../test/testUtil' |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests that connect to our hosted files server and download the integ notifications. |
| 24 | + * |
| 25 | + * IMPORTANT: |
| 26 | + * These tests are dependent on what is hosted on the server (message contents, criteria, etc). |
| 27 | + */ |
| 28 | +export function getNotificationsE2ESuite(getAuthStateFn: () => Promise<Omit<AuthUserState, 'source'>>) { |
| 29 | + return describe('Notifications Integration Test', function () { |
| 30 | + const storageKey = 'aws.notifications.test' as globalKey |
| 31 | + const fetcher = new RemoteFetcher( |
| 32 | + 'https://idetoolkits-hostedfiles.amazonaws.com/Notifications/integ/VSCode/startup/1.x.json', |
| 33 | + 'https://idetoolkits-hostedfiles.amazonaws.com/Notifications/integ/VSCode/emergency/1.x.json' |
| 34 | + ) |
| 35 | + // const panelNode = NotificationsNode.instance |
| 36 | + const sandbox = sinon.createSandbox() |
| 37 | + let auth: ReturnType<typeof createTestAuth> |
| 38 | + let authUtil: AuthUtil |
| 39 | + |
| 40 | + beforeEach(async function () { |
| 41 | + await globals.globalState.update(storageKey, undefined) |
| 42 | + auth = createTestAuth(globals.globalState) |
| 43 | + authUtil = new AuthUtil(auth) |
| 44 | + sandbox.stub(Auth, 'instance').value(auth) |
| 45 | + sandbox.stub(AuthUtil, 'instance').value(authUtil) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(async function () { |
| 49 | + await globals.globalState.update(storageKey, undefined) |
| 50 | + sandbox.restore() |
| 51 | + }) |
| 52 | + |
| 53 | + /** |
| 54 | + * A way to track notifications displayed in the IDE. |
| 55 | + * See {@link setupTestWindow} for usage. |
| 56 | + */ |
| 57 | + function msgHandler(text: string, fn: (m: ShownMessage) => Promise<void>) { |
| 58 | + return { |
| 59 | + seen: false, // have we seen and processed this message |
| 60 | + text, // title of the notification to match the message on |
| 61 | + fn, // what to do with the message |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function setupTestWindow(toHandle: ReturnType<typeof msgHandler>[]) { |
| 66 | + const testWindow = getTestWindow() |
| 67 | + testWindow.onDidShowMessage(async (message) => { |
| 68 | + const handler = toHandle.find((h) => message.message.includes(h.text)) |
| 69 | + if (handler) { |
| 70 | + await handler.fn(message) |
| 71 | + handler.seen = true |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + return testWindow |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Set up the test window, activate the notifications module, and wait for |
| 80 | + * messages to resolve in the UI. |
| 81 | + */ |
| 82 | + async function runTest(toHandle: ReturnType<typeof msgHandler>[]) { |
| 83 | + setupTestWindow(toHandle) |
| 84 | + |
| 85 | + const initialState = await getAuthStateFn() |
| 86 | + await activate(globals.context, initialState, getAuthStateFn, { |
| 87 | + fetcher, |
| 88 | + storageKey, |
| 89 | + }) |
| 90 | + |
| 91 | + await waitUntil(async () => toHandle.every((h) => h.seen), { timeout: 12000 }) |
| 92 | + } |
| 93 | + |
| 94 | + it('can fetch unauthenticated notifications', async function () { |
| 95 | + await runTest([ |
| 96 | + msgHandler('New Amazon Q features are available!', async (m: ShownMessage) => { |
| 97 | + assert.ok(!m.modal) |
| 98 | + assert.ok(m.items.find((i) => i.title.includes('Learn more'))) |
| 99 | + m.close() |
| 100 | + }), |
| 101 | + msgHandler( |
| 102 | + 'Signing into Amazon Q is broken, please try this workaround while we work on releasing a fix.', |
| 103 | + async (m: ShownMessage) => { |
| 104 | + assert.ok(!m.modal) |
| 105 | + m.selectItem('Learn more') |
| 106 | + await assertTextEditorContains( |
| 107 | + 'There is currently a bug that is preventing users from signing into Amazon Q.', |
| 108 | + false |
| 109 | + ) |
| 110 | + } |
| 111 | + ), |
| 112 | + ]) |
| 113 | + |
| 114 | + assert.equal(NotificationsNode.instance.getChildren().length, 2) |
| 115 | + assertTelemetry('toolkit_showNotification', [ |
| 116 | + { id: 'TARGETED_NOTIFICATION:startup2' }, |
| 117 | + { id: 'TARGETED_NOTIFICATION:emergency1' }, |
| 118 | + ]) |
| 119 | + assertTelemetry('toolkit_invokeAction', [ |
| 120 | + { action: 'OK', source: 'TARGETED_NOTIFICATION:startup2' }, |
| 121 | + { action: 'Learn more', source: 'TARGETED_NOTIFICATION:emergency1' }, |
| 122 | + ]) |
| 123 | + |
| 124 | + deactivate() |
| 125 | + }) |
| 126 | + |
| 127 | + it('can fetch authenticated notifications', async function () { |
| 128 | + await auth.useConnection(await authUtil.connectToAwsBuilderId()) |
| 129 | + await runTest([ |
| 130 | + msgHandler('New Amazon Q features available: inline chat', async (m: ShownMessage) => { |
| 131 | + assert.ok(!m.modal) |
| 132 | + m.selectItem('Learn more') |
| 133 | + await assertTextEditorContains( |
| 134 | + 'You can now use Amazon Q inline in your IDE, without ever touching the mouse or using copy and paste.', |
| 135 | + false |
| 136 | + ) |
| 137 | + }), |
| 138 | + msgHandler('Amazon Q may delete user data', async (m: ShownMessage) => { |
| 139 | + assert.ok(m.modal) |
| 140 | + assert.ok(m.items.find((i) => i.title.includes('Update and Reload'))) |
| 141 | + m.close() |
| 142 | + }), |
| 143 | + ]) |
| 144 | + |
| 145 | + assert.equal(NotificationsNode.instance.getChildren().length, 3) // includes one startup notification that wasn't checked here. (checked in another test) |
| 146 | + assertTelemetry('toolkit_showNotification', [ |
| 147 | + { id: 'TARGETED_NOTIFICATION:startup1' }, |
| 148 | + { id: 'TARGETED_NOTIFICATION:startup2' }, |
| 149 | + { id: 'TARGETED_NOTIFICATION:emergency2' }, |
| 150 | + ]) |
| 151 | + assertTelemetry('toolkit_invokeAction', [ |
| 152 | + { action: 'Learn more', source: 'TARGETED_NOTIFICATION:startup1' }, |
| 153 | + { action: 'OK', source: 'TARGETED_NOTIFICATION:emergency2' }, |
| 154 | + ]) |
| 155 | + |
| 156 | + deactivate() |
| 157 | + }) |
| 158 | + }) |
| 159 | +} |
0 commit comments