Файли
openidea/src/features/notebook/session.test.ts
T
LU XIANGRUIтаwildcatDownstairs d959a2a583 feat(notebook): run notebooks on a remote Jupyter server (#6669)
Connect to Jupyter Server... in the notebook kernel drop-down and Find Action takes any URL a Jupyter server prints (lab/notebook/JupyterHub user URLs are reduced to the server base) and a token. The token is checked against /api/kernelspecs and stored in the OS credential store; only the base URL is written to disk. Saved servers are listed in the kernel drop-down with their kernel specs, remembered per notebook, and can be removed again.

A remote kernel is started with POST api/kernels, driven over its channels WebSocket (through the configured HTTP proxy via the new ide_http::tunnel), interrupted with POST .../interrupt and deleted on restart, close, failed or cancelled start. A dropped socket is reopened for the same kernel and session for about 15 s so buffered output is replayed. The kernel transport now has a Link with local (ZeroMQ) and remote variants, sharing execution, output routing and the Jupyter Variables window. Token-less servers get the _xsrf cookie and header.

Closes #6526

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 14:00:21 +08:00

719 рядки
28 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import type * as TauriCore from "@tauri-apps/api/core";
import { EditorState } from "@codemirror/state";
import type { IdeEvent, JupyterServer, NotebookSnapshot } from "@/bindings";
import { IpcError } from "@/platform/ipc";
import type * as Ipc from "@/platform/ipc";
import {
caretAfterHistory,
closeNotebook,
markNotebookActive,
NotebookSession,
notebookBackend,
notebookSession,
notebookSessions,
} from "./session";
type KernelEvent = Extract<IdeEvent, { type: "notebookKernel" }>;
const kernelListeners = vi.hoisted(() => new Set<(event: KernelEvent) => void>());
vi.mock("@/platform/ipc", async (original) => {
const actual = await original<typeof Ipc>();
return {
...actual,
onIdeEvent: (_type: string, listener: (event: KernelEvent) => void) => {
kernelListeners.add(listener);
return () => kernelListeners.delete(listener);
},
};
});
vi.mock("@tauri-apps/api/core", async (original) => ({
...(await original<typeof TauriCore>()),
Channel: class {
onmessage = (_event: unknown) => {};
},
}));
afterEach(() => kernelListeners.clear());
function kernelFailure(message: string) {
for (const listener of kernelListeners)
listener({ type: "notebookKernel", id: "document-a", path: "/p/a.ipynb", state: "dead", message });
}
function deferred<T>() {
let resolve!: (value: T) => void;
let reject!: (error: unknown) => void;
const promise = new Promise<T>((yes, no) => {
resolve = yes;
reject = no;
});
return { promise, resolve, reject };
}
function snapshot(): NotebookSnapshot {
return {
id: "document-a",
path: "/p/a.ipynb",
version: 1,
dirty: false,
nbformat: 4,
nbformatMinor: 5,
metadata: {},
canUndo: false,
canRedo: false,
cells: [
{
id: "cell-a",
cellType: "code",
source: "original",
metadata: {},
attachments: {},
executionCount: null,
outputs: [],
},
],
};
}
function model() {
let document = snapshot();
const backend: typeof notebookBackend = {
...notebookBackend,
open: async () => structuredClone(document),
get: async () => structuredClone(document),
environments: async () => [[], null],
servers: async () => [],
close: async () => null,
interrupt: async () => null,
cancel: async () => {},
edit: async (_id, version, edit) => {
if (version !== document.version) throw new IpcError({ kind: "outOfSync", message: "The document changed" });
if (edit.type !== "setSource") throw new Error("This regression model accepts source edits only");
document = {
...document,
version: document.version + 1,
dirty: true,
canUndo: true,
cells: document.cells.map((cell) => (cell.id === edit.cell ? { ...cell, source: edit.source } : cell)),
};
return structuredClone(document);
},
save: async () => {
document = { ...document, dirty: false };
return structuredClone(document);
},
reload: async () => {
document = snapshot();
return structuredClone(document);
},
cellDocs: async () =>
document.cells
.filter((cell) => cell.cellType === "code")
.map((cell, i) => ({ cell: cell.id, doc: 100 + i, uri: `vscode-notebook-cell:/p/a.ipynb#${cell.id}` })),
};
return {
backend,
read: () => document,
replace: (next: NotebookSnapshot) => {
document = next;
},
};
}
describe("shared notebook mutation session", () => {
it("lists local interpreters without waiting for saved Jupyter servers", async () => {
const m = model();
let answer: (servers: JupyterServer[]) => void = () => {};
m.backend.environments = async () => [[{ name: "Python 3.12", path: "/py" } as never], "/py"];
m.backend.servers = () => new Promise((resolve) => (answer = resolve));
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await vi.waitFor(() => expect(session.getState().environments).toHaveLength(1));
expect(session.getState().servers).toEqual([]);
answer([{ url: "http://h/", kernels: [{ name: "python3", displayName: "Python 3" }] }]);
await vi.waitFor(() => expect(session.getState().servers).toHaveLength(1));
await session.close();
});
it("fetches the code cells' language-server documents when the code cells change", async () => {
const m = model();
const cellDocs = vi.fn(m.backend.cellDocs);
m.backend.cellDocs = cellDocs;
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await vi.waitFor(() => expect(session.getState().cellDocs.get("cell-a")?.doc).toBe(100));
expect(session.cellOfUri("vscode-notebook-cell:/p/a.ipynb#cell-a")).toBe("cell-a");
expect(session.cellOfUri("vscode-notebook-cell:/p/a.ipynb#other")).toBeNull();
// Typing keeps the same cells and the same documents: nothing is republished.
session.edit({ type: "setSource", cell: "cell-a", source: "changed" });
await session.settled();
expect(m.read().cells[0]!.source).toBe("changed");
expect(session.getState().cellDocs.get("cell-a")?.doc).toBe(100);
const added = { ...m.read().cells[0]!, id: "cell-b", source: "" };
m.replace({ ...m.read(), version: m.read().version + 1, cells: [...m.read().cells, added] });
session.edit({ type: "setSource", cell: "cell-a", source: "again" });
await session.settled();
await vi.waitFor(() => expect(session.getState().cellDocs.get("cell-b")?.doc).toBe(101));
});
it("picks up a new DocId the backend hands the same cell", async () => {
// A restarted language server, or a reopened notebook, gives a cell a new DocId while its
// id and source stay the same. Keeping the old one made formatting answer
// "DocId#N is not open".
const m = model();
let base = 100;
const cellDocs = vi.fn(async () =>
m
.read()
.cells.filter((cell) => cell.cellType === "code")
.map((cell) => ({ cell: cell.id, doc: base, uri: `vscode-notebook-cell:/p/a.ipynb#${cell.id}` })),
);
m.backend.cellDocs = cellDocs;
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await vi.waitFor(() => expect(session.getState().cellDocs.get("cell-a")?.doc).toBe(100));
base = 200;
session.edit({ type: "setSource", cell: "cell-a", source: "changed" });
await session.settled();
await vi.waitFor(() => expect(session.getState().cellDocs.get("cell-a")?.doc).toBe(200));
});
it("asks the editor to show a revealed location", () => {
const session = new NotebookSession("/p/a.ipynb", model().backend, true);
session.reveal("cell-a", { line: 1, col: 2 });
const first = session.getState().reveal!;
expect(first).toMatchObject({ cell: "cell-a", selection: { line: 1, col: 2 } });
expect(session.selectedCell).toBe("cell-a");
session.reveal("cell-a");
expect(session.getState().reveal!.seq).toBeGreaterThan(first.seq);
});
it("reveals the nearest cell with an editor before or after a cell", async () => {
const m = model();
const cell = (id: string, cellType: string) => ({ ...m.read().cells[0]!, id, cellType });
m.replace({
...m.read(),
cells: [cell("a", "code"), cell("md", "markdown"), cell("raw", "raw"), cell("b", "code")],
});
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
// A Markdown preview and a raw cell have no editor.
expect(session.revealAdjacent("a", 1, 3)).toBe(true);
expect(session.getState().reveal).toMatchObject({ cell: "b", selection: { line: 0, col: 3 }, align: "auto" });
session.markdownEditing.add("md");
expect(session.revealAdjacent("b", -1, 0)).toBe(true);
expect(session.getState().reveal).toMatchObject({ cell: "md", selection: { col: 0 } });
expect(session.getState().reveal!.selection!.line).toBeGreaterThan(1e6);
expect(session.revealAdjacent("a", -1, 0)).toBe(false);
expect(session.revealAdjacent("b", 1, 0)).toBe(false);
});
it("retires event listeners even when the backend project disappears during close", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
const kernel = session.getState().kernel;
const failure = new IpcError({ kind: "notFound", message: "The backend project was replaced" });
m.backend.close = async () => {
throw failure;
};
await expect(session.close()).rejects.toBe(failure);
kernelFailure("Late failure from the retired kernel");
expect(session.getState().error).toBeNull();
expect(session.getState().kernel).toBe(kernel);
});
it("still releases the backend document when interrupting a running batch fails during close", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
const running = deferred<NotebookSnapshot>();
const retired = new IpcError({ kind: "notFound", message: "notebook project has closed" });
const started = deferred<void>();
m.backend.execute = () => {
started.resolve();
return running.promise;
};
m.backend.interrupt = async () => {
running.reject(retired);
throw retired;
};
const closed = vi.fn(async () => null);
m.backend.close = closed;
const execution = session.execute().catch(() => {});
await started.promise;
await session.close();
await execution;
expect(closed).toHaveBeenCalledWith("document-a");
});
it("retains rapid typing over an older acknowledgement and serializes the latest intent", async () => {
const m = model();
const started = deferred<void>();
const release = deferred<void>();
const edit = m.backend.edit;
let first = true;
m.backend.edit = async (...args) => {
if (first) {
first = false;
started.resolve();
await release.promise;
}
return edit(...args);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "first" });
await started.promise;
session.edit({ type: "setSource", cell: "cell-a", source: "second" });
session.edit({ type: "setSource", cell: "cell-a", source: "latest typing" });
const visible: string[] = [];
const unsubscribe = session.subscribe(() => visible.push(session.cells()[0]!.source));
expect(session.cells()[0]!.source).toBe("latest typing");
expect(session.isDirty()).toBe(true);
release.resolve();
await session.flush();
expect(visible.every((source) => source === "latest typing")).toBe(true);
expect(m.read().cells[0]!.source).toBe("latest typing");
expect(m.read().version).toBe(3);
await session.save();
expect(session.isDirty()).toBe(false);
unsubscribe();
await session.close();
});
it("rebases stable-cell source intent after an OutOfSync response", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
m.replace({ ...snapshot(), version: 4, cells: [{ ...snapshot().cells[0]!, source: "other split changed this" }] });
session.edit({ type: "setSource", cell: "cell-a", source: "retained user intent" });
await session.flush();
expect(m.read().cells[0]!.source).toBe("retained user intent");
expect(session.getState().snapshot?.version).toBe(5);
expect(session.getState().pending).toBe(0);
await session.close();
});
it("orders typing after an in-flight undo so its response cannot overwrite the new source", async () => {
const m = model();
const started = deferred<void>();
const release = deferred<void>();
m.backend.undo = async () => {
started.resolve();
await release.promise;
const current = m.read();
const undone = {
...current,
version: current.version + 1,
canRedo: true,
cells: current.cells.map((cell) => ({ ...cell, source: "original" })),
};
m.replace(undone);
return structuredClone(undone);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "before undo" });
const undo = session.undo();
session.edit({ type: "setSource", cell: "cell-a", source: "immediately after undo" });
await started.promise;
session.edit({ type: "setSource", cell: "cell-a", source: "typing after undo" });
expect(session.cells()[0]!.source).toBe("typing after undo");
release.resolve();
await undo;
await session.flush();
expect(m.read().cells[0]!.source).toBe("typing after undo");
expect(session.cells()[0]!.source).toBe("typing after undo");
expect(m.read().version).toBe(4);
await session.close();
});
it("keeps failed and subsequent source visible/dirty until an explicit retry succeeds", async () => {
const m = model();
const edit = m.backend.edit;
let unavailable = true;
m.backend.edit = async (...args) => {
if (unavailable) throw new Error("Backend disconnected");
return edit(...args);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "unsent draft" });
await expect(session.flush()).rejects.toThrow("Backend disconnected");
session.edit({ type: "setSource", cell: "cell-a", source: "more typing after failure" });
expect(session.cells()[0]!.source).toBe("more typing after failure");
expect(session.pendingSources()).toEqual([{ cell: "cell-a", source: "more typing after failure" }]);
expect(session.isDirty()).toBe(true);
expect(m.read().cells[0]!.source).toBe("original");
unavailable = false;
await session.save();
expect(m.read().cells[0]!.source).toBe("more typing after failure");
expect(session.isDirty()).toBe(false);
await session.close();
});
it("freezes edit admission during a close decision and resumes only after cancellation", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "checkpoint" });
await session.prepareClose();
session.edit({ type: "setSource", cell: "cell-a", source: "must not enter closing document" });
expect(session.cells()[0]!.source).toBe("checkpoint");
session.cancelClose();
session.edit({ type: "setSource", cell: "cell-a", source: "resumed" });
await session.flush();
expect(m.read().cells[0]!.source).toBe("resumed");
await session.close();
});
it("retains a kernel failure arriving before a late source acknowledgement and subsequent undo", async () => {
const m = model();
const started = deferred<void>();
const release = deferred<void>();
const edit = m.backend.edit;
m.backend.edit = async (...args) => {
started.resolve();
await release.promise;
return edit(...args);
};
m.backend.undo = async () => {
const current = m.read();
const undone = {
...current,
version: current.version + 1,
cells: current.cells.map((cell) => ({ ...cell, source: "original" })),
};
m.replace(undone);
return structuredClone(undone);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "pending draft" });
await started.promise;
const failure = "Kernel process exited";
kernelFailure(failure);
release.resolve();
await session.flush();
expect(session.cells()[0]!.source).toBe("pending draft");
expect(session.getState().pending).toBe(0);
expect(session.getState().error).toBe(failure);
await session.undo();
expect(session.cells()[0]!.source).toBe("original");
expect(session.getState().error).toBe(failure);
await session.close();
});
it("retains an external save conflict through editing until save succeeds", async () => {
const m = model();
const save = m.backend.save;
const conflict = new Error("File changed on disk");
m.backend.save = async () => {
throw conflict;
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await expect(session.save()).rejects.toBe(conflict);
session.report(conflict); // The editor's promise catch must preserve ownership.
session.edit({ type: "setSource", cell: "cell-a", source: "draft after conflict" });
await session.flush();
expect(m.read().cells[0]!.source).toBe("draft after conflict");
expect(session.isDirty()).toBe(true);
expect(session.getState().error).toBe(conflict.message);
m.backend.save = save;
await session.save();
expect(session.isDirty()).toBe(false);
expect(session.getState().error).toBeNull();
await session.close();
});
it("clears a recovered mutation failure on source retry without requiring save", async () => {
const m = model();
const edit = m.backend.edit;
const failure = new Error("Edit transport unavailable");
m.backend.edit = async () => {
throw failure;
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "retained draft" });
await expect(session.save()).rejects.toBe(failure);
session.report(failure);
expect(session.getState().error).toBe(failure.message);
expect(session.getState().pending).toBe(1);
expect(session.cells()[0]!.source).toBe("retained draft");
m.backend.edit = edit;
await session.flush();
expect(m.read().cells[0]!.source).toBe("retained draft");
expect(session.getState().pending).toBe(0);
expect(session.isDirty()).toBe(true);
expect(session.getState().error).toBeNull();
await session.close();
});
it("reveals a retained save conflict after a newer mutation failure recovers", async () => {
const m = model();
const conflict = new Error("External save conflict");
m.backend.save = async () => {
throw conflict;
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await expect(session.save()).rejects.toBe(conflict);
const edit = m.backend.edit;
const mutation = new Error("Edit rejected");
m.backend.edit = async () => {
throw mutation;
};
session.edit({ type: "setSource", cell: "cell-a", source: "draft" });
await expect(session.flush()).rejects.toBe(mutation);
expect(session.getState().error).toBe(mutation.message);
m.backend.edit = edit;
await session.flush();
expect(session.cells()[0]!.source).toBe("draft");
expect(session.getState().error).toBe(conflict.message);
await session.close();
});
it("retains execution failure across editing and save, then clears it after successful execution", async () => {
const m = model();
const failure = new Error("Selected interpreter lacks ipykernel");
m.backend.execute = async () => {
throw failure;
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await expect(session.execute()).rejects.toBe(failure);
session.report(failure);
session.edit({ type: "setSource", cell: "cell-a", source: "print(42)" });
await session.save();
expect(m.read().cells[0]!.source).toBe("print(42)");
expect(session.getState().error).toBe(failure.message);
m.backend.execute = async () => structuredClone(m.read());
await session.execute();
expect(session.getState().running).toBe(false);
expect(session.getState().error).toBeNull();
await session.close();
});
it("recovers kernel failure on restart without erasing a save conflict", async () => {
const m = model();
const conflict = new Error("External save conflict");
m.backend.save = async () => {
throw conflict;
};
m.backend.restart = async () => structuredClone(m.read());
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await expect(session.save()).rejects.toBe(conflict);
kernelFailure("Kernel died");
await session.restart();
expect(session.getState().kernel).toBe("idle");
expect(session.getState().frozen).toBe(false);
expect(session.getState().error).toBe(conflict.message);
await session.close();
});
it("retains environment discovery failure through editing, save, execution and restart until discovery recovers", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
const environments = m.backend.environments;
const failure = new Error("Environment discovery unavailable");
m.backend.environments = async () => {
throw failure;
};
await session.detectEnvironments();
session.edit({ type: "setSource", cell: "cell-a", source: "draft" });
await session.save();
m.backend.execute = async () => structuredClone(m.read());
await session.execute();
m.backend.restart = async () => structuredClone(m.read());
await session.restart();
expect(session.getState().error).toBe(failure.message);
m.backend.environments = environments;
await session.detectEnvironments();
expect(session.getState().error).toBeNull();
await session.close();
});
it("recovers reload and document failures after discard without clearing a kernel failure", async () => {
const m = model();
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
const conflict = new Error("External save conflict");
m.backend.save = async () => {
throw conflict;
};
await expect(session.save()).rejects.toBe(conflict);
const edit = m.backend.edit;
m.backend.edit = async () => {
throw new Error("Edit unavailable");
};
session.edit({ type: "setSource", cell: "cell-a", source: "discarded draft" });
await expect(session.flush()).rejects.toThrow("Edit unavailable");
const reload = m.backend.reload;
const failure = new Error("Reload unavailable");
m.backend.reload = async () => {
throw failure;
};
await expect(session.discard()).rejects.toBe(failure);
session.report(failure);
kernelFailure("Kernel is still dead");
m.backend.reload = reload;
m.backend.edit = edit;
await session.discard();
expect(session.cells()[0]!.source).toBe("original");
expect(session.getState().pending).toBe(0);
expect(session.isDirty()).toBe(false);
expect(session.getState().error).toBe("Kernel is still dead");
session.cancelClose();
m.backend.restart = async () => structuredClone(m.read());
await session.restart();
expect(session.getState().error).toBeNull();
await session.close();
});
it("retains restart failure across editing and clears it only after a successful restart", async () => {
const m = model();
const failure = new Error("Kernel restart failed");
m.backend.restart = async () => {
throw failure;
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
await expect(session.restart()).rejects.toBe(failure);
session.report(failure);
expect(session.getState().frozen).toBe(false);
session.edit({ type: "setSource", cell: "cell-a", source: "editable after failed restart" });
await session.flush();
expect(session.cells()[0]!.source).toBe("editable after failed restart");
expect(session.getState().error).toBe(failure.message);
m.backend.restart = async () => structuredClone(m.read());
await session.restart();
expect(session.getState().error).toBeNull();
await session.close();
});
});
describe("caretAfterHistory", () => {
it("goes to the end of the text the history step changed", () => {
// Undo of typing: the inserted text is gone, the caret sits where it was.
expect(caretAfterHistory("a = 1\nb = 22\n", "a = 1\nb = 2\n", 0)).toEqual({ line: 1, col: 5 });
// Redo of typing: the caret follows the re-inserted text.
expect(caretAfterHistory("a = 1\n", "a = 1\nprint(a)\n", 0)).toEqual({ line: 2, col: 0 });
expect(caretAfterHistory("x\n", "x = 10\n", 0)).toEqual({ line: 0, col: 6 });
});
it("keeps the old caret when the cell did not change", () => {
expect(caretAfterHistory("abc\ndef", "abc\ndef", 6)).toEqual({ line: 1, col: 2 });
});
});
describe("undo and redo keep the focused cell's caret", () => {
/** A backend whose history responses set every cell's source back to the original. */
function historyModel(direction: "undo" | "redo") {
const m = model();
m.backend[direction] = async () => {
const current = m.read();
const reverted = {
...current,
version: current.version + 1,
canRedo: true,
cells: current.cells.map((cell) => ({ ...cell, source: "original" })),
};
m.replace(reverted);
return structuredClone(reverted);
};
return m;
}
it("reveals the undone change in the focused cell after an undo", async () => {
const m = historyModel("undo");
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "original + typed" });
await session.settled();
// The caret had moved to the start; undo removes " + typed", so it goes where that was.
session.focusedCell = "cell-a";
session.editors.set("cell-a", EditorState.create({ doc: "original + typed", selection: { anchor: 0 } }));
await session.undo();
expect(session.getState().reveal).toMatchObject({
cell: "cell-a",
selection: { line: 0, col: 8 },
align: "auto",
});
await session.close();
});
it("reveals the focused cell's caret after a redo", async () => {
const m = historyModel("redo");
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.focusedCell = "cell-a";
session.editors.set("cell-a", EditorState.create({ doc: "original", selection: { anchor: 4 } }));
await session.redo();
expect(session.getState().reveal).toMatchObject({ cell: "cell-a", selection: { line: 0, col: 4 } });
await session.close();
});
it("does not reveal a location when no cell editor is focused", async () => {
const m = historyModel("undo");
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.edit({ type: "setSource", cell: "cell-a", source: "typed text" });
await session.settled();
await session.undo();
expect(session.getState().reveal).toBeNull();
await session.close();
});
it("skips the caret restore when the undo removes the focused cell", async () => {
const m = historyModel("undo");
m.backend.undo = async () => {
const current = m.read();
const emptied = { ...current, version: current.version + 1, cells: [] };
m.replace(emptied);
return structuredClone(emptied);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.focusedCell = "cell-a";
session.editors.set("cell-a", EditorState.create({ doc: "typed text", selection: { anchor: 6 } }));
await session.undo();
expect(session.getState().reveal).toBeNull();
await session.close();
});
it("skips the caret restore when the focused cell lost its editor", async () => {
const m = historyModel("undo");
m.backend.undo = async () => {
const current = m.read();
const markdown = { ...current.cells[0]!, cellType: "markdown" as const };
const converted = { ...current, version: current.version + 1, cells: [markdown] };
m.replace(converted);
return structuredClone(converted);
};
const session = new NotebookSession("/p/a.ipynb", m.backend, true);
await session.load();
session.focusedCell = "cell-a";
session.editors.set("cell-a", EditorState.create({ doc: "typed text", selection: { anchor: 6 } }));
await session.undo();
expect(session.getState().reveal).toBeNull();
await session.close();
});
});
describe("notebookSessions", () => {
it("ends with the notebook made active last", async () => {
const [a, b] = ["/p/a.ipynb", "/p/b.ipynb"];
notebookSession(a);
notebookSession(b);
markNotebookActive(a);
markNotebookActive("/p/unopened.ipynb");
expect([...notebookSessions()].map((s) => s.path)).toEqual([b, a]);
await Promise.all([closeNotebook(a), closeNotebook(b)]);
});
});