Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookVariablesDataSource.test.ts
5236 views
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 assert from 'assert';
6
import { AsyncIterableProducer } from '../../../../../base/common/async.js';
7
import { CancellationToken } from '../../../../../base/common/cancellation.js';
8
import { URI } from '../../../../../base/common/uri.js';
9
import { mock } from '../../../../../base/test/common/mock.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
11
import { INotebookVariableElement, NotebookVariableDataSource } from '../../browser/contrib/notebookVariables/notebookVariablesDataSource.js';
12
import { NotebookTextModel } from '../../common/model/notebookTextModel.js';
13
import { INotebookKernel, INotebookKernelService, VariablesResult } from '../../common/notebookKernelService.js';
14
15
16
suite('NotebookVariableDataSource', () => {
17
let dataSource: NotebookVariableDataSource;
18
const notebookModel = { uri: 'one.ipynb', languages: ['python'] } as unknown as NotebookTextModel;
19
let provideVariablesCalled: boolean;
20
21
type VariablesResultWithAction = VariablesResult & { action?: () => void };
22
let results: VariablesResultWithAction[];
23
24
const kernel = new class extends mock<INotebookKernel>() {
25
override hasVariableProvider = true;
26
override provideVariables(
27
notebookUri: URI,
28
parentId: number | undefined,
29
kind: 'named' | 'indexed',
30
start: number,
31
token: CancellationToken
32
): AsyncIterableProducer<VariablesResult> {
33
provideVariablesCalled = true;
34
return new AsyncIterableProducer<VariablesResult>((emitter) => {
35
for (let i = 0; i < results.length; i++) {
36
if (token.isCancellationRequested) {
37
break;
38
}
39
if (results[i].action) {
40
results[i].action!();
41
}
42
emitter.emitOne(results[i]);
43
}
44
});
45
}
46
};
47
48
const kernelService = new class extends mock<INotebookKernelService>() {
49
override getMatchingKernel(notebook: NotebookTextModel) {
50
return { selected: kernel, all: [], suggestions: [], hidden: [] };
51
}
52
};
53
54
ensureNoDisposablesAreLeakedInTestSuite();
55
56
setup(() => {
57
provideVariablesCalled = false;
58
dataSource = new NotebookVariableDataSource(kernelService);
59
results = [
60
{ id: 1, name: 'a', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
61
];
62
});
63
64
test('Root element should return children', async () => {
65
const variables = await dataSource.getChildren({ kind: 'root', notebook: notebookModel });
66
67
assert.strictEqual(variables.length, 1);
68
});
69
70
test('Get children of list element', async () => {
71
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 5 } as INotebookVariableElement;
72
results = [
73
{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
74
{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },
75
{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },
76
{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },
77
{ id: 6, name: 'fifth', value: '5', hasNamedChildren: false, indexedChildrenCount: 0 },
78
];
79
80
const variables = await dataSource.getChildren(parent);
81
82
assert.strictEqual(variables.length, 5);
83
});
84
85
test('Get children for large list', async () => {
86
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 2000 } as INotebookVariableElement;
87
results = [];
88
89
const variables = await dataSource.getChildren(parent);
90
91
assert(variables.length > 1, 'We should have results for groups of children');
92
assert(!provideVariablesCalled, 'provideVariables should not be called');
93
assert.equal(variables[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');
94
});
95
96
test('Get children for very large list', async () => {
97
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 1_000_000 } as INotebookVariableElement;
98
results = [];
99
100
const groups = await dataSource.getChildren(parent);
101
const children = await dataSource.getChildren(groups[99]);
102
103
assert(children.length === 100, 'We should have a full page of child groups');
104
assert(!provideVariablesCalled, 'provideVariables should not be called');
105
assert.equal(children[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');
106
});
107
108
test('Cancel while enumerating through children', async () => {
109
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 10 } as INotebookVariableElement;
110
results = [
111
{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
112
{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },
113
{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },
114
{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },
115
{ id: 5, name: 'fifth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0, action: () => dataSource.cancel() } as VariablesResult,
116
{ id: 7, name: 'sixth', value: '6', hasNamedChildren: false, indexedChildrenCount: 0 },
117
{ id: 8, name: 'seventh', value: '7', hasNamedChildren: false, indexedChildrenCount: 0 },
118
{ id: 9, name: 'eighth', value: '8', hasNamedChildren: false, indexedChildrenCount: 0 },
119
{ id: 10, name: 'ninth', value: '9', hasNamedChildren: false, indexedChildrenCount: 0 },
120
{ id: 11, name: 'tenth', value: '10', hasNamedChildren: false, indexedChildrenCount: 0 },
121
];
122
123
const variables = await dataSource.getChildren(parent);
124
125
assert.equal(variables.length, 5, 'Iterating should have been cancelled');
126
});
127
});
128
129