Files
xieyi-weixin-/test_proactive_autostart.py
T
2026-11-02 14:05:03 +08:00

107 lines
5.0 KiB
Python

"""Exercise the actual GUI slot and listener entry point without a live bot."""
import io
import types
import unittest
from unittest.mock import Mock, patch
import wechat_gui_qt as gui
class AutoStartTest(unittest.TestCase):
def setUp(self):
self.request = dict(owner='owner', ids=['task'])
self.host = types.SimpleNamespace(_running=False, runtime_settings={'send_mode': 'auto'},
settings_page=Mock(), proactive_controller=Mock(), append_log=Mock(),
save_runtime_settings=Mock(return_value=True), _schedule_console_push=Mock())
self.host.start_monitoring = Mock(side_effect=lambda **kwargs: setattr(self.host, '_running', True))
gateway = Mock()
gateway.owner.return_value = 'owner'
self.gateway = gateway
self.enterContext(patch('proactive_contact_service.PatientGateway', return_value=gateway))
def start(self):
gui.MainWindow._start_proactive_replies(self.host, self.request)
def test_success_starts_in_background_and_acknowledges(self):
self.start()
self.host.start_monitoring.assert_called_once_with(background=True)
self.host.proactive_controller.submit.assert_called_once_with(dict(action='reply-activation-done', **self.request))
self.assertEqual(self.host._proactive_reply_start_error, '')
def test_existing_auto_listener_is_reused_and_review_mode_switches(self):
self.host._running = True
self.start()
self.host.start_monitoring.assert_not_called()
self.host.save_runtime_settings.assert_not_called()
self.host.runtime_settings['send_mode'] = 'review'
self.start()
self.host.settings_page.auto_send_mode.setChecked.assert_called_once_with(True)
self.host.settings_page.review_send_mode.setChecked.assert_called_once_with(False)
self.host.save_runtime_settings.assert_called_once()
self.host.start_monitoring.assert_not_called()
def test_probe_pending_does_not_start_duplicate_listener(self):
self.host._protocol_start_pending = True
self.start()
self.start()
self.host.start_monitoring.assert_not_called()
self.host.proactive_controller.submit.assert_not_called()
self.assertEqual(self.host._proactive_reply_waiting, {'owner': self.request})
self.host._running = True
self.start()
self.host.proactive_controller.submit.assert_called_once()
def test_start_failure_is_visible_and_not_acknowledged(self):
self.host.start_monitoring.side_effect = None
self.start()
self.assertIn('未能启动', self.host._proactive_reply_start_error)
self.host.proactive_controller.submit.assert_not_called()
self.host.runtime_settings['send_mode'] = 'review'
self.host.save_runtime_settings.return_value = False
self.start()
self.assertIn('保存失败', self.host._proactive_reply_start_error)
self.host.proactive_controller.submit.assert_not_called()
def test_exit_or_other_login_does_not_start(self):
self.gateway.owner.return_value = 'other'
self.start()
self.host.start_monitoring.assert_not_called()
self.gateway.owner.return_value = 'owner'
self.host._exit_requested = True
self.start()
self.host.start_monitoring.assert_not_called()
def test_background_listener_start_keeps_window_open(self):
host = self.host
host._thread = None
host.persona_page = Mock()
host._queue = Mock()
host.set_status = Mock()
host.enter_capsule_mode = Mock()
host._proactive_reply_waiting = {'owner': self.request}
host.runtime_settings.update(reply_transport=gui.VISUAL, auto_reply_text='', poll_interval=1,
mouse_idle_enabled=False, mouse_idle_seconds=5, message_batch_window_seconds=8,
send_delay_seconds=2, foreign_draft_autosend_minutes=0)
with patch('sys.stdout', new=io.StringIO()), patch.object(gui, 'LogQueue', return_value=io.StringIO()), \
patch.object(gui, 'BotThread') as thread, patch.object(gui.QTimer, 'singleShot') as timer:
gui.MainWindow.start_monitoring(host, background=True)
thread.return_value.start.assert_called_once()
timer.assert_not_called()
self.assertTrue(host._running)
host.proactive_controller.submit.assert_called_once_with(dict(action='reply-activation-done', **self.request))
self.assertEqual(host._proactive_reply_waiting, {})
def test_manual_stop_cancels_waiting_activation(self):
self.host._protocol_start_pending = True
self.start()
self.host._thread = None
self.host._finish_thread = Mock()
gui.MainWindow.stop_monitoring(self.host)
self.assertEqual(self.host._proactive_reply_waiting, {})
self.assertFalse(self.host._protocol_start_pending)
self.assertEqual(self.host._monitor_start_epoch, 1)
self.host.proactive_controller.submit.assert_called_once()
if __name__ == '__main__':
unittest.main()