Files
lc-mcp/tests/test_levelup.py
T
ywsandClaude Opus 5.5 5fdb9fd221 普通镜牢转交 AALC,核对镜牢实际完成局数,合成与任务互斥
转交与互斥(原未提交改动,合入选队分支后提交):
- MCP 初始化说明和工具描述引导常规日常/领奖/普通镜牢走
  task_status -> run_task -> 轮询;jev_mirror_agent 默认转交 AALC,
  明确的速通配置、决策模型或未完成速通局仍走逐步模式。
- 新增 ~/lc_mcp/game.lock:run_task 子进程与模块合成脚本互斥,
  合成脚本另查 task_status,状态不明时不接管游戏。
- all 任务的 HDR 提示无 GUI 时直接确认并记录;预检按窗口位置
  决定是否计标题栏,set_windows=false 跳过;levelup card/weakest
  同步失败返回非零。急停提示改为 End 键。

镜牢结果(对接文档指出的缺口):
- 预检拒绝空队列和没有适用当前难度队伍的配置:AALC 会一局不打
  就以 0 退出。可用判定与 Mirror_task 的固定用途规则一致。
- 监听 mirror_signal 统计实际完成局数;少于应打局数记 [ERROR]
  并以退出码 4 结束,task_status 为 failed。
- mirror 指定 count 时临时关闭 infinite_dungeons(否则次数变成
  9999),原值写入恢复文件,结束或强杀后写回。

验证:Linux / Python 3.12 全套 507 项通过;git diff --check 通过。
用上游真实 Mirror_task 回放了无可用队伍和正常完成两种情况;
Windows 实机仍需回归。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 03:27:03 +00:00

274 lines
12 KiB
Python

from pathlib import Path
import sys
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import lc_ui # noqa: E402
import levelup # noqa: E402
def shot(w, h, items=()):
return {'ok': True, 'screen': {'path': 'x.png', 'w': w, 'h': h},
'items': [dict(i) for i in items]}
class ScaleTest(unittest.TestCase):
"""脚本坐标按 1600x900 写, 实际客户区可能是 1280x720 或 1920x1080。"""
def setUp(self):
lc_ui._scale = None
def test_720p_items_and_clicks_use_base_coordinates(self):
calls = []
def probe(tool, args=None, quiet=True):
calls.append((tool, args))
return shot(1280, 720, [{'text': 'Level Up', 'x': 800, 'y': 400, 'w': 80, 'h': 20}])
with mock.patch.object(lc_ui, 'probe', probe):
item = lc_ui.items()[0]
lc_ui.click(1600, 900)
lc_ui.long_press(800, 450)
self.assertEqual((item['x'], item['y'], item['w']), (1000, 500, 100))
self.assertEqual(calls[1], ('click', {'x': 1280, 'y': 720, 'times': 1}))
self.assertEqual(calls[2], ('drag', {'from_x': 640, 'from_y': 360, 'to_x': 640, 'to_y': 360}))
def test_click_before_any_state_reads_client_size_first(self):
calls = []
def probe(tool, args=None, quiet=True):
calls.append((tool, args))
return shot(1920, 1080)
with mock.patch.object(lc_ui, 'probe', probe):
lc_ui.click(100, 100)
self.assertEqual(calls, [('get_state', None), ('click', {'x': 120, 'y': 120, 'times': 1})])
class ProgressResultTest(unittest.TestCase):
def run_progress(self, detail=True, level=40, uptie=True):
card = (0, 0, 300, 400, 40, True, 'identity')
with mock.patch.object(levelup, 'state', return_value={'items': []}), \
mock.patch.object(levelup, 'scan_grid', return_value=[card]), \
mock.patch.object(levelup, 'dismiss_modal', return_value=True), \
mock.patch.object(levelup, 'long_press'), \
mock.patch.object(levelup, 'click'), \
mock.patch.object(levelup, 'detail_page_open', return_value=detail), \
mock.patch.object(levelup, 'read_detail', return_value=(level, 'identity', None)), \
mock.patch.object(levelup, 'do_level_up', return_value=(True, '等级增加')) as upgrade, \
mock.patch.object(levelup, 'do_uptie', return_value=(uptie, '同步结果')), \
mock.patch.object(levelup.time, 'sleep'), \
mock.patch('builtins.print'):
code = levelup.cmd_progress(target=34)
return code, upgrade.call_count
def test_unopened_details_are_not_a_successful_noop(self):
self.assertEqual(self.run_progress(detail=False), (1, 0))
def test_unreadable_details_do_not_trust_grid_level(self):
self.assertEqual(self.run_progress(level=None), (1, 0))
def test_verified_max_level_is_a_successful_noop(self):
self.assertEqual(self.run_progress(level=40), (0, 0))
def test_failed_uptie_is_a_failed_command(self):
self.assertEqual(self.run_progress(level=30, uptie=False), (1, 1))
def test_successful_upgrade_and_uptie(self):
self.assertEqual(self.run_progress(level=30), (0, 1))
class CardResultTest(unittest.TestCase):
def run_command(self, command, upgrade=True, sync=True, uptie=True):
argv = ['levelup.py'] + command + (['--uptie'] if uptie else [])
with mock.patch.object(sys, 'argv', argv), \
mock.patch.object(levelup, 'state', return_value={
'items': [{'text': 'Level Up IDENTITIES Facades'}]}), \
mock.patch.object(levelup, 'scan_grid', return_value=[
(0, 0, 335, 260, 20, True, 'identity')]), \
mock.patch.object(levelup, 'dismiss_modal'), \
mock.patch.object(levelup, 'long_press'), \
mock.patch.object(levelup, 'click'), \
mock.patch.object(levelup, 'do_level_up', return_value=(upgrade, '升级结果')), \
mock.patch.object(levelup, 'do_uptie', return_value=(sync, '同步结果')) as synchronize, \
mock.patch.object(levelup.time, 'sleep'), \
mock.patch('builtins.print'):
code = levelup.main()
return code, synchronize.call_count
def test_failed_uptie_fails_both_cli_commands(self):
for command in (['card', '0', '0'], ['weakest']):
with self.subTest(command=command):
self.assertEqual(self.run_command(command, sync=False), (1, 1))
def test_both_operations_must_succeed(self):
self.assertEqual(self.run_command(['card', '0', '0']), (0, 1))
self.assertEqual(self.run_command(['card', '0', '0'], upgrade=False), (1, 1))
def test_upgrade_without_uptie_keeps_its_result(self):
self.assertEqual(self.run_command(['card', '0', '0'], uptie=False), (0, 0))
class Screens:
"""按顺序返回预设画面的假 state(), 记录点击。"""
def __init__(self, *screens):
self.screens = list(screens)
self.clicks = []
self.keys = []
def state(self):
texts = self.screens.pop(0) if len(self.screens) > 1 else self.screens[0]
return {'items': [{'text': t, 'x': x, 'y': y} for t, x, y in texts]}
def patch(self):
return mock.patch.multiple(levelup, state=self.state, time=mock.Mock(),
click=lambda *a, **k: self.clicks.append(a),
keypress=lambda k: self.keys.append(k))
DETAIL = [('Level Up', 1164, 235), ('Uptie', 1367, 235), ('547/885', 400, 250)]
POWERUP = [('Owned:3', 900, 380), ('Owned:0', 900, 485), ('Owned:7', 900, 595), ('Cancel', 603, 670)]
class LevelUpVerdictTest(unittest.TestCase):
def test_detail_page_without_exp_change_is_not_success(self):
# 旧逻辑只要回到详情页(有 Level Up / Uptie)就算成功
screens = Screens(DETAIL, POWERUP, POWERUP, DETAIL, DETAIL)
with screens.patch():
ok, desc = levelup.do_level_up(hint=17)
self.assertFalse(ok, desc)
def test_level_increase_is_success(self):
after = [('Level Up', 1164, 235), ('Uptie', 1367, 235), ('100/1261', 400, 250)]
screens = Screens(DETAIL, POWERUP, POWERUP, after, after)
with screens.patch():
ok, desc = levelup.do_level_up(hint=17)
self.assertTrue(ok, desc)
self.assertIn('17->19', desc)
def test_popup_still_open_after_confirm_is_cancelled(self):
screens = Screens(DETAIL, POWERUP, POWERUP, POWERUP)
with screens.patch():
ok, desc = levelup.do_level_up(hint=17)
self.assertFalse(ok)
self.assertEqual(screens.clicks[-1], levelup.POWERUP_CANCEL)
def test_popup_still_open_but_level_rose_is_still_success(self):
"""2026-10-13 实机: Zwei Lv.33 -> Lv.40(MAX), 券也扣了, 但 Confirm 后弹窗仍在。
旧逻辑据此直接判 FAIL, 是假阴性。详情页的等级才是事实来源。
"""
after = [('Level Up', 1164, 235), ('Uptie', 1367, 235), ('0/6075', 400, 250)]
# 注意 state() 的消耗次数: 详情页 x1, 弹窗打开 x1, ticket_balance() x1,
# 点完 MAX 再读 x1, Confirm 之后再读 x1 —— 所以弹窗要连给 4 次。
screens = Screens(DETAIL, POWERUP, POWERUP, POWERUP, POWERUP, after, after)
with screens.patch():
ok, desc = levelup.do_level_up(hint=33)
self.assertTrue(ok, desc)
# 等级必须是从详情页读出来的(547/885 -> 17, 0/6075 -> 34), 不是从弹窗猜的
self.assertIn('17->34', desc)
# 仍然要把残留弹窗关掉, 不能把它留给下一步
self.assertIn(levelup.POWERUP_CANCEL, screens.clicks)
UPTIE_POPUP = [('Currency Req.', 700, 300), ('Skills Gained', 700, 400),
('Owned:2', 900, 450), ('Uptie', 1085, 580)]
class UptieVerdictTest(unittest.TestCase):
def test_popup_still_open_is_failure_even_with_skills_gained(self):
screens = Screens(UPTIE_POPUP, UPTIE_POPUP)
with screens.patch():
ok, desc = levelup.do_uptie()
self.assertFalse(ok, desc)
self.assertEqual(screens.keys, [])
def test_success_dialog_is_confirmed_without_esc(self):
done = [('Uptied!', 700, 300), ('Confirm', 772, 661)]
screens = Screens(UPTIE_POPUP, done, done)
with screens.patch():
ok, desc = levelup.do_uptie()
self.assertTrue(ok, desc)
self.assertEqual(screens.keys, [])
self.assertEqual(screens.clicks[-1], levelup.UPTIE_DONE_CONFIRM)
def test_unknown_screen_presses_esc_once_and_never_clicks_blind(self):
story = [("I swear to you! I will return this favor", 500, 700)]
screens = Screens(UPTIE_POPUP, story, story, story)
with screens.patch():
ok, desc = levelup.do_uptie()
self.assertFalse(ok)
self.assertEqual(screens.keys, ['esc'])
self.assertNotIn(levelup.UPTIE_CANCEL, screens.clicks)
def test_cutscene_skip_prompt_counts_as_success(self):
story = [("I swear to you!", 500, 700)]
skip = [('Skip this story?', 700, 400), ('Confirm', 900, 600)]
screens = Screens(UPTIE_POPUP, story, story, skip, skip)
with screens.patch():
ok, desc = levelup.do_uptie()
self.assertTrue(ok, desc)
self.assertEqual(screens.clicks[-1], (900, 600))
class DetailPageDetectTest(unittest.TestCase):
"""详情页判定。
2026-10-13 实机: Zwei 满级后详情页上的按钮是 MAX 而不是 Level Up,
代码却用 'Level Up' not in txt 判断"长按有没有打开详情", 于是把成功的
长按判成失败, 六张卡全部跳过, 升级环节空跑还报"无需升级"。
"""
def test_normal_detail_page(self):
self.assertTrue(levelup.detail_page_open('Level Up | Uptie | 547/885'))
def test_maxed_identity_without_level_up_label(self):
# 满级: 有 MAX 和 Uptie, 但没有 'Level Up'
t = 'Zwei Assoc. South Section 6 | LV40 | 0/6075 | MAX | Uptie++ | Summary | Sanity'
self.assertTrue(levelup.detail_page_open(t))
def test_sections_only(self):
self.assertTrue(levelup.detail_page_open('Summary | Skill | E.G.O | Sanity'))
def test_grid_is_not_detail_page(self):
t = 'TEAMS#1 | IDENTITIES | E.G.O | Facades | Details | ALL | SELECTED'
self.assertFalse(levelup.detail_page_open(t))
class MaxLevelReadTest(unittest.TestCase):
"""满级时进度条反推会偏低, 要以直读为准。
实测 Zwei LV40/MAX 的进度条是 0/6075, 而 6075 在表里对应 Lv.35,
反推成 34 —— 比真实等级低了 6 级。
"""
def _read(self, items):
screens = Screens([(i['text'], i['x'], i['y']) for i in items])
with screens.patch():
return levelup.read_detail(hint=None)
def test_maxed_uses_direct_level(self):
items = [{'text': 'Zwei Assoc. South Section 6', 'x': 1079, 'y': 155},
{'text': 'LV40', 'x': 900, 'y': 218},
{'text': '0/6075', 'x': 1000, 'y': 234},
{'text': 'MAX', 'x': 700, 'y': 240}]
lvl, name, bar = self._read(items)
self.assertEqual(lvl, 40)
def test_non_maxed_still_prefers_bar(self):
# 进度条 547/885 反推 Lv.17, 直读 LV17 一致
items = [{'text': 'LCB Sinner', 'x': 900, 'y': 155},
{'text': 'LV17', 'x': 900, 'y': 218},
{'text': '547/885', 'x': 1000, 'y': 234}]
lvl, name, bar = self._read(items)
self.assertEqual(lvl, 17)
if __name__ == '__main__':
unittest.main()