按计划任务的方式(pythonw, 无控制台)实跑, 发现"合成前狂气(OCR): None"重试
72 秒仍然读不到 —— 而同一块底栏的体力(read_enkephalin)却一直读得到。
同屏两个读数一个成一个不成, 说明是我的读取逻辑坏了, 不是画面问题。
dump 底栏 OCR 一眼就看到了:
UNACY @(674,795) <- 本来应该是 "LUNACY", 首字母 L 被吃掉
9699 @(664,823) <- 数字明明在
而代码写的是 `'LUNACY' in text` —— "UNACY" 不含 "LUNACY", 所以每次都被判成
"读不到", 狂气校验直接退化成 None -> 跳过比对。**第三道防线一直是形同虚设的**,
而且因为日志里只写"读不到", 看起来像是画面没渲染好, 很难往这个方向想。
修法: 认词根 "NACY"(LUNACY / UNACY / NACY 都能命中, 这个词根不会出现在别处);
再兜一层按位置找标签 —— 底栏那一带(y 775~815, x<700)最靠左的短文本元素。
实测修复后: 找到 'UNACY' @(674,795), read_lunacy() 连读三次都是 9699(稳定)。
新增 5 个用例(截断标签能认、完整标签能认、截断标签下能读出数字、标签完全读不出
时按位置兜底、彻底没有标签时返回 None)。全套 272/272 通过。
372 lines
16 KiB
Python
372 lines
16 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""convert_modules 的纯逻辑测试。
|
|
|
|
这个脚本会操作真实游戏, 所以这里只测不碰硬件的部分: 屏幕判定、资源读数、
|
|
狂气相对比对用的解析。合成动作本身靠实机验证。
|
|
"""
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
|
import convert_modules as cm # noqa: E402
|
|
|
|
|
|
class QuietLogMixin:
|
|
"""把脚本的 log() 静音。
|
|
|
|
cm.log 会同时写 stdout 和 ~/lc_mcp/modules/convert.log —— 那是值守时要看的
|
|
真实运行记录, 不能被测试噪音污染(实测跑一次测试就往里塞了"服务未监听"
|
|
和"等服务就绪超时"两条, 看起来像是真出事了)。
|
|
"""
|
|
|
|
def setUp(self):
|
|
patcher = mock.patch.object(cm, 'log', lambda m: None)
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
|
|
class ScreenDetectTest(QuietLogMixin, unittest.TestCase):
|
|
|
|
def test_main_menu_needs_enough_markers(self):
|
|
good = 'Window | Sinners | Drive | Theater | Extract | Dispense'
|
|
self.assertTrue(cm.on_main_menu(good))
|
|
|
|
def test_partial_screen_is_not_main_menu(self):
|
|
# 标题页只有一两个词, 不能当成主界面 —— 否则会在标题页上找模组图标
|
|
self.assertFalse(cm.on_main_menu('TOUCH TO START | Clear all caches'))
|
|
self.assertFalse(cm.on_main_menu('Window | Sinners'))
|
|
|
|
def test_panel_marker(self):
|
|
self.assertTrue(cm.panel_open('Charge Enkephalin | Modules | Use Lunacy'))
|
|
self.assertFalse(cm.panel_open('Window | Sinners | Drive'))
|
|
|
|
|
|
class LunacyLabelTest(QuietLogMixin, unittest.TestCase):
|
|
"""⛔ 实测坑: OCR 会把 "LUNACY" 读成 "UNACY"(首字母 L 被吃掉)。
|
|
|
|
原来写的是 `'LUNACY' in text`, 而 "UNACY" 不含 "LUNACY" —— 于是每次都被判成
|
|
"读不到", 狂气校验退化成 None -> 跳过比对, 第三道防线形同虚设。同一块底栏的
|
|
体力却一直读得到, 所以只看日志很难发现, 是 dump 了底栏 OCR 才抓到的。
|
|
"""
|
|
|
|
def _with(self, its):
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
return cm.find_lunacy_label()
|
|
|
|
def test_matches_truncated_label(self):
|
|
lab = self._with([{'text': 'UNACY', 'x': 674, 'y': 795, 'w': 44, 'h': 22}])
|
|
self.assertIsNotNone(lab)
|
|
self.assertEqual(lab['text'], 'UNACY')
|
|
|
|
def test_matches_full_label(self):
|
|
self.assertIsNotNone(self._with([{'text': 'LUNACY', 'x': 670, 'y': 795}]))
|
|
|
|
def test_full_read_works_with_truncated_label(self):
|
|
its = [{'text': 'UNACY', 'x': 674, 'y': 795, 'w': 44, 'h': 22},
|
|
{'text': '9699', 'x': 664, 'y': 823, 'w': 70, 'h': 37}]
|
|
with mock.patch.object(cm, 'items', lambda: its), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertEqual(cm.read_lunacy(), 9699)
|
|
|
|
def test_falls_back_to_position_when_label_unreadable(self):
|
|
# 标签整个读不出来时, 按位置兜底: 底栏那一带最靠左的短文本
|
|
its = [{'text': '???', 'x': 674, 'y': 795, 'w': 44, 'h': 22},
|
|
{'text': '9699', 'x': 664, 'y': 823, 'w': 70, 'h': 37}]
|
|
lab = self._with(its)
|
|
self.assertIsNotNone(lab)
|
|
with mock.patch.object(cm, 'items', lambda: its), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertEqual(cm.read_lunacy(), 9699)
|
|
|
|
def test_no_label_at_all_returns_none(self):
|
|
its = [{'text': 'Window', 'x': 994, 'y': 809}]
|
|
self.assertIsNone(self._with(its))
|
|
|
|
|
|
class ReadOwnedTest(QuietLogMixin, unittest.TestCase):
|
|
|
|
def test_reads_owned(self):
|
|
its = [{'text': 'Enkephalin Module Owned 29', 'x': 820, 'y': 334}]
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
self.assertEqual(cm.read_owned(), 29)
|
|
|
|
def test_falls_back_to_bare_number(self):
|
|
its = [{'text': 'Owned', 'x': 820, 'y': 334},
|
|
{'text': '29', 'x': 900, 'y': 334}]
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
self.assertEqual(cm.read_owned(), 29)
|
|
|
|
def test_none_when_absent(self):
|
|
with mock.patch.object(cm, 'items', lambda: [{'text': 'Window', 'x': 1, 'y': 800}]):
|
|
self.assertIsNone(cm.read_owned())
|
|
|
|
|
|
class ReadEnkephalinTest(QuietLogMixin, unittest.TestCase):
|
|
"""只认主界面底栏的 x/126。
|
|
|
|
合成面板里也有一个 x/126, 但那是"合成后"的预览值 —— 拿它当余额会把
|
|
"体力不足"误判成"还有体力", 于是每轮都去点确认。
|
|
"""
|
|
|
|
def test_reads_bottom_bar(self):
|
|
its = [{'text': '13/126', 'x': 451, 'y': 827}]
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
self.assertEqual(cm.read_enkephalin(), 13)
|
|
|
|
def test_ignores_panel_preview(self):
|
|
# y≈508 是合成面板里的预览, 不是余额
|
|
its = [{'text': '132/126', 'x': 709, 'y': 508}]
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
self.assertIsNone(cm.read_enkephalin())
|
|
|
|
def test_handles_spaces_and_garbage(self):
|
|
its = [{'text': '13 / 126', 'x': 451, 'y': 827}]
|
|
with mock.patch.object(cm, 'items', lambda: its):
|
|
self.assertEqual(cm.read_enkephalin(), 13)
|
|
|
|
|
|
class LunacyReadTest(QuietLogMixin, unittest.TestCase):
|
|
"""狂气数字是像素艺术字, OCR 会误读(实测 6636 读成 9699)。
|
|
|
|
所以脚本不对绝对值下结论, 只做**前后相对比对**: 同一个画面同一套字体,
|
|
误读方式一致, 两次读数不同就说明数字真的变了。
|
|
"""
|
|
|
|
def _frames(self, values):
|
|
seq = list(values)
|
|
|
|
def fake():
|
|
v = seq.pop(0) if len(seq) > 1 else seq[0]
|
|
return [{'text': 'LUNACY', 'x': 670, 'y': 795},
|
|
{'text': str(v), 'x': 664, 'y': 823}]
|
|
return fake
|
|
|
|
def test_majority_wins_over_outlier(self):
|
|
with mock.patch.object(cm, 'items', self._frames([9699, 6636, 9699, 9699, 9699])), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertEqual(cm.read_lunacy(), 9699)
|
|
|
|
def test_none_when_no_label(self):
|
|
with mock.patch.object(cm, 'items', lambda: [{'text': 'Window', 'x': 1, 'y': 800}]), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertIsNone(cm.read_lunacy())
|
|
|
|
|
|
class EnkephalinGateTest(QuietLogMixin, unittest.TestCase):
|
|
"""体力读数是"要不要合成"的闸门, 读不到时不能贸然去点合成。
|
|
|
|
单次读成 None 的话, 原来那层"体力不足就收工"的判断会被整个跳过 —— 于是明明
|
|
只是体力不够, 却要去点合成、最后报成 FAIL(退出码 1), 给值守留下一条误导性
|
|
的失败记录。底栏淡入时读不到是实测发生过的(2026-10-16 11:01 的狂气读数)。
|
|
"""
|
|
|
|
def _main(self, enk_seq):
|
|
seq = list(enk_seq)
|
|
called = []
|
|
with mock.patch.object(cm, 'read_enkephalin', lambda: seq.pop(0) if seq else None), \
|
|
mock.patch.object(cm, 'read_lunacy', lambda **k: 9699), \
|
|
mock.patch.object(cm, 'ensure_server', lambda *a: True), \
|
|
mock.patch.object(cm, 'ensure_display', lambda *a: True), \
|
|
mock.patch.object(cm, 'launch_game', lambda *a: True), \
|
|
mock.patch.object(cm, 'wait_main_menu', lambda *a: True), \
|
|
mock.patch.object(cm, 'ensure_window_size', lambda *a: (1600, 900)), \
|
|
mock.patch.object(cm, 'close_panel', lambda: True), \
|
|
mock.patch.object(cm, 'close_game', lambda: None), \
|
|
mock.patch.object(cm, 'convert_once',
|
|
lambda tries=2: (called.append(1),
|
|
(True, '模组 29 -> 30 (+1)', 1))[1]), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
code = cm.main([])
|
|
return code, bool(called)
|
|
|
|
def test_enough_enkephalin_goes_ahead(self):
|
|
code, called = self._main([25])
|
|
self.assertEqual(code, 0)
|
|
self.assertTrue(called)
|
|
|
|
def test_not_enough_skips_without_touching_confirm(self):
|
|
code, called = self._main([4])
|
|
self.assertEqual(code, 0, '体力不足是正常情况, 不该报失败')
|
|
self.assertFalse(called, '体力不足时绝不该去点合成')
|
|
|
|
def test_unreadable_enkephalin_skips_instead_of_failing(self):
|
|
code, called = self._main([None, None, None, None])
|
|
self.assertEqual(code, 0, '读不到就跳过, 不要报成 FAIL')
|
|
self.assertFalse(called)
|
|
|
|
def test_read_is_retried(self):
|
|
"""前几次读不到要接着读, 不能一次读不到就放弃。"""
|
|
code, called = self._main([None, None, 25])
|
|
self.assertEqual(code, 0)
|
|
self.assertTrue(called, '重试后读到了就该正常合成')
|
|
|
|
|
|
class ConvertRetryTest(QuietLogMixin, unittest.TestCase):
|
|
""""点了确认但模组没变"有两种原因, 该有不同的处置。
|
|
|
|
1. 游戏限制了本轮/本日能换的数量 —— 正常, 重试也没用
|
|
2. 界面这一次没响应 —— 偶发, 重开面板通常就好
|
|
只做一次分不清这两者, 所以默认重试一次。
|
|
"""
|
|
|
|
def _run(self, attempts):
|
|
seq = list(attempts)
|
|
with mock.patch.object(cm, '_convert_attempt', lambda: seq.pop(0)), \
|
|
mock.patch.object(cm, 'close_panel', lambda: True), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
return cm.convert_once()
|
|
|
|
def test_retries_once_then_succeeds(self):
|
|
ok, info, got = self._run([(False, '点了确认但模组没变(29)', 0),
|
|
(True, '模组 29 -> 30 (+1)', 1)])
|
|
self.assertTrue(ok)
|
|
self.assertEqual(got, 1)
|
|
self.assertIn('29 -> 30', info)
|
|
|
|
def test_both_attempts_fail_reports_the_reason(self):
|
|
ok, info, got = self._run([(False, '点了确认但模组没变(29)', 0),
|
|
(False, '点了确认但模组没变(29)', 0)])
|
|
self.assertFalse(ok)
|
|
self.assertIn('没变', info)
|
|
|
|
def test_no_retry_when_first_succeeds(self):
|
|
calls = []
|
|
|
|
def once():
|
|
calls.append(1)
|
|
return True, '模组 29 -> 30 (+1)', 1
|
|
|
|
with mock.patch.object(cm, '_convert_attempt', once):
|
|
cm.convert_once()
|
|
self.assertEqual(len(calls), 1, '成功了就不该再试')
|
|
|
|
def test_retry_closes_the_panel_first(self):
|
|
"""重试前必须把面板关掉重开 —— 不关的话还是那个没响应的界面。"""
|
|
closed = []
|
|
seq = [(False, 'no', 0), (True, 'ok', 1)]
|
|
with mock.patch.object(cm, '_convert_attempt', lambda: seq.pop(0)), \
|
|
mock.patch.object(cm, 'close_panel', lambda: closed.append(1) or True), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
cm.convert_once()
|
|
self.assertEqual(len(closed), 1)
|
|
|
|
|
|
class DisplayRecoveryTest(QuietLogMixin, unittest.TestCase):
|
|
"""断线时桌面被拆掉, 截图全黑 —— 先修显示再动游戏。
|
|
|
|
看门狗每分钟修一次, 但整轮合成只跑 3 分钟; 断线正好落在这 3 分钟里的话,
|
|
这轮必然失败而下一轮要等 4 小时。实测 2026-10-16 一天内会话掉了两次。
|
|
"""
|
|
|
|
def test_ok_when_rendering(self):
|
|
with mock.patch.object(cm, 'render_ok', lambda: True), \
|
|
mock.patch.object(cm.subprocess, 'run') as run:
|
|
self.assertTrue(cm.ensure_display())
|
|
run.assert_not_called() # 正常时不该去折腾看门狗
|
|
|
|
def test_runs_watchdog_then_rechecks(self):
|
|
seq = [False, False, True] # 先挂两次, 第三次恢复
|
|
calls = []
|
|
|
|
def fake_run(cmd, **kw):
|
|
calls.append(cmd)
|
|
return mock.Mock(returncode=0)
|
|
|
|
with mock.patch.object(cm, 'render_ok', lambda: seq.pop(0)), \
|
|
mock.patch.object(cm.subprocess, 'run', fake_run), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertTrue(cm.ensure_display(timeout_s=10))
|
|
self.assertEqual(len(calls), 1, '应该跑一次看门狗')
|
|
self.assertIn('keep_display.ps1', ' '.join(calls[0]))
|
|
|
|
def test_reports_failure_when_never_recovers(self):
|
|
with mock.patch.object(cm, 'render_ok', lambda: False), \
|
|
mock.patch.object(cm.subprocess, 'run', lambda *a, **k: mock.Mock()), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertFalse(cm.ensure_display(timeout_s=0))
|
|
|
|
def test_watchdog_launch_failure_does_not_crash(self):
|
|
# 看门狗自己起不来时也要给出结论, 而不是抛异常
|
|
def boom(*a, **k):
|
|
raise OSError('powershell not found')
|
|
|
|
with mock.patch.object(cm, 'render_ok', lambda: False), \
|
|
mock.patch.object(cm.subprocess, 'run', boom), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertFalse(cm.ensure_display(timeout_s=0))
|
|
|
|
|
|
class ServerBootstrapTest(QuietLogMixin, unittest.TestCase):
|
|
"""值守时没人手动救服务, 所以脚本要能自己确认/拉起 lc-mcp 的 TCP 服务。
|
|
|
|
计划任务 LcMCPServer 配了 RestartCount=3, 但那只在进程"崩溃"时生效;
|
|
服务压根没起来(或重启后还没登录)时, 合成会因为连不上而失败。
|
|
"""
|
|
|
|
def test_listening_returns_bool(self):
|
|
self.assertIsInstance(cm.server_listening(timeout=0.5), bool)
|
|
|
|
def test_ensure_server_short_circuits_when_listening(self):
|
|
with mock.patch.object(cm, 'server_listening', lambda **kw: True):
|
|
self.assertTrue(cm.ensure_server(timeout_s=1))
|
|
|
|
def test_ensure_server_gives_up_when_never_listening(self):
|
|
calls = []
|
|
with mock.patch.object(cm, 'server_listening', lambda **kw: False), \
|
|
mock.patch.object(cm.subprocess, 'Popen',
|
|
lambda *a, **k: calls.append(a)), \
|
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
|
self.assertFalse(cm.ensure_server(timeout_s=0))
|
|
self.assertEqual(len(calls), 1, '应该尝试拉起服务一次')
|
|
|
|
def test_server_port_matches_the_project_default(self):
|
|
# 服务端 lc_mcp/server.py 用的就是 8765, 改这里要同步改
|
|
self.assertEqual(cm.SERVER_PORT, 8765)
|
|
|
|
|
|
class ForbiddenCoordinateTest(QuietLogMixin, unittest.TestCase):
|
|
"""Use Lunacy 页签的坐标只能用于"确认没点到它", 不能出现在任何点击路径上。
|
|
|
|
这条做成测试, 是因为以后改动布局时很容易顺手把它接进某个分支 ——
|
|
那是一步就能花掉狂气的操作。用 AST 查实参, 而不是查文本: 文本里
|
|
文档说明本来就会提到这个常量名。
|
|
"""
|
|
|
|
def _source(self):
|
|
return (Path(__file__).resolve().parents[1] / 'scripts' / 'convert_modules.py') \
|
|
.read_text(encoding='utf-8')
|
|
|
|
def test_no_click_call_passes_the_use_lunacy_tab(self):
|
|
import ast
|
|
tree = ast.parse(self._source())
|
|
offenders = []
|
|
for node in ast.walk(tree):
|
|
if not isinstance(node, ast.Call):
|
|
continue
|
|
f = node.func
|
|
name = getattr(f, 'id', None) or getattr(f, 'attr', None)
|
|
if name != 'click':
|
|
continue
|
|
for arg in list(node.args):
|
|
# click(*USE_LUNACY_TAB) 或 click(USE_LUNACY_TAB[0], ...)
|
|
if isinstance(arg, ast.Starred):
|
|
arg = arg.value
|
|
if isinstance(arg, ast.Name) and arg.id == 'USE_LUNACY_TAB':
|
|
offenders.append('line %d' % node.lineno)
|
|
if isinstance(arg, ast.Subscript) and \
|
|
getattr(arg.value, 'id', None) == 'USE_LUNACY_TAB':
|
|
offenders.append('line %d' % node.lineno)
|
|
self.assertFalse(offenders, 'click() 用到了 Use Lunacy 页签坐标: ' + ', '.join(offenders))
|
|
|
|
def test_the_constant_still_exists_and_is_where_we_think(self):
|
|
"""万一布局变了, 这条会提醒重新量坐标, 而不是让上面那条静静地失效。"""
|
|
self.assertEqual(cm.USE_LUNACY_TAB, (797, 283))
|
|
self.assertLess(cm.TAB_MODULES[0], cm.USE_LUNACY_TAB[0]) # Modules 在左
|
|
self.assertLess(cm.USE_LUNACY_TAB[0], cm.TAB_USE_BOXES[0]) # Use Boxes 在右
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|