Skip to content

Commit 4ebaef1

Browse files
committed
add unit test
1 parent 8d09ddb commit 4ebaef1

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Enable inline completion in Jupyter Notebook"
4+
}

packages/amazonq/.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
1414
"env": {
1515
"SSMDOCUMENT_LANGUAGESERVER_PORT": "6010",
16-
"WEBPACK_DEVELOPER_SERVER": "http://localhost:8080"
16+
"WEBPACK_DEVELOPER_SERVER": "http://localhost:8080",
1717
// Below allows for overrides used during development
18-
// "__AMAZONQLSP_PATH": "${workspaceFolder}/../../../language-servers/app/aws-lsp-codewhisperer-runtimes/out/agent-standalone.js",
19-
// "__AMAZONQLSP_UI": "${workspaceFolder}/../../../language-servers/chat-client/build/amazonq-ui.js"
18+
"__AMAZONQLSP_PATH": "${workspaceFolder}/../../../language-servers/app/aws-lsp-codewhisperer-runtimes/out/agent-standalone.js",
19+
"__AMAZONQLSP_UI": "${workspaceFolder}/../../../language-servers/chat-client/build/amazonq-ui.js"
2020
},
2121
"envFile": "${workspaceFolder}/.local.env",
2222
"outFiles": ["${workspaceFolder}/dist/**/*.js", "${workspaceFolder}/../core/dist/**/*.js"],

packages/amazonq/src/app/inline/notebookUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function extractFileContextInNotebooks(
8080
): InlineCompletionWithReferencesParams['fileContextOverride'] | undefined {
8181
let caretLeftFileContext = ''
8282
let caretRightFileContext = ''
83-
let languageName = runtimeLanguageContext.normalizeLanguage(document.languageId) ?? document.languageId
83+
const languageName = runtimeLanguageContext.normalizeLanguage(document.languageId) ?? document.languageId
8484
if (document.uri.scheme === 'vscode-notebook-cell') {
8585
const notebook = getEnclosingNotebook(document)
8686
if (notebook) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as vscode from 'vscode'
7+
import * as assert from 'assert'
8+
import { createMockDocument } from 'aws-core-vscode/test'
9+
import { convertCellContent, getNotebookContext } from '../../../../src/app/inline/notebookUtil'
10+
import { CodeWhispererConstants } from 'aws-core-vscode/codewhisperer'
11+
12+
export function createNotebookCell(
13+
document: vscode.TextDocument = createMockDocument('def example():\n return "test"'),
14+
kind: vscode.NotebookCellKind = vscode.NotebookCellKind.Code,
15+
notebook: vscode.NotebookDocument = {} as any,
16+
index: number = 0,
17+
outputs: vscode.NotebookCellOutput[] = [],
18+
metadata: { readonly [key: string]: any } = {},
19+
executionSummary?: vscode.NotebookCellExecutionSummary
20+
): vscode.NotebookCell {
21+
return {
22+
document,
23+
kind,
24+
notebook,
25+
index,
26+
outputs,
27+
metadata,
28+
executionSummary,
29+
}
30+
}
31+
32+
describe('Notebook Util', function () {
33+
describe('convertCellContent', function () {
34+
it('should return code cell content as-is', function () {
35+
const codeCell = createNotebookCell(
36+
createMockDocument('def example():\n return "test"'),
37+
vscode.NotebookCellKind.Code
38+
)
39+
const result = convertCellContent(codeCell)
40+
assert.strictEqual(result, 'def example():\n return "test"')
41+
})
42+
43+
it('should convert markdown cell content to comments for Python', function () {
44+
const markdownCell = createNotebookCell(
45+
createMockDocument('# Heading\nSome text'),
46+
vscode.NotebookCellKind.Markup
47+
)
48+
const result = convertCellContent(markdownCell)
49+
assert.strictEqual(result, '# # Heading\n# Some text')
50+
})
51+
})
52+
53+
describe('getNotebookContext', function () {
54+
it('should combine context from multiple cells', function () {
55+
const currentDoc = createMockDocument('cell2 content', 'b.ipynb')
56+
const notebook = {
57+
getCells: () => [
58+
createNotebookCell(createMockDocument('cell1 content', 'a.ipynb'), vscode.NotebookCellKind.Code),
59+
createNotebookCell(currentDoc, vscode.NotebookCellKind.Code),
60+
createNotebookCell(createMockDocument('cell3 content', 'c.ipynb'), vscode.NotebookCellKind.Code),
61+
],
62+
} as vscode.NotebookDocument
63+
64+
const position = new vscode.Position(0, 5)
65+
66+
const { caretLeftFileContext, caretRightFileContext } = getNotebookContext(notebook, currentDoc, position)
67+
68+
assert.strictEqual(caretLeftFileContext, 'cell1 content\ncell2')
69+
assert.strictEqual(caretRightFileContext, ' content\ncell3 content')
70+
})
71+
72+
it('should respect character limits', function () {
73+
const longContent = 'a'.repeat(10000)
74+
const notebook = {
75+
getCells: () => [createNotebookCell(createMockDocument(longContent), vscode.NotebookCellKind.Code)],
76+
} as vscode.NotebookDocument
77+
78+
const currentDoc = createMockDocument(longContent)
79+
const position = new vscode.Position(0, 5000)
80+
81+
const { caretLeftFileContext, caretRightFileContext } = getNotebookContext(notebook, currentDoc, position)
82+
83+
assert.ok(caretLeftFileContext.length <= CodeWhispererConstants.charactersLimit)
84+
assert.ok(caretRightFileContext.length <= CodeWhispererConstants.charactersLimit)
85+
})
86+
})
87+
})

0 commit comments

Comments
 (0)