177 lines
10 KiB
Python
177 lines
10 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):
|
|
@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': '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()
|