hermes-agent/tests/cli/test_cli_copy_command.py
Teknium a0770d0954 fix(cli): flush-left responses + native clipboard /copy for clean copy/paste
Streamed response text carried a 4-space _STREAM_PAD indent and the
final-response Rich Panel used padding=(1, 4), so every line selected
out of the terminal came with leading whitespace. Both now render
flush-left (pad empty, panel padding=(1, 0)); the table-realignment
width budgets were widened to match.

/copy now writes the ORIGINAL message text through native clipboard
tools (pbcopy / PowerShell Set-Clipboard via base64 / wl-copy / xclip /
xsel — same fallback chain as the TUI's writeClipboardText), falling
back to OSC 52 only when no native backend succeeds. This is the
TUI-equivalent answer to soft-wrap mangling: the clipboard gets the raw
text, not the rendered layout.
2026-07-28 23:53:16 -07:00

85 lines
2.7 KiB
Python

"""Tests for CLI /copy command."""
from unittest.mock import MagicMock, patch
from cli import HermesCLI
def _make_cli() -> HermesCLI:
cli_obj = HermesCLI.__new__(HermesCLI)
cli_obj.config = {}
cli_obj.console = MagicMock()
cli_obj.agent = None
cli_obj.conversation_history = []
cli_obj.session_id = "sess-copy-test"
cli_obj._pending_input = MagicMock()
cli_obj._app = None
return cli_obj
def test_copy_copies_latest_assistant_message():
cli_obj = _make_cli()
cli_obj.conversation_history = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "first"},
{"role": "assistant", "content": "latest"},
]
with patch("hermes_cli.clipboard.write_clipboard_text", return_value=True) as mock_copy:
result = cli_obj.process_command("/copy")
assert result is True
mock_copy.assert_called_once_with("latest")
def test_copy_with_index_uses_requested_assistant_message():
cli_obj = _make_cli()
cli_obj.conversation_history = [
{"role": "assistant", "content": "one"},
{"role": "assistant", "content": "two"},
]
with patch("hermes_cli.clipboard.write_clipboard_text", return_value=True) as mock_copy:
cli_obj.process_command("/copy 1")
mock_copy.assert_called_once_with("one")
def test_copy_strips_reasoning_blocks_before_copy():
cli_obj = _make_cli()
cli_obj.conversation_history = [
{
"role": "assistant",
"content": "<REASONING_SCRATCHPAD>internal</REASONING_SCRATCHPAD>\nVisible answer",
}
]
with patch("hermes_cli.clipboard.write_clipboard_text", return_value=True) as mock_copy:
cli_obj.process_command("/copy")
mock_copy.assert_called_once_with("Visible answer")
def test_copy_falls_back_to_osc52_when_native_tools_fail():
cli_obj = _make_cli()
cli_obj.conversation_history = [{"role": "assistant", "content": "hello"}]
with patch("hermes_cli.clipboard.write_clipboard_text", return_value=False), \
patch.object(cli_obj, "_write_osc52_clipboard") as mock_osc52:
cli_obj.process_command("/copy")
mock_osc52.assert_called_once_with("hello")
def test_copy_invalid_index_does_not_copy():
cli_obj = _make_cli()
cli_obj.conversation_history = [{"role": "assistant", "content": "only"}]
with patch("hermes_cli.clipboard.write_clipboard_text") as mock_copy, \
patch.object(cli_obj, "_write_osc52_clipboard") as mock_osc52, \
patch("cli._cprint") as mock_print:
cli_obj.process_command("/copy 99")
mock_copy.assert_not_called()
mock_osc52.assert_not_called()
assert any("Invalid response number" in str(call) for call in mock_print.call_args_list)