Files
Netcatty/components/terminal/TerminalToolbar.test.ts
T
4c82424adf feat(terminal): open current session logs from the toolbar (#3834)
* feat: open saved session logs from terminal toolbar

Add one-click access to the current automatic or manual session log, flush before opening, and reuse application selection with a remembered log preference. Preserve recording controls and current-session ownership across worker routing and reconnects.

Refs #3807

* fix(logging): preserve saved logs on failed reconnect and localize failures

Address both Codex review findings with generation- and identity-checked replacement cleanup and localized user-facing error handling.

* fix(logging): guard successful main-process session replacement

Add shared inactive-log cleanup to fallback start handlers with regression coverage. Registration activation follows separately.

* fix(logging): enable log lifecycle cleanup for main-process fallback

* fix(terminal): keep log cleanup and tuck open-log into More actions

Always end a raw session-log write stream on finalize, including when a
flush already disabled it. Keep immediate snapshot retries, and pause
them only while prepare is waiting so a disk error can be reported.
Show open, reveal, and open-with in the toolbar overflow menu when
logging is active, instead of a default toolbar button.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(logging): back off failed snapshot writes and drop retired log actions

Persistent snapshot failures retry on a growing delay instead of a tight
loop, and still flush immediately when new output arrives. Hide open-log
actions after a successful reconnect retires a stopped manual log.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(terminal): keep open-log actions after automatic logging is turned off

The current automatic log keeps recording until reconnect, so the menu
stays available for that connection and clears on the next successful
connect.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(terminal): derive log menu availability from saved session logs

* fix(terminal): nest session log actions in one More actions submenu

Collapse Open, Show in folder, and the two open-with actions into a Session log submenu that follows the Terminal Encoding menu, and drop the remembered-application hint.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 22:15:21 +08:00

239 lines
8.7 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nProvider } from "../../application/i18n/I18nProvider.tsx";
import type { Host } from "../../types.ts";
import { TERMINAL_TOOLBAR_LAYOUT_DEFAULTS, TerminalToolbar } from "./TerminalToolbar.tsx";
const toolbarSource = readFileSync(new URL("./TerminalToolbar.tsx", import.meta.url), "utf8");
const sshHost: Host = {
id: "host-1",
label: "Host",
hostname: "example.com",
username: "root",
tags: [],
os: "linux",
protocol: "ssh",
};
const serialHost: Host = {
...sshHost,
id: "serial-1",
label: "Serial",
hostname: "/dev/tty.usbserial",
protocol: "serial",
};
const pluginHost: Host = {
...sshHost,
id: "plugin-1",
label: "Plugin",
hostname: "com.example.transport.connection",
protocol: "plugin:com.example.transport.connection",
};
const renderToolbar = (
host: Host,
status: "connecting" | "connected" | "disconnected" = "connected",
props: Partial<React.ComponentProps<typeof TerminalToolbar>> = {},
) =>
renderToStaticMarkup(
React.createElement(
I18nProvider,
{ locale: "en" },
React.createElement(TerminalToolbar, {
sessionId: 'session-1',
status,
host,
onOpenSFTP: () => {},
onOpenScripts: () => {},
onOpenTheme: () => {},
...props,
}),
),
);
test("keeps SFTP visible before the terminal overflow menu for SSH sessions", () => {
const markup = renderToolbar(sshHost);
const sftpIndex = markup.indexOf('aria-label="Open SFTP"');
const moreIndex = markup.indexOf('aria-label="More actions"');
assert.notEqual(sftpIndex, -1);
assert.notEqual(moreIndex, -1);
assert.ok(sftpIndex < moreIndex);
});
test("browser action remains available when an SSH terminal is disconnected", () => {
const disconnectedSsh = renderToolbar(sshHost, "disconnected", { onOpenBrowser: () => {} });
const serial = renderToolbar(serialHost, "connected", { onOpenBrowser: () => {} });
assert.match(disconnectedSsh, /aria-label="Open Browser"/);
assert.doesNotMatch(disconnectedSsh, /aria-label="Open Browser"[^>]*disabled/);
assert.doesNotMatch(serial, /aria-label="Open Browser"/);
});
test("browser action is available for supported local and mosh terminals", () => {
for (const protocol of ['local', 'mosh'] as const) {
const markup = renderToolbar({ ...sshHost, protocol }, "connected", { onOpenBrowser: () => {} });
assert.match(markup, /aria-label="Open Browser"/);
}
});
test("keeps Scripts visible before the terminal overflow menu", () => {
const markup = renderToolbar(sshHost);
const scriptsIndex = markup.indexOf('aria-label="Scripts"');
const moreIndex = markup.indexOf('aria-label="More actions"');
assert.notEqual(scriptsIndex, -1);
assert.notEqual(moreIndex, -1);
assert.ok(scriptsIndex < moreIndex);
assert.equal(markup.match(/Scripts/g)?.length, 1);
assert.match(markup, /type="button"[^>]*aria-label="Scripts"/);
});
test("shows manual session log button when requested", () => {
const markup = renderToolbar(sshHost, "connected", {
showLogButton: true,
onToggleSessionLog: () => {},
});
const logIndex = markup.indexOf('aria-label="Start session log"');
const scriptsIndex = markup.indexOf('aria-label="Scripts"');
assert.notEqual(logIndex, -1);
assert.notEqual(scriptsIndex, -1);
assert.ok(logIndex < scriptsIndex);
});
test("marks manual session log button active while logging", () => {
const markup = renderToolbar(sshHost, "connected", {
showLogButton: true,
onToggleSessionLog: () => {},
isSessionLogging: true,
});
assert.match(
markup,
/aria-label="Stop session log"[^>]*aria-pressed="true"[^>]*style="background-color:var\(--terminal-toolbar-btn-active\)"/,
);
});
test("shows SFTP for local terminal sessions", () => {
const markup = renderToolbar({
...sshHost,
id: "local-1",
protocol: "local",
});
assert.equal(markup.includes('aria-label="Open SFTP"'), true);
});
test("hides SSH history for plugin terminal sessions", () => {
const markup = renderToolbar(pluginHost, "connected", {
onOpenHistory: () => {},
});
assert.equal(markup.includes('aria-label="Command history"'), false);
});
test("shows YMODEM send only for connected serial sessions", () => {
const connectedSerial = renderToolbar(serialHost, "connected", {
onSendYmodem: () => {},
onReceiveYmodem: () => {},
});
const disconnectedSerial = renderToolbar(serialHost, "disconnected", {
onSendYmodem: () => {},
onReceiveYmodem: () => {},
});
const ssh = renderToolbar(sshHost, "connected", {
onSendYmodem: () => {},
onReceiveYmodem: () => {},
});
const local = renderToolbar({
...sshHost,
id: "local-1",
protocol: "local",
}, "connected", {
onSendYmodem: () => {},
onReceiveYmodem: () => {},
});
assert.equal(connectedSerial.includes('aria-label="Send with YMODEM"'), true);
assert.equal(connectedSerial.includes('aria-label="Receive with YMODEM"'), true);
assert.doesNotMatch(connectedSerial, /aria-label="Send with YMODEM"[^>]*disabled/);
assert.equal(disconnectedSerial.includes('aria-label="Send with YMODEM - Available after connect"'), true);
assert.equal(disconnectedSerial.includes('aria-label="Receive with YMODEM - Available after connect"'), true);
assert.match(disconnectedSerial, /aria-label="Send with YMODEM - Available after connect"[^>]*disabled/);
assert.match(disconnectedSerial, /aria-label="Receive with YMODEM - Available after connect"[^>]*disabled/);
assert.equal(ssh.includes('aria-label="Send with YMODEM"'), false);
assert.equal(ssh.includes('aria-label="Receive with YMODEM"'), false);
assert.equal(local.includes('aria-label="Send with YMODEM"'), false);
assert.equal(local.includes('aria-label="Receive with YMODEM"'), false);
});
test("uses the terminal active button color for pressed toolbar actions", () => {
const markup = renderToolbar(sshHost, "connected", {
isSearchOpen: true,
onToggleSearch: () => {},
});
assert.match(
markup,
/aria-label="Search terminal"[^>]*style="background-color:var\(--terminal-toolbar-btn-active\)"/,
);
});
test("compact scripts popover hosts bulk-delete confirm outside the popover", () => {
// Dialog focus leaves PopoverContent; if confirm stays inside ScriptsSidePanel,
// the popover closes, isVisible clears pendingDeleteIds, and the prompt dies.
assert.match(toolbarSource, /onBulkDeleteRequest=\{setPendingScriptDeleteIds\}/);
assert.match(toolbarSource, /<VaultDeleteConfirmDialog/);
// Popup vault mutation goes through the prop; the event only clears popover selection.
assert.match(toolbarSource, /onDeleteSnippets\?\.\(new Set\(ids\)\)/);
assert.match(toolbarSource, /netcatty:snippets:delete/);
const scriptsPopoverIdx = toolbarSource.indexOf("open={scriptsPopoverOpen}");
const popoverEndIdx = toolbarSource.indexOf("</Popover>", scriptsPopoverIdx);
const dialogIdx = toolbarSource.indexOf("<VaultDeleteConfirmDialog", popoverEndIdx);
assert.ok(scriptsPopoverIdx >= 0, "scripts popover open binding");
assert.ok(popoverEndIdx > scriptsPopoverIdx, "scripts popover closes");
assert.ok(dialogIdx > popoverEndIdx, "confirm dialog is a sibling after the popover");
assert.equal(
toolbarSource.includes("document.querySelector('[data-vault-delete-confirm=\"true\"]')"),
false,
);
});
for (const host of [serialHost, pluginHost]) {
test(`hides SFTP for ${host.protocol} sessions`, () => {
assert.equal(renderToolbar(host).includes('aria-label="Open SFTP"'), false);
});
}
test('open session log stays in the overflow menu until logging is active', () => {
assert.equal(TERMINAL_TOOLBAR_LAYOUT_DEFAULTS.placement.openSessionLog, 'collapse');
const hidden = renderToolbar(sshHost, 'disconnected', { onSessionLogAction: () => {} });
assert.doesNotMatch(hidden, /aria-label="Open session log"/);
assert.doesNotMatch(hidden, /Open session log/);
for (const status of ['connected', 'disconnected'] as const) {
const markup = renderToolbar(sshHost, status, {
onSessionLogAction: () => {},
canOpenSessionLog: true,
});
assert.doesNotMatch(markup, /aria-label="Open session log"/);
assert.doesNotMatch(markup, /aria-label="Session log actions"/);
assert.match(markup, /aria-label="More actions"/);
}
assert.match(toolbarSource, /case 'openSessionLog':/);
assert.match(toolbarSource, /data-session-log-submenu="true"/);
assert.match(toolbarSource, /terminal\.toolbar\.sessionLog\.menu/);
assert.match(toolbarSource, /terminal\.toolbar\.sessionLog\.\$\{action\}/);
assert.equal(toolbarSource.includes('rememberApplication'), false);
assert.equal(toolbarSource.includes('Open session log'), false);
});