6
6
import * as vscode from 'vscode'
7
7
import assert from 'assert'
8
8
import {
9
- assertTelemetry ,
10
9
closeAllEditors ,
11
10
getTestWindow ,
12
11
registerAuthHook ,
@@ -15,7 +14,7 @@ import {
15
14
toTextEditor ,
16
15
using ,
17
16
} from 'aws-core-vscode/test'
18
- import { RecommendationHandler , RecommendationService } from 'aws-core-vscode/codewhisperer'
17
+ import { RecommendationHandler , RecommendationService , session } from 'aws-core-vscode/codewhisperer'
19
18
import { Commands , globals , sleep , waitUntil } from 'aws-core-vscode/shared'
20
19
import { loginToIdC } from '../amazonq/utils/setup'
21
20
@@ -50,31 +49,47 @@ describe('Amazon Q Inline', async function () {
50
49
const textContents =
51
50
contents ??
52
51
`function fib() {
53
-
54
-
52
+
53
+
55
54
}`
56
55
await toTextEditor ( textContents , fileName , tempFolder , {
57
56
selection : new vscode . Range ( new vscode . Position ( 1 , 4 ) , new vscode . Position ( 1 , 4 ) ) ,
58
57
} )
59
58
}
60
59
61
60
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 ( ) {
69
61
const ok = await waitUntil (
70
62
async ( ) =>
71
- globals . telemetry . logger . query ( {
72
- metricName : 'codewhisperer_userTriggerDecision' ,
73
- } ) . length > 0 ,
63
+ RecommendationHandler . instance . isSuggestionVisible ( ) || session . getSuggestionState ( 0 ) === 'Showed' ,
74
64
waitOptions
75
65
)
76
66
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 ) } ` )
78
93
}
79
94
}
80
95
@@ -119,10 +134,7 @@ describe('Amazon Q Inline', async function () {
119
134
120
135
assert . ok ( suggestionAccepted , 'Editor contents should have changed' )
121
136
122
- await waitForTelemetry ( )
123
- assertTelemetry ( 'codewhisperer_userTriggerDecision' , {
124
- codewhispererSuggestionState : 'Accept' ,
125
- } )
137
+ await waitForTelemetry ( 'codewhisperer_userTriggerDecision' , 'Accept' )
126
138
} )
127
139
128
140
it ( `${ name } invoke reject` , async function ( ) {
@@ -131,11 +143,7 @@ describe('Amazon Q Inline', async function () {
131
143
132
144
// Contents haven't changed
133
145
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' )
139
147
} )
140
148
141
149
it ( `${ name } invoke discard` , async function ( ) {
0 commit comments