mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* fix(terminal): soft-hide the tab that just left when hibernate keep is full Evict the oldest soft-hidden session to a full snapshot, and keep the session the user just left on hideRuntimeOnly. Disconnected sessions, alternate screen, and inline images keep their existing branches. Wake replay order is unchanged. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(terminal): full-hibernate when keep-count eviction is refused A soft-hidden session with inline images consumes the eviction request and stays mounted. Soft-hiding the tab that just left anyway let that protected session pin the oldest slot, so every later tab was retained too. Walk oldest-first, keep the leaving tab soft-hidden only after one victim actually leaves the set, and full-hibernate it when none can. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(terminal): retain a leaving tab only after eviction hibernates Waking a soft-hidden victim clears its marker before fullHibernateRuntime finishes. Treating that as success soft-hid the tab that just left even when drain, snapshot, or alternate-screen failure restored the victim. Await hibernate completion outside the pure keep-count helper, and put a failed victim back in the soft-hidden set so it stays a candidate. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(terminal): reserve hibernate slots outside domain policy Keep the keep-count decision pure. The application walk reserves the leaving tab before awaiting a victim, rechecks that slot before soft-hide, and does not restore a victim that became visible or had hibernate turned off while the upgrade was in flight. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
146 lines
6.0 KiB
TypeScript
146 lines
6.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { readFileSync } from "node:fs";
|
|
|
|
const runtimeSource = readFileSync(
|
|
new URL("./runtime/createXTermRuntime.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
const terminalSource = readFileSync(new URL("../Terminal.tsx", import.meta.url), "utf8");
|
|
|
|
const readCallbackBody = (source: string, marker: string): string => {
|
|
const start = source.indexOf(marker);
|
|
assert.notEqual(start, -1, `${marker} must exist`);
|
|
|
|
const bodyStart = source.indexOf("{", start);
|
|
assert.notEqual(bodyStart, -1, `${marker} must have a body`);
|
|
|
|
let depth = 0;
|
|
for (let index = bodyStart; index < source.length; index += 1) {
|
|
const char = source[index];
|
|
if (char === "{") depth += 1;
|
|
else if (char === "}") {
|
|
depth -= 1;
|
|
if (depth === 0) return source.slice(bodyStart, index + 1);
|
|
}
|
|
}
|
|
|
|
assert.fail(`${marker} body must close`);
|
|
};
|
|
|
|
test("the image addon is created from settings and only when inline images are on", () => {
|
|
assert.match(runtimeSource, /import \{ ImageAddon \} from "@xterm\/addon-image"/);
|
|
assert.match(
|
|
runtimeSource,
|
|
/const inlineImageOptions = resolveTerminalInlineImageAddonOptions\(settings\);[\s\S]{0,400}?if \(inlineImageOptions\) \{[\s\S]{0,300}?new ImageAddon\(inlineImageOptions\)[\s\S]{0,120}?term\.loadAddon\(imageAddon\)/,
|
|
"the addon must be constructed from resolved settings and skipped when they resolve to null",
|
|
);
|
|
|
|
const openIndex = runtimeSource.indexOf("term.open(ctx.container)");
|
|
const imageAddonIndex = runtimeSource.indexOf("new ImageAddon(inlineImageOptions)");
|
|
const webglIndex = runtimeSource.indexOf("const webglController = createWebglRendererController(");
|
|
|
|
assert.ok(openIndex !== -1 && imageAddonIndex !== -1 && webglIndex !== -1);
|
|
assert.ok(
|
|
openIndex < imageAddonIndex,
|
|
"the addon needs the render service, so it must be loaded after term.open",
|
|
);
|
|
assert.ok(
|
|
imageAddonIndex < webglIndex,
|
|
"the addon must patch setRenderer before the WebGL renderer is created, so image layers survive renderer swaps",
|
|
);
|
|
});
|
|
|
|
test("the image addon is disposed before the terminal that owns its layers", () => {
|
|
const disposeBody = readCallbackBody(runtimeSource, " dispose: () => {");
|
|
const imageDisposeIndex = disposeBody.indexOf("imageAddon.dispose()");
|
|
const termDisposeIndex = disposeBody.indexOf("term.dispose()");
|
|
|
|
assert.notEqual(imageDisposeIndex, -1, "runtime dispose must release the image addon");
|
|
assert.notEqual(termDisposeIndex, -1);
|
|
assert.ok(
|
|
imageDisposeIndex < termDisposeIndex,
|
|
"image storage and canvas layers must be released before xterm tears down its screen element",
|
|
);
|
|
});
|
|
|
|
test("hasInlineImages reports live image storage instead of a sticky flag", () => {
|
|
assert.match(runtimeSource, /hasInlineImages: \(\) => boolean;/);
|
|
assert.match(
|
|
runtimeSource,
|
|
/const hasInlineImages = \(\): boolean => \{[\s\S]{0,200}?imageAddon\.storageUsage > 0/,
|
|
"a session must stop blocking hibernate once its image cache is emptied",
|
|
);
|
|
assert.match(runtimeSource, /\n {4}hasInlineImages,\n/, "the runtime must expose hasInlineImages");
|
|
});
|
|
|
|
test("a session holding inline images degrades to soft-hide instead of full hibernate", () => {
|
|
assert.match(
|
|
terminalSource,
|
|
/const runtimeHasInlineImages = useCallback\(\s*\(\) => xtermRuntimeRef\.current\?\.hasInlineImages\(\) === true,/,
|
|
);
|
|
|
|
const hibernateBody = readCallbackBody(terminalSource, "const hibernateRuntime = useCallback(() =>");
|
|
const gateIndex = hibernateBody.indexOf("if (runtimeHasInlineImages()) {");
|
|
const softHideIndex = hibernateBody.indexOf("hideRuntimeOnly();", gateIndex);
|
|
const keepCountIndex = hibernateBody.indexOf("resolveHibernateKeepRendererCount(terminalSettings)");
|
|
const evictionIndex = hibernateBody.indexOf("retainLeavingTabAfterEviction(");
|
|
const fullHibernateIndex = hibernateBody.indexOf("void fullHibernateRuntime()");
|
|
|
|
assert.notEqual(gateIndex, -1, "hibernate must check for inline images");
|
|
assert.ok(gateIndex < softHideIndex, "the inline image branch must soft-hide the runtime");
|
|
assert.ok(
|
|
gateIndex < keepCountIndex && gateIndex < evictionIndex && gateIndex < fullHibernateIndex,
|
|
"the inline image branch must run before the keep-count, eviction and full-hibernate paths",
|
|
);
|
|
});
|
|
|
|
test("full hibernate and the eviction upgrade both re-check inline images", () => {
|
|
const fullHibernateBody = readCallbackBody(
|
|
terminalSource,
|
|
"const fullHibernateRuntime = useCallback(async (): Promise<boolean> =>",
|
|
);
|
|
const canFinishIndex = fullHibernateBody.indexOf("const canFinishHibernate = () => (");
|
|
const imageCheckIndex = fullHibernateBody.indexOf("&& !runtimeHasInlineImages()", canFinishIndex);
|
|
const canFinishEndIndex = fullHibernateBody.indexOf(");", canFinishIndex);
|
|
|
|
assert.notEqual(canFinishIndex, -1);
|
|
assert.notEqual(imageCheckIndex, -1, "canFinishHibernate must reject sessions holding images");
|
|
assert.ok(
|
|
imageCheckIndex < canFinishEndIndex,
|
|
"the image check must be part of canFinishHibernate so it is re-evaluated after every async step",
|
|
);
|
|
|
|
const evictionIndex = terminalSource.indexOf(
|
|
"terminalHiddenRendererStore.consumeEvictionRequest(sessionId)",
|
|
);
|
|
const evictionGuardIndex = terminalSource.indexOf(
|
|
"if (runtimeHasInlineImages()) return;",
|
|
evictionIndex,
|
|
);
|
|
const evictionUpgradeIndex = terminalSource.indexOf(
|
|
"upgradeSoftHiddenRuntimeToHibernate();",
|
|
evictionIndex,
|
|
);
|
|
const upgradeBody = readCallbackBody(
|
|
terminalSource,
|
|
"const upgradeSoftHiddenRuntimeToHibernate = useCallback(() =>",
|
|
);
|
|
|
|
assert.notEqual(evictionIndex, -1);
|
|
assert.notEqual(
|
|
evictionGuardIndex,
|
|
-1,
|
|
"an evicted soft-hidden session with images must stay soft-hidden",
|
|
);
|
|
assert.ok(
|
|
evictionGuardIndex < evictionUpgradeIndex,
|
|
"the image guard must run before the renderer is woken for the hibernate upgrade",
|
|
);
|
|
assert.match(
|
|
upgradeBody,
|
|
/wakeSoftHiddenRuntimeRef\.current\?\.\(\);[\s\S]*?fullHibernateRuntime\(\)/,
|
|
"the shared upgrade path must wake the soft-hidden renderer before hibernating",
|
|
);
|
|
});
|