Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookVariablesDataSource.test.ts
5236 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*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { AsyncIterableProducer } from '../../../../../base/common/async.js';6import { CancellationToken } from '../../../../../base/common/cancellation.js';7import { URI } from '../../../../../base/common/uri.js';8import { mock } from '../../../../../base/test/common/mock.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';10import { INotebookVariableElement, NotebookVariableDataSource } from '../../browser/contrib/notebookVariables/notebookVariablesDataSource.js';11import { NotebookTextModel } from '../../common/model/notebookTextModel.js';12import { INotebookKernel, INotebookKernelService, VariablesResult } from '../../common/notebookKernelService.js';131415suite('NotebookVariableDataSource', () => {16let dataSource: NotebookVariableDataSource;17const notebookModel = { uri: 'one.ipynb', languages: ['python'] } as unknown as NotebookTextModel;18let provideVariablesCalled: boolean;1920type VariablesResultWithAction = VariablesResult & { action?: () => void };21let results: VariablesResultWithAction[];2223const kernel = new class extends mock<INotebookKernel>() {24override hasVariableProvider = true;25override provideVariables(26notebookUri: URI,27parentId: number | undefined,28kind: 'named' | 'indexed',29start: number,30token: CancellationToken31): AsyncIterableProducer<VariablesResult> {32provideVariablesCalled = true;33return new AsyncIterableProducer<VariablesResult>((emitter) => {34for (let i = 0; i < results.length; i++) {35if (token.isCancellationRequested) {36break;37}38if (results[i].action) {39results[i].action!();40}41emitter.emitOne(results[i]);42}43});44}45};4647const kernelService = new class extends mock<INotebookKernelService>() {48override getMatchingKernel(notebook: NotebookTextModel) {49return { selected: kernel, all: [], suggestions: [], hidden: [] };50}51};5253ensureNoDisposablesAreLeakedInTestSuite();5455setup(() => {56provideVariablesCalled = false;57dataSource = new NotebookVariableDataSource(kernelService);58results = [59{ id: 1, name: 'a', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },60];61});6263test('Root element should return children', async () => {64const variables = await dataSource.getChildren({ kind: 'root', notebook: notebookModel });6566assert.strictEqual(variables.length, 1);67});6869test('Get children of list element', async () => {70const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 5 } as INotebookVariableElement;71results = [72{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },73{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },74{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },75{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },76{ id: 6, name: 'fifth', value: '5', hasNamedChildren: false, indexedChildrenCount: 0 },77];7879const variables = await dataSource.getChildren(parent);8081assert.strictEqual(variables.length, 5);82});8384test('Get children for large list', async () => {85const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 2000 } as INotebookVariableElement;86results = [];8788const variables = await dataSource.getChildren(parent);8990assert(variables.length > 1, 'We should have results for groups of children');91assert(!provideVariablesCalled, 'provideVariables should not be called');92assert.equal(variables[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');93});9495test('Get children for very large list', async () => {96const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 1_000_000 } as INotebookVariableElement;97results = [];9899const groups = await dataSource.getChildren(parent);100const children = await dataSource.getChildren(groups[99]);101102assert(children.length === 100, 'We should have a full page of child groups');103assert(!provideVariablesCalled, 'provideVariables should not be called');104assert.equal(children[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');105});106107test('Cancel while enumerating through children', async () => {108const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 10 } as INotebookVariableElement;109results = [110{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },111{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },112{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },113{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },114{ id: 5, name: 'fifth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0, action: () => dataSource.cancel() } as VariablesResult,115{ id: 7, name: 'sixth', value: '6', hasNamedChildren: false, indexedChildrenCount: 0 },116{ id: 8, name: 'seventh', value: '7', hasNamedChildren: false, indexedChildrenCount: 0 },117{ id: 9, name: 'eighth', value: '8', hasNamedChildren: false, indexedChildrenCount: 0 },118{ id: 10, name: 'ninth', value: '9', hasNamedChildren: false, indexedChildrenCount: 0 },119{ id: 11, name: 'tenth', value: '10', hasNamedChildren: false, indexedChildrenCount: 0 },120];121122const variables = await dataSource.getChildren(parent);123124assert.equal(variables.length, 5, 'Iterating should have been cancelled');125});126});127128129