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
3296 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 { AsyncIterableObject, AsyncIterableSource } 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
): AsyncIterableObject<VariablesResult> {
33
provideVariablesCalled = true;
34
const source = new AsyncIterableSource<VariablesResult>();
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
source.emitOne(results[i]);
43
}
44
45
setTimeout(() => source.resolve(), 0);
46
return source.asyncIterable;
47
}
48
};
49
50
const kernelService = new class extends mock<INotebookKernelService>() {
51
override getMatchingKernel(notebook: NotebookTextModel) {
52
return { selected: kernel, all: [], suggestions: [], hidden: [] };
53
}
54
};
55
56
ensureNoDisposablesAreLeakedInTestSuite();
57
58
setup(() => {
59
provideVariablesCalled = false;
60
dataSource = new NotebookVariableDataSource(kernelService);
61
results = [
62
{ id: 1, name: 'a', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
63
];
64
});
65
66
test('Root element should return children', async () => {
67
const variables = await dataSource.getChildren({ kind: 'root', notebook: notebookModel });
68
69
assert.strictEqual(variables.length, 1);
70
});
71
72
test('Get children of list element', async () => {
73
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 5 } as INotebookVariableElement;
74
results = [
75
{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
76
{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },
77
{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },
78
{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },
79
{ id: 6, name: 'fifth', value: '5', hasNamedChildren: false, indexedChildrenCount: 0 },
80
];
81
82
const variables = await dataSource.getChildren(parent);
83
84
assert.strictEqual(variables.length, 5);
85
});
86
87
test('Get children for large list', async () => {
88
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 2000 } as INotebookVariableElement;
89
results = [];
90
91
const variables = await dataSource.getChildren(parent);
92
93
assert(variables.length > 1, 'We should have results for groups of children');
94
assert(!provideVariablesCalled, 'provideVariables should not be called');
95
assert.equal(variables[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');
96
});
97
98
test('Get children for very large list', async () => {
99
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 1_000_000 } as INotebookVariableElement;
100
results = [];
101
102
const groups = await dataSource.getChildren(parent);
103
const children = await dataSource.getChildren(groups[99]);
104
105
assert(children.length === 100, 'We should have a full page of child groups');
106
assert(!provideVariablesCalled, 'provideVariables should not be called');
107
assert.equal(children[0].extHostId, parent.extHostId, 'ExtHostId should match the parent since we will use it to get the real children');
108
});
109
110
test('Cancel while enumerating through children', async () => {
111
const parent = { kind: 'variable', notebook: notebookModel, id: '1', extHostId: 1, name: 'list', value: '[...]', hasNamedChildren: false, indexedChildrenCount: 10 } as INotebookVariableElement;
112
results = [
113
{ id: 2, name: 'first', value: '1', hasNamedChildren: false, indexedChildrenCount: 0 },
114
{ id: 3, name: 'second', value: '2', hasNamedChildren: false, indexedChildrenCount: 0 },
115
{ id: 4, name: 'third', value: '3', hasNamedChildren: false, indexedChildrenCount: 0 },
116
{ id: 5, name: 'fourth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0 },
117
{ id: 5, name: 'fifth', value: '4', hasNamedChildren: false, indexedChildrenCount: 0, action: () => dataSource.cancel() } as VariablesResult,
118
{ id: 7, name: 'sixth', value: '6', hasNamedChildren: false, indexedChildrenCount: 0 },
119
{ id: 8, name: 'seventh', value: '7', hasNamedChildren: false, indexedChildrenCount: 0 },
120
{ id: 9, name: 'eighth', value: '8', hasNamedChildren: false, indexedChildrenCount: 0 },
121
{ id: 10, name: 'ninth', value: '9', hasNamedChildren: false, indexedChildrenCount: 0 },
122
{ id: 11, name: 'tenth', value: '10', hasNamedChildren: false, indexedChildrenCount: 0 },
123
];
124
125
const variables = await dataSource.getChildren(parent);
126
127
assert.equal(variables.length, 5, 'Iterating should have been cancelled');
128
});
129
});
130
131