-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
Copy pathnotebook.kernel.test.ts
494 lines (415 loc) · 18.1 KB
/
notebook.kernel.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import 'mocha';
import { TextDecoder } from 'util';
import * as vscode from 'vscode';
import { asPromise, assertNoRpc, closeAllEditors, createRandomFile, DeferredPromise, disposeAll, revertAllDirty, saveAllEditors } from '../utils';
async function createRandomNotebookFile() {
return createRandomFile('', undefined, '.vsctestnb');
}
async function openRandomNotebookDocument() {
const uri = await createRandomNotebookFile();
return vscode.workspace.openNotebookDocument(uri);
}
export async function saveAllFilesAndCloseAll() {
await saveAllEditors();
await closeAllEditors();
}
async function withEvent<T>(event: vscode.Event<T>, callback: (e: Promise<T>) => Promise<void>) {
const e = asPromise<T>(event);
await callback(e);
}
function sleep(ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
export class Kernel {
readonly controller: vscode.NotebookController;
readonly associatedNotebooks = new Set<string>();
constructor(id: string, label: string, viewType: string = 'notebookCoreTest') {
this.controller = vscode.notebooks.createNotebookController(id, viewType, label);
this.controller.executeHandler = this._execute.bind(this);
this.controller.supportsExecutionOrder = true;
this.controller.supportedLanguages = ['typescript', 'javascript'];
this.controller.onDidChangeSelectedNotebooks(e => {
if (e.selected) {
this.associatedNotebooks.add(e.notebook.uri.toString());
} else {
this.associatedNotebooks.delete(e.notebook.uri.toString());
}
});
}
protected async _execute(cells: vscode.NotebookCell[]): Promise<void> {
for (const cell of cells) {
await this._runCell(cell);
}
}
protected async _runCell(cell: vscode.NotebookCell) {
// create a single output with exec order 1 and output is plain/text
// of either the cell itself or (iff empty) the cell's document's uri
const task = this.controller.createNotebookCellExecution(cell);
task.start(Date.now());
task.executionOrder = 1;
await sleep(10); // Force to be take some time
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text(cell.document.getText() || cell.document.uri.toString(), 'text/plain')
])]);
task.end(true);
}
}
async function assertKernel(kernel: Kernel, notebook: vscode.NotebookDocument): Promise<void> {
const success = await vscode.commands.executeCommand('notebook.selectKernel', {
extension: 'vscode.vscode-api-tests',
id: kernel.controller.id
});
assert.ok(success, `expected selected kernel to be ${kernel.controller.id}`);
assert.ok(kernel.associatedNotebooks.has(notebook.uri.toString()));
}
const apiTestContentProvider: vscode.NotebookContentProvider = {
openNotebook: async (resource: vscode.Uri): Promise<vscode.NotebookData> => {
if (/.*empty\-.*\.vsctestnb$/.test(resource.path)) {
return {
metadata: {},
cells: []
};
}
const dto: vscode.NotebookData = {
metadata: { custom: { testMetadata: false } },
cells: [
{
value: 'test',
languageId: 'typescript',
kind: vscode.NotebookCellKind.Code,
outputs: [],
metadata: { custom: { testCellMetadata: 123 } },
executionSummary: { timing: { startTime: 10, endTime: 20 } }
},
{
value: 'test2',
languageId: 'typescript',
kind: vscode.NotebookCellKind.Code,
outputs: [
new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('Hello World', 'text/plain')
],
{
testOutputMetadata: true,
['text/plain']: { testOutputItemMetadata: true }
})
],
executionSummary: { executionOrder: 5, success: true },
metadata: { custom: { testCellMetadata: 456 } }
}
]
};
return dto;
},
saveNotebook: async (_document: vscode.NotebookDocument, _cancellation: vscode.CancellationToken) => {
return;
},
saveNotebookAs: async (_targetResource: vscode.Uri, _document: vscode.NotebookDocument, _cancellation: vscode.CancellationToken) => {
return;
},
backupNotebook: async (_document: vscode.NotebookDocument, _context: vscode.NotebookDocumentBackupContext, _cancellation: vscode.CancellationToken) => {
return {
id: '1',
delete: () => { }
};
}
};
(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('Notebook Kernel API tests', function () {
const testDisposables: vscode.Disposable[] = [];
const suiteDisposables: vscode.Disposable[] = [];
suiteTeardown(async function () {
assertNoRpc();
await revertAllDirty();
await closeAllEditors();
disposeAll(suiteDisposables);
suiteDisposables.length = 0;
});
suiteSetup(function () {
suiteDisposables.push(vscode.workspace.registerNotebookContentProvider('notebookCoreTest', apiTestContentProvider));
});
let defaultKernel: Kernel;
setup(async function () {
// there should be ONE default kernel in this suite
defaultKernel = new Kernel('mainKernel', 'Notebook Default Kernel');
testDisposables.push(defaultKernel.controller);
await saveAllFilesAndCloseAll();
});
teardown(async function () {
disposeAll(testDisposables);
testDisposables.length = 0;
await saveAllFilesAndCloseAll();
});
test('cell execute command takes arguments', async () => {
const notebook = await openRandomNotebookDocument();
await vscode.window.showNotebookDocument(notebook);
assert.strictEqual(vscode.window.activeNotebookEditor !== undefined, true, 'notebook first');
const editor = vscode.window.activeNotebookEditor!;
const cell = editor.notebook.cellAt(0);
await withEvent(vscode.workspace.onDidChangeNotebookDocument, async event => {
await vscode.commands.executeCommand('notebook.execute');
await event;
assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked
});
await withEvent(vscode.workspace.onDidChangeNotebookDocument, async event => {
await vscode.commands.executeCommand('notebook.cell.clearOutputs');
await event;
assert.strictEqual(cell.outputs.length, 0, 'should clear');
});
const secondResource = await createRandomNotebookFile();
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async event => {
await vscode.commands.executeCommand('notebook.cell.execute', { start: 0, end: 1 }, notebook.uri);
await event;
assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked
assert.strictEqual(vscode.window.activeNotebookEditor?.notebook.uri.fsPath, secondResource.fsPath);
});
});
test('cell execute command takes arguments 2', async () => {
const notebook = await openRandomNotebookDocument();
await vscode.window.showNotebookDocument(notebook);
let firstCellExecuted = false;
let secondCellExecuted = false;
const def = new DeferredPromise<void>();
testDisposables.push(vscode.workspace.onDidChangeNotebookDocument(e => {
e.cellChanges.forEach(change => {
if (change.cell.index === 0 && change.executionSummary) {
firstCellExecuted = true;
}
if (change.cell.index === 1 && change.executionSummary) {
secondCellExecuted = true;
}
});
if (firstCellExecuted && secondCellExecuted) {
def.complete();
}
}));
vscode.commands.executeCommand('notebook.cell.execute', { document: notebook.uri, ranges: [{ start: 0, end: 1 }, { start: 1, end: 2 }] });
await def.p;
await saveAllFilesAndCloseAll();
});
test('document execute command takes arguments', async () => {
const notebook = await openRandomNotebookDocument();
await vscode.window.showNotebookDocument(notebook);
assert.strictEqual(vscode.window.activeNotebookEditor !== undefined, true, 'notebook first');
const editor = vscode.window.activeNotebookEditor!;
const cell = editor.notebook.cellAt(0);
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await vscode.commands.executeCommand('notebook.execute', notebook.uri);
await event;
assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked
});
});
test('cell execute and select kernel', async function () {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
assert.strictEqual(vscode.window.activeNotebookEditor === editor, true, 'notebook first');
const cell = editor.notebook.cellAt(0);
const alternativeKernel = new class extends Kernel {
constructor() {
super('secondaryKernel', 'Notebook Secondary Test Kernel');
this.controller.supportsExecutionOrder = false;
}
override async _runCell(cell: vscode.NotebookCell) {
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my second output', 'text/plain')
])]);
task.end(true);
}
};
testDisposables.push(alternativeKernel.controller);
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(defaultKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked
assert.strictEqual(cell.outputs[0].items.length, 1);
assert.strictEqual(cell.outputs[0].items[0].mime, 'text/plain');
assert.deepStrictEqual(new TextDecoder().decode(cell.outputs[0].items[0].data), cell.document.getText());
});
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(alternativeKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked
assert.strictEqual(cell.outputs[0].items.length, 1);
assert.strictEqual(cell.outputs[0].items[0].mime, 'text/plain');
assert.deepStrictEqual(new TextDecoder().decode(cell.outputs[0].items[0].data), 'my second output');
});
});
test('onDidChangeCellExecutionState is fired', async () => {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell = editor.notebook.cellAt(0);
let eventCount = 0;
const def = new DeferredPromise<void>();
testDisposables.push(vscode.notebooks.onDidChangeNotebookCellExecutionState(e => {
try {
assert.strictEqual(e.cell.document.uri.toString(), cell.document.uri.toString(), 'event should be fired for the executing cell');
if (eventCount === 0) {
assert.strictEqual(e.state, vscode.NotebookCellExecutionState.Pending, 'should be set to Pending');
} else if (eventCount === 1) {
assert.strictEqual(e.state, vscode.NotebookCellExecutionState.Executing, 'should be set to Executing');
assert.strictEqual(cell.outputs.length, 0, 'no outputs yet: ' + JSON.stringify(cell.outputs[0]));
} else if (e.state === vscode.NotebookCellExecutionState.Idle) {
assert.strictEqual(cell.outputs.length, 1, 'should have an output');
def.complete();
}
eventCount++;
} catch (err) {
def.error(err);
}
}));
vscode.commands.executeCommand('notebook.cell.execute', { document: notebook.uri, ranges: [{ start: 0, end: 1 }] });
await def.p;
});
test('Output changes are applied once the promise resolves', async function () {
let called = false;
const verifyOutputSyncKernel = new class extends Kernel {
constructor() {
super('verifyOutputSyncKernel', '');
}
override async _execute(cells: vscode.NotebookCell[]) {
const [cell] = cells;
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('Some output', 'text/plain')
])]);
assert.strictEqual(cell.notebook.cellAt(0).outputs.length, 1);
assert.deepStrictEqual(new TextDecoder().decode(cell.notebook.cellAt(0).outputs[0].items[0].data), 'Some output');
task.end(undefined);
called = true;
}
};
const notebook = await openRandomNotebookDocument();
await vscode.window.showNotebookDocument(notebook);
await assertKernel(verifyOutputSyncKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
assert.strictEqual(called, true);
verifyOutputSyncKernel.controller.dispose();
});
test('executionSummary', async () => {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell = editor.notebook.cellAt(0);
assert.strictEqual(cell.executionSummary?.success, undefined);
assert.strictEqual(cell.executionSummary?.executionOrder, undefined);
await vscode.commands.executeCommand('notebook.cell.execute');
assert.strictEqual(cell.outputs.length, 1, 'should execute');
assert.ok(cell.executionSummary);
assert.strictEqual(cell.executionSummary!.success, true);
assert.strictEqual(typeof cell.executionSummary!.executionOrder, 'number');
});
test('initialize executionSummary', async () => {
const document = await openRandomNotebookDocument();
const cell = document.cellAt(0);
assert.strictEqual(cell.executionSummary?.success, undefined);
assert.strictEqual(cell.executionSummary?.timing?.startTime, 10);
assert.strictEqual(cell.executionSummary?.timing?.endTime, 20);
});
test('execution cancelled when delete while executing', async () => {
const document = await openRandomNotebookDocument();
const cell = document.cellAt(0);
let executionWasCancelled = false;
const cancelledKernel = new class extends Kernel {
constructor() {
super('cancelledKernel', '');
}
override async _execute(cells: vscode.NotebookCell[]) {
const [cell] = cells;
const exe = this.controller.createNotebookCellExecution(cell);
exe.token.onCancellationRequested(() => executionWasCancelled = true);
}
};
testDisposables.push(cancelledKernel.controller);
await vscode.window.showNotebookDocument(document);
await assertKernel(cancelledKernel, document);
await vscode.commands.executeCommand('notebook.cell.execute');
// Delete executing cell
const edit = new vscode.WorkspaceEdit();
edit.set(cell!.notebook.uri, [vscode.NotebookEdit.replaceCells(new vscode.NotebookRange(cell!.index, cell!.index + 1), [])]);
await vscode.workspace.applyEdit(edit);
assert.strictEqual(executionWasCancelled, true);
});
test('appendOutput to different cell', async function () {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell0 = editor.notebook.cellAt(0);
const notebookEdit = new vscode.NotebookEdit(new vscode.NotebookRange(1, 1), [new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'test 2', 'javascript')]);
const edit = new vscode.WorkspaceEdit();
edit.set(notebook.uri, [notebookEdit]);
await vscode.workspace.applyEdit(edit);
const cell1 = editor.notebook.cellAt(1);
const nextCellKernel = new class extends Kernel {
constructor() {
super('nextCellKernel', 'Append to cell kernel');
}
override async _runCell(cell: vscode.NotebookCell) {
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.appendOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output')
])], cell1);
await task.appendOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output 2')
])], cell1);
task.end(true);
}
};
testDisposables.push(nextCellKernel.controller);
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(nextCellKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell0.outputs.length, 0, 'should not change cell 0');
assert.strictEqual(cell1.outputs.length, 2, 'should update cell 1');
assert.strictEqual(cell1.outputs[0].items.length, 1);
assert.deepStrictEqual(new TextDecoder().decode(cell1.outputs[0].items[0].data), 'my output');
});
});
test('replaceOutput to different cell', async function () {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell0 = editor.notebook.cellAt(0);
const notebookEdit = new vscode.NotebookEdit(new vscode.NotebookRange(1, 1), [new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'test 2', 'javascript')]);
const edit = new vscode.WorkspaceEdit();
edit.set(notebook.uri, [notebookEdit]);
await vscode.workspace.applyEdit(edit);
const cell1 = editor.notebook.cellAt(1);
const nextCellKernel = new class extends Kernel {
constructor() {
super('nextCellKernel', 'Replace to cell kernel');
}
override async _runCell(cell: vscode.NotebookCell) {
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output')
])], cell1);
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output 2')
])], cell1);
task.end(true);
}
};
testDisposables.push(nextCellKernel.controller);
await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(nextCellKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell0.outputs.length, 0, 'should not change cell 0');
assert.strictEqual(cell1.outputs.length, 1, 'should update cell 1');
assert.strictEqual(cell1.outputs[0].items.length, 1);
assert.deepStrictEqual(new TextDecoder().decode(cell1.outputs[0].items[0].data), 'my output 2');
});
});
});