|
| 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 * as vscode from 'vscode' |
| 8 | +import * as path from 'path' |
| 9 | +import { setValidConnection, skipTestIfNoValidConn } from '../util/connection' |
| 10 | +import { ConfigurationEntry } from '../../codewhisperer/models/model' |
| 11 | +import * as codewhispererClient from '../../codewhisperer/client/codewhisperer' |
| 12 | +import { RecommendationHandler } from '../../codewhisperer/service/recommendationHandler' |
| 13 | +import { |
| 14 | + createMockTextEditor, |
| 15 | + createTextDocumentChangeEvent, |
| 16 | + resetCodeWhispererGlobalVariables, |
| 17 | +} from '../../test/codewhisperer/testUtil' |
| 18 | +import { KeyStrokeHandler } from '../../codewhisperer/service/keyStrokeHandler' |
| 19 | +import { sleep } from '../../shared/utilities/timeoutUtils' |
| 20 | +import { invokeRecommendation } from '../../codewhisperer/commands/invokeRecommendation' |
| 21 | +import { getTestWorkspaceFolder } from '../../testInteg/integrationTestsUtilities' |
| 22 | +import { session } from '../../codewhisperer/util/codeWhispererSession' |
| 23 | + |
| 24 | +describe('CodeWhisperer service invocation', async function () { |
| 25 | + let validConnection: boolean |
| 26 | + const client = new codewhispererClient.DefaultCodeWhispererClient() |
| 27 | + const config: ConfigurationEntry = { |
| 28 | + isShowMethodsEnabled: true, |
| 29 | + isManualTriggerEnabled: true, |
| 30 | + isAutomatedTriggerEnabled: true, |
| 31 | + isSuggestionsWithCodeReferencesEnabled: true, |
| 32 | + } |
| 33 | + |
| 34 | + before(async function () { |
| 35 | + validConnection = await setValidConnection() |
| 36 | + }) |
| 37 | + |
| 38 | + beforeEach(function () { |
| 39 | + void resetCodeWhispererGlobalVariables() |
| 40 | + RecommendationHandler.instance.clearRecommendations() |
| 41 | + // valid connection required to run tests |
| 42 | + skipTestIfNoValidConn(validConnection, this) |
| 43 | + }) |
| 44 | + |
| 45 | + it('manual trigger returns valid recommendation response', async function () { |
| 46 | + // check that handler is empty before invocation |
| 47 | + const requestIdBefore = RecommendationHandler.instance.requestId |
| 48 | + const sessionIdBefore = session.sessionId |
| 49 | + const validRecsBefore = RecommendationHandler.instance.isValidResponse() |
| 50 | + |
| 51 | + assert.ok(requestIdBefore.length === 0) |
| 52 | + assert.ok(sessionIdBefore.length === 0) |
| 53 | + assert.ok(!validRecsBefore) |
| 54 | + |
| 55 | + const mockEditor = createMockTextEditor() |
| 56 | + await invokeRecommendation(mockEditor, client, config) |
| 57 | + |
| 58 | + const requestId = RecommendationHandler.instance.requestId |
| 59 | + const sessionId = session.sessionId |
| 60 | + const validRecs = RecommendationHandler.instance.isValidResponse() |
| 61 | + |
| 62 | + assert.ok(requestId.length > 0) |
| 63 | + assert.ok(sessionId.length > 0) |
| 64 | + assert.ok(validRecs) |
| 65 | + }) |
| 66 | + |
| 67 | + it('auto trigger returns valid recommendation response', async function () { |
| 68 | + // check that handler is empty before invocation |
| 69 | + const requestIdBefore = RecommendationHandler.instance.requestId |
| 70 | + const sessionIdBefore = session.sessionId |
| 71 | + const validRecsBefore = RecommendationHandler.instance.isValidResponse() |
| 72 | + |
| 73 | + assert.ok(requestIdBefore.length === 0) |
| 74 | + assert.ok(sessionIdBefore.length === 0) |
| 75 | + assert.ok(!validRecsBefore) |
| 76 | + |
| 77 | + const mockEditor = createMockTextEditor() |
| 78 | + |
| 79 | + const mockEvent: vscode.TextDocumentChangeEvent = createTextDocumentChangeEvent( |
| 80 | + mockEditor.document, |
| 81 | + new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 1)), |
| 82 | + '\n' |
| 83 | + ) |
| 84 | + |
| 85 | + await KeyStrokeHandler.instance.processKeyStroke(mockEvent, mockEditor, client, config) |
| 86 | + // wait for 5 seconds to allow time for response to be generated |
| 87 | + await sleep(5000) |
| 88 | + |
| 89 | + const requestId = RecommendationHandler.instance.requestId |
| 90 | + const sessionId = session.sessionId |
| 91 | + const validRecs = RecommendationHandler.instance.isValidResponse() |
| 92 | + |
| 93 | + assert.ok(requestId.length > 0) |
| 94 | + assert.ok(sessionId.length > 0) |
| 95 | + assert.ok(validRecs) |
| 96 | + }) |
| 97 | + |
| 98 | + it('invocation in unsupported language does not generate a request', async function () { |
| 99 | + const workspaceFolder = getTestWorkspaceFolder() |
| 100 | + const appRoot = path.join(workspaceFolder, 'go1-plain-sam-app') |
| 101 | + const appCodePath = path.join(appRoot, 'hello-world', 'go.mod') |
| 102 | + |
| 103 | + // check that handler is empty before invocation |
| 104 | + const requestIdBefore = RecommendationHandler.instance.requestId |
| 105 | + const sessionIdBefore = session.sessionId |
| 106 | + const validRecsBefore = RecommendationHandler.instance.isValidResponse() |
| 107 | + |
| 108 | + assert.ok(requestIdBefore.length === 0) |
| 109 | + assert.ok(sessionIdBefore.length === 0) |
| 110 | + assert.ok(!validRecsBefore) |
| 111 | + |
| 112 | + const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(appCodePath)) |
| 113 | + const editor = await vscode.window.showTextDocument(doc) |
| 114 | + await invokeRecommendation(editor, client, config) |
| 115 | + |
| 116 | + const requestId = RecommendationHandler.instance.requestId |
| 117 | + const sessionId = session.sessionId |
| 118 | + const validRecs = RecommendationHandler.instance.isValidResponse() |
| 119 | + |
| 120 | + assert.ok(requestId.length === 0) |
| 121 | + assert.ok(sessionId.length === 0) |
| 122 | + assert.ok(!validRecs) |
| 123 | + }) |
| 124 | +}) |
0 commit comments