test_battle_flow.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pytest
  2. from module.exception import GameStuckError
  3. from tasks.Component.GeneralBattle.battle_wait import runtime
  4. from tasks.MetaDemon import script_task
  5. @pytest.mark.parametrize('win', [True, False])
  6. def test_boss_battle_unlocks_and_finishes_on_result_screen(monkeypatch, win):
  7. monkeypatch.setattr(runtime, 'pub_ctx', None)
  8. monkeypatch.setattr(runtime, 'pri_ctx', {})
  9. monkeypatch.setattr(runtime, 'task_owner', None)
  10. monkeypatch.setattr(script_task, 'sleep', lambda _: None)
  11. task = script_task.ScriptTask.__new__(script_task.ScriptTask)
  12. result = task.I_MD_WIN if win else task.I_MD_BATTLE_FAILURE
  13. visible = {task.I_TEAM_LOCK.name, task.I_BATTLE_DEMON.name}
  14. clicks = []
  15. frames = iter(range(18))
  16. task.screenshot = lambda: next(frames)
  17. task.appear = lambda rule, **kwargs: rule.name in visible
  18. task.refresh_long_time = lambda: None
  19. task.is_in_real_battle = lambda *args: False
  20. def click(rule, **kwargs):
  21. clicks.append(rule.name)
  22. visible.remove(rule.name)
  23. if rule is task.I_TEAM_LOCK:
  24. visible.add(task.I_TEAM_UNLOCK.name)
  25. elif rule is task.I_BATTLE_DEMON:
  26. visible.add(result.name)
  27. else:
  28. assert rule is result
  29. visible.add(task.I_FIND_DEMON.name)
  30. return True
  31. task.click = click
  32. task.appear_then_click = lambda rule, **kwargs: task.appear(rule) and task.click(rule)
  33. assert task.battle_boss() is win
  34. assert clicks == [task.I_TEAM_LOCK.name, task.I_BATTLE_DEMON.name, result.name]
  35. assert runtime.pub_ctx.per_task.count == 1
  36. def test_missing_unlock_confirmation_stops_before_challenging():
  37. task = object.__new__(script_task.ScriptTask)
  38. task.ui_click = lambda *args, **kwargs: False
  39. with pytest.raises(GameStuckError, match='unlock'):
  40. task.battle_boss()