134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""通用运行设置与连续消息合并窗口测试。"""
|
|
|
|
import json
|
|
import queue
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest import TestCase, main, mock
|
|
|
|
import wechat_gui
|
|
from wechat_bot import WeChatBot
|
|
|
|
|
|
class RuntimeSettingsTest(TestCase):
|
|
def test_legacy_settings_without_batch_window_keep_twenty_second_default(self):
|
|
payload = {
|
|
"auto_reply_text": "在的",
|
|
"poll_interval": 2.0,
|
|
"mouse_idle_enabled": True,
|
|
"mouse_idle_seconds": 5.0,
|
|
}
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "app_settings.json"
|
|
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
|
app = wechat_gui.App.__new__(wechat_gui.App)
|
|
with mock.patch.object(wechat_gui, "APP_SETTINGS_FILE", str(path)):
|
|
settings = app._load_runtime_settings()
|
|
|
|
self.assertEqual(settings["message_batch_window_seconds"], 20.0)
|
|
|
|
def test_invalid_saved_batch_window_falls_back_to_twenty_seconds(self):
|
|
self.assertEqual(
|
|
wechat_gui.normalize_message_batch_window_seconds(True),
|
|
20.0,
|
|
)
|
|
payload = {
|
|
**wechat_gui.App._runtime_defaults(),
|
|
"message_batch_window_seconds": 999,
|
|
}
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "app_settings.json"
|
|
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
|
app = wechat_gui.App.__new__(wechat_gui.App)
|
|
with mock.patch.object(wechat_gui, "APP_SETTINGS_FILE", str(path)):
|
|
settings = app._load_runtime_settings()
|
|
|
|
self.assertEqual(settings["message_batch_window_seconds"], 20.0)
|
|
|
|
def test_runtime_thread_syncs_updated_batch_window_before_next_poll(self):
|
|
observed = []
|
|
fake_bot = mock.Mock()
|
|
fake_bot.connect.return_value = True
|
|
fake_bot._window_ready = True
|
|
fake_bot.hwnd = 100
|
|
fake_bot.L = fake_bot.T = 0
|
|
fake_bot.R = 1600
|
|
fake_bot.B = 900
|
|
fake_bot.input_x = 1200
|
|
fake_bot.input_y = 780
|
|
fake_bot.security_verification_required = False
|
|
fake_bot.reply_count = 0
|
|
fake_bot.false_pos_rows = set()
|
|
|
|
thread = wechat_gui.BotThread(
|
|
queue.Queue(),
|
|
"在的",
|
|
0.001,
|
|
message_batch_window_seconds=7,
|
|
)
|
|
|
|
def poll_once():
|
|
observed.append(fake_bot.message_batch_window_seconds)
|
|
if len(observed) == 1:
|
|
thread.set_message_batch_window_seconds(9)
|
|
else:
|
|
thread.stop_event.set()
|
|
|
|
fake_bot._poll_once.side_effect = poll_once
|
|
with mock.patch("wechat_bot.WeChatBot", return_value=fake_bot):
|
|
thread.run()
|
|
|
|
self.assertEqual(observed, [7.0, 9])
|
|
|
|
def test_classic_settings_save_persists_and_updates_live_thread(self):
|
|
app = wechat_gui.App.__new__(wechat_gui.App)
|
|
app._runtime_save_job = None
|
|
app._last_runtime_settings = {}
|
|
app._reply_var = mock.Mock()
|
|
app._reply_var.get.return_value = "在的"
|
|
app._poll_var = mock.Mock()
|
|
app._poll_var.get.return_value = "2"
|
|
app._idle_seconds_var = mock.Mock()
|
|
app._idle_seconds_var.get.return_value = "5"
|
|
app._batch_window_var = mock.Mock()
|
|
app._batch_window_var.get.return_value = "12.5"
|
|
app._mouse_idle_var = mock.Mock()
|
|
app._mouse_idle_var.get.return_value = True
|
|
app._thread = mock.Mock()
|
|
app._thread.is_alive.return_value = True
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "app_settings.json"
|
|
with mock.patch.object(wechat_gui, "APP_SETTINGS_FILE", str(path)):
|
|
self.assertTrue(app._save_runtime_settings(silent=True))
|
|
saved = json.loads(path.read_text(encoding="utf-8"))
|
|
|
|
self.assertEqual(saved["message_batch_window_seconds"], 12.5)
|
|
app._thread.set_message_batch_window_seconds.assert_called_once_with(12.5)
|
|
|
|
def test_bot_uses_instance_batch_window_when_call_has_no_override(self):
|
|
bot = WeChatBot.__new__(WeChatBot)
|
|
bot.message_batch_window_seconds = 7
|
|
bot._active_session_fp = b"target"
|
|
bot._active_identity_signature = b"identity"
|
|
bot._chat_identity_signature = mock.Mock(return_value=b"identity")
|
|
bot._chat_surface_signature = mock.Mock(return_value=b"surface")
|
|
bot._raw_selected_session_fingerprint = mock.Mock(return_value=b"target")
|
|
bot._selected_session_fingerprint = mock.Mock(return_value=b"target")
|
|
bot._stop_check = mock.Mock()
|
|
bot._stop_check.wait.return_value = True
|
|
|
|
with mock.patch("builtins.print") as output:
|
|
self.assertFalse(bot._wait_for_message_batch(b"target"))
|
|
|
|
rendered = "\n".join(
|
|
" ".join(str(part) for part in call.args)
|
|
for call in output.call_args_list
|
|
)
|
|
self.assertIn("7 秒", rendered)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|