Skip to content

Commit a4a1cf8

Browse files
authored
codewhisperer: refactor keyStrokeHandler test and remove outdated tests (#3897)
codewhisperer: refactor keyStokeHandler test and remove outdated tests
1 parent 7d0d8f7 commit a4a1cf8

File tree

1 file changed

+15
-134
lines changed

1 file changed

+15
-134
lines changed

src/test/codewhisperer/service/keyStrokeHandler.test.ts

Lines changed: 15 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import { ClassifierTrigger } from '../../../codewhisperer/service/classifierTrig
2222
import { CodeWhispererUserGroupSettings } from '../../../codewhisperer/util/userGroupUtil'
2323
import * as CodeWhispererConstants from '../../../codewhisperer/models/constants'
2424

25-
const performance = globalThis.performance ?? require('perf_hooks').performance
26-
2725
describe('keyStrokeHandler', function () {
2826
const config: ConfigurationEntry = {
2927
isShowMethodsEnabled: true,
@@ -72,147 +70,33 @@ describe('keyStrokeHandler', function () {
7270
})
7371

7472
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)
8474
})
8575

8676
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)
12078
})
12179

12280
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)
13282
})
13383

13484
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)
14486
})
14587

14688
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)
19290
})
19391

19492
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-
)
20193
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)
20495
})
20596

20697
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-
)
21398
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)
216100
})
217101

218102
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 () {
243127
},
244128
]
245129
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)
251131
})
252132
})
253133

254134
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')
256136
})
257137

258-
async function testIfRightContextShouldInvoke(
259-
rightContext: string,
138+
async function testShouldInvoke(
139+
input: string,
260140
shouldTrigger: boolean,
261-
userGroup: CodeWhispererConstants.UserGroup
141+
rightContext: string = '',
142+
userGroup: CodeWhispererConstants.UserGroup = CodeWhispererConstants.UserGroup.Control
262143
) {
263-
const mockEditor = createMockTextEditor(rightContext, 'test.js', 'javascript', 1, 1)
144+
const mockEditor = createMockTextEditor(rightContext, 'test.js', 'javascript')
264145
const mockEvent: vscode.TextDocumentChangeEvent = createTextDocumentChangeEvent(
265146
mockEditor.document,
266147
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 1)),
267-
'{'
148+
input
268149
)
269150
CodeWhispererUserGroupSettings.instance.userGroup = userGroup
270151
await KeyStrokeHandler.instance.processKeyStroke(mockEvent, mockEditor, mockClient, config)

0 commit comments

Comments
 (0)