Skip to content

Commit 964be29

Browse files
committed
tests(amazonq): update inline tests for flare
1 parent 6995107 commit 964be29

File tree

4 files changed

+74
-92
lines changed

4 files changed

+74
-92
lines changed

packages/amazonq/src/app/inline/completion.ts

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
window,
1818
TextEditor,
1919
InlineCompletionTriggerKind,
20+
Range,
2021
} from 'vscode'
2122
import { LanguageClient } from 'vscode-languageclient'
2223
import {
@@ -32,11 +33,13 @@ import {
3233
ReferenceLogViewProvider,
3334
ImportAdderProvider,
3435
CodeSuggestionsState,
36+
vsCodeState,
3537
} from 'aws-core-vscode/codewhisperer'
3638
import { InlineGeneratingMessage } from './inlineGeneratingMessage'
3739
import { LineTracker } from './stateTracker/lineTracker'
3840
import { InlineTutorialAnnotation } from './tutorials/inlineTutorialAnnotation'
3941
import { TelemetryHelper } from './telemetryHelper'
42+
import { getLogger } from 'aws-core-vscode/shared'
4043

4144
export class InlineCompletionManager implements Disposable {
4245
private disposable: Disposable
@@ -204,54 +207,68 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
204207
context: InlineCompletionContext,
205208
token: CancellationToken
206209
): Promise<InlineCompletionItem[] | InlineCompletionList> {
207-
if (this.isNewSession) {
208-
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
209-
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
210-
// return early when suggestions are disabled with auto trigger
210+
try {
211+
// only exposed for testing
212+
vsCodeState.isRecommendationsActive = true
213+
if (this.isNewSession) {
214+
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
215+
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
216+
// return early when suggestions are disabled with auto trigger
217+
return []
218+
}
219+
220+
// tell the tutorial that completions has been triggered
221+
await this.inlineTutorialAnnotation.triggered(context.triggerKind)
222+
TelemetryHelper.instance.setInvokeSuggestionStartTime()
223+
TelemetryHelper.instance.setTriggerType(context.triggerKind)
224+
225+
// make service requests if it's a new session
226+
await this.recommendationService.getAllRecommendations(
227+
this.languageClient,
228+
document,
229+
position,
230+
context,
231+
token
232+
)
233+
}
234+
// get active item from session for displaying
235+
const items = this.sessionManager.getActiveRecommendation()
236+
const session = this.sessionManager.getActiveSession()
237+
const editor = window.activeTextEditor
238+
if (!session || !items.length || !editor) {
211239
return []
212240
}
213241

214-
// tell the tutorial that completions has been triggered
215-
await this.inlineTutorialAnnotation.triggered(context.triggerKind)
216-
TelemetryHelper.instance.setInvokeSuggestionStartTime()
217-
TelemetryHelper.instance.setTriggerType(context.triggerKind)
218-
219-
// make service requests if it's a new session
220-
await this.recommendationService.getAllRecommendations(
221-
this.languageClient,
222-
document,
223-
position,
224-
context,
225-
token
226-
)
227-
}
228-
// get active item from session for displaying
229-
const items = this.sessionManager.getActiveRecommendation()
230-
const session = this.sessionManager.getActiveSession()
231-
if (!session || !items.length) {
232-
return []
233-
}
234-
const editor = window.activeTextEditor
235-
for (const item of items) {
236-
item.command = {
237-
command: 'aws.amazonq.acceptInline',
238-
title: 'On acceptance',
239-
arguments: [
240-
session.sessionId,
241-
item,
242-
editor,
243-
session.requestStartTime,
242+
const start = document.validatePosition(editor.selection.active)
243+
const end = position
244+
for (const item of items) {
245+
item.command = {
246+
command: 'aws.amazonq.acceptInline',
247+
title: 'On acceptance',
248+
arguments: [
249+
session.sessionId,
250+
item,
251+
editor,
252+
session.requestStartTime,
253+
position.line,
254+
session.firstCompletionDisplayLatency,
255+
],
256+
}
257+
item.range = new Range(start, end)
258+
ReferenceInlineProvider.instance.setInlineReference(
244259
position.line,
245-
session.firstCompletionDisplayLatency,
246-
],
260+
item.insertText as string,
261+
item.references
262+
)
263+
ImportAdderProvider.instance.onShowRecommendation(document, position.line, item)
247264
}
248-
ReferenceInlineProvider.instance.setInlineReference(
249-
position.line,
250-
item.insertText as string,
251-
item.references
252-
)
253-
ImportAdderProvider.instance.onShowRecommendation(document, position.line, item)
265+
return items as InlineCompletionItem[]
266+
} catch (e) {
267+
getLogger('amazonqLsp').error('Completion request failed: %O', e)
268+
return []
269+
} finally {
270+
// only exposed for testing
271+
vsCodeState.isRecommendationsActive = false
254272
}
255-
return items as InlineCompletionItem[]
256273
}
257274
}

packages/amazonq/test/e2e/inline/inline.test.ts

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@
55

66
import * as vscode from 'vscode'
77
import assert from 'assert'
8-
import {
9-
closeAllEditors,
10-
getTestWindow,
11-
registerAuthHook,
12-
resetCodeWhispererGlobalVariables,
13-
TestFolder,
14-
toTextEditor,
15-
using,
16-
} from 'aws-core-vscode/test'
17-
import { session } from 'aws-core-vscode/codewhisperer'
8+
import { closeAllEditors, registerAuthHook, TestFolder, toTextEditor, using } from 'aws-core-vscode/test'
189
import { Commands, globals, sleep, waitUntil, collectionUtil } from 'aws-core-vscode/shared'
1910
import { loginToIdC } from '../amazonq/utils/setup'
11+
import { vsCodeState } from 'aws-core-vscode/codewhisperer'
2012

2113
describe('Amazon Q Inline', async function () {
2214
const retries = 3
@@ -40,7 +32,6 @@ describe('Amazon Q Inline', async function () {
4032
const folder = await TestFolder.create()
4133
tempFolder = folder.path
4234
await closeAllEditors()
43-
await resetCodeWhispererGlobalVariables()
4435
})
4536

4637
afterEach(async function () {
@@ -70,17 +61,6 @@ describe('Amazon Q Inline', async function () {
7061
})
7162
}
7263

73-
async function waitForRecommendations() {
74-
const suggestionShown = await waitUntil(async () => session.getSuggestionState(0) === 'Showed', waitOptions)
75-
if (!suggestionShown) {
76-
throw new Error(`Suggestion did not show. Suggestion States: ${JSON.stringify(session.suggestionStates)}`)
77-
}
78-
console.table({
79-
'suggestions states': JSON.stringify(session.suggestionStates),
80-
recommendations: session.recommendations,
81-
})
82-
}
83-
8464
/**
8565
* Waits for a specific telemetry event to be emitted with the expected suggestion state.
8666
* It looks like there might be a potential race condition in codewhisperer causing telemetry
@@ -134,8 +114,9 @@ describe('Amazon Q Inline', async function () {
134114
await invokeCompletion()
135115
originalEditorContents = vscode.window.activeTextEditor?.document.getText()
136116

137-
// wait until the ghost text appears
138-
await waitForRecommendations()
117+
// wait until all the recommendations have finished
118+
await waitUntil(() => Promise.resolve(vsCodeState.isRecommendationsActive === true), waitOptions)
119+
await waitUntil(() => Promise.resolve(vsCodeState.isRecommendationsActive === false), waitOptions)
139120
}
140121

141122
beforeEach(async () => {
@@ -148,14 +129,12 @@ describe('Amazon Q Inline', async function () {
148129
try {
149130
await setup()
150131
console.log(`test run ${attempt} succeeded`)
151-
logUserDecisionStatus()
152132
break
153133
} catch (e) {
154134
console.log(`test run ${attempt} failed`)
155135
console.log(e)
156136
logUserDecisionStatus()
157137
attempt++
158-
await resetCodeWhispererGlobalVariables()
159138
}
160139
}
161140
if (attempt === retries) {
@@ -201,29 +180,6 @@ describe('Amazon Q Inline', async function () {
201180
assert.deepStrictEqual(vscode.window.activeTextEditor?.document.getText(), originalEditorContents)
202181
})
203182
})
204-
205-
it(`${name} invoke on unsupported filetype`, async function () {
206-
await setupEditor({
207-
name: 'test.zig',
208-
contents: `fn doSomething() void {
209-
210-
}`,
211-
})
212-
213-
/**
214-
* Add delay between editor loading and invoking completion
215-
* @see beforeEach in supported filetypes for more information
216-
*/
217-
await sleep(1000)
218-
await invokeCompletion()
219-
220-
if (name === 'automatic') {
221-
// It should never get triggered since its not a supported file type
222-
// assert.deepStrictEqual(RecommendationService.instance.isRunning, false)
223-
} else {
224-
await getTestWindow().waitForMessage('currently not supported by Amazon Q inline suggestions')
225-
}
226-
})
227183
})
228184
}
229185
})

packages/core/src/codewhisperer/models/model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ interface VsCodeState {
3333
* Flag indicates whether codewhisperer is doing vscode.TextEditor.edit
3434
*/
3535
isCodeWhispererEditing: boolean
36+
/**
37+
* Keeps track of whether or not recommendations are currently running
38+
*/
39+
isRecommendationsActive: boolean
3640
/**
3741
* Timestamp of previous user edit
3842
*/
@@ -44,6 +48,9 @@ interface VsCodeState {
4448
export const vsCodeState: VsCodeState = {
4549
isIntelliSenseActive: false,
4650
isCodeWhispererEditing: false,
51+
// hack to globally keep track of whether or not recommendations are currently running. This allows us to know
52+
// when recommendations have ran during e2e tests
53+
isRecommendationsActive: false,
4754
lastUserModificationTime: 0,
4855
isFreeTierLimitReached: false,
4956
}

packages/core/src/shared/telemetry/exemptMetrics.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const validationExemptMetrics: Set<string> = new Set([
2929
'codewhisperer_codePercentage',
3030
'codewhisperer_userModification',
3131
'codewhisperer_userTriggerDecision',
32+
'codewhisperer_perceivedLatency', // flare doesn't currently set result property
33+
'codewhisperer_serviceInvocation', // flare doesn't currently set result property
3234
'dynamicresource_selectResources',
3335
'dynamicresource_copyIdentifier',
3436
'dynamicresource_mutateResource',

0 commit comments

Comments
 (0)