test_battle_wait.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import pytest
  2. from tasks.Component.GeneralBattle.battle_wait import (
  3. BattleWait,
  4. BattleWaitPlan,
  5. HookSignal,
  6. battle_wait_strategy,
  7. )
  8. @pytest.fixture(autouse=True)
  9. def reset_battle_wait_plan(monkeypatch):
  10. monkeypatch.setattr(battle_wait_strategy, 'battle_wait_plan', None)
  11. monkeypatch.setattr(battle_wait_strategy, 'options', None)
  12. def test_default_plan_contains_default_hooks_and_sequence():
  13. plan = BattleWaitPlan()
  14. assert tuple(getattr(plan, hook) for hook in BattleWaitPlan.HOOKS_DEFAULT) == (
  15. 'default',
  16. 'default',
  17. 'default',
  18. 'default',
  19. 'default',
  20. 'default',
  21. )
  22. assert plan.sequence == 'completion>interrupt>success>failure>idle'
  23. assert plan.function_setup_name == '_bw_setup_default'
  24. def test_decorator_passes_its_plan_to_the_wrapped_function():
  25. strategy = battle_wait_strategy('reserve_default', 'idle_default', failure='custom')
  26. @strategy
  27. def battle_wait(owner, *, battle_wait_plan):
  28. return battle_wait_plan
  29. plan = battle_wait(object())
  30. assert plan.reserve == 'default'
  31. assert plan.idle == 'default'
  32. assert plan.failure == 'custom'
  33. def test_with_context_uses_a_temporary_plan_and_restores_the_default_plan():
  34. strategy = battle_wait_strategy('success_default')
  35. @strategy
  36. def battle_wait(owner, *, battle_wait_plan):
  37. return battle_wait_plan
  38. default_plan = battle_wait_strategy.battle_wait_plan
  39. with battle_wait_strategy('success_default', failure='custom'):
  40. temporary_plan = battle_wait(object())
  41. assert temporary_plan.failure == 'custom'
  42. assert battle_wait_strategy.battle_wait_plan is default_plan
  43. assert battle_wait(object()) is default_plan
  44. def test_event_and_strategy_can_be_configured_with_both_supported_forms():
  45. plan = BattleWaitPlan('yyy_default', abcd='edf')
  46. assert plan.yyy == 'default'
  47. assert plan.abcd == 'edf'
  48. assert plan.sequence_function_names()[4:6] == [
  49. '_bw_yyy_default',
  50. '_bw_abcd_edf',
  51. ]
  52. def test_an_event_cannot_be_configured_with_two_strategies():
  53. with pytest.raises(ValueError, match="configured more than once"):
  54. BattleWaitPlan('success_default', success='custom')
  55. def test_setup_runs_before_the_wait_loop():
  56. class OrderedBattleWait(BattleWait):
  57. def __init__(self):
  58. self.events = []
  59. def screenshot(self):
  60. self.events.append('screenshot')
  61. def _bw_setup_record(self, bw_ctx):
  62. self.events.append('setup')
  63. return HookSignal.DONE
  64. def _bw_completion_finish(self, bw_ctx):
  65. self.events.append('completion')
  66. return HookSignal.DONE
  67. battle_wait = OrderedBattleWait()
  68. plan = BattleWaitPlan('setup_record', 'completion_finish')
  69. assert battle_wait.battle_wait_with_strategy(battle_wait_plan=plan) is True
  70. assert battle_wait.events == ['setup', 'screenshot', 'completion']
  71. def test_custom_hook_is_resolved_and_executed_in_the_configured_sequence():
  72. class CustomBattleWait(BattleWait):
  73. def __init__(self):
  74. self.events = []
  75. def screenshot(self):
  76. pass
  77. def _bw_setup_record(self, bw_ctx):
  78. self.events.append('setup')
  79. return HookSignal.DONE
  80. def _bw_yyy_record(self, bw_ctx):
  81. self.events.append('yyy')
  82. return HookSignal.CONTINUE
  83. def _bw_completion_finish(self, bw_ctx):
  84. self.events.append('completion')
  85. return HookSignal.DONE
  86. battle_wait = CustomBattleWait()
  87. plan = BattleWaitPlan(
  88. 'setup_record',
  89. 'yyy_record',
  90. 'completion_finish',
  91. sequence='yyy > completion > interrupt > success > failure > idle',
  92. )
  93. assert battle_wait.battle_wait_with_strategy(battle_wait_plan=plan) is True
  94. assert battle_wait.events == ['setup', 'yyy', 'completion']
  95. def test_custom_sequence_controls_hook_order():
  96. plan = BattleWaitPlan(
  97. 'yyy_default',
  98. sequence='failure > yyy > completion > interrupt > success > idle',
  99. )
  100. assert plan.sequence_function_names() == [
  101. '_bw_failure_default',
  102. '_bw_yyy_default',
  103. '_bw_completion_default',
  104. '_bw_interrupt_default',
  105. '_bw_success_default',
  106. '_bw_idle_default',
  107. ]
  108. def test_custom_events_without_sequence_are_inserted_before_idle_in_argument_order():
  109. plan = BattleWaitPlan('yyy_default', 'abcd_edf')
  110. assert plan.sequence == 'completion>interrupt>success>failure>yyy>abcd>idle'
  111. def test_dynamic_override_does_not_modify_the_default_plan():
  112. strategy = battle_wait_strategy('success_default')
  113. @strategy
  114. def battle_wait(owner, *, battle_wait_plan):
  115. return battle_wait_plan
  116. default_plan = battle_wait_strategy.battle_wait_plan
  117. overridden_plan = battle_wait(object(), random_click_swipt_enable=True)
  118. assert overridden_plan is not default_plan
  119. assert overridden_plan.randomclick == 'default'
  120. assert not hasattr(default_plan, 'randomclick')
  121. assert battle_wait_strategy.battle_wait_plan is default_plan
  122. def test_dynamic_override_is_only_valid_for_the_current_call():
  123. strategy = battle_wait_strategy('success_default')
  124. @strategy
  125. def battle_wait(owner, *, battle_wait_plan):
  126. return battle_wait_plan
  127. battle_wait(object(), random_click_swipt_enable=True)
  128. plan_without_override = battle_wait(object(), random_click_swipt_enable=False)
  129. assert not hasattr(plan_without_override, 'randomclick')
  130. # 验证单层装饰器保存自己的 options,with_options() 只在上下文内临时覆盖,退出后恢复。
  131. def test_decorator_options_and_with_options_are_scoped_to_the_current_call():
  132. received_options = []
  133. decorator_options = {
  134. 'completion': {'source': 'decorator'},
  135. 'success': {'excludes': ['C_REWARD_1']},
  136. }
  137. context_options = {
  138. 'success': {'excludes': ['C_END_MESSAGE_RIGHT_TOP']},
  139. }
  140. strategy = battle_wait_strategy(
  141. 'setup_record', 'completion_record', options=decorator_options
  142. )
  143. class OptionBattleWait(BattleWait):
  144. def screenshot(self):
  145. pass
  146. def _bw_setup_record(self, bw_ctx):
  147. return HookSignal.DONE
  148. def _bw_completion_record(self, bw_ctx):
  149. received_options.append(bw_ctx.options)
  150. return HookSignal.DONE
  151. @strategy
  152. def battle_wait(self, *args, **kwargs):
  153. return self.battle_wait_with_strategy(*args, **kwargs)
  154. battle_wait = object.__new__(OptionBattleWait)
  155. assert battle_wait.battle_wait() is True
  156. assert received_options[-1] == decorator_options
  157. with strategy.with_options(context_options):
  158. assert battle_wait.battle_wait() is True
  159. assert received_options[-1] == {
  160. 'completion': {'source': 'decorator'},
  161. 'success': {'excludes': ['C_END_MESSAGE_RIGHT_TOP']},
  162. }
  163. strategy_text = str(strategy)
  164. assert 'options=' in strategy_text
  165. assert 'C_END_MESSAGE_RIGHT_TOP' in strategy_text
  166. assert battle_wait.battle_wait() is True
  167. assert received_options[-1] == decorator_options
  168. assert 'C_REWARD_1' in str(strategy)