Fix background Unity input and shop boundaries; record login failure

Browse Source
This commit is contained in:
Codex Agent
2026-10-03 16:52:49 +08:00
parent 6cd0b2742a
commit 073e407346
13 changed files with 850 additions and 29 deletions
+74
View File
@@ -0,0 +1,74 @@
import sys
import unittest
from types import SimpleNamespace
from unittest import mock
from lc_mcp.aalc_engine import AalcEngine
from lc_mcp.fake_engine import FakeEngine
from lc_mcp.protocol import Server
class BackgroundRestoreTest(unittest.TestCase):
def test_mouse_message_targets_game_without_system_pointer(self):
api = mock.Mock()
api.MAKELONG.return_value = 77
constants = SimpleNamespace(MK_LBUTTON=1)
engine = AalcEngine.__new__(AalcEngine)
engine.screen = SimpleNamespace(handle=SimpleNamespace(hwnd=456))
with mock.patch.dict(sys.modules, win32api=api, win32con=constants):
engine._background_mouse(512, 100, 200, held=True)
api.PostMessage.assert_called_once_with(456, 512, 1, 77)
api.SetCursorPos.assert_not_called()
def test_drag_releases_on_error_without_legacy_cursor_handler(self):
engine = AalcEngine.__new__(AalcEngine)
engine.cfg = SimpleNamespace(simulator=False, win_input_type='background')
engine.auto = mock.Mock()
engine._grab = mock.Mock(return_value=SimpleNamespace(width=800, height=600))
engine._background_mouse = mock.Mock()
constants = SimpleNamespace(WM_MOUSEMOVE=512, WM_LBUTTONDOWN=513, WM_LBUTTONUP=514)
with mock.patch.dict(sys.modules, win32con=constants), \
mock.patch('lc_mcp.aalc_engine.time.sleep', side_effect=[None, RuntimeError('stopped')]):
with self.assertRaises(RuntimeError):
engine.drag(100, 100, 100, 500, 1.5)
self.assertEqual(engine._background_mouse.call_args, mock.call(514, 100, 100))
engine.auto.input_handler.set_mouse_pos.assert_not_called()
engine.auto.mouse_drag.assert_not_called()
def test_window_move_click_restores_after_release_without_system_pointer(self):
engine = AalcEngine.__new__(AalcEngine)
engine.cfg = SimpleNamespace(simulator=False, win_input_type='window_move')
engine.auto = mock.Mock()
engine.screen = mock.Mock()
engine.auto.input_handler._set_window_pos.return_value = (20, 30)
engine._background_mouse = mock.Mock()
engine._restore_background_position = mock.Mock()
constants = SimpleNamespace(WM_MOUSEMOVE=512, WM_LBUTTONDOWN=513, WM_LBUTTONUP=514)
with mock.patch.dict(sys.modules, win32con=constants), \
mock.patch('lc_mcp.aalc_engine.time.sleep'):
engine.click(100, 200)
engine._restore_background_position.assert_called_once_with((20, 30))
engine.auto.input_handler.set_mouse_pos.assert_not_called()
engine.screen.handle.set_window_transparent.assert_called_once_with(True)
def test_restore_uses_noactivate_and_preserves_foreground(self):
gui = mock.Mock()
gui.GetForegroundWindow.return_value = 123
gui.IsWindowVisible.return_value = True
engine = AalcEngine.__new__(AalcEngine)
engine.cfg = SimpleNamespace(simulator=False)
engine.screen = SimpleNamespace(handle=SimpleNamespace(hwnd=456))
with mock.patch.dict(sys.modules, {'win32gui': gui,
'win32con': SimpleNamespace(SW_SHOWNOACTIVATE=4)}):
result = engine.show_game_background()
gui.ShowWindow.assert_called_once_with(456, 4)
gui.SetForegroundWindow.assert_not_called()
self.assertFalse(result['foreground_changed'])
def test_running_task_blocks_restore(self):
tasks = mock.Mock()
tasks.running.return_value = True
tasks.info = {'task': 'mirror'}
engine = FakeEngine()
server = Server(engine, tasks)
self.assertFalse(server.call_tool('show_game_background', {})['ok'])
self.assertEqual(engine.actions, [])
+16 -8
View File
@@ -414,7 +414,7 @@ class RuntimeTest(unittest.TestCase):
[mock.call(1, 2, True), mock.call(1, 3, True),
mock.call(1, 3, False), mock.call(1, 2, False)])
def test_background_click_holds_and_releases_without_moving_window(self):
def test_background_click_holds_and_releases_without_moving_pointer_or_window(self):
from lc_mcp.aalc_engine import AalcEngine
engine = AalcEngine.__new__(AalcEngine)
engine.cfg = mock.Mock(simulator=False, win_input_type='background')
@@ -422,19 +422,26 @@ class RuntimeTest(unittest.TestCase):
handler = mock.Mock(spec=['set_mouse_pos','set_active','mouse_down','mouse_up'])
engine.auto.input_handler = handler
sequence = mock.Mock()
sequence.attach_mock(handler.mouse_down, 'down')
sequence.attach_mock(handler.mouse_up, 'up')
with mock.patch('lc_mcp.aalc_engine.time.sleep') as sleep:
engine._background_mouse = mock.Mock()
sequence.attach_mock(engine._background_mouse, 'message')
constants = mock.Mock(WM_MOUSEMOVE=512, WM_LBUTTONDOWN=513, WM_LBUTTONUP=514)
with mock.patch.dict(sys.modules, win32con=constants), \
mock.patch('lc_mcp.aalc_engine.time.sleep') as sleep:
sequence.attach_mock(sleep, 'sleep')
engine.click(100, 200)
self.assertEqual(sequence.mock_calls, [mock.call.down(100, 200),
mock.call.sleep(.12), mock.call.up(100, 200),
self.assertEqual(sequence.mock_calls, [mock.call.message(512, 100, 200),
mock.call.sleep(.12),
mock.call.message(513, 100, 200, held=True),
mock.call.sleep(.12), mock.call.message(514, 100, 200),
mock.call.sleep(.12)])
engine.auto.mouse_click.assert_not_called()
with mock.patch('lc_mcp.aalc_engine.time.sleep', side_effect=RuntimeError('interrupted')):
handler.set_mouse_pos.assert_not_called()
handler.mouse_down.assert_not_called()
with mock.patch.dict(sys.modules, win32con=constants), \
mock.patch('lc_mcp.aalc_engine.time.sleep', side_effect=[None, RuntimeError('interrupted')]):
with self.assertRaises(RuntimeError):
engine.click(100, 200)
self.assertEqual(handler.mouse_up.call_count, 2)
self.assertEqual(engine._background_mouse.call_args, mock.call(514, 100, 200))
engine.screen.handle.set_window_pos.assert_not_called()
def test_background_keypress_holds_and_releases_on_error(self):
@@ -501,6 +508,7 @@ server.main(['--stdio'])
with self.assertRaises(ValueError):
engine.drag(100, 100, 800, 500)
engine.auto.mouse_drag.assert_not_called()
engine.cfg.simulator = True
engine.drag(100, 100, 100, 500, 1.5)
engine.auto.mouse_drag.assert_called_once_with(100, 100, drag_time=1.5, dx=0, dy=400)
+53
View File
@@ -1,5 +1,6 @@
import sys
import unittest
import tempfile
from pathlib import Path
from unittest import mock
@@ -10,6 +11,58 @@ from decision_router import DecisionRouter
class EnglishMirrorBoundaryTest(unittest.TestCase):
def test_server_error_only_offers_retry_above_background_reward_cards(self):
state = {'items': [
{'text': text, 'x': 1000, 'y': 500, 'w': 120, 'h': 35, 'score': 1}
for text in ['Select Encounter Reward Card', 'A servererror hasoccurred.',
'Retry 4', 'Close', 'Gain55~83Cost', 'Confirm']]}
with mock.patch.object(agent, 'button_enabled', return_value=True):
options = agent.candidates(normalize(state))
self.assertEqual(set(options), {'wait', 'requires_analysis', 'retry_network'})
def test_purchased_badge_blocks_reopening_even_when_ocr_omits_it(self):
from PIL import Image
item = {'text': '替换奥提斯的技能', 'x': 838, 'y': 496, 'w': 196, 'h': 30}
with tempfile.TemporaryDirectory() as folder:
canvas = Image.new('L', (1920, 1080))
with Image.open(agent.REPO_ROOT / 'assets/ui/purchased_english.png') as badge:
canvas.paste(badge.convert('L'), (874, 330))
bought = str(Path(folder) / 'bought.png')
blank = str(Path(folder) / 'blank.png')
canvas.save(bought)
Image.new('L', (1920, 1080)).save(blank)
self.assertTrue(agent.purchased_slot({'screen': {'path': bought, 'w': 1920, 'h': 1080}}, item))
self.assertFalse(agent.purchased_slot({'screen': {'path': blank, 'w': 1920, 'h': 1080}}, item))
def test_english_skill_payment_confirm_requires_validated_skill_pair(self):
state = {'floor': 1, 'screen': {'w': 1920, 'h': 1080}, 'items': [
{'text': text, 'x': x, 'y': y, 'w': 100, 'h': 35, 'score': 1}
for text, x, y in [("Replace Outis's Skill", 382, 179),
('SKILL2', 781, 318), ('SKILL3', 1532, 319),
('Spend 75 Cost to replace this Skill?', 728, 734),
('Confirm', 1161, 796), ('Cancel', 732, 793)]]}
with mock.patch.object(agent, 'button_enabled', return_value=True):
self.assertIn('buy_swap', agent.candidates(normalize(state)))
state['items'][1]['text'] = 'SKILL1'
self.assertNotIn('buy_swap', agent.candidates(state))
def test_split_enhance_menu_and_truncated_skill_shop_label_are_recognized(self):
state = {'items': [
{'text': text, 'x': x, 'y': y, 'w': 100, 'h': 30, 'score': 1}
for text, x, y in [('Enhance', 222, 564), ('Gifts', 238, 587),
("Replace Outis's Skil", 838, 496)]]}
labels = [item['text'] for item in normalize(state)['items']]
self.assertIn('强化饰品', labels)
self.assertTrue(any('技能替换' in label and '奥提斯' in label for label in labels))
def test_stock_refresh_does_not_use_keyword_refresh(self):
state = {'floor': 1, 'symbol_upgraded': True, 'items': [
{'text': text, 'x': x, 'y': 167, 'w': 89, 'h': 31, 'score': 1}
for text, x in [('商店', 337), ('刷新', 1478), ('刷新', 1694), ('离开', 1626)]]}
with mock.patch.object(agent, 'button_enabled', return_value=True):
options = agent.candidates(state)
self.assertEqual(options['shop_refresh']['action']['args']['x'], 1522)
def test_map_header_without_recognized_floor_number_stops_battle(self):
for label in ('Exploring Floor', 'Exploring FloorT'):
with self.subTest(label=label):