Files
lc-mcp/tests/test_jev_mirror_agent.py
T
Codex Agent 0ddd8650b7 镜牢决策补强: 确认重试回退、快感合成闸门与英文 OCR 别名
- aalc_engine: window_move 下按压保持 0.5s, 避免 Unity 漏点
- jev_mirror_agent: 技能描述抖动时 Enter 未推进改点已核对按钮并限制连续重试;
  结果页指纹忽略剧情与时钟变化; 追加的苦难不再误判为单个饰品场景;
  第 3 层起离开商店前强制做一次有界快感合成检查
- lc_scene_aliases: 补 Mounting Trials / Acquire E.G.O Gift 等标签,
  识别拆分的 Win/Bate 胜率按钮与 Max HP / Defense Level 文本
- tests: 覆盖上述回归点
2026-10-22 15:44:02 +08:00

589 lines
34 KiB
Python

import json
import os
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
import jev_client # noqa: E402 Jev 请求经由 jev_client 发出
class SceneRegressionTest(unittest.TestCase):
def setUp(self):
stop = mock.patch.object(agent, 'STOP_FILE')
self.stop = stop.start()
self.stop.exists.return_value = False
self.addCleanup(stop.stop)
def test_takeover_interrupts_between_p_and_enter(self):
self.stop.exists.side_effect = [False,False,True]
bridge = mock.Mock()
bridge.call.return_value = {'ok':True}
result = agent.execute(bridge,{'tool':'battle','args':{}})
self.assertTrue(result['stopped'])
self.assertEqual(result['pairs'],0)
self.assertEqual(bridge.call.call_args_list,[mock.call('keypress',{'key':'p'})])
def test_takeover_stops_remaining_dialog_confirmations(self):
self.stop.exists.side_effect = [False,True]
bridge = mock.Mock()
bridge.call.return_value = {'ok':True}
with mock.patch.object(agent.time,'sleep'):
result=agent.execute(bridge,{'tool':'advance_dialog','args':{'presses':5}})
self.assertTrue(result['stopped'])
self.assertEqual(bridge.call.call_count,1)
@mock.patch.object(agent,'button_enabled',return_value=True)
def test_floor_pack_rules_are_enforced_before_decision(self,_enabled):
state={'floor':4,'screen':{'w':1920,'h':1080},'items':[
{'text':t,'x':x,'y':y,'w':100,'h':40} for t,x,y in
[('选择4层主题卡包',750,153),('2号线',276,665),('3号线',909,665),('刷新1/3',1570,49)]]}
options=agent.candidates(state)
self.assertNotIn('pack_drag_1',options)
self.assertIn('pack_drag_3',options)
state['floor']=5
state['items'][0]['text']='选择5层主题卡包'
options=agent.candidates(state)
self.assertEqual(set(options),{'wait','requires_analysis','required_pack'})
self.assertEqual(agent.fixed_fallback(options),'required_pack')
state['items'][1]['text']='1号线'
options=agent.candidates(state)
self.assertIn('pack_refresh',options)
self.assertFalse(any(k.startswith('pack_drag') for k in options))
state['items'][-1]['text']='刷新0/3'
self.assertNotIn('pack_refresh',agent.candidates(state))
@mock.patch.object(agent,'button_enabled',return_value=True)
def test_thunderbranch_observation_exception_keeps_startup_battery_priority(self,_enabled):
state={'items':[{'text':t,'x':700,'y':500,'w':150,'h':40} for t in
('额外E.G.O饰品观测','可选择0/1','雷枝','拒绝饰品','确认')]}
options=agent.candidates(state)
self.assertIn('select_thunderbranch',options)
self.assertNotIn('decline_extra_observation',options)
state['items'].append({'text':'标准负载电池','x':900,'y':500,'w':150,'h':40})
options=agent.candidates(state)
self.assertIn('select_startup_battery',options)
self.assertNotIn('select_thunderbranch',options)
state['items']=state['items'][:-1]
state['items'][0]['text']='选择E.G.O饰品'
self.assertNotIn('select_thunderbranch',agent.candidates(state))
def test_ambiguous_event_is_not_assumed_free(self):
options={'event_1':{'description':'当前事件选项:拔出荧光灯;结果:'},
'event_scroll':{},'wait':{},'requires_analysis':{}}
self.assertEqual(agent.fixed_fallback(options),'event_scroll')
options['event_2']={'description':'当前事件选项:按下按钮;结果:选择后获得破裂饰品'}
self.assertEqual(agent.fixed_fallback(options),'event_2')
@mock.patch.object(agent,'button_enabled',return_value=True)
def test_due_thrill_fusion_prevents_leaving_before_bounded_check(self,_enabled):
state={'symbol_upgraded':True,'floor':3,'thrill_fusion_due':True,'items':[
{'text':t,'x':100,'y':100,'w':100,'h':40} for t in ('商店','刷新','离开')]}
self.assertEqual(set(agent.candidates(state,refresh_used=4)), {'wait','requires_analysis'})
state['thrill_fusion_due']=False
self.assertTrue(any(k.startswith('button_') for k in agent.candidates(state,refresh_used=4)))
def test_sinner_very_high_precedes_high_and_generic_gold(self):
options={'sinner_1':{'description':'预计成功率高'},
'sinner_2':{'description':'预计成功率极高'},
'sinner_card_0':{'description':'金黄色(高/极高)'}}
self.assertEqual(agent.fixed_fallback(options),'sinner_2')
@mock.patch.object(agent,'button_enabled',return_value=True)
def test_pending_thunderbranch_cannot_leave_shop(self,_enabled):
state={'symbol_upgraded':True,'floor':3,'thunderbranch_pending':True,'items':[
{'text':t,'x':100,'y':100,'w':100,'h':40} for t in
('商店','刷新','强化饰品','离开')]}
options=agent.candidates(state,refresh_used=4)
self.assertEqual(set(options),{'wait','requires_analysis','prepare_thunderbranch'})
self.assertEqual(agent.fixed_fallback(options),'prepare_thunderbranch')
def test_jev_rate_limit_fails_without_sleep_or_second_request(self):
error=jev_client.urllib.error.HTTPError('https://example.test',429,'rate limit',{},None)
with mock.patch.object(jev_client.urllib.request,'urlopen',side_effect=error) as request, mock.patch.object(jev_client.time,'sleep') as sleep:
with self.assertRaises(jev_client.urllib.error.HTTPError):
agent.decide({'apiKey':'test','model':'jev-test'}, {'items':[]},{'wait':{'description':'wait'}},[])
self.assertEqual(request.call_count,1)
sleep.assert_not_called()
def test_jev_uses_configured_model_and_returns_probabilities_without_images(self):
response = mock.MagicMock()
response.__enter__.return_value = response
response.read.return_value = b'{"model":"jev-test","answers":{"action":{"type":"choice","choice":"wait","confidence":0.5,"probabilities":{"wait":0.75,"inspect":0.25}}}}'
state = {'screen': {'path': 'LOCAL_SCREENSHOT_ONLY.png'},
'items': [{'text': 'EVENT', 'x': 10, 'y': 20}]}
options = {'wait': {'description': 'wait'}, 'inspect': {'description': 'inspect'}}
with mock.patch.object(jev_client.urllib.request, 'urlopen', return_value=response) as request:
result = agent.decide({'apiKey': 'test', 'model': 'jev-test'}, state, options, [])
payload = agent.json.loads(request.call_args.args[0].data.decode('utf-8'))
self.assertEqual(payload['model'], 'jev-test')
self.assertIsInstance(payload['state'], str)
self.assertNotIn('LOCAL_SCREENSHOT_ONLY', payload['state'])
self.assertEqual(result['confidence'], .5)
self.assertEqual(result['probabilities'], {'wait': .75, 'inspect': .25})
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_english_receiving_text_is_not_event_rec_indicator(self):
state = {'items': [{'text': 'Receive this E.G.O Gift', 'x': 600,
'y': 300, 'w': 320, 'h': 40}]}
self.assertNotIn('event_advance', agent.candidates(state))
def test_exact_event_rec_indicator_still_advances_dialogue(self):
state = {'items': [{'text': 'REC', 'x': 900, 'y': 200, 'w': 50, 'h': 30}]}
self.assertIn('event_advance', agent.candidates(state))
def test_single_validated_confirmation_needs_no_model(self):
options = {'wait': {}, 'requires_analysis': {}, 'confirm_startup_battery': {}}
self.assertEqual(agent.unambiguous_routine(options), 'confirm_startup_battery')
def test_route_choices_and_unknown_single_actions_still_need_model(self):
for actions in ({'route_1': {}}, {'event_scroll': {}},
{'modal_confirm': {}, 'route_1': {}}):
self.assertIsNone(agent.unambiguous_routine(dict(wait={}, requires_analysis={}, **actions)))
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':'advance_dialog','args':{'presses':5}})
self.assertEqual(options['event_finish']['fallback_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)]]}
options=agent.candidates(state)
self.assertEqual(options['event_finish']['action'],{'tool':'advance_dialog','args':{'presses':5}})
self.assertEqual(options['event_finish']['fallback_action'],{'tool':'click','args':{'x':1644,'y':963}})
@mock.patch.object(agent,'button_enabled',return_value=True)
def test_layer_confirmation_requires_full_selection_then_batches_enter(self,_enabled):
state={'items':[{'text':t,'x':1500,'y':900,'w':200,'h':40} for t in
('追加的苦难','获得E.G.O饰品','选择1/2')]}
self.assertNotIn('hardship_confirm',agent.candidates(state))
state['items'][-1]['text']='选择2/2'
options=agent.candidates(state)
self.assertEqual(options['hardship_confirm']['action'],{'tool':'advance_dialog','args':{'presses':3}})
self.assertEqual(options['hardship_confirm']['fallback_action']['args'],{'x':1600,'y':920})
@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(k.startswith('button_') for k in agent.candidates(state)))
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_generic_button_clicks_the_checked_item(self, _enabled):
# 重新按文字搜索会多一次截图+OCR, 且可能点到背景里同名的另一个按钮
state = {'items': [{'text': '继续', 'x': 100, 'y': 200, 'w': 80, 'h': 40}]}
self.assertEqual(agent.candidates(state)['button_0']['action'],
{'tool': 'click', 'args': {'x': 140, 'y': 220}})
def test_screenshot_decoded_once_for_repeated_checks(self):
from PIL import Image
with tempfile.TemporaryDirectory() as folder:
shot = Path(folder) / 'shot.png'
Image.new('L', (200, 100), 200).save(shot)
state = {'screen': {'path': str(shot)}}
item = {'x': 10, 'y': 10, 'w': 50, 'h': 20}
with mock.patch.object(Image, 'open', wraps=Image.open) as opened:
self.assertTrue(agent.button_enabled(state, item))
self.assertTrue(agent.button_enabled(state, dict(item, x=100)))
self.assertEqual(opened.call_count, 1)
Image.new('L', (200, 101), 0).save(shot) # 同路径的新内容不能命中旧缓存
self.assertFalse(agent.button_enabled(state, item))
def test_map_templates_are_loaded_once(self):
screenshot = Path(__file__).resolve().parent / 'fixtures' / 'floor4_white_events.png'
if not (agent.REPO_ROOT / 'vendor' / 'aalc' / 'assets').is_dir():
self.skipTest('AALC submodule assets unavailable')
state = {'screen': {'path': str(screenshot)}}
first = agent.map_candidates(state)
loads = agent._map_template.cache_info().misses
self.assertEqual(agent.map_candidates(state), first)
self.assertEqual(agent._map_template.cache_info().misses, loads)
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))
class MainLoopTest(unittest.TestCase):
def run_main(self, home, bridge, steps=3):
config = home / 'cfg.json'
config.write_text('{}', encoding='utf-8')
with mock.patch.object(agent, 'RUNTIME_HOME', home), \
mock.patch.object(agent, 'STOP_FILE', home / 'JEV_STOP'), \
mock.patch.object(agent, 'Bridge', return_value=bridge) as make_bridge, \
mock.patch.object(agent, 'acquire_single_instance', return_value=lambda: None), \
mock.patch.object(agent.time, 'sleep'), \
mock.patch.dict(os.environ, {'OPENCODE_API_KEY': 'test-key', 'LC_FIXED_FALLBACK': '1'}), \
mock.patch.object(sys, 'argv', ['agent', str(config), '--steps', str(steps)]):
agent.main()
return make_bridge
def test_tool_errors_are_observed_instead_of_aborting(self):
with tempfile.TemporaryDirectory() as folder:
home = Path(folder)
bridge = mock.Mock()
bridge.call.return_value = {'ok': False, 'error': '截图失败'}
make_bridge = self.run_main(home, bridge, steps=2)
make_bridge.assert_called_once()
self.assertIs(make_bridge.call_args.kwargs['raise_on_error'], False)
status = json.loads((home / 'jev_mirror_status.json').read_text())
# 没有因异常退出, 而是按“截图暂不可用”等待到步数用完
self.assertEqual((status['state'], status.get('reason')), ('stopped', 'step_limit'))
records = [json.loads(line) for line in (home / 'jev_mirror_log.jsonl').read_text().splitlines()]
self.assertEqual([r['choice'] for r in records], ['wait', 'wait'])
class SessionTest(unittest.TestCase):
def test_tool_errors_can_be_returned_instead_of_raised(self):
import mcp_session
session = mcp_session.Session.__new__(mcp_session.Session)
session.request = mock.Mock(return_value={'isError': True, 'content': [
{'type': 'text', 'text': json.dumps({'ok': False, 'error': 'text not found'})}]})
session.raise_on_error, session.timeout = False, 30
self.assertEqual(session.call('click_text', {'text': 'x'}), {'ok': False, 'error': 'text not found'})
session.raise_on_error = True
with self.assertRaises(RuntimeError):
session.call('click_text', {'text': 'x'})
if __name__ == '__main__':
unittest.main()