# -*- coding: utf-8 -*- """长时间没有「开始执行 / 结束执行」就结束任务。 feedback/runs/2026-11-15_2026-11-21/logs-raw: 11-21 14:01 起镜牢入口认不出, 大厅 ↔ 驱动来回点 5 小时 20 分; 11-17 00:45 起全员阵亡后 re_start() 的 while True 转了 5 小时 40 分。两次都没有报错, 只是不再有这两种日志。 """ import logging import sys import threading import types import unittest from pathlib import Path from types import SimpleNamespace from unittest import mock REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from lc_mcp import aalc_patches as ap # noqa: E402 AALC_LOG = logging.getLogger('AALC') class FakeImageUtils: calls = 0 @staticmethod def match_template(screenshot, template, bbox, model='clam'): FakeImageUtils.calls += 1 return (1, 1), 0.3 class FakeAuto: def __init__(self): self.shots = 0 def take_screenshot(self, gray=True): self.shots += 1 return object() class ProgressWatchdogTest(unittest.TestCase): def setUp(self): self.now = [0.0] self.lines = [] self.auto = FakeAuto() self.utils = type('ImageUtils', (), {'match_template': FakeImageUtils.__dict__['match_template']}) def install(self, env=None): image_module = types.ModuleType('utils.image_utils') image_module.ImageUtils = self.utils modules = {'utils': types.ModuleType('utils'), 'utils.image_utils': image_module, 'module': types.ModuleType('module'), 'module.automation': SimpleNamespace(auto=self.auto)} with mock.patch.dict(sys.modules, modules): handler = ap.install_progress_watchdog(log=self.lines.append, env=env or {}, clock=lambda: self.now[0]) if handler is not None: self.addCleanup(AALC_LOG.removeHandler, handler) return handler def frame(self, minutes): """主线程过了 minutes 分钟后识一次图。""" self.now[0] += minutes * 60 return self.utils.match_template(None, None, None) def milestone(self, text): with mock.patch.object(AALC_LOG, 'level', logging.DEBUG), mock.patch.object(AALC_LOG, 'propagate', False): AALC_LOG.info(text) def test_quiet_for_less_than_the_limit_is_fine(self): self.assertIsNotNone(self.install()) self.assertEqual(self.frame(39), ((1, 1), 0.3)) self.assertIn('patch: 40 分钟没有「开始执行/结束执行」就结束任务', '\n'.join(self.lines)) def test_each_milestone_restarts_the_clock(self): self.install() for _ in range(5): # 5 场 30 分钟的战斗, 每场之间都有新日志 self.frame(30) self.milestone('结束执行 一次战斗') self.frame(39) self.assertNotIn('[ERROR]', '\n'.join(self.lines)) def test_no_progress_ends_the_task(self): self.install() self.milestone('开始执行 体力换饼') with mock.patch.object(ap.decisions, 'record') as record: with self.assertRaises(ap.TaskAbort) as caught: self.frame(41) self.assertIn('41 分钟没有进展(最后一条: 开始执行 体力换饼)', str(caught.exception)) self.assertIn('[ERROR] [lc_mcp] 41 分钟没有新的「开始执行/结束执行」', '\n'.join(self.lines)) record.assert_called_once_with('progress', None, 'stall', minutes=41, last='开始执行 体力换饼') def test_screenshot_loops_are_covered_too(self): # while auto.take_screenshot() is None: continue —— 这种循环不识图 self.install() self.now[0] += 41 * 60 with self.assertRaises(ap.TaskAbort), mock.patch.object(ap.decisions, 'record'): self.auto.take_screenshot() def test_other_milestone_like_text_does_not_count(self): self.install() self.frame(30) self.milestone('目标图片:home/drive_assets.png, 相似度:0.98') with self.assertRaises(ap.TaskAbort), mock.patch.object(ap.decisions, 'record'): self.frame(11) def test_duration_suffix_is_trimmed_from_the_last_milestone(self): self.install() self.milestone('结束执行 一次镜牢 耗时:01:15:46') with self.assertRaises(ap.TaskAbort) as caught, mock.patch.object(ap.decisions, 'record'): self.frame(45) self.assertIn('最后一条: 结束执行 一次镜牢)', str(caught.exception)) def test_other_threads_never_abort(self): self.install() self.now[0] += 60 * 60 errors = [] def monitor(): try: self.utils.match_template(None, None, None) except BaseException as e: # noqa: B902 - 要看 TaskAbort 是否漏到这条线程 errors.append(e) worker = threading.Thread(target=monitor) worker.start() worker.join() self.assertEqual(errors, []) def test_env_sets_or_disables_the_limit(self): self.install(env={ap.PROGRESS_STALL_ENV: '90'}) self.frame(80) with self.assertRaises(ap.TaskAbort), mock.patch.object(ap.decisions, 'record'): self.frame(11) def test_env_zero_does_not_install(self): self.assertIsNone(self.install(env={ap.PROGRESS_STALL_ENV: '0'})) self.assertEqual(self.frame(600), ((1, 1), 0.3)) def test_bad_env_falls_back_to_default(self): self.install(env={ap.PROGRESS_STALL_ENV: 'abc'}) self.assertIn('不是数字, 按默认 40 分钟', '\n'.join(self.lines)) self.frame(39) if __name__ == '__main__': unittest.main()