Files
Netcatty/components/terminal/connectionProgress.test.ts
T
陈大猫andClaude Opus 5.5 fa554888a6 perf(terminal): stop per-second churn while connecting and while idle (#4118)
* perf(terminal): stop per-second churn while connecting and while idle

- Connecting sessions: the 200 ms progress animation and 1 s countdown were
  Terminal state threaded into the TerminalView ctx, so every connecting
  session (hidden tabs and startup restores included) re-rendered Terminal +
  TerminalView up to six times a second. Terminal now only publishes the
  timeout deadline and real progress events; the connection dialog animates
  and counts down locally (connectionTicker.ts). HostConnectionTestDialog
  keeps passing progress/timeLeft as before.
- Monitor runtime: polled every second unconditionally and woke every
  subscriber each time because takenAt always changes. It now polls at 1 s
  only while something is monitored (here or in another window), 10 s
  otherwise, and skips notifying for unchanged idle replies.
- Auto-reconnect progress log: capped at the newest 300 lines (it grew by a
  few lines every 5 s retry for as long as an outage lasted).
- App Lock: report activity at most once a second instead of an IPC per
  keystroke / wheel tick / pointer event.
- Tray/Dock: the renderer resends hosts only when the host list changed, and
  the main process rebuilds the macOS Dock menu (sort + one native item per
  host) only then, not on every session status change.

Real app, profiling build, main -> this change:
  3 SSH sessions connecting (3 s)   14 commits / 35.3 ms -> 8 / 1.7 ms
  idle (10 s)                       13 commits / 3.1 ms  -> 2 / 0.1 ms

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

* fix(terminal): update observe-popup reconnect deps assertion for capped progress logs

CI failed because #4118 wrapped setProgressLogs in useCallback to apply
capConnectionProgressLogs and added it to scheduleAutoReconnect deps.
The observe-popup source assertion still expected the old deps list.

The attachExistingSession early-return behavior is unchanged; only the
stale dependency regex needed updating.

---------

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

69 lines
3.1 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import {
CONNECTION_PROGRESS_CAP,
CONNECTION_PROGRESS_START,
MAX_CONNECTION_PROGRESS_LOGS,
advanceIndeterminateConnectionProgress,
capConnectionProgressLogs,
advanceMonotonicConnectionProgress,
resolveHopConnectionProgress,
} from "./connectionProgress";
test("indeterminate connection progress only moves forward toward the cap", () => {
assert.equal(advanceIndeterminateConnectionProgress(CONNECTION_PROGRESS_CAP), CONNECTION_PROGRESS_CAP);
assert.equal(advanceIndeterminateConnectionProgress(94), CONNECTION_PROGRESS_CAP);
const first = advanceIndeterminateConnectionProgress(CONNECTION_PROGRESS_START);
assert.ok(first > CONNECTION_PROGRESS_START);
assert.ok(first < CONNECTION_PROGRESS_CAP);
const second = advanceIndeterminateConnectionProgress(first);
assert.ok(second > first);
});
test("hop connection progress stays within the visual cap", () => {
assert.equal(resolveHopConnectionProgress(1, 1), 90);
assert.equal(resolveHopConnectionProgress(1, 2), 50);
assert.equal(resolveHopConnectionProgress(2, 2), 90);
assert.equal(resolveHopConnectionProgress(0, 1), 10);
});
test("connection progress updates never rewind", () => {
assert.equal(advanceMonotonicConnectionProgress(40, 10), 40);
assert.equal(advanceMonotonicConnectionProgress(40, 90), 90);
assert.equal(advanceMonotonicConnectionProgress(5, 5), 5);
});
test("connecting seeds progress once and hop/phase changes do not snap it back", () => {
const effectsSource = readFileSync(new URL("./useTerminalEffects.ts", import.meta.url), "utf8");
assert.match(
effectsSource,
/if \(status === "connecting"\) \{\s*setIsDisconnectedDialogDismissed\(false\);\s*setProgressValue\(CONNECTION_PROGRESS_START\);/,
);
// The indeterminate animation and countdown tick inside the connection
// dialog (connectionTicker.ts); Terminal only publishes the deadline.
assert.doesNotMatch(effectsSource, /setProgressValue\(advanceIndeterminateConnectionProgress\)/);
assert.match(effectsSource, /setConnectionDeadlineAt\(Date\.now\(\) \+ connectionTimeout\)/);
const tickerSource = readFileSync(new URL("./connectionTicker.ts", import.meta.url), "utf8");
assert.match(tickerSource, /setValue\(advanceIndeterminateConnectionProgress\)/);
assert.doesNotMatch(effectsSource, /setProgressValue\(5\)/);
const starterSource = readFileSync(
new URL("./runtime/createTerminalSessionStarters.ts", import.meta.url),
"utf8",
);
assert.match(starterSource, /advanceMonotonicConnectionProgress/);
assert.match(starterSource, /resolveHopConnectionProgress/);
});
test("connection progress logs keep only the newest lines", () => {
const short = ["a", "b"];
assert.equal(capConnectionProgressLogs(short), short);
const long = Array.from({ length: MAX_CONNECTION_PROGRESS_LOGS + 5 }, (_, i) => `line ${i}`);
const capped = capConnectionProgressLogs(long);
assert.equal(capped.length, MAX_CONNECTION_PROGRESS_LOGS);
assert.equal(capped[0], "line 5");
assert.equal(capped.at(-1), `line ${MAX_CONNECTION_PROGRESS_LOGS + 4}`);
});