Skip to content

Commit

Permalink
update tests and pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zixlin7 committed Jan 23, 2025
1 parent 5254a56 commit 00574ae
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import { resetCodeWhispererGlobalVariables, createMockTextEditor } from 'aws-cor
import { assertTelemetryCurried } from 'aws-core-vscode/test'

describe('onAcceptance', function () {
const session = CodeWhispererSessionState.instance.getSession()
describe('onAcceptance', function () {
beforeEach(async function () {
const session = CodeWhispererSessionState.instance.getSession()
await resetCodeWhispererGlobalVariables()
session.reset()
})

afterEach(function () {
const session = CodeWhispererSessionState.instance.getSession()
sinon.restore()
session.reset()
})
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('onAcceptance', function () {
})

it('Should report telemetry that records this user decision event', async function () {
const session = CodeWhispererSessionState.instance.getSession()
const testStartUrl = 'testStartUrl'
sinon.stub(AuthUtil.instance, 'startUrl').value(testStartUrl)
const mockEditor = createMockTextEditor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import { globals } from 'aws-core-vscode/shared'
import { extensionVersion } from 'aws-core-vscode/shared'

describe('onInlineAcceptance', function () {
const session = CodeWhispererSessionState.instance.getSession()
describe('onInlineAcceptance', function () {
beforeEach(async function () {
const session = CodeWhispererSessionState.instance.getSession()
await resetCodeWhispererGlobalVariables()
session.reset()
})

afterEach(function () {
const session = CodeWhispererSessionState.instance.getSession()
sinon.restore()
session.reset()
})
Expand All @@ -50,6 +51,7 @@ describe('onInlineAcceptance', function () {
})

it('Should report telemetry that records this user decision event', async function () {
const session = CodeWhispererSessionState.instance.getSession()
await globals.globalState.update('CODEWHISPERER_USER_GROUP', {
version: extensionVersion,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { createMockDocument, resetCodeWhispererGlobalVariables } from 'aws-core-vscode/test'

describe('completionProviderService', function () {
const session = CodeWhispererSessionState.instance.getSession()
beforeEach(async function () {
await resetCodeWhispererGlobalVariables()
})
Expand All @@ -40,6 +39,7 @@ describe('completionProviderService', function () {

describe('getCompletionItem', function () {
it('should return targetCompletionItem given input', function () {
const session = CodeWhispererSessionState.instance.getSession()
session.startPos = new vscode.Position(0, 0)
RecommendationHandler.instance.requestId = 'mock_requestId_getCompletionItem'
session.sessionId = 'mock_sessionId_getCompletionItem'
Expand Down Expand Up @@ -96,6 +96,7 @@ describe('completionProviderService', function () {

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

it('should return empty completion items when recommendation is empty', async function () {
const session = CodeWhispererSessionState.instance.getSession()
session.recommendations = []
const mockPosition = new vscode.Position(14, 83)
const mockDocument = createMockDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import { createMockTextEditor, resetCodeWhispererGlobalVariables, createMockDocument } from 'aws-core-vscode/test'

describe('inlineCompletionService', function () {
const session = CodeWhispererSessionState.instance.getSession()
beforeEach(async function () {
await resetCodeWhispererGlobalVariables()
})
Expand All @@ -47,6 +46,7 @@ describe('inlineCompletionService', function () {
})

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

describe('clearInlineCompletionStates', function () {
it('should remove inline reference and recommendations', async function () {
const session = CodeWhispererSessionState.instance.getSession()
const fakeReferences = [
{
message: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
// import * as supplementalContextUtil from 'aws-core-vscode/codewhisperer'

describe('recommendationHandler', function () {
const session = CodeWhispererSessionState.instance.getSession()
const config: ConfigurationEntry = {
isShowMethodsEnabled: true,
isManualTriggerEnabled: true,
Expand Down Expand Up @@ -56,6 +55,7 @@ describe('recommendationHandler', function () {
})

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

it('should assign request id correctly', async function () {
const session = CodeWhispererSessionState.instance.getSession()
const mockServerResult = {
recommendations: [{ content: "print('Hello World!')" }, { content: '' }],
$response: {
Expand All @@ -100,7 +101,7 @@ describe('recommendationHandler', function () {
const handler = new RecommendationHandler()
sinon.stub(handler, 'getServerResponse').resolves(mockServerResult)
sinon.stub(handler, 'isCancellationRequested').returns(false)
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter', false)
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter', false)
assert.strictEqual(handler.requestId, 'test_request')
assert.strictEqual(session.sessionId, 'test_request')
assert.strictEqual(session.triggerType, 'AutoTrigger')
Expand Down Expand Up @@ -129,9 +130,10 @@ describe('recommendationHandler', function () {
strategy: 'empty',
})
sinon.stub(performance, 'now').returns(0.0)
const session = CodeWhispererSessionState.instance.getSession()
session.startPos = new vscode.Position(1, 0)
session.startCursorOffset = 2
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter')
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter')
const assertTelemetry = assertTelemetryCurried('codewhisperer_serviceInvocation')
assertTelemetry({
codewhispererRequestId: 'test_request',
Expand Down Expand Up @@ -168,10 +170,11 @@ describe('recommendationHandler', function () {
const handler = new RecommendationHandler()
sinon.stub(handler, 'getServerResponse').resolves(mockServerResult)
sinon.stub(performance, 'now').returns(0.0)
const session = CodeWhispererSessionState.instance.getSession()
session.startPos = new vscode.Position(1, 0)
session.requestIdList = ['test_request_empty']
session.startCursorOffset = 2
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, 'Enter')
await handler.getRecommendations(mockClient, mockEditor, 'AutoTrigger', config, session, 'Enter')
const assertTelemetry = assertTelemetryCurried('codewhisperer_userDecision')
assertTelemetry({
codewhispererRequestId: 'test_request_empty',
Expand All @@ -193,6 +196,7 @@ describe('recommendationHandler', function () {
sinon.restore()
})
it('should return true if any response is not empty', function () {
const session = CodeWhispererSessionState.instance.getSession()
const handler = new RecommendationHandler()
session.recommendations = [
{
Expand All @@ -205,12 +209,14 @@ describe('recommendationHandler', function () {
})

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

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

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

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

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

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

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

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

describe('on event change', async function () {
beforeEach(function () {
const session = CodeWhispererSessionState.instance.getSession()
const fakeReferences = [
{
message: '',
Expand All @@ -275,12 +285,14 @@ describe('recommendationHandler', function () {
})

it('should remove inline reference onEditorChange', async function () {
const session = CodeWhispererSessionState.instance.getSession()
session.sessionId = 'aSessionId'
RecommendationHandler.instance.requestId = 'aRequestId'
await RecommendationHandler.instance.onEditorChange()
assert.strictEqual(ReferenceInlineProvider.instance.refs.length, 0)
})
it('should remove inline reference onFocusChange', async function () {
const session = CodeWhispererSessionState.instance.getSession()
session.sessionId = 'aSessionId'
RecommendationHandler.instance.requestId = 'aRequestId'
await RecommendationHandler.instance.onFocusChange()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function aCompletion(): Completion {
}

describe('telemetryHelper', function () {
const session = CodeWhispererSessionState.instance.getSession()
describe('clientComponentLatency', function () {
let sut: TelemetryHelper

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

it('resetClientComponentLatencyTime should reset state variables', function () {
const session = CodeWhispererSessionState.instance.getSession()
session.invokeSuggestionStartTime = 100
session.preprocessEndTime = 200
session.sdkApiCallStartTime = 300
Expand Down Expand Up @@ -290,6 +290,7 @@ describe('telemetryHelper', function () {
})

it('Should call telemetry record for each recommendations with proper arguments', async function () {
const session = CodeWhispererSessionState.instance.getSession()
const telemetryHelper = new TelemetryHelper()
const response = [{ content: "print('Hello')" }]
const requestIdList = ['test_x', 'test_x', 'test_y']
Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/codewhisperer/commands/onInlineAcceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,17 @@ export async function onInlineAcceptance(acceptanceEntry: OnRecommendationAccept
}

RecommendationHandler.instance.reportUserDecisions(acceptanceEntry.acceptIndex)
if (acceptanceEntry.acceptIndex === 0) {
const nextSession = CodeWhispererSessionState.instance.getNextSession()
nextSession.startPos = acceptanceEntry.editor.selection.active
CodeWhispererSessionState.instance.setSession(nextSession)
if (nextSession.recommendations.length) {
await RecommendationHandler.instance.tryShowRecommendation()
}
await promoteNextSessionIfAvailable(acceptanceEntry)
}
}

async function promoteNextSessionIfAvailable(acceptanceEntry: OnRecommendationAcceptanceEntry) {
if (acceptanceEntry.acceptIndex === 0 && acceptanceEntry.editor) {
const nextSession = CodeWhispererSessionState.instance.getNextSession()
nextSession.startPos = acceptanceEntry.editor.selection.active
CodeWhispererSessionState.instance.setSession(nextSession)
if (nextSession.recommendations.length) {
await RecommendationHandler.instance.tryShowRecommendation()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class InlineCompletionService {
editor,
triggerType,
config,
session,
autoTriggerType,
true,
page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class RecommendationHandler {
editor: vscode.TextEditor,
triggerType: CodewhispererTriggerType,
config: ConfigurationEntry,
session: CodeWhispererSession,
autoTriggerType?: CodewhispererAutomatedTriggerType,
pagination: boolean = true,
page: number = 0,
Expand All @@ -171,7 +172,7 @@ export class RecommendationHandler {
let invocationResult: 'Succeeded' | 'Failed' = 'Failed'
let errorMessage: string | undefined = undefined
let errorCode: string | undefined = undefined
let currentSession = CodeWhispererSessionState.instance.getSession()
let currentSession = session
if (isNextSession) {
getLogger().debug('pre-fetching next recommendation for model routing')
currentSession = new CodeWhispererSession()
Expand Down Expand Up @@ -202,7 +203,6 @@ export class RecommendationHandler {
if (pagination && !generate) {
if (page === 0) {
if (isNextSession) {
const session = CodeWhispererSessionState.instance.getSession()
const request = session.requestContext.request as ListRecommendationsRequest
currentSession.requestContext = {
request: {
Expand Down Expand Up @@ -364,6 +364,7 @@ export class RecommendationHandler {
editor,
triggerType,
config,
currentSession,
autoTriggerType,
pagination,
page,
Expand Down Expand Up @@ -729,6 +730,7 @@ export class RecommendationHandler {
editor,
session.triggerType,
await this.getConfigEntry(),
session,
session.autoTriggerType,
true,
0,
Expand Down
Loading

0 comments on commit 00574ae

Please sign in to comment.