|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import * as assert from 'assert'; |
| 6 | +import { AsyncIterableObject, AsyncIterableSource } from 'vs/base/common/async'; |
| 7 | +import { CancellationToken } from 'vs/base/common/cancellation'; |
| 8 | +import { URI } from 'vs/base/common/uri'; |
| 9 | +import { mock } from 'vs/base/test/common/mock'; |
| 10 | +import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils'; |
| 11 | +import { INotebookVariableElement, NotebookVariableDataSource } from 'vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariablesDataSource'; |
| 12 | +import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; |
| 13 | +import { INotebookKernel, INotebookKernelService, VariablesResult } from 'vs/workbench/contrib/notebook/common/notebookKernelService'; |
| 14 | + |
| 15 | + |
| 16 | +suite('NotebookVariableDataSource', () => { |
| 17 | + let dataSource: NotebookVariableDataSource; |
| 18 | + const notebookModel = { uri: 'one.ipynb', languages: ['python'] } as unknown as NotebookTextModel; |
| 19 | + let provideVariablesCalled = false; |
| 20 | + |
| 21 | + type VariablesResultWithAction = VariablesResult & { action?: () => void }; |
| 22 | + let results: VariablesResultWithAction[] = [ |
| 23 | + { id: 1, name: 'a', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 24 | + ]; |
| 25 | + |
| 26 | + const kernel = new class extends mock<INotebookKernel>() { |
| 27 | + override hasVariableProvider = true; |
| 28 | + override provideVariables( |
| 29 | + notebookUri: URI, |
| 30 | + parentId: number | undefined, |
| 31 | + kind: 'named' | 'indexed', |
| 32 | + start: number, |
| 33 | + token: CancellationToken |
| 34 | + ): AsyncIterableObject<VariablesResult> { |
| 35 | + provideVariablesCalled = true; |
| 36 | + const source = new AsyncIterableSource<VariablesResult>(); |
| 37 | + for (let i = 0; i < results.length; i++) { |
| 38 | + if (token.isCancellationRequested) { |
| 39 | + break; |
| 40 | + } |
| 41 | + if (results[i].action) { |
| 42 | + results[i].action!(); |
| 43 | + } |
| 44 | + source.emitOne(results[i]); |
| 45 | + } |
| 46 | + |
| 47 | + setTimeout(() => source.resolve(), 0); |
| 48 | + return source.asyncIterable; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const kernelService = new class extends mock<INotebookKernelService>() { |
| 53 | + override getMatchingKernel(notebook: NotebookTextModel) { |
| 54 | + return { selected: kernel, all: [], suggestions: [], hidden: [] }; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + ensureNoDisposablesAreLeakedInTestSuite(); |
| 59 | + |
| 60 | + setup(() => { |
| 61 | + provideVariablesCalled = false; |
| 62 | + dataSource = new NotebookVariableDataSource(kernelService); |
| 63 | + }); |
| 64 | + |
| 65 | + test('Root element should return children', async () => { |
| 66 | + const variables = await dataSource.getChildren({ kind: 'root', notebook: notebookModel }); |
| 67 | + |
| 68 | + assert.strictEqual(variables.length, 1); |
| 69 | + }); |
| 70 | + |
| 71 | + test('Get children of list element', async () => { |
| 72 | + const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 5 } as INotebookVariableElement; |
| 73 | + results = [ |
| 74 | + { id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 75 | + { id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 76 | + { id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 77 | + { id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 78 | + { id: 6, name: 'fifth', value: '5', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 79 | + ]; |
| 80 | + |
| 81 | + const variables = await dataSource.getChildren(parent); |
| 82 | + |
| 83 | + assert.strictEqual(variables.length, 5); |
| 84 | + }); |
| 85 | + |
| 86 | + test('Get children for large list', async () => { |
| 87 | + const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 2000 } as INotebookVariableElement; |
| 88 | + results = []; |
| 89 | + |
| 90 | + const variables = await dataSource.getChildren(parent); |
| 91 | + |
| 92 | + assert(variables.length > 1, 'We should have results for groups of children'); |
| 93 | + assert(!provideVariablesCalled, 'provideVariables should not be called'); |
| 94 | + assert.equal(variables[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('Cancel while enumerating through children', async () => { |
| 98 | + const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 10 } as INotebookVariableElement; |
| 99 | + results = [ |
| 100 | + { id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 101 | + { id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 102 | + { id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 103 | + { id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 104 | + { id: 5, name: 'fifth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0, action: () => dataSource.cancel() } as VariablesResult, |
| 105 | + { id: 7, name: 'sixth', value: '6', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 106 | + { id: 8, name: 'seventh', value: '7', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 107 | + { id: 9, name: 'eighth', value: '8', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 108 | + { id: 10, name: 'ninth', value: '9', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 109 | + { id: 11, name: 'tenth', value: '10', hasNamedChildren: false, indexedChildrenCount: 0 }, |
| 110 | + ]; |
| 111 | + |
| 112 | + const variables = await dataSource.getChildren(parent); |
| 113 | + |
| 114 | + assert.equal(variables.length, 5, 'Iterating should have been cancelled'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments