test_battle_integration.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from types import SimpleNamespace
  2. import pytest
  3. from tasks.ActivityShikigami.config import ActivityShikigami
  4. from tasks.ActivityShikigami.script_task import ScriptTask
  5. from tasks.Component.GeneralBattle.battle import Battle
  6. from tasks.Component.GeneralBattle.battle_wait import battle_wait_strategy, runtime
  7. @pytest.fixture(autouse=True)
  8. def isolated_runtime(monkeypatch):
  9. monkeypatch.setattr(runtime, 'task_owner', None)
  10. monkeypatch.setattr(runtime, 'pub_ctx', None)
  11. monkeypatch.setattr(runtime, 'pri_ctx', {})
  12. monkeypatch.setattr('tasks.Component.GeneralBattle.battle_wait.random.random', lambda: 1)
  13. class ActivityReplay(ScriptTask):
  14. """Exercise the real activity entry point and battle hooks without a device."""
  15. def __init__(self):
  16. self.conf = ActivityShikigami()
  17. self.conf.general_climb.run_sequence = 'pass,ap'
  18. self.run_idx = 0
  19. self.frames = 0
  20. self.result_visible = True
  21. self.win = True
  22. self.clicks = []
  23. self.device = SimpleNamespace(
  24. stuck_record_add=lambda name: None,
  25. click_record_clear=lambda: None,
  26. )
  27. def screenshot(self):
  28. self.frames += 1
  29. assert self.frames < 40, 'Activity battle did not settle'
  30. def is_in_battle(self, is_screenshot=True):
  31. return True
  32. def is_in_real_battle(self, is_screenshot=True):
  33. return False
  34. def appear(self, asset, **kwargs):
  35. result = self.I_UI_REWARD if self.win else self.I_FALSE
  36. return self.result_visible and asset is result
  37. def appear_then_click(self, asset, **kwargs):
  38. return False
  39. def click(self, asset, **kwargs):
  40. self.clicks.append(asset.name)
  41. self.result_visible = False
  42. def ui_click_until_disappear(self, asset, **kwargs):
  43. self.click(asset)
  44. def loadout_show(self, *args):
  45. pass
  46. def state_show(self):
  47. pass
  48. @pytest.mark.parametrize('win', [True, False])
  49. def test_activity_entry_uses_its_reward_strategy_and_counts_success_only(win):
  50. task = ActivityReplay()
  51. task.win = win
  52. # Decorating another task later must not replace Activity's selected hooks.
  53. battle_wait_strategy(success='default')(lambda owner, **kwargs: None)
  54. assert ScriptTask.battle_wait is Battle.battle_wait
  55. assert task.start_battle() is win
  56. assert task.count_map == {'pass': int(win), 'ap': 0}
  57. assert task.battle_count_reached(1)
  58. assert task.clicks == ['exclude_click_activity' if win else task.I_FALSE.name]
  59. assert battle_wait_strategy._context.get() is None
  60. def test_switching_climb_type_preserves_counts_and_resets_battle_state():
  61. task = ActivityReplay()
  62. assert task.start_battle() is True
  63. assert task.switch_next() is True
  64. assert task.count_map == {'pass': 1, 'ap': 0}
  65. assert not task.battle_count_reached(1)
  66. task.result_visible = True
  67. assert task.start_battle() is True
  68. assert task.count_map == {'pass': 1, 'ap': 1}
  69. assert task.switch_next() is False