# -*- coding: utf-8 -*- """编队滑动补丁: 几何用 AALC 真实的 scroll_swipe.py, 适配器/pyautogui 用记录调用的假对象。""" import importlib.util import os import sys import types import unittest from unittest import mock REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, REPO) from lc_mcp import aalc_input_patch as patch # noqa: E402 SCROLL_SWIPE = os.path.join(REPO, 'vendor', 'aalc', 'module', 'automation', 'input_handlers', 'scroll_swipe.py') def aalc_modules(input_module=None): """只装入 AALC 的 scroll_swipe (纯 math) 和可选的假 input 模块, 不触发 AALC 包初始化。""" spec = importlib.util.spec_from_file_location('module.automation.input_handlers.scroll_swipe', SCROLL_SWIPE) scroll_swipe = importlib.util.module_from_spec(spec) spec.loader.exec_module(scroll_swipe) mods = {name: types.ModuleType(name) for name in ('module', 'module.automation', 'module.automation.input_handlers')} for m in mods.values(): m.__path__ = [] handlers = mods['module.automation.input_handlers'] handlers.scroll_swipe = scroll_swipe mods['module.automation.input_handlers.scroll_swipe'] = scroll_swipe if input_module is not None: handlers.input = input_module mods['module.automation.input_handlers.input'] = input_module mods['module.automation'].input_handlers = handlers mods['module'].automation = mods['module.automation'] return mods, scroll_swipe class FakeBackground: def __init__(self): self.calls = [] def get_mouse_position(self): return (5, 5) def set_active(self): self.calls.append(('active',)) def set_mouse_pos(self, x, y, **kwargs): self.calls.append(('pos', x, y, kwargs)) def mouse_down(self, x, y): self.calls.append(('down', x, y)) def mouse_up(self, x, y): self.calls.append(('up', x, y)) def mouse_move(self, coordinate): self.calls.append(('restore', coordinate)) @unittest.skipUnless(os.path.isfile(SCROLL_SWIPE), 'AALC submodule unavailable') class TeamScrollPatchTest(unittest.TestCase): def setUp(self): self.mods, self.scroll_swipe = aalc_modules() self.sleeps = [] for p in (mock.patch.dict(sys.modules, self.mods), mock.patch.object(patch.time, 'sleep', side_effect=self.sleeps.append)): p.start() self.addCleanup(p.stop) def test_background_keeps_aalc_geometry_and_actually_moves(self): dy = -385 * 1080 / 1440 # AALC 按名称翻页的距离 adapter = FakeBackground() patch._background_team_scroll(adapter, 500, 600, duration=0.3, dy=dy) moves = [c for c in adapter.calls if c[0] == 'pos'] down = next(c for c in adapter.calls if c[0] == 'down') up = next(c for c in adapter.calls if c[0] == 'up') self.assertEqual(down[1:], (500, 600)) self.assertAlmostEqual(up[2] - down[2], dy) # 行程与 AALC 的 Windows 标定一致 self.assertGreater(len(moves), 50) # 原补丁在此为 0 个移动事件 self.assertTrue(all(not c[3] for c in moves)) # 不经过适配器的 duration 参数 plan, settle = self.scroll_swipe.build_windows_scroll_swipe_plan(500, 600, 0, dy, 0.3) visited = {(round(c[1], 6), round(c[2], 6)) for c in moves} for point, _ in plan: self.assertIn((round(point[0], 6), round(point[1], 6)), visited) self.assertIn(patch.ESCAPE_HOLD_SECONDS, self.sleeps) self.assertEqual(self.sleeps[-1], patch.SETTLE_SECONDS) moving = sum(d for _, d in plan) self.assertAlmostEqual(sum(self.sleeps), moving + patch.ESCAPE_HOLD_SECONDS + patch.SETTLE_SECONDS) self.assertEqual(adapter.calls[-1], ('restore', (5, 5))) def test_foreground_disables_pyautogui_pause(self): calls = [] fake = types.SimpleNamespace( moveTo=lambda *a, **k: calls.append(('move', a, k)), mouseDown=lambda *a, **k: calls.append(('down', a, k)), mouseUp=lambda *a, **k: calls.append(('up', a, k))) adapter = FakeBackground() adapter.pos_offset = lambda x, y: (x + 100, y + 200) with mock.patch.dict(sys.modules, {'pyautogui': fake}): patch._foreground_team_scroll(adapter, 500, 600, duration=0.3, dy=-288, move_back=False) self.assertTrue(all(k.get('_pause') is False for _, _, k in calls)) self.assertEqual([c[0] for c in calls].count('down'), 1) self.assertEqual(calls[-1][:2], ('up', (600, 512))) # 终点 (500, 312) 加窗口偏移 # pyautogui 默认每次调用后停 0.1s; 关掉后总耗时只由计划决定 self.assertLess(sum(self.sleeps), 2.0) def test_zero_distance_presses_and_releases_in_place(self): adapter = FakeBackground() patch._background_team_scroll(adapter, 10, 20, dy=0, move_back=False) self.assertEqual([c[0] for c in adapter.calls], ['pos', 'active', 'down', 'up']) self.assertEqual(self.sleeps, []) def test_release_happens_even_if_moving_fails(self): adapter = FakeBackground() adapter.set_mouse_pos = mock.Mock(side_effect=[None, RuntimeError('lost window')]) with self.assertRaises(RuntimeError): patch._background_team_scroll(adapter, 10, 20, dy=-200) self.assertEqual(adapter.calls[-1][0], 'up') class InstallTest(unittest.TestCase): def test_opt_in_and_idempotent(self): class Input: pass class BackgroundInput: pass fake_input = types.ModuleType('module.automation.input_handlers.input') fake_input.Input, fake_input.BackgroundInput = Input, BackgroundInput mods = {name: types.ModuleType(name) for name in ('module', 'module.automation', 'module.automation.input_handlers')} for m in mods.values(): m.__path__ = [] mods['module.automation.input_handlers'].input = fake_input mods['module.automation.input_handlers.input'] = fake_input with mock.patch.dict(sys.modules, mods): with mock.patch.dict(os.environ, {}, clear=True): self.assertEqual(patch.install(), []) # 默认关闭 self.assertFalse(hasattr(Input, 'mouse_swipe_for_team_scroll')) with mock.patch.dict(os.environ, {'LCP_TEAM_SCROLL_PATCH': '1'}): self.assertEqual(patch.install(), ['Input', 'BackgroundInput']) self.assertIs(Input.mouse_swipe_for_team_scroll, patch._foreground_team_scroll) self.assertIs(BackgroundInput.mouse_swipe_for_team_scroll, patch._background_team_scroll) self.assertEqual(patch.install(), []) # 幂等 if __name__ == '__main__': unittest.main()