Files
Netcatty/components/terminal/connectionTicker.test.tsx
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

87 lines
2.9 KiB
TypeScript

import assert from 'node:assert/strict';
import { mock, test } from 'node:test';
import React from 'react';
import { act, create } from 'react-test-renderer';
import { CONNECTION_PROGRESS_CAP } from './connectionProgress';
import { useAnimatedConnectionProgress, useConnectionCountdownSeconds } from './connectionTicker';
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
function renderHookValue<P>(useValue: (props: P) => number, initialProps: P) {
const result = { current: Number.NaN, renders: 0 };
function Probe(props: P) {
result.current = useValue(props);
result.renders += 1;
return null;
}
let renderer!: ReturnType<typeof create>;
act(() => {
renderer = create(React.createElement(Probe as React.FC<P>, initialProps as P & React.Attributes));
});
return {
result,
rerender(props: P) {
act(() => {
renderer.update(React.createElement(Probe as React.FC<P>, props as P & React.Attributes));
});
},
unmount() {
act(() => renderer.unmount());
},
};
}
test('animated connection progress advances only while animating and follows the base', () => {
mock.timers.enable({ apis: ['setInterval'] });
try {
const hook = renderHookValue(
({ base, animating }: { base: number; animating: boolean }) => useAnimatedConnectionProgress(base, animating),
{ base: 5, animating: false },
);
act(() => mock.timers.tick(1000));
assert.equal(hook.result.current, 5, 'no animation while the timeout is not running');
hook.rerender({ base: 5, animating: true });
act(() => mock.timers.tick(200));
const afterOneTick = hook.result.current;
assert.ok(afterOneTick > 5);
act(() => mock.timers.tick(10_000));
assert.ok(hook.result.current <= CONNECTION_PROGRESS_CAP);
// A hop event raises the base above the animation: adopt it.
hook.rerender({ base: 100, animating: false });
assert.equal(hook.result.current, 100);
// A new attempt restarts from a lower base.
hook.rerender({ base: 5, animating: true });
assert.equal(hook.result.current, 5);
hook.unmount();
} finally {
mock.timers.reset();
}
});
test('connection countdown ticks from the deadline and freezes when the timer stops', () => {
mock.timers.enable({ apis: ['setInterval', 'Date'], now: 1_000_000 });
try {
const hook = renderHookValue(
({ deadlineAt }: { deadlineAt: number | null }) => useConnectionCountdownSeconds(deadlineAt, 30),
{ deadlineAt: null },
);
assert.equal(hook.result.current, 30, 'initial seconds before a deadline exists');
hook.rerender({ deadlineAt: 1_000_000 + 20_000 });
assert.equal(hook.result.current, 20);
act(() => mock.timers.tick(3000));
assert.equal(hook.result.current, 17);
hook.rerender({ deadlineAt: null });
act(() => mock.timers.tick(5000));
assert.equal(hook.result.current, 17, 'stopped timer keeps the last value');
hook.unmount();
} finally {
mock.timers.reset();
}
});