Files
Netcatty/components/notes/NoteSourceEditor.test.tsx
T
陈大猫andClaude Opus 5.5 d56150953d perf(terminal): cap live WebGL contexts, release them eagerly, bound note undo history (#4122)
* perf(terminal): cap live WebGL contexts and stop the context-loss cascade

Chromium keeps at most 16 WebGL contexts per renderer and force-loses the
one that drew least recently past that. Hidden terminal panes keep their
WebGL renderer unless hibernation is on, so with more than 16 terminal
tabs every forced loss was rebuilt 50 ms later, evicting another context.
With 20 local tabs, half of all tab switches landed on a lost context
(xterm waits 3 s before falling back), and terminals that lost three
contexts within a minute were parked on the DOM renderer for good.

- Add a shared context budget (12). Past it, renderers of panes hidden for
  at least 3 s are released, least recently seen first. Visible panes are
  never released or refused; a released pane rebuilds when shown.
- A context lost while hidden waits for the reveal instead of rebuilding
  at once, and no longer counts toward the breaker.
- The short-hide CSS reveal path now also asks for the renderer (a no-op
  while it is live).
- Disposing the addon left its context alive until GC. Lose it explicitly
  so GPU memory and the Chromium slot come back right away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(notes): bound the source editor's undo history

Every undo step in the markdown source editor holds a full copy of the
note, and the stack never shrank. A long session on a large note kept
every intermediate version alive (a 100 KB note with 1,000 steps holds
~200 MB of UTF-16 text).

Keep the 200 most recent steps and at most 8M characters across them,
dropping the oldest first. The newest step always survives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 00:22:42 -08:00

292 lines
9.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import React from "react";
import { act, create, type ReactTestRenderer } from "react-test-renderer";
import {
MAX_SOURCE_HISTORY_CHARS,
MAX_SOURCE_HISTORY_STEPS,
NoteSourceEditor,
type NoteSourceEditorHandle,
pushSourceHistory,
} from "./NoteSourceEditor.tsx";
const snapshot = (value: string) => ({ value, selectionStart: 0, selectionEnd: 0 });
test("source undo history keeps only the most recent steps", () => {
const stack: Array<ReturnType<typeof snapshot>> = [];
for (let step = 0; step < MAX_SOURCE_HISTORY_STEPS + 50; step += 1) {
pushSourceHistory(stack, snapshot(`v${step}`));
}
assert.equal(stack.length, MAX_SOURCE_HISTORY_STEPS);
assert.equal(stack[0].value, "v50");
assert.equal(stack.at(-1)?.value, `v${MAX_SOURCE_HISTORY_STEPS + 49}`);
});
test("source undo history drops old full-note copies past the text budget", () => {
const stack: Array<ReturnType<typeof snapshot>> = [];
const third = Math.floor(MAX_SOURCE_HISTORY_CHARS / 3) + 1;
for (const label of ["a", "b", "c", "d"]) pushSourceHistory(stack, snapshot(label.repeat(third)));
assert.deepEqual(stack.map((entry) => entry.value[0]), ["c", "d"]);
// The newest step always survives, even when it alone is over budget.
pushSourceHistory(stack, snapshot("e".repeat(MAX_SOURCE_HISTORY_CHARS + 1)));
assert.deepEqual(stack.map((entry) => entry.value[0]), ["e"]);
});
test("NoteSourceEditor toolbar undo restores coalesced ordinary typing", async () => {
const actEnvironment = globalThis as typeof globalThis & {
IS_REACT_ACT_ENVIRONMENT?: boolean;
requestAnimationFrame?: (callback: FrameRequestCallback) => number;
};
const previousActEnvironment = actEnvironment.IS_REACT_ACT_ENVIRONMENT;
const previousAnimationFrame = actEnvironment.requestAnimationFrame;
actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
actEnvironment.requestAnimationFrame = (callback) => {
callback(0);
return 0;
};
const editorRef = React.createRef<NoteSourceEditorHandle>();
const changes: string[] = [];
const textareaNode = {
selectionStart: 1,
selectionEnd: 1,
focus: () => undefined,
setSelectionRange(start: number, end: number) {
this.selectionStart = start;
this.selectionEnd = end;
},
scrollTop: 0,
scrollTo: () => undefined,
};
let renderer: ReactTestRenderer | null = null;
try {
await act(async () => {
renderer = create(
<NoteSourceEditor
ref={editorRef}
noteId="note-1"
value="a"
onChange={(nextValue) => changes.push(nextValue)}
/>,
{
createNodeMock: (element) => element.type === "textarea"
? textareaNode
: { scrollTop: 0 },
},
);
});
const textarea = renderer!.root.findByType("textarea");
await act(async () => {
textarea.props.onChange({
target: { value: "ab" },
nativeEvent: { inputType: "insertText" },
});
});
await act(async () => {
textarea.props.onChange({
target: { value: "abc" },
nativeEvent: { inputType: "insertText" },
});
});
await act(async () => {
editorRef.current?.insertAction("undo");
});
let prevented = false;
await act(async () => {
renderer!.root.findByType("textarea").props.onKeyDown({
key: "z",
metaKey: true,
ctrlKey: false,
altKey: false,
shiftKey: true,
preventDefault: () => { prevented = true; },
});
});
const composedTextarea = renderer!.root.findByType("textarea");
await act(async () => {
composedTextarea.props.onCompositionStart();
composedTextarea.props.onChange({
target: { value: "abc你", selectionStart: 4 },
nativeEvent: { inputType: "insertCompositionText" },
});
});
await act(async () => {
renderer!.root.findByType("textarea").props.onCompositionEnd();
});
await act(async () => {
renderer!.root.findByType("textarea").props.onCompositionStart();
renderer!.root.findByType("textarea").props.onChange({
target: { value: "abc你好", selectionStart: 5 },
nativeEvent: { inputType: "insertCompositionText" },
});
});
await act(async () => {
renderer!.root.findByType("textarea").props.onCompositionEnd();
});
await act(async () => editorRef.current?.insertAction("undo"));
await act(async () => editorRef.current?.insertAction("undo"));
assert.equal(prevented, true);
assert.deepEqual(changes, ["ab", "abc", "a", "abc", "abc你", "abc你好", "abc你", "abc"]);
assert.equal(renderer!.root.findByType("textarea").props.value, "abc");
} finally {
await act(async () => {
renderer?.unmount();
});
actEnvironment.IS_REACT_ACT_ENVIRONMENT = previousActEnvironment;
actEnvironment.requestAnimationFrame = previousAnimationFrame;
}
});
test("NoteSourceEditor keeps formatting and later typing as separate undo steps", async () => {
const actEnvironment = globalThis as typeof globalThis & {
IS_REACT_ACT_ENVIRONMENT?: boolean;
requestAnimationFrame?: (callback: FrameRequestCallback) => number;
};
const previousActEnvironment = actEnvironment.IS_REACT_ACT_ENVIRONMENT;
const previousAnimationFrame = actEnvironment.requestAnimationFrame;
actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
actEnvironment.requestAnimationFrame = (callback) => {
callback(0);
return 0;
};
const editorRef = React.createRef<NoteSourceEditorHandle>();
const changes: string[] = [];
const textareaNode = {
selectionStart: 0,
selectionEnd: 0,
focus: () => undefined,
setSelectionRange(start: number, end: number) {
this.selectionStart = start;
this.selectionEnd = end;
},
scrollTop: 0,
scrollTo: () => undefined,
};
let renderer: ReactTestRenderer | null = null;
try {
await act(async () => {
renderer = create(
<NoteSourceEditor
ref={editorRef}
noteId="note-1"
value=""
onChange={(nextValue) => changes.push(nextValue)}
/>,
{
createNodeMock: (element) => element.type === "textarea"
? textareaNode
: { scrollTop: 0 },
},
);
});
await act(async () => {
editorRef.current?.insertAction("bold");
});
const formatted = "**bold text**";
const edited = "**bold text!**";
const textarea = renderer!.root.findByType("textarea");
await act(async () => {
textarea.props.onChange({
target: { value: edited },
nativeEvent: { inputType: "insertText" },
});
});
await act(async () => {
editorRef.current?.insertAction("undo");
});
await act(async () => {
editorRef.current?.insertAction("undo");
});
assert.deepEqual(changes, [formatted, edited, formatted, ""]);
assert.equal(renderer!.root.findByType("textarea").props.value, "");
} finally {
await act(async () => {
renderer?.unmount();
});
actEnvironment.IS_REACT_ACT_ENVIRONMENT = previousActEnvironment;
actEnvironment.requestAnimationFrame = previousAnimationFrame;
}
});
test("NoteSourceEditor undo and redo restore toolbar formatting selections", async () => {
const actEnvironment = globalThis as typeof globalThis & {
IS_REACT_ACT_ENVIRONMENT?: boolean;
requestAnimationFrame?: (callback: FrameRequestCallback) => number;
};
const previousActEnvironment = actEnvironment.IS_REACT_ACT_ENVIRONMENT;
const previousAnimationFrame = actEnvironment.requestAnimationFrame;
actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
actEnvironment.requestAnimationFrame = (callback) => {
callback(0);
return 0;
};
const editorRef = React.createRef<NoteSourceEditorHandle>();
const textareaNode = {
selectionStart: 0,
selectionEnd: 5,
focus: () => undefined,
setSelectionRange(start: number, end: number) {
this.selectionStart = start;
this.selectionEnd = end;
},
scrollTop: 0,
scrollTo: () => undefined,
};
let renderer: ReactTestRenderer | null = null;
try {
await act(async () => {
renderer = create(
<NoteSourceEditor
ref={editorRef}
noteId="note-1"
value="hello"
onChange={() => undefined}
/>,
{
createNodeMock: (element) => element.type === "textarea"
? textareaNode
: { scrollTop: 0 },
},
);
});
await act(async () => editorRef.current?.insertAction("bold"));
assert.deepEqual(
{ start: textareaNode.selectionStart, end: textareaNode.selectionEnd },
{ start: 2, end: 7 },
);
await act(async () => editorRef.current?.insertAction("undo"));
assert.equal(renderer!.root.findByType("textarea").props.value, "hello");
assert.deepEqual(
{ start: textareaNode.selectionStart, end: textareaNode.selectionEnd },
{ start: 0, end: 5 },
);
await act(async () => editorRef.current?.insertAction("redo"));
assert.equal(renderer!.root.findByType("textarea").props.value, "**hello**");
assert.deepEqual(
{ start: textareaNode.selectionStart, end: textareaNode.selectionEnd },
{ start: 2, end: 7 },
);
} finally {
await act(async () => {
renderer?.unmount();
});
actEnvironment.IS_REACT_ACT_ENVIRONMENT = previousActEnvironment;
actEnvironment.requestAnimationFrame = previousAnimationFrame;
}
});