Skip to content

Commit 00574ae

Browse files
committed
update tests and pr feedback
1 parent 5254a56 commit 00574ae

File tree

11 files changed

+107
-72
lines changed

11 files changed

+107
-72
lines changed

packages/amazonq/test/unit/codewhisperer/commands/onAcceptance.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import { resetCodeWhispererGlobalVariables, createMockTextEditor } from 'aws-cor
1818
import { assertTelemetryCurried } from 'aws-core-vscode/test'
1919

2020
describe('onAcceptance', function () {
21-
const session = CodeWhispererSessionState.instance.getSession()
2221
describe('onAcceptance', function () {
2322
beforeEach(async function () {
23+
const session = CodeWhispererSessionState.instance.getSession()
2424
await resetCodeWhispererGlobalVariables()
2525
session.reset()
2626
})
2727

2828
afterEach(function () {
29+
const session = CodeWhispererSessionState.instance.getSession()
2930
sinon.restore()
3031
session.reset()
3132
})
@@ -71,6 +72,7 @@ describe('onAcceptance', function () {
7172
})
7273

7374
it('Should report telemetry that records this user decision event', async function () {
75+
const session = CodeWhispererSessionState.instance.getSession()
7476
const testStartUrl = 'testStartUrl'
7577
sinon.stub(AuthUtil.instance, 'startUrl').value(testStartUrl)
7678
const mockEditor = createMockTextEditor()

packages/amazonq/test/unit/codewhisperer/commands/onInlineAcceptance.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import { globals } from 'aws-core-vscode/shared'
1818
import { extensionVersion } from 'aws-core-vscode/shared'
1919

2020
describe('onInlineAcceptance', function () {
21-
const session = CodeWhispererSessionState.instance.getSession()
2221
describe('onInlineAcceptance', function () {
2322
beforeEach(async function () {
23+
const session = CodeWhispererSessionState.instance.getSession()
2424
await resetCodeWhispererGlobalVariables()
2525
session.reset()
2626
})
2727

2828
afterEach(function () {
29+
const session = CodeWhispererSessionState.instance.getSession()
2930
sinon.restore()
3031
session.reset()
3132
})
@@ -50,6 +51,7 @@ describe('onInlineAcceptance', function () {
5051
})
5152

5253
it('Should report telemetry that records this user decision event', async function () {
54+
const session = CodeWhispererSessionState.instance.getSession()
5355
await globals.globalState.update('CODEWHISPERER_USER_GROUP', {
5456
version: extensionVersion,
5557
})

packages/amazonq/test/unit/codewhisperer/service/completionProvider.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
import { createMockDocument, resetCodeWhispererGlobalVariables } from 'aws-core-vscode/test'
1818

1919
describe('completionProviderService', function () {
20-
const session = CodeWhispererSessionState.instance.getSession()
2120
beforeEach(async function () {
2221
await resetCodeWhispererGlobalVariables()
2322
})
@@ -40,6 +39,7 @@ describe('completionProviderService', function () {
4039

4140
describe('getCompletionItem', function () {
4241
it('should return targetCompletionItem given input', function () {
42+
const session = CodeWhispererSessionState.instance.getSession()
4343
session.startPos = new vscode.Position(0, 0)
4444
RecommendationHandler.instance.requestId = 'mock_requestId_getCompletionItem'
4545
session.sessionId = 'mock_sessionId_getCompletionItem'
@@ -96,6 +96,7 @@ describe('completionProviderService', function () {
9696

9797
describe('getCompletionItems', function () {
9898
it('should return completion items for each non-empty recommendation', async function () {
99+
const session = CodeWhispererSessionState.instance.getSession()
99100
session.recommendations = [
100101
{ content: "\n\t\tconsole.log('Hello world!');\n\t}" },
101102
{ content: '\nvar a = 10' },
@@ -107,6 +108,7 @@ describe('completionProviderService', function () {
107108
})
108109

109110
it('should return empty completion items when recommendation is empty', async function () {
111+
const session = CodeWhispererSessionState.instance.getSession()
110112
session.recommendations = []
111113
const mockPosition = new vscode.Position(14, 83)
112114
const mockDocument = createMockDocument()

packages/amazonq/test/unit/codewhisperer/service/inlineCompletionService.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
import { createMockTextEditor, resetCodeWhispererGlobalVariables, createMockDocument } from 'aws-core-vscode/test'
2323

2424
describe('inlineCompletionService', function () {
25-
const session = CodeWhispererSessionState.instance.getSession()
2625
beforeEach(async function () {
2726
await resetCodeWhispererGlobalVariables()
2827
})
@@ -47,6 +46,7 @@ describe('inlineCompletionService', function () {
4746
})
4847

4948
it('should call checkAndResetCancellationTokens before showing inline and next token to be null', async function () {
49+
const session = CodeWhispererSessionState.instance.getSession()
5050
const mockEditor = createMockTextEditor()
5151
sinon.stub(RecommendationHandler.instance, 'getRecommendations').resolves({
5252
result: 'Succeeded',
@@ -71,6 +71,7 @@ describe('inlineCompletionService', function () {
7171

7272
describe('clearInlineCompletionStates', function () {
7373
it('should remove inline reference and recommendations', async function () {
74+
const session = CodeWhispererSessionState.instance.getSession()
7475
const fakeReferences = [
7576
{
7677
message: '',

packages/amazonq/test/unit/codewhisperer/service/recommendationHandler.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
// import * as supplementalContextUtil from 'aws-core-vscode/codewhisperer'
2727

2828
describe('recommendationHandler', function () {
29-
const session = CodeWhispererSessionState.instance.getSession()
3029
const config: ConfigurationEntry = {
3130
isShowMethodsEnabled: true,
3231
isManualTriggerEnabled: true,
@@ -56,6 +55,7 @@ describe('recommendationHandler', function () {
5655
})
5756

5857
it('should assign correct recommendations given input', async function () {
58+
const session = CodeWhispererSessionState.instance.getSession()
5959
assert.strictEqual(CodeWhispererCodeCoverageTracker.instances.size, 0)
6060
assert.strictEqual(
6161
CodeWhispererCodeCoverageTracker.getTracker(mockEditor.document.languageId)?.serviceInvocationCount,
@@ -75,7 +75,7 @@ describe('recommendationHandler', function () {
7575
}
7676
const handler = new RecommendationHandler()
7777
sinon.stub(handler, 'getServerResponse').resolves(mockServerResult)
78-
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter', false)
78+
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter', false)
7979
const actual = session.recommendations
8080
const expected: RecommendationsList = [{ content: "print('Hello World!')" }, { content: '' }]
8181
assert.deepStrictEqual(actual, expected)
@@ -86,6 +86,7 @@ describe('recommendationHandler', function () {
8686
})
8787

8888
it('should assign request id correctly', async function () {
89+
const session = CodeWhispererSessionState.instance.getSession()
8990
const mockServerResult = {
9091
recommendations: [{ content: "print('Hello World!')" }, { content: '' }],
9192
$response: {
@@ -100,7 +101,7 @@ describe('recommendationHandler', function () {
100101
const handler = new RecommendationHandler()
101102
sinon.stub(handler, 'getServerResponse').resolves(mockServerResult)
102103
sinon.stub(handler, 'isCancellationRequested').returns(false)
103-
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter', false)
104+
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter', false)
104105
assert.strictEqual(handler.requestId, 'test_request')
105106
assert.strictEqual(session.sessionId, 'test_request')
106107
assert.strictEqual(session.triggerType, 'AutoTrigger')
@@ -129,9 +130,10 @@ describe('recommendationHandler', function () {
129130
strategy: 'empty',
130131
})
131132
sinon.stub(performance, 'now').returns(0.0)
133+
const session = CodeWhispererSessionState.instance.getSession()
132134
session.startPos = new vscode.Position(1, 0)
133135
session.startCursorOffset = 2
134-
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter')
136+
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter')
135137
const assertTelemetry = assertTelemetryCurried('codewhisperer_serviceInvocation')
136138
assertTelemetry({
137139
codewhispererRequestId: 'test_request',
@@ -168,10 +170,11 @@ describe('recommendationHandler', function () {
168170
const handler = new RecommendationHandler()
169171
sinon.stub(handler, 'getServerResponse').resolves(mockServerResult)
170172
sinon.stub(performance, 'now').returns(0.0)
173+
const session = CodeWhispererSessionState.instance.getSession()
171174
session.startPos = new vscode.Position(1, 0)
172175
session.requestIdList = ['test_request_empty']
173176
session.startCursorOffset = 2
174-
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter')
177+
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter')
175178
const assertTelemetry = assertTelemetryCurried('codewhisperer_userDecision')
176179
assertTelemetry({
177180
codewhispererRequestId: 'test_request_empty',
@@ -193,6 +196,7 @@ describe('recommendationHandler', function () {
193196
sinon.restore()
194197
})
195198
it('should return true if any response is not empty', function () {
199+
const session = CodeWhispererSessionState.instance.getSession()
196200
const handler = new RecommendationHandler()
197201
session.recommendations = [
198202
{
@@ -205,12 +209,14 @@ describe('recommendationHandler', function () {
205209
})
206210

207211
it('should return false if response is empty', function () {
212+
const session = CodeWhispererSessionState.instance.getSession()
208213
const handler = new RecommendationHandler()
209214
session.recommendations = []
210215
assert.ok(!handler.isValidResponse())
211216
})
212217

213218
it('should return false if all response has no string length', function () {
219+
const session = CodeWhispererSessionState.instance.getSession()
214220
const handler = new RecommendationHandler()
215221
session.recommendations = [{ content: '' }, { content: '' }]
216222
assert.ok(!handler.isValidResponse())
@@ -223,6 +229,7 @@ describe('recommendationHandler', function () {
223229
})
224230

225231
it('should set the completion type to block given a multi-line suggestion', function () {
232+
const session = CodeWhispererSessionState.instance.getSession()
226233
session.setCompletionType(0, { content: 'test\n\n \t\r\nanother test' })
227234
assert.strictEqual(session.getCompletionType(0), 'Block')
228235

@@ -234,6 +241,7 @@ describe('recommendationHandler', function () {
234241
})
235242

236243
it('should set the completion type to line given a single-line suggestion', function () {
244+
const session = CodeWhispererSessionState.instance.getSession()
237245
session.setCompletionType(0, { content: 'test' })
238246
assert.strictEqual(session.getCompletionType(0), 'Line')
239247

@@ -242,6 +250,7 @@ describe('recommendationHandler', function () {
242250
})
243251

244252
it('should set the completion type to line given a multi-line completion but only one-lien of non-blank sequence', function () {
253+
const session = CodeWhispererSessionState.instance.getSession()
245254
session.setCompletionType(0, { content: 'test\n\t' })
246255
assert.strictEqual(session.getCompletionType(0), 'Line')
247256

@@ -258,6 +267,7 @@ describe('recommendationHandler', function () {
258267

259268
describe('on event change', async function () {
260269
beforeEach(function () {
270+
const session = CodeWhispererSessionState.instance.getSession()
261271
const fakeReferences = [
262272
{
263273
message: '',
@@ -275,12 +285,14 @@ describe('recommendationHandler', function () {
275285
})
276286

277287
it('should remove inline reference onEditorChange', async function () {
288+
const session = CodeWhispererSessionState.instance.getSession()
278289
session.sessionId = 'aSessionId'
279290
RecommendationHandler.instance.requestId = 'aRequestId'
280291
await RecommendationHandler.instance.onEditorChange()
281292
assert.strictEqual(ReferenceInlineProvider.instance.refs.length, 0)
282293
})
283294
it('should remove inline reference onFocusChange', async function () {
295+
const session = CodeWhispererSessionState.instance.getSession()
284296
session.sessionId = 'aSessionId'
285297
RecommendationHandler.instance.requestId = 'aRequestId'
286298
await RecommendationHandler.instance.onFocusChange()

packages/amazonq/test/unit/codewhisperer/util/telemetryHelper.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function aCompletion(): Completion {
3939
}
4040

4141
describe('telemetryHelper', function () {
42-
const session = CodeWhispererSessionState.instance.getSession()
4342
describe('clientComponentLatency', function () {
4443
let sut: TelemetryHelper
4544

@@ -52,6 +51,7 @@ describe('telemetryHelper', function () {
5251
})
5352

5453
it('resetClientComponentLatencyTime should reset state variables', function () {
54+
const session = CodeWhispererSessionState.instance.getSession()
5555
session.invokeSuggestionStartTime = 100
5656
session.preprocessEndTime = 200
5757
session.sdkApiCallStartTime = 300
@@ -290,6 +290,7 @@ describe('telemetryHelper', function () {
290290
})
291291

292292
it('Should call telemetry record for each recommendations with proper arguments', async function () {
293+
const session = CodeWhispererSessionState.instance.getSession()
293294
const telemetryHelper = new TelemetryHelper()
294295
const response = [{ content: "print('Hello')" }]
295296
const requestIdList = ['test_x', 'test_x', 'test_y']

packages/core/src/codewhisperer/commands/onInlineAcceptance.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,17 @@ export async function onInlineAcceptance(acceptanceEntry: OnRecommendationAccept
143143
}
144144

145145
RecommendationHandler.instance.reportUserDecisions(acceptanceEntry.acceptIndex)
146-
if (acceptanceEntry.acceptIndex === 0) {
147-
const nextSession = CodeWhispererSessionState.instance.getNextSession()
148-
nextSession.startPos = acceptanceEntry.editor.selection.active
149-
CodeWhispererSessionState.instance.setSession(nextSession)
150-
if (nextSession.recommendations.length) {
151-
await RecommendationHandler.instance.tryShowRecommendation()
152-
}
146+
await promoteNextSessionIfAvailable(acceptanceEntry)
147+
}
148+
}
149+
150+
async function promoteNextSessionIfAvailable(acceptanceEntry: OnRecommendationAcceptanceEntry) {
151+
if (acceptanceEntry.acceptIndex === 0 && acceptanceEntry.editor) {
152+
const nextSession = CodeWhispererSessionState.instance.getNextSession()
153+
nextSession.startPos = acceptanceEntry.editor.selection.active
154+
CodeWhispererSessionState.instance.setSession(nextSession)
155+
if (nextSession.recommendations.length) {
156+
await RecommendationHandler.instance.tryShowRecommendation()
153157
}
154158
}
155159
}

packages/core/src/codewhisperer/service/inlineCompletionService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class InlineCompletionService {
128128
editor,
129129
triggerType,
130130
config,
131+
session,
131132
autoTriggerType,
132133
true,
133134
page

packages/core/src/codewhisperer/service/recommendationHandler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class RecommendationHandler {
162162
editor: vscode.TextEditor,
163163
triggerType: CodewhispererTriggerType,
164164
config: ConfigurationEntry,
165+
session: CodeWhispererSession,
165166
autoTriggerType?: CodewhispererAutomatedTriggerType,
166167
pagination: boolean = true,
167168
page: number = 0,
@@ -171,7 +172,7 @@ export class RecommendationHandler {
171172
let invocationResult: 'Succeeded' | 'Failed' = 'Failed'
172173
let errorMessage: string | undefined = undefined
173174
let errorCode: string | undefined = undefined
174-
let currentSession = CodeWhispererSessionState.instance.getSession()
175+
let currentSession = session
175176
if (isNextSession) {
176177
getLogger().debug('pre-fetching next recommendation for model routing')
177178
currentSession = new CodeWhispererSession()
@@ -202,7 +203,6 @@ export class RecommendationHandler {
202203
if (pagination && !generate) {
203204
if (page === 0) {
204205
if (isNextSession) {
205-
const session = CodeWhispererSessionState.instance.getSession()
206206
const request = session.requestContext.request as ListRecommendationsRequest
207207
currentSession.requestContext = {
208208
request: {
@@ -364,6 +364,7 @@ export class RecommendationHandler {
364364
editor,
365365
triggerType,
366366
config,
367+
currentSession,
367368
autoTriggerType,
368369
pagination,
369370
page,
@@ -729,6 +730,7 @@ export class RecommendationHandler {
729730
editor,
730731
session.triggerType,
731732
await this.getConfigEntry(),
733+
session,
732734
session.autoTriggerType,
733735
true,
734736
0,

0 commit comments

Comments
 (0)