- driver_input: 只在"原地连点"(与上次点击相差<=4px)时先挪开 10px 再点, 强制产生 MOUSEMOVE; 按下 60→100ms, 到位等待 50→80ms (LCP_DRIVER_PRESS_MS / SETTLE_MS / WIGGLE 可调) - aalc_patches: 「获得E.G.O饰品」弹窗第 3 次起绕过驱动代理改软件点击(pyautogui), 第 8 次发回车, 20 次才中止; 新增 data/team_names.json 覆盖体系默认队名 (LCP_TEAM_NAMES=0 关闭) - docs: 2026-11-15 实测(6 连刷 6/6、键盘 4/4 进入、卡包按体系、镜像迷宫+震颤), ISSUES 状态更新 + I-14 前台被抢 单测: 709 passed。
664 lines
30 KiB
Python
664 lines
30 KiB
Python
import os
|
|
import sys
|
|
import types
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
from lc_mcp import aalc_patches # noqa: E402
|
|
|
|
|
|
class SystemTeamNamesTest(unittest.TestCase):
|
|
|
|
def test_all_ten_systems_are_mapped(self):
|
|
expected = {'burn', 'bleed', 'tremor', 'rupture', 'poise',
|
|
'sinking', 'charge', 'slash', 'pierce', 'blunt'}
|
|
self.assertEqual(set(aalc_patches.SYSTEM_TEAM_NAMES), expected)
|
|
|
|
def test_names_are_non_empty_strings(self):
|
|
for system, names in aalc_patches.SYSTEM_TEAM_NAMES.items():
|
|
self.assertIsInstance(names, tuple, system)
|
|
self.assertTrue(names, system)
|
|
for name in names:
|
|
self.assertIsInstance(name, str)
|
|
self.assertTrue(name.strip(), system)
|
|
|
|
def test_poise_accepts_both_spellings(self):
|
|
# 游戏里叫「呼吸法」, system_cn_zh 里叫「呼吸」, 两个都要能命中
|
|
self.assertIn('呼吸法', aalc_patches.SYSTEM_TEAM_NAMES['poise'])
|
|
self.assertIn('呼吸', aalc_patches.SYSTEM_TEAM_NAMES['poise'])
|
|
|
|
def test_preferences_point_at_real_systems_and_names(self):
|
|
# 2026-11-14 用户指定: 充能优先, 没有就退到震颤
|
|
for system, names in aalc_patches.TEAM_PREFERENCES.items():
|
|
self.assertIn(system, aalc_patches.SYSTEM_TEAM_NAMES)
|
|
self.assertTrue(names, system)
|
|
for name in names:
|
|
self.assertIsInstance(name, str)
|
|
self.assertTrue(name.strip())
|
|
self.assertEqual(aalc_patches.TEAM_PREFERENCES['charge'], ('震颤',))
|
|
|
|
|
|
class RegionTest(unittest.TestCase):
|
|
|
|
def test_team_list_region(self):
|
|
self.assertTrue(aalc_patches._in_list(130, 493)) # 实测「流血」的位置
|
|
self.assertTrue(aalc_patches._in_list(255, 305))
|
|
self.assertFalse(aalc_patches._in_list(130, 120)) # 顶栏不算列表
|
|
self.assertFalse(aalc_patches._in_list(600, 493)) # 右侧面板
|
|
self.assertFalse(aalc_patches._in_list(130, 700))
|
|
|
|
def test_header_region(self):
|
|
self.assertTrue(aalc_patches._in_header(242, 125)) # 实测顶栏「流血」
|
|
self.assertFalse(aalc_patches._in_header(130, 493))
|
|
self.assertFalse(aalc_patches._in_header(900, 125))
|
|
|
|
def test_regions_do_not_overlap(self):
|
|
for x, y in [(130, 493), (242, 125), (600, 300), (1200, 600)]:
|
|
self.assertFalse(
|
|
aalc_patches._in_list(x, y) and aalc_patches._in_header(x, y),
|
|
(x, y),
|
|
)
|
|
|
|
|
|
def ocr_engine(lines):
|
|
"""假的 module.ocr: 引擎按 [(文本, x, y)] 逐行返回, 同名文本可以出现多次(真实引擎就是这样)。"""
|
|
result = SimpleNamespace(txts=tuple(t for t, _, _ in lines),
|
|
boxes=[[[x - 10, y - 5], [x + 10, y - 5], [x + 10, y + 5], [x - 10, y + 5]]
|
|
for _, x, y in lines])
|
|
return SimpleNamespace(ocr=SimpleNamespace(run=lambda image: result))
|
|
|
|
|
|
class TeamOcrTest(unittest.TestCase):
|
|
|
|
def test_engine_lines_keep_duplicate_names(self):
|
|
# 选中「流血」后顶栏和列表都是「流血」; 经 {文本: 坐标} 只会剩一条
|
|
engine = ocr_engine([('流血', 242.0, 125.0), (' 流血 ', 130.0, 493.0), ('', 1.0, 1.0)])
|
|
auto = mock.Mock()
|
|
with mock.patch.dict(sys.modules, {'module': types.ModuleType('module'), 'module.ocr': engine}):
|
|
self.assertEqual(aalc_patches._team_ocr(auto), [('流血', 242.0, 125.0), ('流血', 130.0, 493.0)])
|
|
auto._run_ocr_for_text.assert_not_called()
|
|
|
|
|
|
class TeamOcrFallbackTest(unittest.TestCase):
|
|
"""OCR 引擎导入不了时退回 AALC 的 _run_ocr_for_text。"""
|
|
|
|
def setUp(self):
|
|
patcher = mock.patch.dict(sys.modules, {'module.ocr': None})
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def test_returns_list_and_strips_spaces(self):
|
|
auto = mock.Mock()
|
|
auto._run_ocr_for_text.return_value = {
|
|
'编队#20': [132.0, 340.5],
|
|
' 流血 ': [130.5, 493.0],
|
|
'': [0, 0],
|
|
}
|
|
items = aalc_patches._team_ocr(auto)
|
|
self.assertIn(('编队#20', 132.0, 340.5), items)
|
|
self.assertIn(('流血', 130.5, 493.0), items)
|
|
self.assertEqual(len(items), 2) # 空串被丢掉
|
|
|
|
def test_empty_ocr_gives_empty_list(self):
|
|
auto = mock.Mock()
|
|
auto._run_ocr_for_text.return_value = {}
|
|
self.assertEqual(aalc_patches._team_ocr(auto), [])
|
|
|
|
def test_bad_position_is_skipped_not_fatal(self):
|
|
auto = mock.Mock()
|
|
auto._run_ocr_for_text.return_value = {'流血': [], '斩击': [1.0, 2.0]}
|
|
self.assertEqual(aalc_patches._team_ocr(auto), [('斩击', 1.0, 2.0)])
|
|
|
|
|
|
class NormalizeTeamNameTest(unittest.TestCase):
|
|
|
|
def test_known_ocr_misreads_are_fixed(self):
|
|
# 2026-11-14 实测误读样本
|
|
self.assertEqual(aalc_patches.normalize_team_name('震颐'), '震颤')
|
|
self.assertEqual(aalc_patches.normalize_team_name('沉论'), '沉沦')
|
|
self.assertEqual(aalc_patches.normalize_team_name('编#16'), '编队#16')
|
|
self.assertEqual(aalc_patches.normalize_team_name('编队#16'), '编队#16')
|
|
|
|
def test_spaces_and_full_width_forms(self):
|
|
self.assertEqual(aalc_patches.normalize_team_name(' 流\u3000血 '), '流血')
|
|
self.assertEqual(aalc_patches.normalize_team_name('流血(X)'), '流血(X)')
|
|
self.assertEqual(aalc_patches.normalize_team_name('编队#20'), '编队#20')
|
|
|
|
|
|
class TeamNameMatchTest(unittest.TestCase):
|
|
|
|
def test_exact_prefix_and_miss(self):
|
|
match = aalc_patches.team_name_match
|
|
self.assertEqual(match('流血', ('流血',)), 'exact')
|
|
self.assertEqual(match('流血队', ('流血',)), 'prefix')
|
|
self.assertEqual(match('流血(X)', ('流血',)), 'prefix')
|
|
self.assertIsNone(match('编队#13', ('流血',)))
|
|
self.assertIsNone(match('出血', ('流血',)))
|
|
self.assertIsNone(match('血', ('流血',)))
|
|
|
|
def test_misread_text_matches_configured_name(self):
|
|
self.assertEqual(aalc_patches.team_name_match('震颐', aalc_patches.SYSTEM_TEAM_NAMES['tremor']), 'exact')
|
|
self.assertEqual(aalc_patches.team_name_match('沉论队', aalc_patches.SYSTEM_TEAM_NAMES['sinking']),
|
|
'prefix')
|
|
|
|
def test_single_character_names_never_prefix_match(self):
|
|
self.assertIsNone(aalc_patches.team_name_match('流血', ('流',)))
|
|
self.assertEqual(aalc_patches.team_name_match('流', ('流',)), 'exact')
|
|
|
|
|
|
class PickTeamItemTest(unittest.TestCase):
|
|
|
|
def test_exact_beats_prefix(self):
|
|
items = [('流血队', 130, 400), ('流血', 130, 450)]
|
|
self.assertEqual(aalc_patches.pick_team_item(items, ('流血',))[0], ('流血', 130, 450))
|
|
|
|
def test_names_order_decides_between_exact_hits(self):
|
|
items = [('呼吸', 130, 400), ('呼吸法', 130, 450)]
|
|
self.assertEqual(aalc_patches.pick_team_item(items, ('呼吸法', '呼吸'))[0], ('呼吸法', 130, 450))
|
|
|
|
def test_unique_prefix_is_picked(self):
|
|
hit, reason = aalc_patches.pick_team_item([('斩击', 130, 330), ('流血队', 130, 490)], ('流血',))
|
|
self.assertEqual(hit, ('流血队', 130, 490))
|
|
self.assertIn('前缀', reason)
|
|
|
|
def test_one_team_matching_two_names_is_still_unique(self):
|
|
hit, _ = aalc_patches.pick_team_item([('呼吸法队', 130, 400)], ('呼吸法', '呼吸'))
|
|
self.assertEqual(hit, ('呼吸法队', 130, 400))
|
|
|
|
def test_ambiguous_prefix_is_not_clicked(self):
|
|
hit, reason = aalc_patches.pick_team_item([('流血队', 130, 400), ('流血2', 130, 450)], ('流血',))
|
|
self.assertIsNone(hit)
|
|
self.assertIn('流血2/流血队', reason)
|
|
|
|
def test_nothing_matches(self):
|
|
self.assertEqual(aalc_patches.pick_team_item([('斩击', 130, 330)], ('流血',)), (None, ''))
|
|
|
|
|
|
class _FakeTeamScreen:
|
|
"""编队界面: 每下拖一次翻一屏(实测一次拖动 ≈ 5~6 行), 点哪条顶栏就变成哪条。"""
|
|
|
|
def __init__(self, screens, header='剧情关卡', click_changes_header=True):
|
|
self.screens = screens
|
|
self.header = header
|
|
self.click_changes_header = click_changes_header
|
|
self.swipes_down = 0
|
|
self.clicks = []
|
|
|
|
@property
|
|
def page(self):
|
|
return min(self.swipes_down, len(self.screens) - 1)
|
|
|
|
def take_screenshot(self):
|
|
return object()
|
|
|
|
@property
|
|
def screenshot(self):
|
|
return self # 交给假 OCR 引擎的"截图"就是这块界面
|
|
|
|
def find_element(self, *args, **kwargs):
|
|
return True
|
|
|
|
def mouse_click(self, *args):
|
|
pass
|
|
|
|
def mouse_swipe_for_team_scroll(self, x, y, dy, duration):
|
|
self.swipes_down = 0 if dy > 0 else self.swipes_down + 1
|
|
|
|
def read(self):
|
|
"""OCR 引擎逐行结果: 顶栏在上、列表在下; 同名条目各出现一次。"""
|
|
lines = [(self.header, 242.0, 125.0)] if self.header else []
|
|
lines += [(text, 130.0, 320.0 + 50 * row) for row, text in enumerate(self.screens[self.page])]
|
|
return SimpleNamespace(txts=tuple(t for t, _, _ in lines),
|
|
boxes=[[[x - 10, y - 5], [x + 10, y - 5], [x + 10, y + 5], [x - 10, y + 5]]
|
|
for _, x, y in lines])
|
|
|
|
def mouse_action_with_pos(self, pos, offset=False):
|
|
self.clicks.append(tuple(pos))
|
|
if self.click_changes_header:
|
|
row = int(round((pos[1] - 320.0) / 50))
|
|
self.header = self.screens[self.page][row]
|
|
|
|
|
|
class SelectTeamByNameTest(unittest.TestCase):
|
|
|
|
TOP = ['剧情关卡', '斩击', '突刺', '打击', '烧伤']
|
|
MIDDLE = ['流血', '震颤', '破裂', '沉沦', '呼吸法']
|
|
BOTTOM = ['充能', '编队#13', '编队#14']
|
|
|
|
def setUp(self):
|
|
self.lines = []
|
|
engine = SimpleNamespace(ocr=SimpleNamespace(run=lambda screen: screen.read()))
|
|
modules = mock.patch.dict(sys.modules, {'module': types.ModuleType('module'), 'module.ocr': engine})
|
|
modules.start()
|
|
self.addCleanup(modules.stop)
|
|
for patcher in (mock.patch.object(aalc_patches.time, 'sleep'),
|
|
mock.patch.object(aalc_patches.decisions, 'record')):
|
|
self.records = patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def select(self, auto, names):
|
|
return aalc_patches._select_team_by_name(auto, names, log=self.lines.append)
|
|
|
|
@property
|
|
def log(self):
|
|
return '\n'.join(self.lines)
|
|
|
|
def test_renamed_team_with_suffix_is_selected(self):
|
|
auto = _FakeTeamScreen([self.TOP, ['流血队', '震颤', '破裂'], self.BOTTOM])
|
|
self.assertTrue(self.select(auto, ('流血',)))
|
|
self.assertEqual(auto.header, '流血队')
|
|
self.assertEqual(len(auto.clicks), 1)
|
|
self.assertIn('已按体系名选中编队 流血队', self.log)
|
|
|
|
def test_misread_list_entry_is_selected(self):
|
|
auto = _FakeTeamScreen([self.TOP, ['流血', '震颐', '破裂', '沉论'], self.BOTTOM])
|
|
self.assertTrue(self.select(auto, ('沉沦',)))
|
|
self.assertEqual(auto.header, '沉论')
|
|
self.assertIn('选中编队 沉沦', self.log)
|
|
|
|
def test_current_team_needs_no_switch(self):
|
|
auto = _FakeTeamScreen([self.TOP, self.MIDDLE], header='沉论')
|
|
self.assertTrue(self.select(auto, ('沉沦',)))
|
|
self.assertEqual(auto.clicks, [])
|
|
self.assertIn('当前已经是编队 沉沦', self.log)
|
|
|
|
def test_current_team_that_is_also_in_the_list(self):
|
|
# 列表这一屏里也有「流血」: 顶栏那条不能被列表那条盖掉
|
|
auto = _FakeTeamScreen([self.MIDDLE], header='流血')
|
|
self.assertTrue(self.select(auto, ('流血',)))
|
|
self.assertEqual(auto.clicks, [])
|
|
self.assertIn('当前已经是编队 流血', self.log)
|
|
|
|
def test_current_team_with_suffix_is_confirmed_in_the_list(self):
|
|
# 顶栏 "流血队" 只是前缀命中, 不能直接当成目标; 列表里唯一后再点一次
|
|
auto = _FakeTeamScreen([self.TOP, ['流血队', '震颤']], header='流血队')
|
|
self.assertTrue(self.select(auto, ('流血',)))
|
|
self.assertEqual(len(auto.clicks), 1)
|
|
self.assertNotIn('无需切换', self.log)
|
|
|
|
def test_exact_team_wins_over_current_suffixed_team(self):
|
|
auto = _FakeTeamScreen([self.TOP, ['流血2', '流血', '震颤']], header='流血2')
|
|
self.assertTrue(self.select(auto, ('流血',)))
|
|
self.assertEqual(auto.header, '流血')
|
|
|
|
def test_name_order_is_priority_not_screen_order(self):
|
|
# 首选项「充能」在最后一屏, 次选「震颤」在第二屏 —— 应先翻到充能
|
|
auto = _FakeTeamScreen([self.TOP, self.MIDDLE, self.BOTTOM])
|
|
self.assertTrue(self.select(auto, ('充能', '震颤')))
|
|
self.assertEqual(auto.header, '充能')
|
|
|
|
def test_falls_back_to_second_name(self):
|
|
# 列表里没有「充能」, 第二顺位「震颤」应该被选中
|
|
auto = _FakeTeamScreen([self.TOP, ['流血', '震颤', '破裂'], ['编队#13', '编队#14']])
|
|
self.assertTrue(self.select(auto, ('充能', '震颤')))
|
|
self.assertEqual(auto.header, '震颤')
|
|
|
|
def test_missing_team_logs_every_screen_and_never_clicks(self):
|
|
auto = _FakeTeamScreen([self.TOP, self.MIDDLE, self.BOTTOM])
|
|
self.assertFalse(self.select(auto, ('编队#999',)))
|
|
self.assertEqual(auto.clicks, [])
|
|
self.assertIn('第 1 屏没有 编队#999; 这一屏的名字: 剧情关卡/斩击/突刺/打击/烧伤', self.log)
|
|
self.assertIn('第 3 屏没有 编队#999; 这一屏的名字: 充能/编队#13/编队#14', self.log)
|
|
self.assertIn('没找到 编队#999(共 6 轮)', self.log)
|
|
|
|
def test_ambiguous_suffixes_are_not_clicked(self):
|
|
auto = _FakeTeamScreen([['流血队', '流血2', '斩击']])
|
|
self.assertFalse(self.select(auto, ('流血',)))
|
|
self.assertEqual(auto.clicks, [])
|
|
self.assertIn('前缀匹配到多条(流血2/流血队), 不点', self.log)
|
|
|
|
def test_decisions_are_recorded(self):
|
|
self.select(_FakeTeamScreen([self.TOP, ['流血队', '震颤']]), ('流血',))
|
|
self.records.assert_called_with('team', '流血队', 'prefix', wanted=['流血'])
|
|
self.select(_FakeTeamScreen([self.TOP], header='流血'), ('流血',))
|
|
self.records.assert_called_with('team', '流血', 'current', wanted=['流血'])
|
|
self.select(_FakeTeamScreen([self.TOP, self.BOTTOM]), ('编队#999',))
|
|
args, kwargs = self.records.call_args
|
|
self.assertEqual(args, ('team', None, 'not_found'))
|
|
self.assertEqual(kwargs['seen'][0], self.TOP)
|
|
self.assertEqual(len(kwargs['seen']), 6)
|
|
|
|
def test_header_not_changing_is_reported(self):
|
|
auto = _FakeTeamScreen([self.TOP, self.MIDDLE], click_changes_header=False)
|
|
self.assertFalse(self.select(auto, ('流血',)))
|
|
self.assertIn('点了 流血 但顶栏没变(顶栏读到: 剧情关卡)', self.log)
|
|
|
|
|
|
class _FakeMirror:
|
|
def __init__(self, team_setting, team_num):
|
|
self.system = team_setting['system']
|
|
self.team_number = team_num
|
|
|
|
|
|
class InstallSystemTeamSelectionTest(unittest.TestCase):
|
|
"""用假模块验证挂载点与回退行为, 不依赖 AALC / Windows。"""
|
|
|
|
def setUp(self):
|
|
self.calls = []
|
|
|
|
def original(num):
|
|
self.calls.append(num)
|
|
return 'fallback'
|
|
|
|
self.original = original
|
|
self.mirror_mod = types.ModuleType('tasks.mirror.mirror')
|
|
self.mirror_mod.Mirror = _FakeMirror
|
|
self.mirror_mod.select_battle_team = original
|
|
|
|
self.auto = mock.Mock()
|
|
auto_mod = types.ModuleType('module.automation')
|
|
auto_mod.auto = self.auto
|
|
|
|
tasks_pkg = types.ModuleType('tasks')
|
|
tasks_pkg.__path__ = []
|
|
mirror_pkg = types.ModuleType('tasks.mirror')
|
|
mirror_pkg.__path__ = []
|
|
mirror_pkg.mirror = self.mirror_mod
|
|
module_pkg = types.ModuleType('module')
|
|
module_pkg.__path__ = []
|
|
|
|
self._patcher = mock.patch.dict(sys.modules, {
|
|
'tasks': tasks_pkg,
|
|
'tasks.mirror': mirror_pkg,
|
|
'tasks.mirror.mirror': self.mirror_mod,
|
|
'module': module_pkg,
|
|
'module.automation': auto_mod,
|
|
})
|
|
self._patcher.start()
|
|
self.addCleanup(self._patcher.stop)
|
|
|
|
def install(self):
|
|
return aalc_patches.install_system_team_selection(log=lambda *_: None)
|
|
|
|
def test_returns_original_and_replaces_module_global(self):
|
|
original = self.install()
|
|
self.assertIs(original, self.original)
|
|
self.assertIsNot(self.mirror_mod.select_battle_team, self.original)
|
|
# __init__ 也被包装, 但仍然正常构造
|
|
mirror = self.mirror_mod.Mirror({'system': 'bleed'}, 1)
|
|
self.assertEqual(mirror.system, 'bleed')
|
|
|
|
def test_known_system_uses_name_search(self):
|
|
self.install()
|
|
self.mirror_mod.Mirror({'system': 'bleed'}, 1)
|
|
with mock.patch.object(aalc_patches, '_select_team_by_name', return_value=True) as sel:
|
|
self.assertIs(self.mirror_mod.select_battle_team(1), True)
|
|
self.assertEqual(sel.call_args[0][1], ('流血',))
|
|
self.assertEqual(self.calls, []) # 成功后不再走编号回退
|
|
|
|
def test_charge_looks_for_charge_then_tremor(self):
|
|
self.install()
|
|
self.mirror_mod.Mirror({'system': 'charge'}, 1)
|
|
with mock.patch.object(aalc_patches, '_select_team_by_name', return_value=True) as sel:
|
|
self.mirror_mod.select_battle_team(1)
|
|
self.assertEqual(sel.call_args[0][1], ('充能', '震颤'))
|
|
|
|
def test_name_search_failure_does_not_fall_back_to_number(self):
|
|
# 这号没有 编队#1, 静默回退到编号会选中错的队, 必须显式失败
|
|
self.install()
|
|
self.mirror_mod.Mirror({'system': 'bleed'}, 1)
|
|
with mock.patch.object(aalc_patches, '_select_team_by_name', return_value=False):
|
|
self.assertIs(self.mirror_mod.select_battle_team(1), False)
|
|
self.assertEqual(self.calls, [])
|
|
|
|
def test_exception_in_name_search_is_contained(self):
|
|
self.install()
|
|
self.mirror_mod.Mirror({'system': 'bleed'}, 1)
|
|
boom = mock.Mock(side_effect=RuntimeError('ocr down'))
|
|
with mock.patch.object(aalc_patches, '_select_team_by_name', boom):
|
|
self.assertIs(self.mirror_mod.select_battle_team(1), False)
|
|
|
|
def test_unknown_system_falls_back_to_original(self):
|
|
self.install()
|
|
self.mirror_mod.Mirror({'system': 'unknown_system'}, 3)
|
|
self.assertEqual(self.mirror_mod.select_battle_team(3), 'fallback')
|
|
self.assertEqual(self.calls, [3])
|
|
|
|
def test_missing_mirror_module_is_not_fatal(self):
|
|
with mock.patch.dict(sys.modules, {'tasks.mirror': None, 'tasks.mirror.mirror': None}):
|
|
self.assertIsNone(self.install())
|
|
|
|
|
|
class TeamScreenDetectionTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
engine = SimpleNamespace(ocr=SimpleNamespace(run=lambda screen: screen.read()))
|
|
modules = mock.patch.dict(sys.modules, {'module': types.ModuleType('module'), 'module.ocr': engine})
|
|
modules.start()
|
|
self.addCleanup(modules.stop)
|
|
|
|
def test_team_screen_detected_by_names(self):
|
|
auto = _FakeTeamScreen([['剧情关卡', '斩击', '突刺', '打击', '烧伤']])
|
|
self.assertTrue(aalc_patches._looks_like_team_screen(auto))
|
|
|
|
def test_starlight_screen_is_not_team_screen(self):
|
|
# 星之恩惠页: 满屏星星会让「选队星级」模板命中, 但左侧不是队名列表
|
|
auto = _FakeTeamScreen([['+', '卫星商店', '可免费进行商店关键词刷新1次', '获得1件1级饰品']],
|
|
header='西西弗的星之恩惠')
|
|
self.assertFalse(aalc_patches._looks_like_team_screen(auto))
|
|
|
|
|
|
class TeamScreenGuardTest(unittest.TestCase):
|
|
|
|
def install(self):
|
|
auto = mock.Mock()
|
|
|
|
def fake_find(target, *args, **kwargs):
|
|
return (640, 360) if target == aalc_patches.TEAM_SCREEN_STARS else False
|
|
|
|
auto.find_element = fake_find
|
|
auto_mod = types.ModuleType('module.automation')
|
|
auto_mod.auto = auto
|
|
patcher = mock.patch.dict(sys.modules, {'module': types.ModuleType('module'),
|
|
'module.automation': auto_mod})
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
aalc_patches.install_team_screen_guard(log=lambda *_: None)
|
|
return auto
|
|
|
|
def test_stars_ignored_on_starlight_screen(self):
|
|
auto = self.install()
|
|
with mock.patch.object(aalc_patches, '_is_starlight_screen', return_value=True):
|
|
self.assertFalse(auto.find_element(aalc_patches.TEAM_SCREEN_STARS))
|
|
|
|
def test_stars_kept_on_real_team_screen(self):
|
|
auto = self.install()
|
|
with mock.patch.object(aalc_patches, '_is_starlight_screen', return_value=False):
|
|
self.assertEqual(auto.find_element(aalc_patches.TEAM_SCREEN_STARS), (640, 360))
|
|
self.assertFalse(auto.find_element('mirror/other.png'))
|
|
|
|
|
|
class _LoadingTeamScreen(_FakeTeamScreen):
|
|
"""选队页刚加载: 第 loaded_at 张截图之前编队图标和列表都还没出来。"""
|
|
|
|
def __init__(self, screens, loaded_at=2, **kwargs):
|
|
super().__init__(screens, **kwargs)
|
|
self.loaded_at = loaded_at
|
|
self.shots = 0
|
|
|
|
def take_screenshot(self):
|
|
self.shots += 1
|
|
return object()
|
|
|
|
@property
|
|
def loaded(self):
|
|
return self.shots >= self.loaded_at
|
|
|
|
def find_element(self, *args, **kwargs):
|
|
return self.loaded
|
|
|
|
def read(self):
|
|
if not self.loaded:
|
|
return SimpleNamespace(txts=(), boxes=[])
|
|
return super().read()
|
|
|
|
|
|
class SelectTeamScreenWaitTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.lines = []
|
|
engine = SimpleNamespace(ocr=SimpleNamespace(run=lambda screen: screen.read()))
|
|
for patcher in (mock.patch.dict(sys.modules, {'module': types.ModuleType('module'), 'module.ocr': engine}),
|
|
mock.patch.object(aalc_patches.time, 'sleep'),
|
|
mock.patch.object(aalc_patches.decisions, 'record')):
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def test_waits_for_a_fresh_screenshot_of_the_team_screen(self):
|
|
# 旧写法对着同一张截图认 3 次, 页面加载慢一点就报"不在编队界面"
|
|
auto = _LoadingTeamScreen([['剧情关卡', '流血', '震颤']], loaded_at=3)
|
|
self.assertTrue(aalc_patches._select_team_by_name(auto, ('流血',), log=self.lines.append))
|
|
self.assertEqual(auto.header, '流血')
|
|
|
|
def test_gives_up_when_the_team_screen_never_shows(self):
|
|
auto = _LoadingTeamScreen([['剧情关卡', '流血']], loaded_at=99)
|
|
self.assertFalse(aalc_patches._select_team_by_name(auto, ('流血',), log=self.lines.append))
|
|
self.assertIn('不在编队界面, 跳过按名字选队', '\n'.join(self.lines))
|
|
self.assertEqual(auto.clicks, [])
|
|
|
|
|
|
class _EnterScreen(_FakeTeamScreen):
|
|
"""选队页右下角有「进入」; 记下点「进入」时顶栏是哪支队, 以及整屏 OCR 跑了几次。"""
|
|
|
|
def __init__(self, screens, enter=True, **kwargs):
|
|
super().__init__(screens, **kwargs)
|
|
self.enter = enter
|
|
self.entered_with = []
|
|
self.ocr_runs = 0
|
|
|
|
def click_element(self, target, *args, click=True, **kwargs):
|
|
if target != aalc_patches.ENTER_ASSET or not self.enter:
|
|
return False
|
|
if not click:
|
|
return (1100, 650)
|
|
self.entered_with.append(self.header)
|
|
return True
|
|
|
|
def read(self):
|
|
self.ocr_runs += 1
|
|
return super().read()
|
|
|
|
|
|
class EnterSwitchTest(unittest.TestCase):
|
|
"""镜牢入场时 AALC 直接点「进入」, 不走 select_battle_team; 补丁在点之前切队。"""
|
|
|
|
TOP = ['剧情关卡', '斩击', '突刺', '打击', '烧伤']
|
|
MIDDLE = ['流血', '震颤', '破裂', '沉沦', '呼吸法']
|
|
|
|
def install(self, auto, system='bleed'):
|
|
self.lines = []
|
|
self.mirror_mod = types.ModuleType('tasks.mirror.mirror')
|
|
self.mirror_mod.Mirror = type('Mirror', (_FakeMirror,), {})
|
|
self.mirror_mod.select_battle_team = lambda num: False
|
|
tasks_pkg, mirror_pkg = types.ModuleType('tasks'), types.ModuleType('tasks.mirror')
|
|
mirror_pkg.mirror = self.mirror_mod
|
|
engine = SimpleNamespace(ocr=SimpleNamespace(run=lambda screen: screen.read()))
|
|
for patcher in (mock.patch.dict(sys.modules, {
|
|
'tasks': tasks_pkg, 'tasks.mirror': mirror_pkg, 'tasks.mirror.mirror': self.mirror_mod,
|
|
'module': types.ModuleType('module'), 'module.ocr': engine,
|
|
'module.automation': SimpleNamespace(auto=auto)}),
|
|
mock.patch.object(aalc_patches.time, 'sleep'),
|
|
mock.patch.object(aalc_patches.decisions, 'record')):
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
aalc_patches.install_system_team_selection(log=self.lines.append)
|
|
self.mirror_mod.Mirror({'system': system}, 1)
|
|
return auto
|
|
|
|
@property
|
|
def log(self):
|
|
return '\n'.join(self.lines)
|
|
|
|
def test_no_enter_button_means_no_ocr(self):
|
|
# 主循环每轮都会问一次「进入」; 按钮不在画面上时不该为它跑整屏 OCR
|
|
auto = self.install(_EnterScreen([self.TOP], enter=False))
|
|
for _ in range(5):
|
|
self.assertFalse(auto.click_element(aalc_patches.ENTER_ASSET))
|
|
self.assertEqual(auto.ocr_runs, 0)
|
|
|
|
def test_switches_to_the_system_team_before_entering(self):
|
|
auto = self.install(_EnterScreen([self.TOP, self.MIDDLE]))
|
|
self.assertTrue(auto.click_element(aalc_patches.ENTER_ASSET))
|
|
self.assertEqual(auto.entered_with, ['流血'])
|
|
self.assertIn('入场前切队伍: 流血', self.log)
|
|
|
|
def test_already_on_the_team_just_enters(self):
|
|
auto = self.install(_EnterScreen([self.TOP, self.MIDDLE], header='流血'))
|
|
self.assertTrue(auto.click_element(aalc_patches.ENTER_ASSET))
|
|
self.assertEqual(auto.entered_with, ['流血'])
|
|
self.assertEqual(auto.clicks, [])
|
|
self.assertNotIn('入场前切队伍:', self.log)
|
|
|
|
def test_failed_switch_is_an_error_and_is_not_retried_this_run(self):
|
|
auto = self.install(_EnterScreen([self.TOP, ['破裂', '沉沦', '编队#13']]))
|
|
self.assertTrue(auto.click_element(aalc_patches.ENTER_ASSET))
|
|
self.assertEqual(auto.entered_with, ['剧情关卡'])
|
|
self.assertIn('[ERROR] [lc_mcp] 入场前没切到 流血, 本局按游戏当前编队进入', self.log)
|
|
runs = auto.ocr_runs
|
|
self.assertTrue(auto.click_element(aalc_patches.ENTER_ASSET)) # 上游再点一次「进入」
|
|
self.assertEqual(auto.ocr_runs, runs) # 不再从头翻列表
|
|
self.mirror_mod.Mirror({'system': 'bleed'}, 1) # 下一局重新试
|
|
auto.click_element(aalc_patches.ENTER_ASSET)
|
|
self.assertGreater(auto.ocr_runs, runs)
|
|
|
|
def test_lookup_without_click_is_passed_through(self):
|
|
auto = self.install(_EnterScreen([self.TOP, self.MIDDLE]))
|
|
self.assertEqual(auto.click_element(aalc_patches.ENTER_ASSET, click=False), (1100, 650))
|
|
self.assertEqual((auto.ocr_runs, auto.entered_with), (0, []))
|
|
|
|
def test_other_targets_are_untouched(self):
|
|
auto = self.install(_EnterScreen([self.TOP]))
|
|
self.assertFalse(auto.click_element('mirror/road_to_mir/resume_assets.png'))
|
|
self.assertEqual(auto.ocr_runs, 0)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
|
|
class TeamNamesFileTest(unittest.TestCase):
|
|
|
|
def _write(self, data):
|
|
import json
|
|
import tempfile
|
|
fh = tempfile.NamedTemporaryFile('w', suffix='.json', delete=False, encoding='utf-8')
|
|
json.dump(data, fh, ensure_ascii=False)
|
|
fh.close()
|
|
self.addCleanup(lambda: os.unlink(fh.name))
|
|
return fh.name
|
|
|
|
def test_reads_overrides(self):
|
|
path = self._write({'_说明': ['x'], 'tremor': ['镜像迷宫', '震颤']})
|
|
self.assertEqual(aalc_patches.load_team_names(path, log=lambda *_: None),
|
|
{'tremor': ('镜像迷宫', '震颤')})
|
|
|
|
def test_bad_file_falls_back_to_empty(self):
|
|
path = self._write(['not', 'a', 'dict'])
|
|
self.assertEqual(aalc_patches.load_team_names(path, log=lambda *_: None), {})
|
|
|
|
def test_missing_file_falls_back_to_empty(self):
|
|
self.assertEqual(
|
|
aalc_patches.load_team_names(os.path.join(os.path.dirname(__file__), 'no-such.json'),
|
|
log=lambda *_: None), {})
|
|
|
|
def test_override_goes_first_then_defaults(self):
|
|
path = self._write({'tremor': ['镜像迷宫']})
|
|
module = types.ModuleType('tasks.mirror.mirror')
|
|
module.Mirror = _FakeMirror
|
|
module.select_battle_team = lambda num: True
|
|
auto_mod = types.ModuleType('module.automation')
|
|
auto_mod.auto = mock.Mock()
|
|
tasks_pkg = types.ModuleType('tasks'); tasks_pkg.__path__ = []
|
|
mirror_pkg = types.ModuleType('tasks.mirror'); mirror_pkg.__path__ = []
|
|
mirror_pkg.mirror = module
|
|
with mock.patch.dict(sys.modules, {'tasks': tasks_pkg, 'tasks.mirror': mirror_pkg,
|
|
'tasks.mirror.mirror': module,
|
|
'module': types.ModuleType('module'),
|
|
'module.automation': auto_mod}):
|
|
aalc_patches.install_system_team_selection(log=lambda *_: None,
|
|
env={'LCP_TEAM_NAMES': path})
|
|
module.Mirror({'system': 'tremor'}, 1)
|
|
with mock.patch.object(aalc_patches, '_select_team_by_name', return_value=True) as sel:
|
|
module.select_battle_team(1)
|
|
self.assertEqual(sel.call_args[0][1], ('镜像迷宫', '震颤')) |