66import * as vscode from 'vscode'
77import assert from 'assert'
88import {
9- assertTelemetry ,
109 closeAllEditors ,
1110 getTestWindow ,
1211 registerAuthHook ,
@@ -15,7 +14,7 @@ import {
1514 toTextEditor ,
1615 using ,
1716} from 'aws-core-vscode/test'
18- import { RecommendationHandler , RecommendationService } from 'aws-core-vscode/codewhisperer'
17+ import { RecommendationHandler , RecommendationService , session } from 'aws-core-vscode/codewhisperer'
1918import { Commands , globals , sleep , waitUntil } from 'aws-core-vscode/shared'
2019import { loginToIdC } from '../amazonq/utils/setup'
2120
@@ -50,31 +49,47 @@ describe('Amazon Q Inline', async function () {
5049 const textContents =
5150 contents ??
5251 `function fib() {
53-
54-
52+
53+
5554}`
5655 await toTextEditor ( textContents , fileName , tempFolder , {
5756 selection : new vscode . Range ( new vscode . Position ( 1 , 4 ) , new vscode . Position ( 1 , 4 ) ) ,
5857 } )
5958 }
6059
6160 async function waitForRecommendations ( ) {
62- const ok = await waitUntil ( async ( ) => RecommendationHandler . instance . isSuggestionVisible ( ) , waitOptions )
63- if ( ! ok ) {
64- assert . fail ( 'Suggestions failed to become visible' )
65- }
66- }
67-
68- async function waitForTelemetry ( ) {
6961 const ok = await waitUntil (
7062 async ( ) =>
71- globals . telemetry . logger . query ( {
72- metricName : 'codewhisperer_userTriggerDecision' ,
73- } ) . length > 0 ,
63+ RecommendationHandler . instance . isSuggestionVisible ( ) || session . getSuggestionState ( 0 ) === 'Showed' ,
7464 waitOptions
7565 )
7666 if ( ! ok ) {
77- assert . fail ( 'Telemetry failed to be emitted' )
67+ assert . fail (
68+ `Suggestions failed to become visible. Suggestion States: ${ JSON . stringify ( session . suggestionStates ) } `
69+ )
70+ }
71+ }
72+
73+ /**
74+ * Waits for a specific telemetry event to be emitted with the expected suggestion state.
75+ * It looks like there might be a potential race condition in codewhisperer causing telemetry
76+ * events to be emitted in different orders
77+ */
78+ async function waitForTelemetry ( metricName : string, suggestionState : string) {
79+ const ok = await waitUntil ( async ( ) => {
80+ const events = globals . telemetry . logger . query ( {
81+ metricName,
82+ } )
83+ return events . some ( ( event ) => event . codewhispererSuggestionState === suggestionState )
84+ } , waitOptions )
85+ const events = globals . telemetry . logger . query ( {
86+ metricName,
87+ } )
88+ if ( ! ok ) {
89+ assert. fail ( `Telemetry failed to be emitted. Current events: ${ JSON . stringify ( events ) } ` )
90+ }
91+ if ( events . length > 1 && events [ events . length - 1 ] . codewhispererSuggestionState !== suggestionState ) {
92+ assert. fail ( `Telemetry events were emitted in the wrong order. Current events: ${ JSON . stringify ( events ) } ` )
7893 }
7994 }
8095
@@ -119,10 +134,7 @@ describe('Amazon Q Inline', async function () {
119134
120135 assert . ok ( suggestionAccepted , 'Editor contents should have changed' )
121136
122- await waitForTelemetry ( )
123- assertTelemetry ( 'codewhisperer_userTriggerDecision' , {
124- codewhispererSuggestionState : 'Accept' ,
125- } )
137+ await waitForTelemetry ( 'codewhisperer_userTriggerDecision' , 'Accept' )
126138 } )
127139
128140 it ( `${ name } invoke reject` , async function ( ) {
@@ -131,11 +143,7 @@ describe('Amazon Q Inline', async function () {
131143
132144 // Contents haven't changed
133145 assert . deepStrictEqual ( vscode . window . activeTextEditor ?. document . getText ( ) , originalEditorContents )
134-
135- await waitForTelemetry ( )
136- assertTelemetry ( 'codewhisperer_userTriggerDecision' , {
137- codewhispererSuggestionState : 'Reject' ,
138- } )
146+ await waitForTelemetry ( 'codewhisperer_userTriggerDecision' , 'Reject' )
139147 } )
140148
141149 it ( `${ name } invoke discard` , async function ( ) {
0 commit comments