Files
Netcatty/components/terminal/terminalTabVisibilityRecovery.behavior.test.ts
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

187 lines
8.8 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any, react-hooks/exhaustive-deps */
import assert from 'node:assert/strict';
import test from 'node:test';
import React from 'react';
import { act, create, type ReactTestRenderer } from 'react-test-renderer';
import { useTerminalEffects } from './useTerminalEffects';
import { TERMINAL_SESSION_RESTORE_FOCUS_EVENT } from './focusTerminalSession';
// Exercise the real hook's reveal and settling effects. The injected effect
// hooks omit unrelated backend/auth/font setup; no recovery logic is copied.
function visibilityEffect(effect: React.EffectCallback) {
const body = effect.toString();
return body.includes('const becameVisible')
|| (/hiddenAtRef\.current\s*=\s*Date\.now\(\)/.test(body))
|| ((/hiddenAtRef\.current\s*=\s*null/.test(body)) && body.includes('const timer'))
|| (body.includes('const timer1') && body.includes('const timer2'))
|| body.includes('const handleRestoreFocus')
|| body.includes('const recoverTerminalOnAppResume');
}
function harness(t: any, initiallyHasRuntime = true) {
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
let now = 10_000;
let nextId = 0;
const timers = new Map<number, { at: number; run: () => void }>();
const schedule = (run: () => void, delay = 0) => {
const id = ++nextId;
timers.set(id, { at: now + delay, run });
return id;
};
t.mock.method(Date, 'now', () => now);
t.mock.method(globalThis, 'setTimeout', schedule);
t.mock.method(globalThis, 'clearTimeout', (id: number) => timers.delete(id));
const win = new EventTarget();
const doc = Object.assign(new EventTarget(), { visibilityState: 'visible', querySelector: () => null });
let fullscreen: ((value: boolean) => void) | undefined;
let renderer: ReactTestRenderer;
const documentBefore = Object.getOwnPropertyDescriptor(globalThis, 'document');
Object.defineProperty(globalThis, 'document', { configurable: true, value: doc });
const windowBefore = Object.getOwnPropertyDescriptor(globalThis, 'window');
Object.defineProperty(globalThis, 'window', { configurable: true, value: Object.assign(win, { setTimeout: schedule }) });
t.after(() => {
if (renderer) act(() => renderer.unmount());
if (documentBefore) Object.defineProperty(globalThis, 'document', documentBefore);
else delete (globalThis as any).document;
if (windowBefore) Object.defineProperty(globalThis, 'window', windowBefore);
else delete (globalThis as any).window;
});
const fitTimes: number[] = [];
const calls = { ensure: 0, clear: 0, fit: 0, forcedFit: 0, render: 0, focus: 0 };
const term = { options: {}, focus() { calls.focus++; }, rows: 24, cols: 80, buffer: { active: { type: 'normal' } }, scrollToBottom() {}, _core: { _renderService: { _renderRows() { calls.render++; } } } };
const runtime = { ensureWebglRenderer() { calls.ensure++; }, clearTextureAtlas() { calls.clear++; } };
const values: Record<string, any> = {
host: { id: 'host', protocol: 'ssh' }, auth: {}, zmodem: {}, effectiveTheme: { id: 'theme', colors: {} },
terminalSettings: {}, terminalSettingsRef: { current: {} },
terminalBackend: { onWindowFullScreenChanged(callback: (value: boolean) => void) { fullscreen = callback; return () => { fullscreen = undefined; }; } },
pluginDecorationRules: [], sessionId: 'session', paneLayoutKey: 'layout',
isVisible: false, isVisibleRef: { current: false }, splitResizeActive: false, inWorkspace: false,
termRef: { current: initiallyHasRuntime ? term : null }, xtermRuntimeRef: { current: initiallyHasRuntime ? runtime : null },
fitAddonRef: { current: {} }, sessionRef: { current: 'pty' },
containerRef: { current: { clientWidth: 800, clientHeight: 480 } },
lastFittedSizeRef: { current: { width: 800, height: 480 } }, pendingOutputScrollRef: { current: false },
safeFit(options: any) {
calls.fit++;
fitTimes.push(now);
if (options?.force) calls.forcedFit++;
const container = values.containerRef.current;
values.lastFittedSizeRef.current = { width: container.clientWidth, height: container.clientHeight };
},
forceSyncRenderAfterResize() { calls.render++; }, resizeSession() {},
useEffect(effect: React.EffectCallback, deps: React.DependencyList) {
React.useEffect(visibilityEffect(effect) ? effect : () => {}, deps);
},
useLayoutEffect(effect: React.EffectCallback, deps: React.DependencyList) {
React.useLayoutEffect(visibilityEffect(effect) ? effect : () => {}, deps);
},
};
const ctx = new Proxy(values, { get(target, key: string) {
if (!(key in target)) target[key] = key.endsWith('Ref') ? { current: null } : (() => {});
return target[key];
} });
function Component() { useTerminalEffects(ctx); return null; }
act(() => { renderer = create(React.createElement(Component)); });
function advance(ms: number) {
const end = now + ms;
while (true) {
const next = [...timers.entries()].filter(([, item]) => item.at <= end).sort((a, b) => a[1].at - b[1].at)[0];
if (!next) break;
now = next[1].at;
timers.delete(next[0]);
act(() => next[1].run());
}
now = end;
}
return { calls, fitTimes, advance, values,
focus() { const event = Object.assign(new Event(TERMINAL_SESSION_RESTORE_FOCUS_EVENT), { detail: { sessionId: 'session' } }); act(() => { win.dispatchEvent(event); }); },
resume() { act(() => { doc.dispatchEvent(new Event('visibilitychange')); }); },
fullscreen() { act(() => fullscreen?.(true)); },
reveal() { values.isVisible = true; values.isVisibleRef.current = true; act(() => renderer.update(React.createElement(Component))); },
publishRuntime() { values.termRef.current = term; values.xtermRuntimeRef.current = runtime; },
};
}
for (const hiddenMs of [2000, 4000]) {
test(`unchanged reveal after ${hiddenMs}ms does not repeat recovery in settling timers`, t => {
const h = harness(t);
h.advance(hiddenMs);
h.reveal();
assert.equal(h.calls.clear, hiddenMs >= 3000 ? 1 : 0);
// Every reveal asks for the WebGL renderer once: a no-op while it is live,
// a rebuild when Chromium or the context budget took it while hidden.
assert.equal(h.calls.ensure, 1);
h.advance(600);
assert.equal(h.calls.clear, hiddenMs >= 3000 ? 1 : 0);
assert.equal(h.calls.ensure, 1);
assert.equal(h.calls.fit, 0);
});
}
test('late changed bounds still receive a settling fit', t => {
const h = harness(t);
h.advance(4000); h.reveal(); h.advance(200);
h.values.containerRef.current.clientHeight = 460;
h.advance(400);
assert.equal(h.calls.fit, 1);
assert.equal(h.calls.forcedFit, 0);
assert.deepEqual(h.values.lastFittedSizeRef.current, { width: 800, height: 460 });
});
test('runtime published after reveal still receives delayed recovery', t => {
const h = harness(t, false);
h.advance(4000); h.reveal();
assert.equal(h.calls.clear, 0);
h.advance(25); h.publishRuntime(); h.advance(575);
assert.equal(h.calls.ensure, 1);
assert.equal(h.calls.clear, 1);
assert.equal(h.calls.forcedFit, 1);
});
test('focus restoration and its 50ms retry do not refit unchanged bounds', t => {
const h = harness(t);
h.advance(4000); h.reveal(); h.advance(600);
h.focus(); h.advance(50); h.focus(); h.advance(500);
assert.equal(h.calls.focus, 2);
assert.equal(h.calls.fit, 0);
assert.equal(h.calls.clear, 1);
});
test('focus restoration still recovers changed bounds', t => {
const h = harness(t);
h.advance(4000); h.reveal(); h.advance(600);
h.values.containerRef.current.clientWidth = 780;
h.focus(); h.advance(50); h.focus(); h.advance(100);
assert.equal(h.calls.focus, 2);
assert.equal(h.calls.forcedFit, 2);
assert.deepEqual(h.values.lastFittedSizeRef.current, { width: 780, height: 480 });
});
for (const source of ['resume', 'fullscreen'] as const) {
test(`fitted focus retries preserve pending ${source} recovery timers`, t => {
const h = harness(t);
h.advance(4000); h.reveal(); h.advance(600);
h[source](); h.advance(0);
assert.equal(h.calls.forcedFit, 1);
h.focus(); h.advance(50); h.focus(); h.advance(500);
assert.equal(h.calls.focus, 2);
assert.equal(h.calls.forcedFit, 3, 'all three explicit recovery passes must survive focus retries');
assert.deepEqual(h.fitTimes.map(at => at - h.fitTimes[0]), source === 'resume' ? [0, 100, 300] : [0, 150, 400]);
});
}
for (const source of ['resume', 'fullscreen'] as const) {
test(`focus during changed bounds preserves pending ${source} settling passes`, t => {
const h = harness(t);
h.advance(4000); h.reveal(); h.advance(600);
h[source](); h.advance(0);
h.advance(50);
h.values.containerRef.current.clientWidth = 780;
h.focus(); h.advance(500);
assert.equal(h.calls.focus, 1);
assert.equal(h.calls.forcedFit, 3);
assert.deepEqual(h.fitTimes.map(at => at - h.fitTimes[0]), source === 'resume' ? [0, 100, 300] : [0, 150, 400]);
assert.equal(h.values.lastFittedSizeRef.current.width, 780);
});
}