Files
Netcatty/components/terminal/useTerminalAutocomplete.render.test.tsx
T
Fyhax1kiandfyhax1ki d0e024625c feat: add clear-all command history action (#3632)
Add the Global history clear-all action with confirmation and five-language copy. Clear local shell history, autocomplete and compose recall while preserving drafts, keeping peer windows consistent on clears, and allowing new commands afterwards.

Verified with 116 focused checks, two clean independent final reviews, real Electron multi-window SSH-fixture interaction and restart, current-head Codex review, and both required CI jobs.

Co-authored-by: fyhax1ki <fyhax1ki@gmail.com>
2026-10-15 18:00:43 +08:00

102 lines
4.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { useRef } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { useTerminalAutocomplete } from "./autocomplete/useTerminalAutocomplete.ts";
test("useTerminalAutocomplete can render before any autocomplete interaction", () => {
function Probe() {
const termRef = useRef(null);
const containerRef = useRef(null);
const autocomplete = useTerminalAutocomplete({
termRef,
containerRef,
sessionId: "session-1",
hostId: "host-1",
hostOs: "linux",
onAcceptText: () => {},
});
return <span>{typeof autocomplete.repositionPopup}</span>;
}
assert.doesNotThrow(() => {
renderToStaticMarkup(<Probe />);
});
});
test("network-device sessions skip live-preview writeToTerminal (#1193)", () => {
const source = readFileSync(
fileURLToPath(new URL("./autocomplete/useTerminalAutocomplete.ts", import.meta.url)),
"utf8",
);
assert.match(
source,
/livePreview: shouldWriteAutocompleteLivePreview\(rawSettings\.livePreview, isNetworkDevice\)/,
);
assert.match(
source,
/const renderPreviewSelection = useCallback\(\(index: number\) => \{\s*if \(!shouldWriteAutocompleteLivePreview\(\s*settingsRef\.current\.livePreview,\s*isNetworkDeviceRef\.current,\s*\)\) return;[\s\S]*?if \(seq\) writeToTerminal\(seq\);/,
);
assert.match(
source,
/const renderSubDirPath = useCallback\(\(level: number, entry: SubDirEntry\) => \{\s*if \(!shouldWriteAutocompleteLivePreview\(\s*settingsRef\.current\.livePreview,\s*isNetworkDeviceRef\.current,\s*\)\) return;[\s\S]*?if \(seq\) writeToTerminal\(seq\);/,
);
});
test("repositionPopup pins the visible popup to the wrapped command start (#3061)", () => {
const source = readFileSync(
fileURLToPath(new URL("./autocomplete/useTerminalAutocomplete.ts", import.meta.url)),
"utf8",
);
assert.match(source, /resolveAutocompletePopupAnchorInViewport/);
assert.match(source, /nextAutocompletePopupAnchorViewport/);
assert.match(source, /if \(!stateRef\.current\.popupVisible \|\| stateRef\.current\.suggestions\.length === 0\) return;/);
});
test("mount effect re-arms disposedRef after dispose cleanup (HMR / StrictMode)", () => {
// dispose() sets disposedRef=true on effect cleanup. Fast Refresh preserves
// refs, so without resetting on mount, fetchSuggestions stays dead forever
// while handleInput keeps scheduling — "fetch-scheduled" with no popup.
const source = readFileSync(
fileURLToPath(new URL("./autocomplete/useTerminalAutocomplete.ts", import.meta.url)),
"utf8",
);
assert.ok(source.includes("disposedRef.current = false;"));
assert.ok(source.includes("return () => { dispose(); };"));
});
test("single-channel / network-device sessions skip remote path listings", () => {
const source = readFileSync(
fileURLToPath(new URL("./autocomplete/useTerminalAutocomplete.ts", import.meta.url)),
"utf8",
);
assert.match(
source,
/const fetchDirEntries = useCallback\(async \(dirPath: string\): Promise<SubDirEntry\[\]> => \{\s*if \(isNetworkDeviceRef\.current\) return \[\];/,
);
assert.match(source, /skipPathCompletion: isNetworkDeviceRef\.current,/);
});
test("clearing command history restores the live-preview baseline (#3632)", () => {
const source = readFileSync(
fileURLToPath(new URL("./autocomplete/useTerminalAutocomplete.ts", import.meta.url)),
"utf8",
);
// A live-previewed candidate is written into the shell line, so the clear
// handler must mirror the Escape path: restore the typed baseline, reset the
// preview/accepted refs, and only then hide the popup.
assert.match(
source,
/const handleCommandHistoryCleared = useCallback\(\(\) => \{\s*if \(previewActiveRef\.current\) \{\s*renderPreviewSelection\(-1\);[\s\S]*?clearState\(\);\s*previewActiveRef\.current = false;[\s\S]*?lastAcceptedCommandRef\.current = null;\s*\}, \[clearState, renderPreviewSelection\]\);/,
);
assert.match(
source,
/useEffect\(\s*\(\) => subscribeCommandHistoryCleared\(handleCommandHistoryCleared\),\s*\[handleCommandHistoryCleared\],\s*\);/,
);
});