Files
Netcatty/components/TerminalPopupPage.test.ts
T
陈大猫 f99efa8ac1 fix(ui): open AI silent sessions in an attach terminal popup (#2380)
* fix(ui): open AI silent sessions in an attach terminal popup

Tray jumps for hidden MCP sessions attach the existing terminal popup
to the live PTY instead of activating a tab-less main-window surface.
Output is rebound to the popup and restored when the popup closes.

* fix(terminal): register rebindOutput handlers under worker mode

Worker-enabled builds returned early from terminalBridge.registerHandlers
without registering rebind/restore IPC, so the first AI silent-session
attach popup always failed with "No handler registered". Register those
handlers in both paths and rebind display routing via terminalWorkerManager.

* fix(terminal): harden AI attach popup for worker mode lifecycle

Address codex review P1s: resume flow control before restoring output,
route exit/events to the rebound renderer, and replay home scrollback
via a snapshot request before rebinding display output.

* fix(terminal): restore attach routing on popup close and dual-notify exit

Hardening for AI observe popups: restore output from BrowserWindow closed
lifecycle (not only React cleanup), keep attach home for exit events so
main/tray lifecycle updates, and resume paused backend flow on attach.

* fix(terminal): resume flow on attach restore and close snapshot gaps

Resume backend output when attach popups are force-closed, fan out
lifecycle exit events to home+display in non-worker mode, and pause
during snapshot→rebind so live bytes are not dropped from the popup.

* fix(terminal): harden attach restore and worker lifecycle edges

Prefer live display for ZMODEM dialogs after rebind, dual-notify worker
crash exits to attach-home, and avoid clearing attach-home when restore
targets a dead renderer.

* fix(terminal): sync observe-popup state home and fan out SSH exits

Push popup serializations home before restore, fall back to hibernate
snapshots, settle briefly after pause for worker handoff, and dual-notify
SSH exits to attach-home owners in non-worker mode.

* fix(terminal): fan out exits for mosh/telnet/et attach owners

Use shared fanout for in-process protocol exits so attach-home and
display renderers both receive netcatty:exit under non-worker mode.

* fix(terminal): restore attach only onto main app windows

Avoid routing silent-session output to settings/prewarm windows when the
original home renderer is gone; fall back only to registered main windows.

* fix(terminal): harden silent session popup handoff

* fix(terminal): close observed popup with session

* fix(terminal): keep observe popup from reconnecting

* fix(terminal): preserve cleared observe snapshots

* fix(terminal): route transfer prompts to observer

* fix(terminal): keep context after observe restore
2026-07-22 08:58:58 +08:00

138 lines
5.2 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import type { Host, TerminalSession } from '../types';
import type { TerminalPopupPayload } from '../domain/systemManager/types';
import { resolveTerminalPopupHost, resolveTerminalPopupReuseId } from './TerminalPopupPage';
const source = readFileSync(new URL('./TerminalPopupPage.tsx', import.meta.url), 'utf8');
const terminalSource = readFileSync(new URL('./Terminal.tsx', import.meta.url), 'utf8');
const vaultHost = (overrides: Partial<Host> = {}): Host => ({
id: 'host-1',
label: 'Prod',
hostname: 'prod.example.com',
username: 'deploy',
port: 22,
protocol: 'ssh',
tags: [],
os: 'linux',
moshEnabled: true,
...overrides,
});
const sourceSession = (overrides: Partial<TerminalSession> = {}): TerminalSession => ({
id: 'session-1',
hostId: 'host-1',
hostLabel: 'Prod',
hostname: 'prod.example.com',
username: 'deploy',
status: 'connected',
protocol: 'ssh',
port: 22,
moshEnabled: false,
...overrides,
});
const popupPayload = (sourceSessionValue: TerminalSession): TerminalPopupPayload => ({
title: 'Prod · docker: api',
parentSessionId: sourceSessionValue.id,
sourceSession: sourceSessionValue,
startupCommand: 'docker exec -it abc123 sh',
});
test('resolveTerminalPopupHost honors source session transport over saved Mosh host settings', () => {
const host = resolveTerminalPopupHost(popupPayload(sourceSession()), [vaultHost()]);
assert.equal(host.id, 'host-1');
assert.equal(host.hostname, 'prod.example.com');
assert.equal(host.protocol, 'ssh');
assert.equal(host.moshEnabled, false);
assert.equal(host.etEnabled, false);
});
test('resolveTerminalPopupHost still falls back to source session details when the host is missing', () => {
const host = resolveTerminalPopupHost(popupPayload(sourceSession({ hostId: 'missing' })), []);
assert.equal(host.id, 'missing');
assert.equal(host.label, 'Prod');
assert.equal(host.protocol, 'ssh');
assert.equal(host.moshEnabled, false);
});
test('resolveTerminalPopupHost does not turn command popups into serial sessions without serial config', () => {
const host = resolveTerminalPopupHost(
popupPayload(sourceSession({ protocol: 'serial' })),
[vaultHost({ protocol: 'serial' })],
);
assert.equal(host.protocol, 'ssh');
assert.equal(host.moshEnabled, false);
});
test('resolveTerminalPopupHost preserves serial settings when attaching a live session', () => {
const serialConfig = {
path: '/dev/ttyUSB0',
baudRate: 115200,
lineMode: true,
localEcho: true,
} as const;
const payload = popupPayload(sourceSession({
protocol: 'serial',
hostname: serialConfig.path,
port: serialConfig.baudRate,
serialConfig,
}));
payload.attachSessionId = payload.sourceSession.id;
const host = resolveTerminalPopupHost(payload, [vaultHost({ protocol: 'serial', serialConfig })]);
assert.equal(host.protocol, 'serial');
assert.deepEqual(host.serialConfig, serialConfig);
assert.match(source, /serialConfig=\{isAttachMode \? config\.sourceSession\.serialConfig : undefined\}/);
});
test('resolveTerminalPopupReuseId uses the explicit reuse id from the prepared source session', () => {
assert.equal(
resolveTerminalPopupReuseId(popupPayload(sourceSession({ reuseConnectionFromSessionId: 'session-1' }))),
'session-1',
);
assert.equal(
resolveTerminalPopupReuseId(popupPayload(sourceSession({ reuseConnectionFromSessionId: undefined }))),
undefined,
);
});
test('popup terminals resolve complete host config and pass jump hosts into Terminal', () => {
assert.match(source, /proxyProfiles,\s+knownHosts,\s+snippets,\s+snippetPackages,\s+groupConfigs,/);
assert.match(source, /resolveTerminalPopupHost\(config,\s*hosts,\s*\{\s+groupConfigs,\s+proxyProfiles,/);
assert.match(source, /resolveTerminalChainHosts\(\{\s+host,\s+hosts,\s+groupConfigs,\s+proxyProfiles,/);
assert.match(source, /chainHosts=\{chainHosts\}/);
});
test('attach popup close preparation has a bounded timeout', () => {
assert.match(source, /Promise\.race\(\[/);
assert.match(source, /Attach close preparation timed out/);
assert.match(source, /1500/);
});
test('an explicitly closed attached session closes its observe popup', () => {
assert.match(source, /onSessionExit=\{\(_closedSessionId, evt\) => \{\s+if \(isAttachMode\)/);
assert.match(source, /void handleClose\(\)/);
});
test('an attached observe popup never owns automatic reconnect', () => {
const reconnectStart = terminalSource.indexOf('const scheduleAutoReconnect = useCallback');
const reconnectEnd = terminalSource.indexOf('const prepareRestoredReconnect', reconnectStart);
const reconnectSource = terminalSource.slice(reconnectStart, reconnectEnd);
assert.match(reconnectSource, /if \(attachExistingSession\) return false;/);
assert.match(reconnectSource, /\[attachExistingSession, host, t, terminalSettings, updateStatus\]/);
assert.match(terminalSource, /const startReconnect = async[\s\S]*?if \(attachExistingSession\) return;/);
assert.match(
terminalSource,
/useEffect\(\(\) => \{\s+if \(attachExistingSession\) return undefined;\s+return terminalReconnectRegistry\.register/,
);
});