Files
lc-mcp/tests/test_automation_dispatch.py
ywsandClaude Opus 5.5 5fdb9fd221 普通镜牢转交 AALC,核对镜牢实际完成局数,合成与任务互斥
转交与互斥(原未提交改动,合入选队分支后提交):
- MCP 初始化说明和工具描述引导常规日常/领奖/普通镜牢走
  task_status -> run_task -> 轮询;jev_mirror_agent 默认转交 AALC,
  明确的速通配置、决策模型或未完成速通局仍走逐步模式。
- 新增 ~/lc_mcp/game.lock:run_task 子进程与模块合成脚本互斥,
  合成脚本另查 task_status,状态不明时不接管游戏。
- all 任务的 HDR 提示无 GUI 时直接确认并记录;预检按窗口位置
  决定是否计标题栏,set_windows=false 跳过;levelup card/weakest
  同步失败返回非零。急停提示改为 End 键。

镜牢结果(对接文档指出的缺口):
- 预检拒绝空队列和没有适用当前难度队伍的配置:AALC 会一局不打
  就以 0 退出。可用判定与 Mirror_task 的固定用途规则一致。
- 监听 mirror_signal 统计实际完成局数;少于应打局数记 [ERROR]
  并以退出码 4 结束,task_status 为 failed。
- mirror 指定 count 时临时关闭 infinite_dungeons(否则次数变成
  9999),原值写入恢复文件,结束或强杀后写回。

验证:Linux / Python 3.12 全套 507 项通过;git diff --check 通过。
用上游真实 Mirror_task 回放了无可用队伍和正常完成两种情况;
Windows 实机仍需回归。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 03:27:03 +00:00

155 lines
7.6 KiB
Python

"""Ordinary mirror requests delegate to AALC without entering the click loop."""
import contextlib
import io
import json
import os
from pathlib import Path
import sys
import tempfile
from types import SimpleNamespace
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import jev_mirror_agent as agent
from mcp_session import SessionTimeout
from run_state import save_json
class AutomationDispatchTest(unittest.TestCase):
def setUp(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
self.home = Path(folder.name)
for patch in (mock.patch.object(agent, 'RUNTIME_HOME', self.home),
mock.patch.object(agent, 'STOP_FILE', self.home / 'JEV_STOP'),
mock.patch.dict(os.environ, {'LC_FIXED_FALLBACK': '0'})):
patch.start()
self.addCleanup(patch.stop)
def workflow(self, **overrides):
args = dict(workflow='auto', config=None, decision_engine=None, new_run=False, resume=False)
args.update(overrides)
return agent.execution_workflow(SimpleNamespace(**args))
def test_ordinary_request_defaults_to_aalc(self):
self.assertEqual(self.workflow(), 'aalc')
def test_special_requirements_preserve_speedrun(self):
for args in ({'config': 'jev.json'}, {'decision_engine': 'executor'}, {'resume': True},
{'workflow': 'speedrun'}):
with self.subTest(args=args):
self.assertEqual(self.workflow(**args), 'speedrun')
save_json(self.home / 'mirror_current_run.json', {'status': 'requires_decision'})
self.assertEqual(self.workflow(), 'speedrun')
self.assertEqual(self.workflow(new_run=True), 'aalc')
def delegate(self, current, started=None, **kwargs):
bridge = mock.Mock()
bridge.call.side_effect = [current, started or {'ok': True, 'log': 'new.log', 'task': 'mirror'}]
with mock.patch.object(agent, 'Bridge', return_value=bridge):
result = agent.delegate_mirror(**kwargs)
bridge.close.assert_called_once()
return result, bridge
def test_launches_only_mirror_and_returns_monitoring_contract(self):
result, bridge = self.delegate({'ok': True, 'state': 'idle'})
self.assertEqual(result['state'], 'delegated')
self.assertEqual(result['next_call']['tool'], 'task_status')
self.assertEqual(bridge.call.call_args_list, [mock.call('task_status', {'lines': 30}),
mock.call('run_task', {'task': 'mirror', 'count': 1})])
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertFalse(run['dungeon_completed'])
self.assertFalse(run['final_reward_claimed'])
self.assertEqual(self.workflow(resume=True), 'aalc')
def test_running_mirror_is_monitored_without_duplicate_start(self):
result, bridge = self.delegate({'ok': True, 'state': 'running', 'task': 'mirror'})
self.assertEqual(result['state'], 'monitoring')
self.assertEqual(bridge.call.call_count, 1)
def test_other_running_task_is_not_stopped_or_replaced(self):
result, bridge = self.delegate({'ok': True, 'state': 'running', 'task': 'daily'})
self.assertEqual(result['state'], 'busy')
self.assertEqual(bridge.call.call_count, 1)
def test_user_stop_never_restarts_without_new_request(self):
for current in ({'ok': True, 'state': 'stopped', 'task': 'mirror'},
{'ok': True, 'state': 'failed', 'exit_code': 130}):
with self.subTest(current=current):
result, bridge = self.delegate(current)
self.assertEqual(result['state'], 'stopped')
self.assertEqual(bridge.call.call_count, 1)
result, bridge = self.delegate({'ok': True, 'state': 'stopped'}, new_run=True)
self.assertEqual(result['state'], 'delegated')
def test_unknown_or_failed_status_never_triggers_blind_retry(self):
for current in ({}, {'ok': False, 'error': 'offline'}, {'ok': True, 'state': 'unexpected'},
{'ok': True, 'state': 'failed'},
{'ok': True, 'state': 'succeeded', 'errors': ['battle failed']},
{'ok': True, 'state': 'succeeded', 'pending_restore': ['x.restore.json']}):
with self.subTest(current=current):
result, bridge = self.delegate(current)
self.assertEqual(result['state'], 'requires_analysis')
self.assertEqual(bridge.call.call_count, 1)
def test_resume_checks_errors_without_launching_another_run(self):
current = {'ok': True, 'state': 'succeeded', 'task': 'mirror', 'errors': ['battle failed']}
result, bridge = self.delegate(current, resume=True)
self.assertEqual(result['state'], 'requires_analysis')
self.assertEqual(bridge.call.call_count, 1)
def test_resume_does_not_accept_a_different_task_as_its_result(self):
save_json(self.home / 'mirror_current_run.json', {
'workflow': 'aalc', 'status': 'delegated', 'task_log': 'original.log'})
result, bridge = self.delegate({'ok': True, 'state': 'succeeded', 'task': 'mirror',
'log': 'another.log'}, resume=True)
self.assertEqual(result['reason'], 'task_changed')
self.assertEqual(bridge.call.call_count, 1)
def test_unfinished_speedrun_blocks_unrequested_workflow_switch(self):
save_json(self.home / 'mirror_current_run.json', {'status': 'requires_decision'})
with mock.patch.object(agent, 'Bridge') as factory:
result = agent.delegate_mirror()
self.assertEqual(result['reason'], 'unfinished_speedrun')
factory.assert_not_called()
def test_stop_marker_prevents_even_connecting(self):
agent.STOP_FILE.touch()
with mock.patch.object(agent, 'Bridge') as factory:
self.assertEqual(agent.delegate_mirror()['state'], 'stopped')
factory.assert_not_called()
def test_timeout_after_start_is_reported_without_retry_or_clicks(self):
bridge = mock.Mock()
error = SessionTimeout('reply lost')
error.tool = 'run_task'
bridge.call.side_effect = [{'ok': True, 'state': 'idle'}, error]
with mock.patch.object(agent, 'Bridge', return_value=bridge):
result = agent.delegate_mirror()
self.assertEqual(result['state'], 'requires_analysis')
self.assertTrue(result['outcome_unknown'])
self.assertEqual(result['pending_tool'], 'run_task')
self.assertEqual(bridge.call.call_count, 2)
bridge.close.assert_called_once()
def test_plain_cli_takes_aalc_path_without_model_or_ocr(self):
bridge = mock.Mock()
bridge.call.side_effect = [{'ok': True, 'state': 'idle'}, {'ok': True, 'log': 'mirror.log'}]
release = mock.Mock()
with mock.patch.object(sys, 'argv', ['agent']), \
mock.patch.object(agent, 'Bridge', return_value=bridge), \
mock.patch.object(agent, 'acquire_single_instance', return_value=release), \
mock.patch.object(agent, 'DecisionRouter') as router, \
contextlib.redirect_stdout(io.StringIO()):
agent.main()
router.assert_not_called()
release.assert_called_once()
result = json.loads((self.home / 'aalc_mirror_status.json').read_text())
self.assertEqual(result['state'], 'delegated')
self.assertEqual([call.args[0] for call in bridge.call.call_args_list], ['task_status', 'run_task'])
if __name__ == '__main__':
unittest.main()