Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/test/browser/TestMainThreadNotebookKernels.ts
4780 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
6
import { mock } from '../../../test/common/workbenchTestServices.js';
7
import { TestInstantiationService } from '../../../../platform/instantiation/test/common/instantiationServiceMock.js';
8
import { IExtHostContext } from '../../../services/extensions/common/extHostCustomers.js';
9
import { INotebookKernel, INotebookKernelService } from '../../../contrib/notebook/common/notebookKernelService.js';
10
import { Disposable } from '../../../../base/common/lifecycle.js';
11
import { ILanguageService } from '../../../../editor/common/languages/language.js';
12
import { INotebookCellExecution, INotebookExecution, INotebookExecutionStateService } from '../../../contrib/notebook/common/notebookExecutionStateService.js';
13
import { INotebookService } from '../../../contrib/notebook/common/notebookService.js';
14
import { INotebookEditorService } from '../../../contrib/notebook/browser/services/notebookEditorService.js';
15
import { Event } from '../../../../base/common/event.js';
16
import { MainThreadNotebookKernels } from '../../browser/mainThreadNotebookKernels.js';
17
import { ExtensionIdentifier } from '../../../../platform/extensions/common/extensions.js';
18
19
export class TestMainThreadNotebookKernels extends Disposable {
20
private readonly instantiationService: TestInstantiationService;
21
private readonly registeredKernels = new Map<string, INotebookKernel>();
22
private mainThreadNotebookKernels: MainThreadNotebookKernels;
23
private kernelHandle = 0;
24
25
constructor(extHostContext: IExtHostContext) {
26
super();
27
this.instantiationService = this._register(new TestInstantiationService());
28
this.setupDefaultStubs();
29
this.mainThreadNotebookKernels = this._register(this.instantiationService.createInstance(MainThreadNotebookKernels, extHostContext));
30
}
31
32
private setupDefaultStubs(): void {
33
this.instantiationService.stub(ILanguageService, new class extends mock<ILanguageService>() {
34
override getRegisteredLanguageIds() {
35
return ['typescript', 'javascript', 'python'];
36
}
37
});
38
39
this.instantiationService.stub(INotebookKernelService, new class extends mock<INotebookKernelService>() {
40
constructor(private builder: TestMainThreadNotebookKernels) {
41
super();
42
}
43
44
override registerKernel(kernel: INotebookKernel) {
45
this.builder.registeredKernels.set(kernel.id, kernel);
46
return Disposable.None;
47
}
48
override onDidChangeSelectedNotebooks = Event.None;
49
override getMatchingKernel() {
50
return {
51
selected: undefined,
52
suggestions: [],
53
all: [],
54
hidden: []
55
};
56
}
57
}(this));
58
59
this.instantiationService.stub(INotebookExecutionStateService, new class extends mock<INotebookExecutionStateService>() {
60
override createCellExecution(): INotebookCellExecution {
61
return new class extends mock<INotebookCellExecution>() { };
62
}
63
override createExecution(): INotebookExecution {
64
return new class extends mock<INotebookExecution>() { };
65
}
66
});
67
68
this.instantiationService.stub(INotebookService, new class extends mock<INotebookService>() {
69
override getNotebookTextModel() {
70
return undefined;
71
}
72
});
73
74
this.instantiationService.stub(INotebookEditorService, new class extends mock<INotebookEditorService>() {
75
override listNotebookEditors() {
76
return [];
77
}
78
override onDidAddNotebookEditor = Event.None;
79
override onDidRemoveNotebookEditor = Event.None;
80
});
81
}
82
83
get instance(): MainThreadNotebookKernels {
84
return this.mainThreadNotebookKernels;
85
}
86
87
async addKernel(id: string): Promise<void> {
88
const handle = this.kernelHandle++;
89
await this.instance.$addKernel(handle, {
90
id,
91
notebookType: 'test-notebook',
92
extensionId: new ExtensionIdentifier('test.extension'),
93
extensionLocation: { scheme: 'test', path: '/test' },
94
label: 'Test Kernel',
95
description: 'A test kernel',
96
hasVariableProvider: true
97
});
98
}
99
100
getKernel(id: string): INotebookKernel | undefined {
101
return this.registeredKernels.get(id);
102
}
103
}
104
105