Skip to content

Commit

Permalink
Add testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Aug 30, 2023
1 parent cd735aa commit 1cd432a
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions vscode/src/completions/vscodeInlineCompletionItemProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import dedent from 'dedent'
import { describe, expect, test, vi } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import * as vscode from 'vscode'

import { FeatureFlagProvider } from '@sourcegraph/cody-shared/src/experimentation/FeatureFlagProvider'
import { RateLimitError } from '@sourcegraph/cody-shared/src/sourcegraph-api/errors'
import { SourcegraphGraphQLAPIClient } from '@sourcegraph/cody-shared/src/sourcegraph-api/graphql'

import { vsCodeMocks } from '../testutils/mocks'
Expand Down Expand Up @@ -77,7 +78,7 @@ class MockableInlineCompletionItemProvider extends InlineCompletionItemProvider
}

describe('InlineCompletionItemProvider', () => {
test('returns results that span the whole line', async () => {
it('returns results that span the whole line', async () => {
const { document, position } = documentAndPosition('const foo = █', 'typescript')
const fn = vi.fn(getInlineCompletions).mockResolvedValue({
logId: '1',
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('InlineCompletionItemProvider', () => {
`)
})

test('saves lastInlineCompletionResult', async () => {
it('saves lastInlineCompletionResult', async () => {
const { document, position } = documentAndPosition(
dedent`
const foo = █
Expand Down Expand Up @@ -174,7 +175,7 @@ describe('InlineCompletionItemProvider', () => {
})

describe('logger', () => {
test('logs a completion as shown', async () => {
it('logs a completion as shown', async () => {
const spy = vi.spyOn(CompletionLogger, 'suggested')

const { document, position } = documentAndPosition('const foo = █', 'typescript')
Expand All @@ -190,7 +191,7 @@ describe('InlineCompletionItemProvider', () => {
expect(spy).toHaveBeenCalled()
})

test('does not log a completion when the abort handler was triggered after a network fetch', async () => {
it('does not log a completion when the abort handler was triggered after a network fetch', async () => {
const spy = vi.spyOn(CompletionLogger, 'suggested')

let onCancel = () => {}
Expand Down Expand Up @@ -222,7 +223,7 @@ describe('InlineCompletionItemProvider', () => {
expect(spy).not.toHaveBeenCalled()
})

test('does not log a completion if it does not overlap the completion popup', async () => {
it('does not log a completion if it does not overlap the completion popup', async () => {
const spy = vi.spyOn(CompletionLogger, 'suggested')

const { document, position } = documentAndPosition('console.█', 'typescript')
Expand All @@ -241,7 +242,7 @@ describe('InlineCompletionItemProvider', () => {
expect(spy).not.toHaveBeenCalled()
})

test('log a completion if the suffix is inside the completion', async () => {
it('log a completion if the suffix is inside the completion', async () => {
const spy = vi.spyOn(CompletionLogger, 'suggested')

const { document, position } = documentAndPosition('const a = [1, █];', 'typescript')
Expand All @@ -257,7 +258,7 @@ describe('InlineCompletionItemProvider', () => {
expect(spy).toHaveBeenCalled()
})

test('does not log a completion if the suffix does not match', async () => {
it('does not log a completion if the suffix does not match', async () => {
const spy = vi.spyOn(CompletionLogger, 'suggested')

const { document, position } = documentAndPosition('const a = [1, █)(123);', 'typescript')
Expand All @@ -275,7 +276,7 @@ describe('InlineCompletionItemProvider', () => {
})

describe('completeSuggestWidgetSelection', () => {
test('appends the current selected widget item to the doc context for the completer and removes the injected prefix from the result', async () => {
it('appends the current selected widget item to the doc context for the completer and removes the injected prefix from the result', async () => {
const { document, position } = documentAndPosition(
dedent`
function foo() {
Expand Down Expand Up @@ -331,7 +332,7 @@ describe('InlineCompletionItemProvider', () => {
`)
})

test('does not trigger a completion request if the current document context would not allow a suggestion to be shown', async () => {
it('does not trigger a completion request if the current document context would not allow a suggestion to be shown', async () => {
// This case happens when the selected item in the dropdown does not start with the
// exact characters that are already in the document.
// Here, the user has `console.l` in the document but the selected item is `dir`. There
Expand Down Expand Up @@ -362,4 +363,62 @@ describe('InlineCompletionItemProvider', () => {
expect(items).toBe(null)
})
})

describe('error reporting', () => {
it('reports rate limit errors to the user once', async () => {
const { document, position } = documentAndPosition('█')
const fn = vi.fn(getInlineCompletions).mockRejectedValue(new RateLimitError('rate limited oh no', 1234))
const addError = vi.fn()
const provider = new MockableInlineCompletionItemProvider(fn, { statusBar: { addError } as any })

await expect(provider.provideInlineCompletionItems(document, position, DUMMY_CONTEXT)).rejects.toThrow(
'rate limited oh no'
)
expect(addError).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Cody Autocomplete Disabled Due to Rate Limit',
description: "You've used all 1234 daily autocompletions.",
})
)

await expect(provider.provideInlineCompletionItems(document, position, DUMMY_CONTEXT)).rejects.toThrow(
'rate limited oh no'
)
expect(addError).toHaveBeenCalledTimes(1)
})

it('reports unexpected errors grouped by their message once', async () => {
const { document, position } = documentAndPosition('█')
let error = new Error('unexpected')
const fn = vi.fn(getInlineCompletions).mockImplementation(() => Promise.reject(error))
const addError = vi.fn()
const provider = new MockableInlineCompletionItemProvider(fn, { statusBar: { addError } as any })

await expect(provider.provideInlineCompletionItems(document, position, DUMMY_CONTEXT)).rejects.toThrow(
'unexpected'
)
expect(addError).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Cody Autocomplete Encountered an Unexpected Error',
description: 'unexpected',
})
)

await expect(provider.provideInlineCompletionItems(document, position, DUMMY_CONTEXT)).rejects.toThrow(
'unexpected'
)
expect(addError).toHaveBeenCalledTimes(1)

error = new Error('different')
await expect(provider.provideInlineCompletionItems(document, position, DUMMY_CONTEXT)).rejects.toThrow(
'different'
)
expect(addError).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Cody Autocomplete Encountered an Unexpected Error',
description: 'different',
})
)
})
})
})

0 comments on commit 1cd432a

Please sign in to comment.