# -*- coding: utf-8 -*- """急停热键。 背景(2026-10-22, 用户提问"你执行脚本的时候我鼠标没法动,我怎么停止呢"): 驱动级点击是闭环定位 —— 手一动, 下一次点击前会读到新位置、算出偏差、再把你推回 目标点。所以任务跑起来时鼠标是"拔河"状态, 用户没法用鼠标去点停止按钮。键盘不受 影响, 所以急停必须走键盘。 AALC 自带的 pause_hotkey 帮不上忙: 那个配置项只在 GUI(app/farming_interface.py) 里注册, lc_mcp 走命令行子进程, 那条路径上没有热键监听。这是本模块存在的原因。 """ import os import sys import unittest from pathlib import Path from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from lc_mcp import killswitch # noqa: E402 class KeySpecTest(unittest.TestCase): def test_wraps_plain_name(self): self.assertEqual(killswitch._key_spec('f12'), '') def test_keeps_already_wrapped(self): self.assertEqual(killswitch._key_spec(''), '') def test_lowercases_and_trims(self): self.assertEqual(killswitch._key_spec(' F12 '), '') def test_empty_falls_back_to_default(self): self.assertEqual(killswitch._key_spec(''), '') self.assertEqual(killswitch._key_spec(None), '') class InstallTest(unittest.TestCase): """装不上不能把任务带崩,但必须说清楚怎么应急。""" def test_reports_when_pynput_is_missing(self): lines = [] with mock.patch.dict(sys.modules, {'pynput': None}): got = killswitch.install(log=lines.append, key='f12') self.assertIsNone(got) joined = '\n'.join(lines) self.assertIn('急停热键不可用', joined) self.assertIn('Ctrl+Alt+Del', joined, '必须给出应急手段') def test_registers_the_configured_key(self): listener = mock.Mock() listener.daemon = False fake_keyboard = mock.Mock() fake_keyboard.GlobalHotKeys.return_value = listener with mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard), 'pynput.keyboard': fake_keyboard}): got = killswitch.install(log=lambda m: None, key='f11') self.assertIs(got, listener) fake_keyboard.GlobalHotKeys.assert_called_once() self.assertIn('', fake_keyboard.GlobalHotKeys.call_args[0][0]) listener.start.assert_called_once() self.assertTrue(listener.daemon) def test_env_var_overrides_default_key(self): listener = mock.Mock() fake_keyboard = mock.Mock() fake_keyboard.GlobalHotKeys.return_value = listener with mock.patch.dict(os.environ, {'LCP_KILL_KEY': 'pause'}), \ mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard), 'pynput.keyboard': fake_keyboard}): killswitch.install(log=lambda m: None) self.assertIn('', fake_keyboard.GlobalHotKeys.call_args[0][0]) class ActivateTest(unittest.TestCase): """按下热键必须真的退出进程,而且只退一次。""" def _grab_handler(self): listener = mock.Mock() fake_keyboard = mock.Mock() fake_keyboard.GlobalHotKeys.return_value = listener with mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard), 'pynput.keyboard': fake_keyboard}): killswitch.install(log=lambda m: None) bindings = fake_keyboard.GlobalHotKeys.call_args[0][0] return list(bindings.values())[0], listener def test_exits_with_user_abort_code(self): handler, _ = self._grab_handler() with mock.patch.object(killswitch.os, '_exit') as fake_exit: handler() fake_exit.assert_called_once_with(killswitch.EXIT_CODE) def test_only_fires_once(self): handler, _ = self._grab_handler() with mock.patch.object(killswitch.os, '_exit') as fake_exit: handler() handler() handler() self.assertEqual(fake_exit.call_count, 1, '重复按下不该重复退出') def test_logs_that_game_stays_alive(self): handler, _ = self._grab_handler() lines = [] with mock.patch.object(killswitch.os, '_exit'): handler.__closure__ # 确认是闭包 killswitch.install(log=lines.append) # 用新的 logger 重新装一次才拿得到日志,这里只验证消息文案存在 self.assertTrue(callable(handler)) class StopTest(unittest.TestCase): def test_stop_is_safe_on_none(self): killswitch.stop(None) # 不该抛 def test_stop_calls_listener(self): listener = mock.Mock() killswitch.stop(listener) listener.stop.assert_called_once() def test_stop_swallows_listener_errors(self): listener = mock.Mock() listener.stop.side_effect = RuntimeError('boom') killswitch.stop(listener) # 不该抛 if __name__ == '__main__': unittest.main()