test_cli_copy_command.py
1 """Tests for CLI /copy command.""" 2 3 from unittest.mock import MagicMock, patch 4 5 from cli import HermesCLI 6 7 8 def _make_cli() -> HermesCLI: 9 cli_obj = HermesCLI.__new__(HermesCLI) 10 cli_obj.config = {} 11 cli_obj.console = MagicMock() 12 cli_obj.agent = None 13 cli_obj.conversation_history = [] 14 cli_obj.session_id = "sess-copy-test" 15 cli_obj._pending_input = MagicMock() 16 cli_obj._app = None 17 return cli_obj 18 19 20 def test_copy_copies_latest_assistant_message(): 21 cli_obj = _make_cli() 22 cli_obj.conversation_history = [ 23 {"role": "user", "content": "hi"}, 24 {"role": "assistant", "content": "first"}, 25 {"role": "assistant", "content": "latest"}, 26 ] 27 28 with patch.object(cli_obj, "_write_osc52_clipboard") as mock_copy: 29 result = cli_obj.process_command("/copy") 30 31 assert result is True 32 mock_copy.assert_called_once_with("latest") 33 34 35 def test_copy_with_index_uses_requested_assistant_message(): 36 cli_obj = _make_cli() 37 cli_obj.conversation_history = [ 38 {"role": "assistant", "content": "one"}, 39 {"role": "assistant", "content": "two"}, 40 ] 41 42 with patch.object(cli_obj, "_write_osc52_clipboard") as mock_copy: 43 cli_obj.process_command("/copy 1") 44 45 mock_copy.assert_called_once_with("one") 46 47 48 def test_copy_strips_reasoning_blocks_before_copy(): 49 cli_obj = _make_cli() 50 cli_obj.conversation_history = [ 51 { 52 "role": "assistant", 53 "content": "<REASONING_SCRATCHPAD>internal</REASONING_SCRATCHPAD>\nVisible answer", 54 } 55 ] 56 57 with patch.object(cli_obj, "_write_osc52_clipboard") as mock_copy: 58 cli_obj.process_command("/copy") 59 60 mock_copy.assert_called_once_with("Visible answer") 61 62 63 def test_copy_invalid_index_does_not_copy(): 64 cli_obj = _make_cli() 65 cli_obj.conversation_history = [{"role": "assistant", "content": "only"}] 66 67 with patch.object(cli_obj, "_write_osc52_clipboard") as mock_copy, patch("cli._cprint") as mock_print: 68 cli_obj.process_command("/copy 99") 69 70 mock_copy.assert_not_called() 71 assert any("Invalid response number" in str(call) for call in mock_print.call_args_list)