@@ -22,8 +22,6 @@ import { ClassifierTrigger } from '../../../codewhisperer/service/classifierTrig
22
22
import { CodeWhispererUserGroupSettings } from '../../../codewhisperer/util/userGroupUtil'
23
23
import * as CodeWhispererConstants from '../../../codewhisperer/models/constants'
24
24
25
- const performance = globalThis . performance ?? require ( 'perf_hooks' ) . performance
26
-
27
25
describe ( 'keyStrokeHandler' , function ( ) {
28
26
const config : ConfigurationEntry = {
29
27
isShowMethodsEnabled : true ,
@@ -72,147 +70,33 @@ describe('keyStrokeHandler', function () {
72
70
} )
73
71
74
72
it ( 'Should not call invokeAutomatedTrigger when changed text across multiple lines' , async function ( ) {
75
- const mockEditor = createMockTextEditor ( )
76
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
77
- mockEditor . document ,
78
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 1 , 0 ) ) ,
79
- '\nprint(n'
80
- )
81
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
82
- assert . ok ( ! invokeSpy . called )
83
- assert . ok ( ! startTimerSpy . called )
73
+ await testShouldInvoke ( '\nprint(n' , false )
84
74
} )
85
75
86
76
it ( 'Should not call invokeAutomatedTrigger when doing delete or undo (empty changed text)' , async function ( ) {
87
- const mockEditor = createMockTextEditor ( )
88
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
89
- mockEditor . document ,
90
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
91
- ''
92
- )
93
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
94
- assert . ok ( ! invokeSpy . called )
95
- assert . ok ( ! startTimerSpy . called )
96
- } )
97
-
98
- it ( 'Should call invokeAutomatedTrigger if previous text input is within 2 seconds but the new input is new line' , async function ( ) {
99
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
100
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
101
- mockEditor . document ,
102
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
103
- '\n'
104
- )
105
- RecommendationHandler . instance . lastInvocationTime = performance . now ( ) - 1500
106
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
107
- assert . ok ( invokeSpy . called )
108
- } )
109
-
110
- it ( 'Should call invokeAutomatedTrigger if previous text input is within 2 seconds but the new input is a specialcharacter' , async function ( ) {
111
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
112
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
113
- mockEditor . document ,
114
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
115
- '('
116
- )
117
- RecommendationHandler . instance . lastInvocationTime = performance . now ( ) - 1500
118
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
119
- assert . ok ( invokeSpy . called )
77
+ await testShouldInvoke ( '' , false )
120
78
} )
121
79
122
80
it ( 'Should call invokeAutomatedTrigger with Enter when inputing \n' , async function ( ) {
123
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
124
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
125
- mockEditor . document ,
126
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
127
- '\n'
128
- )
129
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
130
- invokeSpy ( 'Enter' , mockEditor , mockClient )
131
- assert . ok ( invokeSpy . called )
81
+ await testShouldInvoke ( '\n' , true )
132
82
} )
133
83
134
84
it ( 'Should call invokeAutomatedTrigger with Enter when inputing \r\n' , async function ( ) {
135
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
136
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
137
- mockEditor . document ,
138
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 2 ) ) ,
139
- '\r\n'
140
- )
141
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
142
- invokeSpy ( 'Enter' , mockEditor , mockClient )
143
- assert . ok ( invokeSpy . called )
85
+ await testShouldInvoke ( '\r\n' , true )
144
86
} )
145
87
146
88
it ( 'Should call invokeAutomatedTrigger with SpecialCharacter when inputing {' , async function ( ) {
147
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
148
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
149
- mockEditor . document ,
150
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
151
- '{'
152
- )
153
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
154
- invokeSpy ( 'SpecialCharacters' , mockEditor , mockClient )
155
- assert . ok ( invokeSpy . called )
156
- } )
157
-
158
- it ( 'Should call invokeAutomatedTrigger with SpecialCharacter when inputing spaces equivalent to \t' , async function ( ) {
159
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
160
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
161
- mockEditor . document ,
162
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
163
- ' '
164
- )
165
- EditorContext . updateTabSize ( 2 )
166
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
167
- invokeSpy ( 'SpecialCharacters' , mockEditor , mockClient )
168
- assert . ok ( invokeSpy . called )
169
- } )
170
-
171
- it ( 'Should not call invokeAutomatedTrigger with SpecialCharacter when inputing spaces not equivalent to \t' , async function ( ) {
172
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
173
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
174
- mockEditor . document ,
175
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
176
- ' '
177
- )
178
- EditorContext . updateTabSize ( 2 )
179
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
180
- assert . ok ( ! invokeSpy . called )
181
- } )
182
-
183
- it ( 'Should not start idle trigger timer when inputing special characters' , async function ( ) {
184
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
185
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
186
- mockEditor . document ,
187
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
188
- '('
189
- )
190
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
191
- assert . ok ( ! startTimerSpy . called )
89
+ await testShouldInvoke ( '{' , true )
192
90
} )
193
91
194
92
it ( 'Should not call invokeAutomatedTrigger for non-special characters for classifier language if classifier says no' , async function ( ) {
195
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
196
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
197
- mockEditor . document ,
198
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
199
- 'a'
200
- )
201
93
sinon . stub ( ClassifierTrigger . instance , 'shouldTriggerFromClassifier' ) . returns ( false )
202
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
203
- assert . ok ( ! invokeSpy . called )
94
+ await testShouldInvoke ( 'a' , false )
204
95
} )
205
96
206
97
it ( 'Should call invokeAutomatedTrigger for non-special characters for classifier language if classifier says yes' , async function ( ) {
207
- const mockEditor = createMockTextEditor ( 'function addTwo' , 'test.js' , 'javascript' )
208
- const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
209
- mockEditor . document ,
210
- new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
211
- 'a'
212
- )
213
98
sinon . stub ( ClassifierTrigger . instance , 'shouldTriggerFromClassifier' ) . returns ( true )
214
- await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
215
- assert . ok ( invokeSpy . called )
99
+ await testShouldInvoke ( 'a' , true )
216
100
} )
217
101
218
102
it ( 'Should skip invoking if there is immediate right context on the same line and not a single } for the user group' , async function ( ) {
@@ -243,28 +127,25 @@ describe('keyStrokeHandler', function () {
243
127
} ,
244
128
]
245
129
casesForSuppressTokenFilling . forEach ( async ( { rightContext, shouldInvoke } ) => {
246
- await testIfRightContextShouldInvoke (
247
- rightContext ,
248
- shouldInvoke ,
249
- CodeWhispererConstants . UserGroup . RightContext
250
- )
130
+ await testShouldInvoke ( '{' , shouldInvoke , rightContext , CodeWhispererConstants . UserGroup . RightContext )
251
131
} )
252
132
} )
253
133
254
134
it ( 'Should not skip invoking based on right context for control group' , async function ( ) {
255
- await testIfRightContextShouldInvoke ( 'add ', true , CodeWhispererConstants . UserGroup . Control )
135
+ await testShouldInvoke ( '{ ', true , 'add' )
256
136
} )
257
137
258
- async function testIfRightContextShouldInvoke (
259
- rightContext : string ,
138
+ async function testShouldInvoke (
139
+ input : string ,
260
140
shouldTrigger : boolean ,
261
- userGroup : CodeWhispererConstants . UserGroup
141
+ rightContext : string = '' ,
142
+ userGroup : CodeWhispererConstants . UserGroup = CodeWhispererConstants . UserGroup . Control
262
143
) {
263
- const mockEditor = createMockTextEditor ( rightContext , 'test.js' , 'javascript' , 1 , 1 )
144
+ const mockEditor = createMockTextEditor ( rightContext , 'test.js' , 'javascript' )
264
145
const mockEvent : vscode . TextDocumentChangeEvent = createTextDocumentChangeEvent (
265
146
mockEditor . document ,
266
147
new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 1 ) ) ,
267
- '{'
148
+ input
268
149
)
269
150
CodeWhispererUserGroupSettings . instance . userGroup = userGroup
270
151
await KeyStrokeHandler . instance . processKeyStroke ( mockEvent , mockEditor , mockClient , config )
0 commit comments