-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Context 1. End to end testing setup for the `auto-edits` 2. https://playwright.dev/docs/test-snapshots is used to detect the differences in the rendering for the auto-edit diff view 3. Closes https://linear.app/sourcegraph/issue/CODY-4570/implement-a-manual-trigger-command ## Test plan Added unit test and e2e tests
- Loading branch information
1 parent
38d0921
commit b06f903
Showing
15 changed files
with
937 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import dedent from 'dedent' | ||
import { getCurrentDocContext } from '../../completions/get-current-doc-context' | ||
import { documentAndPosition } from '../../completions/test-helpers' | ||
import { type CodeToReplaceData, getCurrentFilePromptComponents } from '../prompt/prompt-utils' | ||
|
||
interface CodeToReplaceTestOptions { | ||
maxPrefixLength: number | ||
maxSuffixLength: number | ||
maxPrefixLinesInArea: number | ||
maxSuffixLinesInArea: number | ||
codeToRewritePrefixLines: number | ||
codeToRewriteSuffixLines: number | ||
} | ||
|
||
export function createCodeToReplaceDataForTest( | ||
code: TemplateStringsArray, | ||
options: CodeToReplaceTestOptions, | ||
...values: unknown[] | ||
): CodeToReplaceData { | ||
const { document, position } = documentAndPosition(dedent(code, values)) | ||
const docContext = getCurrentDocContext({ | ||
document, | ||
position, | ||
maxPrefixLength: options.maxPrefixLength, | ||
maxSuffixLength: options.maxSuffixLength, | ||
}) | ||
|
||
return getCurrentFilePromptComponents({ | ||
docContext, | ||
position, | ||
document, | ||
maxPrefixLinesInArea: options.maxPrefixLinesInArea, | ||
maxSuffixLinesInArea: options.maxSuffixLinesInArea, | ||
codeToRewritePrefixLines: options.codeToRewritePrefixLines, | ||
codeToRewriteSuffixLines: options.codeToRewriteSuffixLines, | ||
}).codeToReplace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import dedent from 'dedent' | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
import type * as vscode from 'vscode' | ||
import { getCurrentDocContext } from '../../completions/get-current-doc-context' | ||
import { documentAndPosition } from '../../completions/test-helpers' | ||
import { getDecorationInfoFromPrediction } from '../autoedits-provider' | ||
import type { CodeToReplaceData } from '../prompt/prompt-utils' | ||
import { createCodeToReplaceDataForTest } from '../prompt/test-helper' | ||
import { AutoEditsDefaultRendererManager } from '../renderer/manager' | ||
import { DefaultDecorator } from './decorators/default-decorator' | ||
import type { AutoeditRendererManagerArgs } from './manager' | ||
|
||
function getCodeToReplaceForManager( | ||
code: TemplateStringsArray, | ||
...values: unknown[] | ||
): CodeToReplaceData { | ||
return createCodeToReplaceDataForTest( | ||
code, | ||
{ | ||
maxPrefixLength: 100, | ||
maxSuffixLength: 100, | ||
maxPrefixLinesInArea: 2, | ||
maxSuffixLinesInArea: 2, | ||
codeToRewritePrefixLines: 1, | ||
codeToRewriteSuffixLines: 1, | ||
}, | ||
...values | ||
) | ||
} | ||
|
||
describe('AutoEditsDefaultRendererManager', () => { | ||
const getAutoeditRendererManagerArgs = ( | ||
documentText: string, | ||
prediction: string | ||
): AutoeditRendererManagerArgs => { | ||
const { document, position } = documentAndPosition(documentText) | ||
const docContext = getCurrentDocContext({ | ||
document, | ||
position, | ||
maxPrefixLength: 100, | ||
maxSuffixLength: 100, | ||
}) | ||
const codeToReplaceData = getCodeToReplaceForManager`${documentText}` | ||
const decorationInfo = getDecorationInfoFromPrediction(document, prediction, codeToReplaceData) | ||
return { | ||
prediction, | ||
codeToReplaceData, | ||
document, | ||
position, | ||
docContext, | ||
decorationInfo, | ||
} | ||
} | ||
|
||
describe('maybeRenderDecorationsAndTryMakeInlineCompletionResponse', () => { | ||
let manager: AutoEditsDefaultRendererManager | ||
|
||
beforeEach(() => { | ||
manager = new AutoEditsDefaultRendererManager( | ||
(editor: vscode.TextEditor) => new DefaultDecorator(editor) | ||
) | ||
}) | ||
|
||
const assertInlineCompletionItems = ( | ||
items: vscode.InlineCompletionItem[], | ||
expectedCompletion: string | ||
) => { | ||
expect(items).toHaveLength(1) | ||
expect(items[0].insertText).toEqual(expectedCompletion) | ||
} | ||
|
||
it('should return single line inline completion when possible', async () => { | ||
const documentText = dedent`const a = 1 | ||
const b = 2 | ||
const c = 3 | ||
console█ | ||
function greet() { console.log("Hello") } | ||
const x = 10 | ||
console.log(x) | ||
console.log("end") | ||
` | ||
const prediction = dedent`const c = 3 | ||
console.log(a, b, c) | ||
function greet() { console.log("Hello") } | ||
` | ||
const args = getAutoeditRendererManagerArgs(documentText, prediction) | ||
const result = await manager.maybeRenderDecorationsAndTryMakeInlineCompletionResponse(args) | ||
expect(result).toBeDefined() | ||
assertInlineCompletionItems( | ||
result.inlineCompletions!, | ||
dedent` | ||
console.log(a, b, c) | ||
` | ||
) | ||
}) | ||
|
||
it('should return multi line inline completion when possible', async () => { | ||
const documentText = dedent`const a = 1 | ||
const b = 2 | ||
const c = 3 | ||
console█ | ||
function greet() { console.log("Hello") } | ||
const x = 10 | ||
console.log(x) | ||
console.log("end") | ||
` | ||
const prediction = dedent`const c = 3 | ||
console.log(a, b, c) | ||
const d = 10 | ||
const e = 20 | ||
function greet() { console.log("Hello") } | ||
` | ||
const args = getAutoeditRendererManagerArgs(documentText, prediction) | ||
const result = await manager.maybeRenderDecorationsAndTryMakeInlineCompletionResponse(args) | ||
expect(result).toBeDefined() | ||
assertInlineCompletionItems( | ||
result.inlineCompletions!, | ||
dedent` | ||
console.log(a, b, c) | ||
const d = 10 | ||
const e = 20 | ||
` | ||
) | ||
}) | ||
|
||
it('should return single line inline completion when the suffix is present on same line', async () => { | ||
const documentText = dedent`const a = 1 | ||
const b = 2 | ||
const c = 3 | ||
console█c) | ||
function greet() { console.log("Hello") } | ||
const x = 10 | ||
console.log(x) | ||
console.log("end") | ||
` | ||
const prediction = dedent`const c = 3 | ||
console.log(a, b, c) | ||
function greet() { console.log("Hello") } | ||
` | ||
const args = getAutoeditRendererManagerArgs(documentText, prediction) | ||
const result = await manager.maybeRenderDecorationsAndTryMakeInlineCompletionResponse(args) | ||
expect(result).toBeDefined() | ||
assertInlineCompletionItems( | ||
result.inlineCompletions!, | ||
dedent` | ||
console.log(a, b, c) | ||
` | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.