Files
lc-mcp/tests/test_jev_mirror_agent.py
T

367 lines
22 KiB
Python

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):
def test_single_extra_gift_decline_preserves_core_and_multi_cards(self):
state = {'items': [{'text':t,'x':100,'y':100,'w':100,'h':40} for t in
('获得E.G.O饰品','石版字符','拒绝饰品','选择')]}
self.assertIn('decline_extra_observation', agent.candidates(state))
state['items'][1]['text'] = '标准负载电池'
self.assertNotIn('decline_extra_observation', agent.candidates(state))
state['items'][1]['text'] = '石版字符'
state['items'].append({'text':'获得E.G.O饰品'})
self.assertNotIn('decline_extra_observation', agent.candidates(state))
def test_special_shop_cannot_leave_before_core_upgrade(self):
state = {'floor':2, 'symbol_upgraded':False, 'items':[
{'text':t, 'x':100,'y':100,'w':100,'h':40} for t in
('特殊商店','刷新','强化饰品','替换格里高尔的技能','替换浮士德的技能','离开')]}
self.assertEqual(set(agent.candidates(state)), {'wait','requires_analysis'})
state['symbol_upgraded'] = True
options = agent.candidates(state)
self.assertTrue(any(k.startswith('swap_') for k in options))
self.assertFalse(any('离开' in v['description'] for v in options.values()))
def test_purple_shop_is_reachable_by_lit_arrow(self):
import numpy as np
from PIL import Image
assets = agent.REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror'
canvas = Image.new('RGB', (1920,1080))
def place(name, center, purple=False):
with Image.open(assets / name) as im:
im = im.convert('RGB').resize((round(im.width*.75),round(im.height*.75)))
if name.endswith('mid.png'):
from PIL import ImageEnhance
im = ImageEnhance.Brightness(im).enhance(1.8)
if purple:
gray = np.array(im.convert('L'), dtype=float)
im = Image.fromarray(np.stack((gray*.55,gray*.25,gray*.8),axis=2).astype('uint8'))
canvas.paste(im,(center[0]-im.width//2,center[1]-im.height//2))
place('mybus_default_distance.png',(704,396))
place('road_in_mir/mid.png',(895,430))
place('road_in_mir/shop.png',(1079,433),True)
with tempfile.TemporaryDirectory() as folder:
shot = Path(folder) / 'purple_shop.png'
canvas.save(shot)
options = agent.map_candidates({'screen': {'path': str(shot)}})
self.assertEqual(set(options), {'route_d'})
self.assertIn('商店', options['route_d']['description'])
def test_claimed_rewards_stop_at_lobby_without_entering_again(self):
state = {'claim_requested': True, 'items': [{'text': t} for t in ('探索状态','进入','名与蛛之镜')]}
options = agent.candidates(state)
self.assertEqual(set(options), {'wait','requires_analysis','run_complete'})
self.assertEqual(agent.fixed_fallback(options), 'run_complete')
state['items'].append({'text':'探索中'})
self.assertEqual(set(agent.candidates(state)), {'wait','requires_analysis'})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_claim_confirmation_only_uses_modal(self, _enabled):
state = {'claim_requested': True, 'items': [
{'text':'确认','x':800,'y':700,'w':100,'h':40}, {'text':'进入'}]}
options = agent.candidates(state)
self.assertEqual(set(options), {'wait','requires_analysis','modal_confirm'})
self.assertEqual(options['modal_confirm']['action']['args'], {'x':850,'y':720})
def test_death_screen_stops_battle_candidates_without_accepting_deaths(self):
state = {'items': [{'text': text} for text in ('参加战斗的全体罪人已经死亡。', '重试关卡')]}
self.assertEqual(set(agent.candidates(state, previous_battle=True)), {'wait', 'requires_analysis'})
self.assertFalse(agent.death_retry_screen(['敌方死亡时增加伤害']))
@mock.patch.object(agent.time, 'sleep')
def test_battle_macro_stops_on_party_death(self, _sleep):
bridge = mock.Mock()
bridge.call.side_effect = lambda tool, args=None: ({'ok': True, 'items': [{'text': '参加战斗的全体罪人已经死亡。'}]}
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.assertTrue(result['terminal'])
self.assertEqual(result['pairs'], 2)
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_startup_three_gifts_selects_battery_and_cannot_decline(self, _enabled):
state = {'items': [{'text': text, 'x': x, 'y': y, 'w': 100, 'h': 40}
for text, x, y in [('选择E.G.O饰品', 700, 80), ('可选择0/1', 1300, 80),
('标准负载电池', 800, 480), ('拒绝饰品', 1400, 850),
('确认', 1500, 950)]]}
options = agent.candidates(state, previous_battle=True)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'select_startup_battery'})
self.assertEqual(options['select_startup_battery']['action']['args'], {'x': 850, 'y': 500})
self.assertEqual(agent.fixed_fallback(options), 'select_startup_battery')
state['items'][1]['text'] = '可选择1/1'
options = agent.candidates(state)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'confirm_startup_battery'})
self.assertEqual(options['confirm_startup_battery']['action']['args'], {'x': 1550, 'y': 970})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_unrecognized_gift_choice_is_never_declined(self, _enabled):
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
for text in ('选择E.G.O饰品', '可选择0/1', '拒绝饰品', '确认')]}
self.assertEqual(set(agent.candidates(state)), {'wait', 'requires_analysis'})
state['items'] = [state['items'][2]]
self.assertEqual(set(agent.candidates(state)), {'wait', 'requires_analysis'})
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_explicit_extra_observation_can_be_declined(self, _enabled):
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
for text in ('额外E.G.O饰品观测', '拒绝饰品')]}
self.assertEqual(set(agent.candidates(state)), {'wait', 'requires_analysis', 'decline_extra_observation'})
def test_event_gift_outcome_does_not_become_startup_selection(self):
state = {'items': [{'text': '选项', 'x': 20, 'y': 20, 'w': 100, 'h': 40},
{'text': '按下闪电形状的按钮', 'x': 1200, 'y': 330, 'w': 350, 'h': 40, 'score': 1},
{'text': '选择后获得破裂饰品', 'x': 1200, 'y': 380, 'w': 350, 'h': 40, 'score': 1}]}
self.assertIn('event_1', agent.candidates(state))
def test_server_error_blocks_shop_and_retries_modal(self):
state = {'items': [
{'text': '商店'}, {'text': '刷新'}, {'text': '服务器发生错误。'},
{'text': '重试', 'x': 1069, 'y': 681, 'w': 74, 'h': 41},
{'text': '离开', 'x': 1642, 'y': 938, 'w': 116, 'h': 66}]}
options = agent.candidates(state)
self.assertEqual(set(options), {'wait', 'requires_analysis', 'retry_network'})
self.assertEqual(agent.fixed_fallback(options), 'retry_network')
self.assertEqual(options['retry_network']['action']['args'], {'x': 1106, 'y': 701})
def test_gift_damage_description_is_not_battle(self):
state = {'items': [{'text': '使用技能造成12点伤害时增加破裂强度', 'x': 400, 'y': 200, 'w': 800, 'h': 40}]}
self.assertNotIn('battle', agent.candidates(state))
def test_success_condition_in_event_option_is_not_result(self):
state = {'items': [{'text': '判定成功时获得饰品', 'x': 400, 'y': 200, 'w': 800, 'h': 40}]}
self.assertNotIn('event_finish', agent.candidates(state))
def test_loading_ocr_missing_n_blocks_input(self):
state = {'items': [{'text': 'CONECTING'}, {'text': '商店'}, {'text': '刷新'}]}
self.assertEqual(set(agent.candidates(state)), {'wait', 'requires_analysis'})
def test_fallback_judgement_precedes_reselecting_sinner(self):
options = {'sinner_card_1': {'description': '极高'},
'button_2': {'description': '进行判定'}}
self.assertEqual(agent.fixed_fallback(options), 'button_2')
def test_two_card_reward_uses_observed_anchor(self):
state = {'screen': {'w': 1920, 'h': 1080}, 'items': [
{'text': '选择遭遇战奖励卡', 'x': 800, 'y': 80, 'w': 300, 'h': 40},
{'text': '获得经费', 'x': 1120, 'y': 560, 'w': 100, 'h': 40}]}
options = agent.candidates(state)
self.assertEqual(options['reward_card_anchor_1']['action']['args'], {'x': 1170, 'y': 398})
self.assertEqual(agent.fixed_fallback(options), 'reward_card_anchor_1')
@mock.patch.object(agent.time, 'sleep')
def test_final_claim_click_is_followed_by_enter(self, _sleep):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True}
agent.execute(bridge, {'tool':'claim_final','args':{'x':1292,'y':816}})
self.assertEqual(bridge.call.call_args_list,
[mock.call('click',{'x':1292,'y':816}),
mock.call('keypress',{'key':'enter'})])
@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': 'keypress', 'args': {'key': 'enter'}})
@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_shop_leave_modal_without_ocr_question_mark(self, _enabled):
state = {'symbol_upgraded': True, 'items': [
{'text': t, 'x': x, 'y': y, 'w': 100, 'h': 40}
for t,x,y in [('特殊商店',307,162), ('刷新',1496,167),
('要离开商店吗',854,505), ('×取消',721,722),
('确认',1140,723), ('离开',1642,938)]]}
options = agent.candidates(state, refresh_used=4)
self.assertEqual(set(options), {'wait','requires_analysis','modal_confirm'})
self.assertEqual(options['modal_confirm']['action'], {'tool':'keypress','args':{'key':'enter'}})
@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'})
def test_event_fallback_does_not_treat_encounter_trigger_as_free_reward(self):
options = {'event_1': {'description': '拔出荧光灯;触发遭遇战!'},
'event_scroll': {'description': '查看其他选项'},
'wait': {}, 'requires_analysis': {}}
self.assertEqual(agent.fixed_fallback(options), 'event_scroll')
del options['event_scroll']
self.assertEqual(agent.fixed_fallback(options), 'requires_analysis')
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_event_encounter_result_clicks_start_battle(self, _enabled):
state = {'items': [{'text': t, 'x': x, 'y': y, 'w': 100, 'h': 40}
for t,x,y in [('结果',1088,162), ('开始战斗',1594,943)]]}
self.assertEqual(agent.candidates(state)['event_finish']['action'],
{'tool':'click','args':{'x':1644,'y':963}})
@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))
@mock.patch.object(agent, 'button_enabled', return_value=False)
def test_in_battle_judgement_overrides_previous_battle(self, _enabled):
state = {'items': [{'text': t, 'x':100, 'y':100, 'w':100, 'h':40}
for t in ('选择一位罪人来进行判定', '判定阈值', '12')]}
self.assertNotIn('battle', agent.candidates(state, previous_battle=True))
def test_battle_macro_stops_at_judgement_before_more_inputs(self):
bridge = mock.Mock()
bridge.call.side_effect = ([{'ok': True}] * 4 + [
{'ok': True, 'items': [{'text': '选择一位罪人来进行判定'}]}])
with mock.patch.object(agent, 'STOP_FILE') as stop, mock.patch.object(agent.time, 'sleep'):
stop.exists.return_value = False
result = agent.execute(bridge, {'tool': 'battle', 'args': {}})
self.assertTrue(result['terminal'])
self.assertEqual(result['pairs'], 2)
self.assertEqual(bridge.call.call_count, 5)
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()