Files
Netcatty/components/terminal/runtime/terminalCloseCapture.test.ts
T
陈大猫 f6bac5fcf0 fix(terminal): batch hidden log output (#2399)
* fix(terminal): batch hidden log output

* fix(terminal): preserve ordered hidden output

* fix(terminal): bound hidden output overhead

* fix(terminal): respect custom tab stops in timestamps

* fix(terminal): preserve terminal parser state

* fix(terminal): preserve hidden output boundaries

* fix(terminal): retain batching with prompt formatting

* fix(terminal): preserve prompt boundaries through transforms

* fix(terminal): handle unbatched prompt output

* fix(terminal): preserve batched prompt and time boundaries

* fix(terminal): retain background timestamp boundaries

* fix(terminal): preserve visible timestamp boundaries

* fix(terminal): separate shell output from tui frames

* fix(terminal): separate output after tui exit

* fix(terminal): preserve prompts across output slices

* fix(terminal): flush output before context reads

* fix(terminal): time-slice timestamp marker cleanup

* fix(terminal): preserve prompt columns after bare LF

* fix(terminal): preserve background write yielding

* fix(terminal): retain yields across timestamp boundaries

* perf(terminal): scan output boundaries once

* fix(terminal): track prompt cursor movement

* fix(terminal): resume prompt scan after resets

* fix(terminal): preserve stateful prompt ordering

* fix(terminal): drain close output before capture

* fix(terminal): measure control-only prompt prefixes

* fix(terminal): preserve prompt control boundaries

* fix(terminal): preserve prompt mode toggles

* fix(terminal): bound hidden batches and unfocus policy

Always drain write-coalescer size caps even when scheduled frames are gated,
keep unfocused-but-visible panes on throttled batching instead of per-chunk
force flush, compute prompt starts on post-transform display data, and model
full-width wrap so prompt-on-newline does not insert a blank line.

* perf(terminal): skip off-path timestamps and share alt-screen probes

When host line timestamps are disabled, write plain bytes without the
segmenter/marker hot path. Reuse one alt-screen CSI probe per coalesced
chunk for frame split and schedule mode, and reuse a single full-batch
visual-row measure on the single-segment batched timestamp path.

* fix(terminal): read line-timestamp enable from live hostRef

Session data handlers close over boot-time ctx.host, so toolbar/vault
toggles of showLineTimestamps were ignored. Prefer hostRef.current for
the marker hot-path gate and keep hostRef synced on every Terminal render.

* perf(terminal): per-second timestamp ledger while gutter is off

When line timestamps are hidden, keep a lightweight one-stamp-per-second
ledger without xterm markers. Opening the gutter materializes recent
ledger entries into markers for paint; while visible, keep per-line
markers for accuracy.

* perf(terminal): separate timestamp record from gutter paint

Always record a one-stamp-per-second ledger on the write path with no
xterm markers. Gutter rendering maps the ledger to visible rows, so open
and closed modes share the same cheap record path.

* fix(terminal): fill gutter gaps with previous second stamp

When painting line timestamps from the per-second ledger, blank rows
between stamps inherit the preceding stamp's label so multi-line output
in the same second is fully labeled.

* fix(terminal): stop empty gutter paint and false scrollback rebase

Only rebase ledger anchors when the buffer is actually full and baseY
grows from top trim — not when baseY rises while the buffer is still
growing (which deleted MOTD/login stamps on scroll). Cap gutter paint at
the last content line so empty viewport rows past the cursor stay blank.

* fix(terminal): soft-wrap stamp placement and sparse reflow anchors

Estimate ledger lines with visual-row measurement so narrow terminals
do not pin the next second on a wrap continuation. After each write,
attach at most one xterm marker per stamped second so reflow and
scrollback trim keep anchors honest without per-line marker storms.

* fix(terminal): ignore alt-screen rows when placing timestamp anchors

Stamp line estimates froze incorrectly through vim/less alt-screen
output, so sparse reflow markers landed far below the restored normal
buffer. Skip visual-row advancement while DEC alt-screen is active,
key one-stamp-per-second from the batch arrival date, and only attach
anchors still present in the ledger after capacity trim.

* fix(terminal): seed stamp lines from normal buffer when already on alt

When a write begins on the alternate screen, active cursor is the TUI
row — freezing that through 1049 leave mis-pinned sparse anchors and
dropped them past normal bufferLength. Seed and restore stamp estimates
from buffer.normal, and cover multi-write already-on-alt leave+prompt.

* fix(terminal): keep same-chunk pre-enter line on alt leave

Leave CSI must not always re-read pre-write buffer.normal: when enter
happened in the same walk, the frozen estimate already includes normal
advances before the TUI. Only restore buffer.normal when the write
started already on the alternate screen.

* fix(terminal): pure alt stamp transitions that never rewind lines

Stamp estimates stay in normal-buffer coordinates: seed once (active or
buffer.normal when already on alt), freeze while alt is active, and on
leave only clear altActive — never re-read the live term mid-walk.
Export applyAltScreenAction with a table-driven unit test covering
spurious leave, plus write-path cases for already-on-alt, same-chunk
pre-enter advance, and leave-without-enter.
2026-07-23 22:18:31 +08:00

80 lines
2.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { readFileSync } from "node:fs";
import {
isTerminalCloseGenerationCurrent,
resolveConnectionLogCapturePayload,
resolveHibernateSnapshotCapturePayload,
scheduleTerminalCloseTeardown,
} from "./terminalCloseCapture.ts";
test("resolveConnectionLogCapturePayload returns null when finalize produces empty data", () => {
assert.equal(
resolveConnectionLogCapturePayload(() => ""),
null,
);
});
test("resolveConnectionLogCapturePayload returns buffered connection log data", () => {
assert.deepEqual(
resolveConnectionLogCapturePayload(() => "line one\r\nline two"),
{ data: "line one\r\nline two", source: "connection-log" },
);
});
test("resolveHibernateSnapshotCapturePayload prefers combined snapshot fields", () => {
assert.deepEqual(
resolveHibernateSnapshotCapturePayload({
snapshot: "full snapshot",
viewportSnapshot: "viewport",
scrollbackSnapshot: "scrollback",
alternateScreen: false,
}),
{ data: "full snapshot", source: "hibernate-serialize" },
);
assert.deepEqual(
resolveHibernateSnapshotCapturePayload({
snapshot: "",
viewportSnapshot: "viewport",
scrollbackSnapshot: "scrollback",
alternateScreen: false,
}),
{ data: "scrollbackviewport", source: "hibernate-serialize" },
);
});
test("scheduleTerminalCloseTeardown runs teardown asynchronously", async () => {
let ran = false;
scheduleTerminalCloseTeardown(() => {
ran = true;
});
assert.equal(ran, false);
await new Promise<void>((resolve) => {
setTimeout(resolve, 0);
});
assert.equal(ran, true);
});
test("isTerminalCloseGenerationCurrent rejects stale close generations", () => {
assert.equal(isTerminalCloseGenerationCurrent(1, 1), true);
assert.equal(isTerminalCloseGenerationCurrent(1, 2), false);
});
test("terminal close fully drains pending output before finalizing capture", () => {
const source = readFileSync(
new URL("../useTerminalEffects.ts", import.meta.url),
"utf8",
);
const cleanupStart = source.indexOf("return () => {", source.indexOf("boot();"));
const flushIndex = source.indexOf("await flushPendingTerminalWritesBeforeHibernate(term)", cleanupStart);
const incompleteIndex = source.indexOf("if (!flushed)", flushIndex);
const finalizeIndex = source.indexOf("resolveConnectionLogCapturePayload(finalizeTerminalLogData)", cleanupStart);
assert.ok(cleanupStart >= 0);
assert.ok(flushIndex > cleanupStart);
assert.ok(incompleteIndex > flushIndex);
assert.ok(finalizeIndex > flushIndex);
});