Files
lc-mcp/tests/test_gift_confirm_guard.py
Codex Agent bda1ffb390 驱动点击兜底 + 队名可配置;补 11-15 实机记录
- 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。
2026-11-15 14:15:02 +08:00

262 lines
11 KiB
Python

# -*- coding: utf-8 -*-
"""「获得E.G.O饰品」确认弹窗兜底(ISSUES I-06 / R-04)。
2026-11-14 18:45: AALC 一直点确认模板的匹配点(勾号), 弹窗不关, 连点 5 分钟; 手动点按钮中心一次就过。
上游 select_init_ego_gift 点到确认就 continue, 循环底部的 loop_count 退出判断永远走不到。
端到端用例执行上游真实的 select_init_ego_gift。
"""
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
MIRROR = REPO / 'vendor' / 'aalc' / 'tasks' / 'mirror' / 'mirror.py'
CONFIRM = ap.GIFT_CONFIRM
TICK = (602.0, 531.75) # 1280x720 下确认模板(勾号)的中心
BUTTON = (640, 532) # 按钮中心: 勾号 + 76 * 720/1440
class Popup:
"""只有确认弹窗的假 auto: 点按钮中心才关(closes_on_button), 点勾号永远不关。"""
def __init__(self, closes_on_button=True):
self.open = True
self.closes_on_button = closes_on_button
self.template_clicks = 0
self.button_clicks = []
self.other_ok = False
def click_element(self, target, *args, click=True, **kwargs):
if target == CONFIRM and self.open:
if not click:
return TICK
self.template_clicks += 1
return True
if target == 'other' and self.other_ok:
return True if click else (1, 1)
return False
def mouse_click(self, x, y, *args, **kwargs):
self.button_clicks.append((x, y))
if self.closes_on_button and abs(x - BUTTON[0]) <= 15 and abs(y - BUTTON[1]) <= 15:
self.open = False
return True
class GuardTest(unittest.TestCase):
def install(self, auto, clock=None):
self.lines = []
self.now = [0.0]
modules = {'module': types.ModuleType('module'),
'module.automation': SimpleNamespace(auto=auto),
'module.config': SimpleNamespace(cfg=SimpleNamespace(set_win_size=720))}
with mock.patch.dict(sys.modules, modules):
self.assertIsNotNone(ap.install_gift_confirm_guard(
log=self.lines.append, clock=clock or (lambda: self.now[0])))
return auto
def hit(self, auto, times=1, step=1.5):
results = []
for _ in range(times):
self.now[0] += step
results.append(auto.click_element(CONFIRM))
return results
def test_first_two_hits_are_upstream_clicks(self):
auto = self.install(Popup())
self.assertEqual(self.hit(auto, 2), [True, True])
self.assertEqual(auto.template_clicks, 2)
self.assertEqual(auto.button_clicks, [])
def test_third_hit_clicks_the_button_center_and_closes_it(self):
auto = self.install(Popup())
with mock.patch.object(ap.decisions, 'record') as record:
self.assertEqual(self.hit(auto, 3), [True, True, True])
self.assertEqual(auto.button_clicks, [BUTTON])
self.assertFalse(auto.open)
self.assertIn('确认点了 2 次还在, 改点按钮中心 (640,532)', '\n'.join(self.lines))
record.assert_called_once_with('gift_confirm', 'button_center', 'repeated', clicks=2)
self.assertEqual(auto.click_element(CONFIRM), False) # 关了就找不到
def test_repeat_clicks_bypass_the_driver_proxy(self):
# 2026-11-15: 罗技驱动的物理点击点不掉这个弹窗, 同一坐标用 pyautogui 软件点击可以。
# 第 3 次起必须绕过驱动代理, 直接调底层 handler。
auto = self.install(Popup(closes_on_button=False))
inner = []
auto.input_handler = SimpleNamespace(
_handler=SimpleNamespace(mouse_click=lambda x, y, *a, **k: inner.append((x, y))))
self.hit(auto, 3)
self.assertEqual(len(inner), 1)
self.assertEqual(inner[0], BUTTON)
self.assertEqual(auto.button_clicks, []) # 没再走驱动代理的 mouse_click
def test_enter_fallback_when_software_click_also_fails(self):
auto = self.install(Popup(closes_on_button=False))
inner = []
auto.input_handler = SimpleNamespace(
_handler=SimpleNamespace(mouse_click=lambda x, y, *a, **k: inner.append((x, y))))
auto.key_press = mock.Mock()
self.hit(auto, ap.GIFT_CONFIRM_ENTER_AT)
auto.key_press.assert_called_once_with('enter')
def test_stuck_popup_ends_the_task(self):
auto = self.install(Popup(closes_on_button=False))
with mock.patch.object(ap.decisions, 'record'), self.assertRaises(ap.TaskAbort):
self.hit(auto, ap.GIFT_CONFIRM_LIMIT)
self.assertEqual(len(auto.button_clicks), ap.GIFT_CONFIRM_LIMIT - ap.GIFT_CONFIRM_SHIFT_AT)
self.assertIn('[ERROR] [lc_mcp] 「获得E.G.O饰品」确认连点 20 次弹窗还在, 结束任务', '\n'.join(self.lines))
# 不是 Exception: AALC 的 onetime_mir_process 吞不掉它
self.assertFalse(issubclass(ap.TaskAbort, Exception))
def test_other_successful_click_resets_the_streak(self):
auto = self.install(Popup(closes_on_button=False))
self.hit(auto, 2)
auto.other_ok = True
self.assertTrue(auto.click_element('other'))
self.hit(auto, 2)
self.assertEqual(auto.button_clicks, [])
self.assertEqual(auto.template_clicks, 4)
def test_other_lookup_or_failed_click_does_not_reset(self):
auto = self.install(Popup())
self.hit(auto, 2)
auto.click_element('missing')
auto.other_ok = True
auto.click_element('other', click=False) # 只查不点
self.hit(auto, 1)
self.assertEqual(auto.button_clicks, [BUTTON])
def test_missing_popup_resets_the_streak(self):
auto = self.install(Popup())
self.hit(auto, 2)
auto.open = False
self.assertEqual(self.hit(auto, 1), [False])
auto.open = True
self.hit(auto, 2)
self.assertEqual(auto.button_clicks, [])
def test_hits_far_apart_are_not_a_streak(self):
auto = self.install(Popup())
self.hit(auto, 2)
self.hit(auto, 1, step=ap.GIFT_CONFIRM_GAP + 1)
self.assertEqual(auto.button_clicks, [])
self.assertEqual(auto.template_clicks, 3)
def test_confirm_lookup_without_click_is_untouched(self):
auto = self.install(Popup())
for _ in range(5):
self.assertEqual(auto.click_element(CONFIRM, click=False), TICK)
self.assertEqual(auto.template_clicks, 0)
self.assertEqual(self.hit(auto, 2), [True, True])
self.assertEqual(auto.button_clicks, [])
class Spinning(Exception):
pass
class InitGiftScreen:
"""开局选饰品后的「获得E.G.O饰品」弹窗。点勾号不关, 点按钮中心才关, 关了就是卡包页。"""
def __init__(self, closes_on_button=True, max_shots=200):
self.state = 'popup'
self.closes_on_button = closes_on_button
self.max_shots = max_shots
self.shots = 0
self.model = 'clam'
self.template_clicks = 0
def take_screenshot(self, *args, **kwargs):
self.shots += 1
if self.shots > self.max_shots:
raise Spinning(self.shots)
return object()
def find_element(self, target, *args, **kwargs):
if target == 'mirror/theme_pack/feature_theme_pack_assets.png':
return (640, 100) if self.state == 'theme' else None
if target == CONFIRM and self.state == 'popup':
return TICK
return None
def click_element(self, target, *args, click=True, **kwargs):
pos = self.find_element(target)
if not pos:
return False
if not click:
return pos
self.template_clicks += 1 # 上游点的是匹配点(勾号) ±10 像素, 点不到按钮
return True
def mouse_click(self, x, y, *args, **kwargs):
if self.closes_on_button and abs(x - BUTTON[0]) <= 15 and abs(y - BUTTON[1]) <= 15:
self.state = 'theme'
return True
def mouse_to_blank(self, *args, **kwargs):
pass
def mouse_drag(self, *args, **kwargs):
pass
@unittest.skipUnless(MIRROR.is_file(), 'vendor/aalc 子模块未检出')
class UpstreamInitGiftTest(unittest.TestCase):
"""执行上游真实的 Mirror.select_init_ego_gift。"""
def setUp(self):
tree = ast.parse(MIRROR.read_text(encoding='utf-8'))
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'Mirror')
self.fn_node = next(n for n in cls.body
if isinstance(n, ast.FunctionDef) and n.name == 'select_init_ego_gift')
self.lines = []
def play(self, screen, patched=True):
cfg = SimpleNamespace(set_win_size=720)
ns = dict(auto=screen, cfg=cfg, log=mock.Mock(), sleep=lambda *a: None, retry=lambda: True,
back_init_menu=mock.Mock(), all_systems={}, start_gift={})
exec(compile(ast.Module(body=[self.fn_node], type_ignores=[]), str(MIRROR), 'exec'), ns)
mirror = SimpleNamespace(system='bleed', opening_items=False, observe_ego_gift=False,
observe_ego_gift_selected=[], observe_bbox=None,
_click_init_gift_row=lambda gift_id: False)
if patched:
modules = {'module': types.ModuleType('module'),
'module.automation': SimpleNamespace(auto=screen),
'module.config': SimpleNamespace(cfg=cfg)}
with mock.patch.dict(sys.modules, modules):
ap.install_gift_confirm_guard(log=self.lines.append)
with mock.patch.object(ap.decisions, 'record'):
return ns['select_init_ego_gift'](mirror)
def test_upstream_spins_on_a_popup_its_click_cannot_close(self):
screen = InitGiftScreen()
with self.assertRaises(Spinning):
self.play(screen, patched=False)
self.assertEqual(screen.state, 'popup')
self.assertGreater(screen.template_clicks, 100) # 计数早就负了, 照点不误
def test_guard_clicks_the_button_and_the_loop_moves_on(self):
screen = InitGiftScreen()
self.play(screen)
self.assertEqual(screen.state, 'theme')
self.assertEqual(screen.template_clicks, ap.GIFT_CONFIRM_SHIFT_AT - 1)
self.assertLess(screen.shots, 10)
def test_guard_ends_the_task_when_nothing_closes_it(self):
screen = InitGiftScreen(closes_on_button=False)
with self.assertRaises(ap.TaskAbort):
self.play(screen)
self.assertLessEqual(screen.shots, ap.GIFT_CONFIRM_LIMIT + 1)
self.assertIn('[ERROR]', '\n'.join(self.lines))
if __name__ == '__main__':
unittest.main()