# -*- coding: utf-8 -*- """返回按钮: 上游 V1.6.2 拿 click_element 的返回值当坐标(c9a884a)。 if back_button := auto.click_element("home/back_assets.png", model="normal"): auto.mouse_click(back_button[0], back_button[1]) click_element 点完返回 True, 下一行 True[0] → TypeError。画面上出现首次引导 (home/first_prompt_assets.png)时必走这里。端到端用例执行上游真实的 select_battle_team。 """ import ast import sys 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 = REPO / 'vendor' / 'aalc' MIRROR = AALC / 'tasks' / 'mirror' / 'mirror.py' TEAM = AALC / 'tasks' / 'teams' / 'team_formation.py' BACK, BACK2 = ap.BACK_BUTTONS BACK_POS = (60, 35) def upstream_function(path, name, cls=None, **ns): """把上游某个函数原样编译出来(去掉装饰器), 行号和文件名不变, inspect / linecache 都认。""" tree = ast.parse(path.read_text(encoding='utf-8')) body = tree.body if cls: body = next(n for n in body if isinstance(n, ast.ClassDef) and n.name == cls).body node = next(n for n in body if isinstance(n, ast.FunctionDef) and n.name == name) node.decorator_list = [] exec(compile(ast.Module(body=[node], type_ignores=[]), str(path), 'exec'), ns) return ns[name] class FirstPromptScreen: """首次引导页: 左上角有返回按钮(back 或 back2), 点一次就退出引导。 click_element 按上游 Automation 的语义: 点了返回 True, click=False 时只返回坐标。 """ def __init__(self, button=BACK): self.button = button self.prompt = True self.clicks = [] def take_screenshot(self, *args, **kwargs): return object() def find_element(self, target, *args, **kwargs): if target == 'home/first_prompt_assets.png': return self.prompt return None def click_element(self, target, *args, click=True, **kwargs): if not (self.prompt and target == self.button): return False if not click: return BACK_POS self.mouse_click(*BACK_POS) return True def mouse_click(self, x, y, *args, **kwargs): self.clicks.append((x, y)) self.prompt = False def fake_modules(auto, select_battle_team=None, mirror_methods=None): mirror_mod = types.ModuleType('tasks.mirror.mirror') mirror_mod.Mirror = type('Mirror', (), dict(mirror_methods or {})) team_mod = types.ModuleType('tasks.teams.team_formation') team_mod.select_battle_team = select_battle_team or (lambda num: True) mirror_pkg, teams_pkg = types.ModuleType('tasks.mirror'), types.ModuleType('tasks.teams') mirror_pkg.mirror, teams_pkg.team_formation = mirror_mod, team_mod return {'tasks': types.ModuleType('tasks'), 'tasks.mirror': mirror_pkg, 'tasks.mirror.mirror': mirror_mod, 'tasks.teams': teams_pkg, 'tasks.teams.team_formation': team_mod, 'module': types.ModuleType('module'), 'module.automation': SimpleNamespace(auto=auto)} @unittest.skipUnless(MIRROR.is_file() and TEAM.is_file(), 'vendor/aalc 子模块未检出') class UpstreamSelectBattleTeamTest(unittest.TestCase): """执行上游真实的 select_battle_team: 先处理首次引导, 之后找不到编队页就返回。""" def play(self, screen, patched=True): self.lines = [] ns = dict(auto=screen, cfg=SimpleNamespace(set_win_size=720), log=mock.Mock(), sleep=lambda *a: None) select = upstream_function(TEAM, 'select_battle_team', **ns) if patched: with mock.patch.dict(sys.modules, fake_modules(screen, select)): self.assertIsNotNone(ap.install_back_button_result(log=self.lines.append)) return select(1) def test_upstream_v162_crashes_on_the_first_prompt(self): with self.assertRaisesRegex(TypeError, "'bool' object is not subscriptable"): self.play(FirstPromptScreen(), patched=False) def test_back_button_is_clicked_exactly_once(self): screen = FirstPromptScreen() self.play(screen) self.assertEqual(screen.clicks, [BACK_POS]) self.assertIn('返回按钮: 上游要拿坐标自己点, 这次只找不点', '\n'.join(self.lines)) def test_second_back_template_is_also_handled(self): screen = FirstPromptScreen(button=BACK2) self.play(screen) self.assertEqual(screen.clicks, [BACK_POS]) def test_callers_that_only_test_truthiness_still_get_a_click(self): # back_init_menu: if auto.click_element("home/back_assets.png") or ...: continue screen = FirstPromptScreen() self.play(screen, patched=True) # 引导已被 select_battle_team 点掉 screen.prompt = True screen.clicks.clear() self.assertIs(screen.click_element(BACK), True) self.assertEqual(screen.clicks, [BACK_POS]) def test_other_targets_and_explicit_click_argument_untouched(self): screen = FirstPromptScreen() self.play(screen) screen.prompt = True self.assertEqual(screen.click_element(BACK, click=False), BACK_POS) self.assertIs(screen.click_element('home/other.png'), False) @unittest.skipUnless(MIRROR.is_file() and TEAM.is_file(), 'vendor/aalc 子模块未检出') class InstallBackButtonResultTest(unittest.TestCase): def install(self, **kwargs): self.lines = [] auto = FirstPromptScreen() with mock.patch.dict(sys.modules, fake_modules(auto, **kwargs)): return ap.install_back_button_result(log=self.lines.append) def test_finds_all_three_upstream_call_sites(self): methods = {name: upstream_function(MIRROR, name, cls='Mirror') for name in ('road_to_mir', 'run')} select = upstream_function(TEAM, 'select_battle_team') self.assertIsNotNone(self.install(select_battle_team=select, mirror_methods=methods)) self.assertIn('patch: 返回按钮按坐标交给上游点一次(Mirror.road_to_mir/Mirror.run/select_battle_team)', '\n'.join(self.lines)) def test_not_installed_once_upstream_stops_using_the_result(self): def select_battle_team(num): if auto.click_element("home/back_assets.png", model="normal"): # noqa: F821 return False self.assertIsNone(self.install(select_battle_team=select_battle_team)) self.assertIn('返回按钮补丁不装', '\n'.join(self.lines)) if __name__ == '__main__': unittest.main()