|
| 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