Files
lc-mcp/tests/test_controller_runtime.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

237 lines
13 KiB
Python

"""Controller lifecycle and battle regressions using mocked MCP only."""
import contextlib
import copy
import ctypes
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import battle_batch
import jev_mirror_agent as agent
from mcp_session import SessionCancelled, SessionTimeout
from run_state import save_json
class BattleBatchTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.stop = Path(self.folder.name) / 'JEV_STOP'
def run_helper(self, client):
session = mock.MagicMock()
session.__enter__.return_value = client
output = io.StringIO()
with mock.patch.object(battle_batch, 'Session', return_value=session), \
mock.patch.object(battle_batch, 'STOP_FILE', self.stop), \
mock.patch.object(sys, 'argv', ['battle_batch.py', '--seconds', '1', '--battle-confirmed']), \
mock.patch.object(battle_batch.time, 'sleep'), contextlib.redirect_stdout(output):
battle_batch.main()
return json.loads(output.getvalue())
def test_terminal_screens_never_receive_more_keys(self):
for text in ('进行判定', '选择遭遇战奖励卡', '参加战斗的全体罪人已经死亡。'):
with self.subTest(text=text):
client = mock.Mock()
client.call.return_value = {'ok': True, 'items': [{'text': text}]}
result = self.run_helper(client)
self.assertEqual(result['pairs'], 0)
self.assertEqual(client.call.call_args_list, [mock.call('get_state')])
def test_stop_after_win_rate_prevents_enter(self):
client = mock.Mock()
def call(tool, args=None):
if tool == 'get_state':
return {'ok': True, 'items': [{'text': 'WAVE 1'}, {'text': 'Win Rate'}]}
self.stop.touch()
return {'ok': True}
client.call.side_effect = call
result = self.run_helper(client)
self.assertTrue(result['stopped'])
self.assertFalse(result['outcome_unknown'])
self.assertEqual(client.call.call_args_list,
[mock.call('get_state'), mock.call('click_text', {'text': 'Win Rate'})])
def test_judgement_after_one_turn_ends_without_more_input(self):
client = mock.Mock()
frames = iter([[{'text': 'WAVE 1'}, {'text': '胜率'}], [{'text': '选择一位罪人来进行判定'}]])
client.call.side_effect = lambda tool, args=None: (
{'ok': True, 'items': next(frames)} if tool == 'get_state' else {'ok': True})
result = self.run_helper(client)
self.assertEqual(result['pairs'], 1)
self.assertEqual([c.args[0] for c in client.call.call_args_list],
['get_state', 'click_text', 'keypress', 'get_state'])
def test_cinematic_without_win_rate_sends_no_input(self):
# P 会在胜率/伤害之间切换, 看不到胜率按钮(特写)时只重新观察
client = mock.Mock()
client.call.return_value = {'ok': True, 'items': [{'text': 'TURN 2'}]}
result = self.run_helper(client)
self.assertEqual(result['pairs'], 0)
self.assertTrue(all(c.args[0] == 'get_state' for c in client.call.call_args_list))
class AgentRuntimeTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.home = Path(self.folder.name)
self.config = self.home / 'config.json'
self.config.write_text('{}')
self.kernel = mock.Mock()
self.kernel.CreateMutexW.return_value = 12345
self.kernel.GetLastError.return_value = 0
def run_agent(self, bridge, extra_args=(), constructor_error=None, fixed=True, executor_decider=None):
output = io.StringIO()
environment = dict(os.environ, LC_FIXED_FALLBACK='1' if fixed else '0')
environment.pop('OPENCODE_API_KEY', None)
with mock.patch.object(agent, 'RUNTIME_HOME', self.home), \
mock.patch.object(agent, 'STOP_FILE', self.home / 'JEV_STOP'), \
mock.patch.object(agent, 'Bridge', return_value=bridge, side_effect=constructor_error) as factory, \
mock.patch.object(ctypes, 'WinDLL', return_value=self.kernel, create=True), \
mock.patch.object(ctypes, 'get_last_error', side_effect=lambda: self.kernel.GetLastError(), create=True), \
mock.patch.object(sys, 'argv', ['jev_mirror_agent.py', str(self.config), '--steps', '1', *extra_args]), \
mock.patch.dict(os.environ, environment, clear=True), \
mock.patch.object(agent.time, 'sleep'), \
mock.patch.object(agent, 'button_enabled', return_value=False), \
contextlib.redirect_stdout(output):
agent.main(executor_decider=executor_decider)
status = json.loads((self.home / 'jev_mirror_status.json').read_text(encoding='utf-8'))
return status, factory
def test_old_claim_context_does_not_complete_new_run_at_lobby(self):
save_json(self.home / 'jev_shop_context.json', {'floor': 5, 'claim_requested': True})
save_json(self.home / 'mirror_current_run.json', {
'start_utc': '2026-10-02T00:00:00+00:00', 'status': 'running',
'dungeon_completed': False, 'final_reward_claimed': False})
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [
{'text': text, 'score': 1} for text in ('探索状态', '进入', '名与蛛之镜')]}
status, _ = self.run_agent(bridge)
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(status['state'], 'stopped')
self.assertFalse(run['dungeon_completed'])
self.assertFalse(run['final_reward_claimed'])
bridge.close.assert_called_once()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_bridge_initialization_failure_records_error_and_releases_mutex(self):
status, _ = self.run_agent(mock.Mock(), constructor_error=SessionTimeout('initialization hung'))
self.assertEqual(status['state'], 'requires_analysis')
self.assertTrue(status['outcome_unknown'])
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_stop_file_prevents_bridge_startup(self):
(self.home / 'JEV_STOP').touch()
status, factory = self.run_agent(mock.Mock())
self.assertEqual(status['state'], 'stopped')
factory.assert_not_called()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_pending_request_cancellation_marks_run_stopped_and_logs_uncertainty(self):
bridge = mock.Mock()
bridge.call.side_effect = SessionCancelled('cancelled pending get_state', True)
status, _ = self.run_agent(bridge)
self.assertEqual(status['state'], 'stopped')
self.assertTrue(status['outcome_unknown'])
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(run['status'], 'stopped')
rows = (self.home / 'jev_mirror_log.jsonl').read_text().splitlines()
self.assertEqual(json.loads(rows[-1])['state'], 'stopped')
bridge.close.assert_called_once()
def test_expired_budget_prevents_execute_from_sending_any_key(self):
bridge = mock.Mock()
with mock.patch.object(agent, 'STOP_FILE') as stop:
stop.exists.return_value = False
result = agent.execute(bridge, {'tool': 'keypress', 'args': {'key': 'enter'}}, deadline=0)
self.assertTrue(result['stopped'])
bridge.call.assert_not_called()
def test_changing_modal_ocr_still_falls_back_from_enter_and_bounds_retries(self):
bridge = mock.Mock()
frames = iter(['skill description A', 'skill description B', 'skill description C', 'skill description D'])
bridge.call.side_effect = lambda tool, args=None: (
{'ok': True, 'items': [{'text': next(frames), 'score': 1}]} if tool == 'get_state' else {'ok': True})
options = {'wait': {'action': {'tool': 'wait', 'args': {}}, 'description': 'wait'},
'requires_analysis': {'action': {'tool': 'requires_analysis', 'args': {}}, 'description': 'analysis'},
'buy_swap': {'description': 'validated skill payment',
'action': {'tool': 'keypress', 'args': {'key': 'enter'}},
'fallback_action': {'tool': 'click', 'args': {'x': 790, 'y': 548}}}}
with mock.patch.object(agent, 'candidates', side_effect=lambda *args: copy.deepcopy(options)):
self.run_agent(bridge, extra_args=('--steps', '4'))
inputs = [call.args[0] for call in bridge.call.call_args_list if call.args[0] != 'get_state']
self.assertEqual(inputs, ['keypress', 'click', 'click'])
def test_close_error_still_releases_controller_mutex(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': []}
bridge.close.side_effect = RuntimeError('close failed')
with self.assertRaisesRegex(RuntimeError, 'close failed'):
self.run_agent(bridge)
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_no_jev_config_publishes_execution_model_request_without_input(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [{'text': '路线', 'score': 1}]}
options = {
'wait': {'description': 'wait', 'action': {'tool': 'wait', 'args': {'seconds': .7}}},
'requires_analysis': {'description': 'inspect', 'action': {'tool': 'requires_analysis', 'args': {}}},
'route_w': {'description': 'event above', 'action': {'tool': 'route', 'args': {'key': 'w'}}},
}
with mock.patch.object(agent, 'candidates', return_value=options), mock.patch.object(agent, 'decide') as decide:
status, _ = self.run_agent(bridge, fixed=False)
self.assertEqual(status['state'], 'requires_decision')
self.assertEqual(status['decision_request']['reason'], 'no_jev_model')
self.assertEqual(bridge.call.call_args_list, [mock.call('get_state')])
decide.assert_not_called()
bridge.close.assert_called_once()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
pending = json.loads((self.home / 'decision_request.json').read_text(encoding='utf-8'))
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(pending['run_id'], run['run_id'])
self.assertEqual(run['status'], 'requires_decision')
def test_pending_execution_model_decision_can_resume_same_run(self):
save_json(self.home / 'mirror_current_run.json', {
'run_id': 'pending-run', 'start_utc': '2026-10-02T00:00:00+00:00',
'status': 'requires_decision', 'dungeon_completed': False, 'final_reward_claimed': False})
save_json(self.home / 'jev_shop_context.json', {'run_id': 'pending-run', 'floor': 3})
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': []}
status, _ = self.run_agent(bridge, extra_args=['--resume'])
self.assertEqual(status['state'], 'stopped')
self.assertEqual(status['run_id'], 'pending-run')
def test_embedded_execution_model_selects_and_executes_without_jev(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [{'text': '路线', 'score': 1}]}
options = {
'wait': {'description': 'wait', 'action': {'tool': 'wait', 'args': {'seconds': .7}}},
'requires_analysis': {'description': 'inspect', 'action': {'tool': 'requires_analysis', 'args': {}}},
'route_w': {'description': 'event above', 'action': {'tool': 'route', 'args': {'key': 'w'}}},
}
executor = mock.Mock(side_effect=lambda request: {'request_id': request['request_id'], 'choice': 'route_w'})
with mock.patch.object(agent, 'candidates', return_value=options), mock.patch.object(agent, 'decide') as decide:
status, _ = self.run_agent(bridge, fixed=False, executor_decider=executor)
self.assertEqual(status['state'], 'stopped')
executor.assert_called_once()
decide.assert_not_called()
self.assertEqual(bridge.call.call_args_list, [mock.call('get_state'),
mock.call('keypress', {'key': 'w'}), mock.call('keypress', {'key': 'enter'}),
mock.call('keypress', {'key': 'enter'})])
rows = [json.loads(line) for line in (self.home / 'jev_mirror_log.jsonl').read_text(encoding='utf-8').splitlines()]
self.assertEqual(rows[-1]['decision_engine'], 'executor')
self.assertEqual(rows[-1]['jev_ms'], 0)
if __name__ == '__main__':
unittest.main()