Fix background clicks and mirror scene boundaries with run feedback
This commit is contained in:
@@ -8,6 +8,153 @@ import jev_mirror_agent as agent
|
|||||||
|
|
||||||
|
|
||||||
class SceneRegressionTest(unittest.TestCase):
|
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')
|
@mock.patch.object(agent.time, 'sleep')
|
||||||
def test_final_claim_click_is_followed_by_enter(self, _sleep):
|
def test_final_claim_click_is_followed_by_enter(self, _sleep):
|
||||||
bridge = mock.Mock()
|
bridge = mock.Mock()
|
||||||
@@ -116,7 +263,7 @@ class SceneRegressionTest(unittest.TestCase):
|
|||||||
options = agent.candidates({'items': items}, previous_battle=True)
|
options = agent.candidates({'items': items}, previous_battle=True)
|
||||||
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
|
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
|
||||||
self.assertEqual(options['modal_confirm']['action'],
|
self.assertEqual(options['modal_confirm']['action'],
|
||||||
{'tool': 'click', 'args': {'x': 1140, 'y': 620}})
|
{'tool': 'keypress', 'args': {'key': 'enter'}})
|
||||||
|
|
||||||
@mock.patch.object(agent, 'button_enabled', return_value=True)
|
@mock.patch.object(agent, 'button_enabled', return_value=True)
|
||||||
def test_nested_reward_confirmation_excludes_background_cards(self, _enabled):
|
def test_nested_reward_confirmation_excludes_background_cards(self, _enabled):
|
||||||
@@ -129,6 +276,17 @@ class SceneRegressionTest(unittest.TestCase):
|
|||||||
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
|
self.assertEqual(set(options), {'wait', 'requires_analysis', 'modal_confirm'})
|
||||||
self.assertEqual(options['modal_confirm']['action']['args']['y'], 699)
|
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)
|
@mock.patch.object(agent, 'button_enabled', return_value=True)
|
||||||
def test_event_result_uses_visible_continue_button(self, _enabled):
|
def test_event_result_uses_visible_continue_button(self, _enabled):
|
||||||
state = {'items':[{'text':t,'x':x,'y':y,'w':80,'h':40}
|
state = {'items':[{'text':t,'x':x,'y':y,'w':80,'h':40}
|
||||||
@@ -143,6 +301,21 @@ class SceneRegressionTest(unittest.TestCase):
|
|||||||
options = agent.candidates(state)
|
options = agent.candidates(state)
|
||||||
self.assertEqual(set(options), {'wait', 'requires_analysis'})
|
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)
|
@mock.patch.object(agent, 'button_enabled', return_value=True)
|
||||||
def test_shop_loading_blocks_stale_stock_refresh_and_leave(self, _enabled):
|
def test_shop_loading_blocks_stale_stock_refresh_and_leave(self, _enabled):
|
||||||
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
|
state = {'items': [{'text': text, 'x': 100, 'y': 100, 'w': 100, 'h': 40}
|
||||||
@@ -166,6 +339,23 @@ class SceneRegressionTest(unittest.TestCase):
|
|||||||
state = {'items': [{'text': '破裂+5', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
|
state = {'items': [{'text': '破裂+5', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
|
||||||
self.assertIn('battle', agent.candidates(state, previous_battle=True))
|
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):
|
def test_reward_stops_battle_candidates(self):
|
||||||
state = {'items': [{'text': text, 'x': 100, 'y': 10, 'w': 80, 'h': 40}
|
state = {'items': [{'text': text, 'x': 100, 'y': 10, 'w': 80, 'h': 40}
|
||||||
for text in ('获得E.G.O饰品', '造成更多伤害')]}
|
for text in ('获得E.G.O饰品', '造成更多伤害')]}
|
||||||
|
|||||||
@@ -156,6 +156,29 @@ class AalcTaskArgsTest(unittest.TestCase):
|
|||||||
|
|
||||||
class RuntimeTest(unittest.TestCase):
|
class RuntimeTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_background_click_holds_and_releases_without_moving_window(self):
|
||||||
|
from lc_mcp.aalc_engine import AalcEngine
|
||||||
|
engine = AalcEngine.__new__(AalcEngine)
|
||||||
|
engine.cfg = mock.Mock(simulator=False, win_input_type='background')
|
||||||
|
engine.auto, engine.screen = mock.Mock(), mock.Mock()
|
||||||
|
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:
|
||||||
|
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),
|
||||||
|
mock.call.sleep(.12)])
|
||||||
|
engine.auto.mouse_click.assert_not_called()
|
||||||
|
with mock.patch('lc_mcp.aalc_engine.time.sleep', side_effect=RuntimeError('interrupted')):
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
engine.click(100, 200)
|
||||||
|
self.assertEqual(handler.mouse_up.call_count, 2)
|
||||||
|
engine.screen.handle.set_window_pos.assert_not_called()
|
||||||
|
|
||||||
def test_background_keypress_holds_and_releases_on_error(self):
|
def test_background_keypress_holds_and_releases_on_error(self):
|
||||||
from lc_mcp.aalc_engine import AalcEngine
|
from lc_mcp.aalc_engine import AalcEngine
|
||||||
engine = AalcEngine.__new__(AalcEngine)
|
engine = AalcEngine.__new__(AalcEngine)
|
||||||
|
|||||||
Reference in New Issue
Block a user