Skip to content

Commit 98a615c

Browse files
Backport PR #19143 on branch 4.6.x (Clear stale Table of Contents error markers) (#19182)
Backport PR #19143: Clear stale Table of Contents error markers Co-authored-by: ANUJ KUMAR SINGH <[email protected]>
1 parent 1ec91a4 commit 98a615c

2 files changed

Lines changed: 116 additions & 12 deletions

File tree

‎packages/notebook/src/toc.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ export class NotebookToCModel extends TableOfContentsModel<
244244
*/
245245
protected async getHeadings(): Promise<INotebookHeading[] | null> {
246246
const cells = this.widget.content.widgets;
247+
const currentCells = new Set(cells);
247248
const headings: INotebookHeading[] = [];
248249
const documentLevels = new Array<number>();
250+
const cellToHeadingIndex = new WeakMap<Cell, number>();
249251

250252
// Generate headings by iterating through all notebook cells...
251253
for (let i = 0; i < cells.length; i++) {
@@ -306,12 +308,15 @@ export class NotebookToCModel extends TableOfContentsModel<
306308
}
307309

308310
if (headings.length > 0) {
309-
this._cellToHeadingIndex.set(cell, headings.length - 1);
310-
} else {
311-
// If no headings were found, remove the cell from the map
312-
this._cellToHeadingIndex.delete(cell);
311+
cellToHeadingIndex.set(cell, headings.length - 1);
313312
}
314313
}
314+
315+
this._cellToHeadingIndex = cellToHeadingIndex;
316+
this._runningCells = this._runningCells.filter(cell =>
317+
currentCells.has(cell)
318+
);
319+
this._errorCells = this._errorCells.filter(cell => currentCells.has(cell));
315320
this.updateRunningStatus(headings);
316321
return Promise.resolve(headings);
317322
}
@@ -329,7 +334,8 @@ export class NotebookToCModel extends TableOfContentsModel<
329334
): boolean {
330335
return (
331336
super.isHeadingEqual(heading1, heading2) &&
332-
heading1.cellRef === heading2.cellRef
337+
heading1.cellRef === heading2.cellRef &&
338+
heading1.isRunning === heading2.isRunning
333339
);
334340
}
335341

@@ -463,11 +469,15 @@ export class NotebookToCModel extends TableOfContentsModel<
463469
}
464470

465471
protected updateRunningStatus(headings: INotebookHeading[]): void {
472+
headings.forEach(heading => {
473+
heading.isRunning = RunningStatus.Idle;
474+
});
475+
466476
// Update isRunning
467477
this._runningCells.forEach((cell, index) => {
468478
const headingIndex = this._cellToHeadingIndex.get(cell);
469479
if (headingIndex !== undefined) {
470-
const heading = this.headings[headingIndex];
480+
const heading = headings[headingIndex];
471481
// Running is prioritized over Scheduled, so if a heading is
472482
// running don't change status
473483
if (heading.isRunning !== RunningStatus.Running) {
@@ -480,7 +490,7 @@ export class NotebookToCModel extends TableOfContentsModel<
480490
this._errorCells.forEach((cell, index) => {
481491
const headingIndex = this._cellToHeadingIndex.get(cell);
482492
if (headingIndex !== undefined) {
483-
const heading = this.headings[headingIndex];
493+
const heading = headings[headingIndex];
484494
// Running and Scheduled are prioritized over Error, so only if
485495
// a heading is idle will it be set to Error
486496
if (heading.isRunning === RunningStatus.Idle) {

‎packages/notebook/test/toc.spec.ts‎

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4-
import type { INotebookModel, NotebookPanel } from '@jupyterlab/notebook';
5-
import { NotebookToCModel } from '@jupyterlab/notebook';
4+
import { Sanitizer } from '@jupyterlab/apputils';
5+
import type { Cell } from '@jupyterlab/cells';
66
import type { Context } from '@jupyterlab/docregistry';
7+
import type {
8+
INotebookModel,
9+
KernelError,
10+
NotebookPanel
11+
} from '@jupyterlab/notebook';
12+
import { NotebookToCModel, RunningStatus } from '@jupyterlab/notebook';
713
import { NBTestUtils } from '@jupyterlab/notebook/lib/testutils';
8-
import { Sanitizer } from '@jupyterlab/apputils';
914
import { signalToPromise } from '@jupyterlab/testing';
1015
import * as utils from './utils';
1116

17+
class TestKernelError extends Error {
18+
readonly errorName = 'ZeroDivisionError';
19+
readonly errorValue = 'division by zero';
20+
readonly traceback: string[] = [];
21+
}
22+
23+
class TestNotebookToCModel extends NotebookToCModel {
24+
scheduleExecution(cell: Cell): void {
25+
this.onExecutionScheduled(null, { notebook: this.widget.content, cell });
26+
}
27+
28+
finishExecution(
29+
cell: Cell,
30+
success: boolean,
31+
error: KernelError | null
32+
): void {
33+
this.onExecuted(null, {
34+
notebook: this.widget.content,
35+
cell,
36+
success,
37+
error
38+
});
39+
}
40+
}
41+
1242
describe('@jupyterlab/notebook', () => {
1343
describe('NotebookToCModel', () => {
1444
let context: Context<INotebookModel>;
1545
let panel: NotebookPanel;
16-
let model: NotebookToCModel;
46+
let model: TestNotebookToCModel;
1747
const sanitizer = new Sanitizer();
1848
const initialCells = [{ cell_type: 'markdown', source: '# heading' }];
1949

2050
beforeEach(async () => {
2151
context = await NBTestUtils.createMockContext(false);
2252
panel = utils.createNotebookPanel(context);
23-
model = new NotebookToCModel(panel, null, sanitizer);
53+
model = new TestNotebookToCModel(panel, null, sanitizer);
2454
panel.model!.sharedModel.insertCells(0, initialCells);
2555
});
2656

@@ -72,5 +102,69 @@ describe('@jupyterlab/notebook', () => {
72102
await promise;
73103
});
74104
});
105+
106+
describe('#refresh', () => {
107+
it('should clear error status when an errored cell is deleted', async () => {
108+
panel.model!.sharedModel.insertCells(1, [
109+
{ cell_type: 'code', source: '1 / 2' },
110+
{ cell_type: 'code', source: '1 / 0' },
111+
{ cell_type: 'code', source: '1 / 2' }
112+
]);
113+
114+
await model.refresh();
115+
116+
const erroredCell = panel.content.widgets[2];
117+
model.scheduleExecution(erroredCell);
118+
model.finishExecution(erroredCell, false, new TestKernelError());
119+
120+
expect(model.headings[0].isRunning).toBe(RunningStatus.Error);
121+
122+
panel.model!.sharedModel.deleteCell(2);
123+
expect(panel.content.widgets).not.toContain(erroredCell);
124+
await model.refresh();
125+
126+
expect(model.headings[0].isRunning).toBe(RunningStatus.Idle);
127+
expect(model.headings[0].dataset!['data-running']).toBe(
128+
RunningStatus.Idle.toString()
129+
);
130+
});
131+
132+
it('should preserve execution status updates during heading refresh', async () => {
133+
panel.model!.sharedModel.insertCells(1, [
134+
{ cell_type: 'code', source: '1 / 0' }
135+
]);
136+
137+
await model.refresh();
138+
139+
const headingCell = panel.content.widgets[0];
140+
const erroredCell = panel.content.widgets[1];
141+
const getHeadings = headingCell.getHeadings.bind(headingCell);
142+
let resumeRefresh: (() => void) | undefined;
143+
const refreshGate = new Promise<void>(resolve => {
144+
resumeRefresh = resolve;
145+
});
146+
const getHeadingsSpy = jest
147+
.spyOn(headingCell, 'getHeadings')
148+
.mockImplementation(async () => {
149+
await refreshGate;
150+
return getHeadings();
151+
});
152+
153+
const refresh = model.refresh();
154+
await Promise.resolve();
155+
156+
model.scheduleExecution(erroredCell);
157+
model.finishExecution(erroredCell, false, new TestKernelError());
158+
159+
resumeRefresh!();
160+
await refresh;
161+
getHeadingsSpy.mockRestore();
162+
163+
expect(model.headings[0].isRunning).toBe(RunningStatus.Error);
164+
expect(model.headings[0].dataset!['data-running']).toBe(
165+
RunningStatus.Error.toString()
166+
);
167+
});
168+
});
75169
});
76170
});

0 commit comments

Comments
 (0)