"""Controller lifecycle and battle regressions using mocked MCP only.""" import contextlib 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_p_prevents_enter(self): client = mock.Mock() def call(tool, args=None): if tool == 'get_state': return {'ok': True, 'items': [{'text': 'WAVE 1'}]} 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('keypress', {'key': 'p'})]) def test_judgement_after_one_batch_ends_without_a_second_batch(self): client = mock.Mock() frames = iter(['WAVE 1', '选择一位罪人来进行判定']) client.call.side_effect = lambda tool, args=None: ( {'ok': True, 'items': [{'text': next(frames)}]} if tool == 'get_state' else {'ok': True}) result = self.run_helper(client) self.assertEqual(result['pairs'], 2) self.assertEqual(sum(call.args[0] == 'keypress' for call in client.call.call_args_list), 4) 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', mock.Mock(kernel32=self.kernel), 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_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()