Fix background MCP input and add mirror dungeon feedback records

Browse Source
This commit is contained in:
Codex Agent
2026-10-02 13:58:43 +08:00
parent a4b27f991c
commit da7dcefadd
45 changed files with 6180 additions and 9 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

+167
View File
@@ -0,0 +1,167 @@
import tempfile
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import jev_mirror_agent as agent
class SceneRegressionTest(unittest.TestCase):
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_event_dialog_skip_precedes_judgement_and_options(self, _enabled):
state = {'items': [{'text': text, 'x': 1200, 'y': 400, 'w': 120, 'h': 40, 'score': 1.0}
for text in ('SKIP', '进行判定', '选项', '侧耳倾听')]}
options = agent.candidates(state, previous_battle=True)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'skip_dialog'})
self.assertEqual(options['skip_dialog']['action'],
{'tool': 'click', 'args': {'x': 1260, 'y': 420}})
def test_floor_three_sinclair_cannot_choose_first_floor_swap(self):
state = {'floor': 3, 'items': [
{'text': '当前持有技能', 'x': 900, 'y': 160, 'w': 100, 'h': 30},
{'text': '替换辛克莱的技能', 'x': 400, 'y': 180, 'w': 300, 'h': 40}]}
for x, first, second in ((543, 'SKILL1', 'SKILL2'), (1033, 'SKILL2', 'SKILL3'),
(1520, 'SKILL1', 'SKILL3')):
state['items'].extend({'text': label, 'x': x, 'y': y, 'w': 106, 'h': 42}
for label, y in ((first, 360), (second, 579)))
options = agent.candidates(state)
self.assertIn('skill_swap_3', options)
self.assertNotIn('skill_swap_1', options)
self.assertNotIn('skill_swap_2', options)
state['floor'] = 2
options = agent.candidates(state)
self.assertIn('skill_swap_1', options)
self.assertNotIn('skill_swap_3', options)
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_sold_swap_is_not_offered_again(self, _enabled):
state = {'items': [
{'text': '商店', 'x': 343, 'y': 162, 'w': 86, 'h': 50},
{'text': '刷新', 'x': 1496, 'y': 167, 'w': 54, 'h': 33},
{'text': '替换辛克莱的技能', 'x': 848, 'y': 499, 'w': 179, 'h': 28},
{'text': '已购买', 'x': 897, 'y': 326, 'w': 82, 'h': 34}]}
options = agent.candidates(state)
self.assertNotIn('swap_2', options)
self.assertIn('shop_refresh', options)
def test_real_white_event_routes_work_without_map_heading_ocr(self):
screenshot = Path(__file__).resolve().parent / 'fixtures' / 'floor4_white_events.png'
assets = agent.REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror'
if not assets.is_dir():
self.skipTest('AALC submodule assets unavailable')
options = agent.map_candidates({'screen': {'path': str(screenshot)}})
self.assertIn('route_w', options)
self.assertIn('route_d', options)
self.assertNotIn('route_s', options)
self.assertIn('事件', options['route_w']['description'])
options = agent.candidates({'screen':{'path':str(screenshot)},'items':[]})
self.assertIn('route_d', options)
@mock.patch.object(agent.time, 'sleep')
def test_battle_macro_continues_through_empty_ocr_and_stops_at_reward(self, _sleep):
bridge = mock.Mock()
states = iter([{'ok': True, 'items': []},
{'ok': True, 'items': [{'text': '选择遭遇战奖励卡'}]}])
bridge.call.side_effect = lambda tool, args=None: next(states) if tool == 'get_state' else {'ok': True}
with mock.patch.object(agent, 'STOP_FILE') as stop:
stop.exists.return_value = False
result = agent.execute(bridge, {'tool': 'battle', 'args': {}})
self.assertEqual(result['pairs'], 4)
self.assertTrue(result['terminal'])
keys = [call.args[1]['key'] for call in bridge.call.call_args_list if call.args[0] == 'keypress']
self.assertEqual(keys, ['p', 'enter']*4)
@mock.patch.object(agent.time, 'sleep')
def test_route_enters_without_a_second_snapshot(self, _sleep):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True}
agent.execute(bridge, {'tool': 'route', 'args': {'key': 'w'}})
self.assertEqual(bridge.call.call_args_list,
[mock.call('keypress', {'key': 'w'}),
mock.call('keypress', {'key': 'enter'}),
mock.call('keypress', {'key': 'enter'})])
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_battle_entry_uses_enter_without_party_changes(self, _enabled):
state = {'items': [{'text': '开始战斗', 'x': 100, 'y': 100, 'w': 100, 'h': 40}]}
self.assertEqual(agent.candidates(state)['button_0']['action'],
{'tool': 'keypress', 'args': {'key': 'enter'}})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_shop_refresh_is_capped_and_only_for_missing_party_swap(self, _enabled):
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
for text in ('商店', '刷新', '替换以实玛利的技能')]}
self.assertIn('shop_refresh', agent.candidates(state, refresh_used=3))
self.assertNotIn('shop_refresh', agent.candidates(state, refresh_used=4))
state['items'][-1]['text'] = '替换辛克莱的技能'
self.assertNotIn('shop_refresh', agent.candidates(state))
self.assertIn('swap_2', agent.candidates(state))
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_confirmation_blocks_background_leave(self, _enabled):
items = [{'text': text, 'x': x, 'y': y, 'w': 80, 'h': 40}
for text, x, y in [('要离开商店吗?', 700, 400),
('取消', 700, 600), ('确认', 1100, 600),
('离开', 1650, 950)]]
options = agent.candidates({'items': items}, previous_battle=True)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
self.assertEqual(options['modal_confirm']['action'],
{'tool': 'click', 'args': {'x': 1140, 'y': 620}})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_nested_reward_confirmation_excludes_background_cards(self, _enabled):
state = {'items': [{'text': t, 'x': x, 'y': y, 'w': 80, 'h': 40}
for t,x,y in [('选择遭遇战奖励卡',600,178), ('可选择0/1',1383,178),
('要在不选择奖励的情况下继续吗?',742,489),
('×取消',747,678),
('确认',1102,679), ('确认',1197,776)]]}
options = agent.candidates(state)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
self.assertEqual(options['modal_confirm']['action']['args']['y'], 699)
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_event_result_uses_visible_continue_button(self, _enabled):
state = {'items':[{'text':t,'x':x,'y':y,'w':80,'h':40}
for t,x,y in [('结果',1088,162),('继续',1639,936),
('是在感谢我们吗?',112,707)]]}
options = agent.candidates(state)
self.assertEqual(options['event_finish']['action'],
{'tool':'click','args':{'x':1679,'y':956}})
def test_loading_offers_wait_and_never_auto_escalates(self):
state = {'items': [{'text': 'CONNECTING', 'x': 100, 'y': 10, 'w': 100, 'h': 30}]}
options = agent.candidates(state)
self.assertEqual(set(options), {'wait', 'requires_analysis'})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_shop_loading_blocks_stale_stock_refresh_and_leave(self, _enabled):
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
for text in ('商店', '刷新', '替换辛克莱的技能', '离开', 'CONNECTING')]}
self.assertEqual(set(agent.candidates(state)), {'wait', 'requires_analysis'})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_shop_cannot_leave_with_party_swap_or_remaining_refresh(self, _enabled):
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
for text in ('商店', '刷新', '替换奥提斯的技能', '离开')]}
self.assertNotIn('button_3', agent.candidates(state, refresh_used=4))
state['items'][2]['text'] = '替换李箱的技能'
self.assertNotIn('button_3', agent.candidates(state, refresh_used=3))
self.assertIn('button_3', agent.candidates(state, refresh_used=4))
def test_disabled_selection_is_not_clickable_candidate(self):
state = {'items': [{'text': '选择', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
self.assertFalse(any(o['action']['tool'] == 'click_text' for o in agent.candidates(state).values()))
def test_battle_cinematic_keeps_macro_available(self):
state = {'items': [{'text': '破裂+5', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
self.assertIn('battle', agent.candidates(state, previous_battle=True))
def test_reward_stops_battle_candidates(self):
state = {'items': [{'text': text, 'x': 100, 'y': 10, 'w': 80, 'h': 40}
for text in ('获得E.G.O饰品', '造成更多伤害')]}
self.assertNotIn('battle', agent.candidates(state, previous_battle=True))
if __name__ == '__main__':
unittest.main()
+119 -1
View File
@@ -84,6 +84,16 @@ class ProtocolTest(unittest.TestCase):
self.assertIn('bad arguments', r['error']) self.assertIn('bad arguments', r['error'])
self.assertFalse(call(self.s, 'click')['ok']) self.assertFalse(call(self.s, 'click')['ok'])
def test_drag_duration_and_rejected_arguments(self):
r = call(self.s, 'drag', from_x=200, from_y=300, to_x=200, to_y=600, duration=1.5)
self.assertTrue(r['ok'])
self.assertEqual(self.engine.actions[-1], ('drag', 200, 300, 200, 600, 1.5))
action_count = len(self.engine.actions)
for duration in (-1, 0, 11, float('nan'), float('inf')):
self.assertFalse(call(self.s, 'drag', from_x=1, from_y=2,
to_x=3, to_y=4, duration=duration)['ok'])
self.assertEqual(len(self.engine.actions), action_count)
def test_images(self): def test_images(self):
self.assertEqual(call(self.s, 'find_image', name='battle/turn_assets.png')['x'], 50) self.assertEqual(call(self.s, 'find_image', name='battle/turn_assets.png')['x'], 50)
r = call(self.s, 'find_image', name='home/x.png') r = call(self.s, 'find_image', name='home/x.png')
@@ -110,6 +120,8 @@ class ProtocolTest(unittest.TestCase):
self.assertTrue(r['ok'], r) self.assertTrue(r['ok'], r)
self.assertFalse(call(self.s, 'run_task', task='daily')['ok']) self.assertFalse(call(self.s, 'run_task', task='daily')['ok'])
self.assertIn('stop_task', call(self.s, 'click', x=1, y=1)['error']) self.assertIn('stop_task', call(self.s, 'click', x=1, y=1)['error'])
self.assertIn('stop_task', call(self.s, 'drag', from_x=1, from_y=2,
to_x=3, to_y=4)['error'])
self.assertTrue(call(self.s, 'get_state')['ok']) # 只读工具仍可用 self.assertTrue(call(self.s, 'get_state')['ok']) # 只读工具仍可用
time.sleep(0.5) time.sleep(0.5)
st = call(self.s, 'task_status') st = call(self.s, 'task_status')
@@ -142,6 +154,99 @@ class AalcTaskArgsTest(unittest.TestCase):
self.assertEqual(aalc_task.main(['x', 'nope']), 2) self.assertEqual(aalc_task.main(['x', 'nope']), 2)
class RuntimeTest(unittest.TestCase):
def test_background_keypress_holds_and_releases_on_error(self):
from lc_mcp.aalc_engine import AalcEngine
engine = AalcEngine.__new__(AalcEngine)
engine.key_names = ['enter']
engine.cfg = mock.Mock(simulator=False, win_input_type='background')
engine.auto = mock.Mock()
with mock.patch('lc_mcp.aalc_engine.time.sleep') as sleep:
engine.keypress('enter')
sleep.assert_called_once_with(0.12)
engine.auto.input_handler.key_down.assert_called_once_with('enter')
engine.auto.input_handler.key_up.assert_called_once_with('enter')
engine.auto.key_press.assert_not_called()
with mock.patch('lc_mcp.aalc_engine.time.sleep', side_effect=RuntimeError('interrupted')):
with self.assertRaises(RuntimeError):
engine.keypress('enter')
self.assertEqual(engine.auto.input_handler.key_up.call_count, 2)
def test_pythonw_streams_are_redirected_to_log(self):
with tempfile.TemporaryDirectory() as directory:
with mock.patch.object(srv_mod, 'HOME', directory), \
mock.patch('sys.stdout', None), mock.patch('sys.stderr', None):
stream = srv_mod._configure_streams()
try:
print('启动')
srv_mod.log('ready')
finally:
stream.close()
with open(os.path.join(directory, 'server.log'), encoding='utf-8') as f:
self.assertEqual(f.read(), '启动\n[lc_mcp] ready\n')
def test_stdio_isolates_bootstrap_output_and_uses_utf8(self):
code = '''
from lc_mcp import server
from lc_mcp.protocol import Server
from lc_mcp.fake_engine import FakeEngine
from lc_mcp.tasks import TaskRunner
import tempfile
directory = tempfile.TemporaryDirectory()
def build(fake=False):
print('bootstrap notice')
return Server(FakeEngine(frames=[[{'text':'开始', 'x':1,'y':2,'w':3,'h':4}]]), TaskRunner(directory.name))
server.build_server = build
server.main(['--stdio'])
'''
request = json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'tools/call',
'params': {'name': 'get_state'}}, ensure_ascii=False) + '\n'
env = dict(os.environ, PYTHONUTF8='0', PYTHONIOENCODING='cp936')
p = subprocess.run([sys.executable, '-c', code], cwd=REPO, input=request.encode('utf-8'),
capture_output=True, env=env, timeout=20)
self.assertEqual(p.returncode, 0, p.stderr.decode('utf-8', 'replace'))
result = json.loads(p.stdout.decode('utf-8'))
data = json.loads(result['result']['content'][0]['text'])
self.assertEqual(data['items'][0]['text'], '开始')
self.assertIn(b'bootstrap notice', p.stderr)
def test_drag_checks_bounds_before_sending_input(self):
from lc_mcp.aalc_engine import AalcEngine
engine = AalcEngine.__new__(AalcEngine)
engine._grab = mock.Mock(return_value=mock.Mock(width=800, height=600))
engine.auto = mock.Mock()
with self.assertRaises(ValueError):
engine.drag(100, 100, 800, 500)
engine.auto.mouse_drag.assert_not_called()
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)
def test_template_scale_tracks_client_and_restores_config_on_error(self):
from lc_mcp.aalc_engine import AalcEngine
engine = AalcEngine.__new__(AalcEngine)
engine.list_images = mock.Mock(return_value=['battle/setting_assets.png'])
engine._grab = mock.Mock(return_value=mock.Mock(width=1278, height=731))
engine.cfg = mock.Mock(set_win_size=1080)
engine.cfg.unsaved_set_value.side_effect = lambda key, value: setattr(engine.cfg, key, value)
engine.auto = mock.Mock()
def match(*args, **kwargs):
self.assertEqual(engine.cfg.set_win_size, 718.875)
return (1233, 42)
engine.auto.find_image_element.side_effect = match
self.assertEqual(engine.find_image('battle/setting_assets.png'), (1233, 42))
self.assertEqual(engine.cfg.set_win_size, 1080)
self.assertEqual(engine.auto.clear_img_cache.call_count, 2)
engine.auto.find_image_element.side_effect = RuntimeError('match failed')
with self.assertRaises(RuntimeError):
engine.find_image('battle/setting_assets.png')
self.assertEqual(engine.cfg.set_win_size, 1080)
self.assertEqual(engine.auto.clear_img_cache.call_count, 4)
engine.cfg.set_value.assert_not_called()
def free_port(): def free_port():
with socket.socket() as s: with socket.socket() as s:
s.bind(('127.0.0.1', 0)) s.bind(('127.0.0.1', 0))
@@ -150,6 +255,16 @@ def free_port():
class TransportTest(unittest.TestCase): class TransportTest(unittest.TestCase):
def test_client_disconnect_is_not_a_server_error(self):
handler = srv_mod._Handler.__new__(srv_mod._Handler)
for error in (ConnectionResetError, BrokenPipeError):
handler._handle_session = mock.Mock(side_effect=error('client closed'))
handler.handle()
handler._handle_session.assert_called_once_with()
handler._handle_session = mock.Mock(side_effect=ValueError('internal bug'))
with self.assertRaises(ValueError):
handler.handle()
def setUp(self): def setUp(self):
self.tmp = tempfile.TemporaryDirectory() self.tmp = tempfile.TemporaryDirectory()
self.token_file = os.path.join(self.tmp.name, 'token') self.token_file = os.path.join(self.tmp.name, 'token')
@@ -190,7 +305,8 @@ class TransportTest(unittest.TestCase):
self.assertFalse(out[2]['result']['isError']) self.assertFalse(out[2]['result']['isError'])
def test_bridge_offline(self): def test_bridge_offline(self):
env = dict(os.environ, LCP_PORT=str(free_port()), HOME=self.tmp.name, USERPROFILE=self.tmp.name) env = dict(os.environ, LCP_PORT=str(free_port()), HOME=self.tmp.name, USERPROFILE=self.tmp.name,
PYTHONUTF8='0', PYTHONIOENCODING='cp936')
os.makedirs(os.path.join(self.tmp.name, 'lc_mcp')) os.makedirs(os.path.join(self.tmp.name, 'lc_mcp'))
os.replace(self.token_file, os.path.join(self.tmp.name, 'lc_mcp', 'token')) os.replace(self.token_file, os.path.join(self.tmp.name, 'lc_mcp', 'token'))
req = ('{"jsonrpc":"2.0","id":1,"method":"initialize"}\n' req = ('{"jsonrpc":"2.0","id":1,"method":"initialize"}\n'
@@ -200,6 +316,8 @@ class TransportTest(unittest.TestCase):
out = [json.loads(x) for x in p.stdout.splitlines()] out = [json.loads(x) for x in p.stdout.splitlines()]
self.assertIn('offline', out[0]['result']['serverInfo']['name']) self.assertIn('offline', out[0]['result']['serverInfo']['name'])
self.assertIn('未运行', out[1]['error']['message']) self.assertIn('未运行', out[1]['error']['message'])
self.assertEqual(p.returncode, 1)
self.assertIn('服务未运行或不可达', p.stderr)
def test_stdio_server(self): def test_stdio_server(self):
req = ('{"jsonrpc":"2.0","id":1,"method":"ping"}\n' req = ('{"jsonrpc":"2.0","id":1,"method":"ping"}\n'