计划任务 LcMCPServer 配了 RestartCount=3 / PT1M, 但那只在进程"崩溃"时生效。 服务压根没起来(或重启后还没登录)时, convert_modules 会因为连不上 8765 而失败 —— 而值守场景没人手动救它。 新增 server_listening() / ensure_server(): 先探端口, 不通就用 pythonw 拉起 lc_mcp.server 并等它就绪(最多 90 秒)。已监听时 0.03 秒直接返回, 不引入额外延迟。 同时把 REPO / PYW / SERVER_PORT 提成模块常量, 不再散落魔法值。 新增 4 个用例(端口探测返回 bool、已监听时短路、不通时尝试拉起一次、端口号与项目一致)。 全套 250/250 通过。
172 lines
7.1 KiB
Python
172 lines
7.1 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 ScreenDetectTest(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 ReadOwnedTest(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(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(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 ServerBootstrapTest(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(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()
|