Path: blob/main/extensions/copilot/src/util/common/test/notebooks.spec.ts
13579 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Uri, NotebookCellData, NotebookCellKind, NotebookData } from '../../../vscodeTypes';6import { assert, describe, it } from 'vitest';7import { ExtHostNotebookDocumentData } from './shims/notebookDocument';8import { URI } from '../../vs/base/common/uri';9import { findCell, findNotebook, getNotebookAndCellFromUri, getNotebookCellOutput } from '../notebooks';101112describe('Notebook Common', () => {13it('Does not find notebook', async () => {14const notebooks = createSampleNotebooks();1516assert.isUndefined(findNotebook(Uri.file('foo.ipynb'), notebooks));17});1819it('Finds a notebook', async () => {20const notebooks = createSampleNotebooks();2122assert.isObject(findNotebook(Uri.file('one.ipynb'), notebooks));23});2425it('Finds a notebook', async () => {26const notebooks = createSampleNotebooks();27for (const notebook of notebooks) {28for (const cell of notebook.getCells()) {29const info = getNotebookAndCellFromUri(cell.document.uri, notebooks);30assert.equal(info[0], notebook);31assert.equal(info[1], cell);3233assert.equal(findCell(cell.document.uri, notebook), cell);3435assert.equal(findNotebook(cell.document.uri, notebooks), notebook);3637assert.isUndefined(getNotebookCellOutput(cell.document.uri, notebooks));38}39}40});4142function createSampleNotebooks() {43return [44ExtHostNotebookDocumentData.fromNotebookData(URI.file('one.ipynb'), new NotebookData(createCells([['markdown', '# Hello'], ['markdown', '# Foo Bar'], ['code', 'print(1234)']])), 'jupyter-notebook').document,45ExtHostNotebookDocumentData.fromNotebookData(URI.file('two.ipynb'), new NotebookData(createCells([['markdown', '# Title'], ['code', 'import sys'], ['code', 'sys.executable']])), 'jupyter-notebook').document,46ExtHostNotebookDocumentData.fromNotebookData(URI.file('three.ipynb').with({ scheme: 'ssh' }), new NotebookData(createCells([['markdown', '# Title'], ['code', 'import sys'], ['code', 'sys.executable']])), 'jupyter-notebook').document,47];48}49function createCells(cells: [kind: 'code' | 'markdown', code: string][]) {50return cells.map(([kind, code]) => {51return new NotebookCellData(kind === 'markdown' ? NotebookCellKind.Markup : NotebookCellKind.Code, code, kind === 'markdown' ? 'markdown' : 'python');52});53}54});555657