test_battle_wait.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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, options=None):
  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, options=None):
  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. bw_ctx.success = True
  67. return HookSignal.DONE
  68. battle_wait = OrderedBattleWait()
  69. plan = BattleWaitPlan('setup_record', 'completion_finish')
  70. assert battle_wait.battle_wait_with_strategy(battle_wait_plan=plan) is True
  71. assert battle_wait.events == ['setup', 'screenshot', 'completion']
  72. def test_custom_hook_is_resolved_and_executed_in_the_configured_sequence():
  73. class CustomBattleWait(BattleWait):
  74. def __init__(self):
  75. self.events = []
  76. def screenshot(self):
  77. pass
  78. def _bw_setup_record(self, bw_ctx):
  79. self.events.append('setup')
  80. return HookSignal.DONE
  81. def _bw_yyy_record(self, bw_ctx):
  82. self.events.append('yyy')
  83. return HookSignal.CONTINUE
  84. def _bw_completion_finish(self, bw_ctx):
  85. self.events.append('completion')
  86. bw_ctx.success = True
  87. return HookSignal.DONE
  88. battle_wait = CustomBattleWait()
  89. plan = BattleWaitPlan(
  90. 'setup_record',
  91. 'yyy_record',
  92. 'completion_finish',
  93. sequence='yyy > completion > interrupt > success > failure > idle',
  94. )
  95. assert battle_wait.battle_wait_with_strategy(battle_wait_plan=plan) is True
  96. assert battle_wait.events == ['setup', 'yyy', 'completion']
  97. def test_custom_sequence_controls_hook_order():
  98. plan = BattleWaitPlan(
  99. 'yyy_default',
  100. sequence='failure > yyy > completion > interrupt > success > idle',
  101. )
  102. assert plan.sequence_function_names() == [
  103. '_bw_failure_default',
  104. '_bw_yyy_default',
  105. '_bw_completion_default',
  106. '_bw_interrupt_default',
  107. '_bw_success_default',
  108. '_bw_idle_default',
  109. ]
  110. def test_custom_events_without_sequence_are_inserted_before_idle_in_argument_order():
  111. plan = BattleWaitPlan('yyy_default', 'abcd_edf')
  112. assert plan.sequence == 'completion>interrupt>success>failure>yyy>abcd>idle'
  113. def test_dynamic_override_does_not_modify_the_default_plan():
  114. strategy = battle_wait_strategy('success_default')
  115. @strategy
  116. def battle_wait(owner, *, battle_wait_plan, options=None):
  117. return battle_wait_plan
  118. default_plan = battle_wait_strategy.battle_wait_plan
  119. overridden_plan = battle_wait(object(), random_click_swipt_enable=True)
  120. assert overridden_plan is not default_plan
  121. assert overridden_plan.randomclick == 'default'
  122. assert not hasattr(default_plan, 'randomclick')
  123. assert battle_wait_strategy.battle_wait_plan is default_plan
  124. def test_dynamic_override_is_only_valid_for_the_current_call():
  125. strategy = battle_wait_strategy('success_default')
  126. @strategy
  127. def battle_wait(owner, *, battle_wait_plan, options=None):
  128. return battle_wait_plan
  129. battle_wait(object(), random_click_swipt_enable=True)
  130. plan_without_override = battle_wait(object(), random_click_swipt_enable=False)
  131. assert not hasattr(plan_without_override, 'randomclick')
  132. # options 与 plan 同语义: 装饰器覆盖全局, with_options 临时覆盖并在退出时还原(__exit__ 还原 _previous_options)。
  133. # 本测试同时锁定 __exit__ 的还原行为 —— 若还原缺失, 最后一组断言会失败。
  134. def test_decorator_options_and_with_options_are_scoped_to_the_current_call():
  135. received_options = []
  136. decorator_options = {
  137. 'completion': {'source': 'decorator'},
  138. 'success': {'excludes': ['C_REWARD_1']},
  139. }
  140. context_options = {
  141. 'success': {'excludes': ['C_END_MESSAGE_RIGHT_TOP']},
  142. }
  143. strategy = battle_wait_strategy(
  144. 'setup_record', 'completion_record', options=decorator_options
  145. )
  146. class OptionBattleWait(BattleWait):
  147. def screenshot(self):
  148. pass
  149. def _bw_setup_record(self, bw_ctx):
  150. return HookSignal.DONE
  151. def _bw_completion_record(self, bw_ctx):
  152. received_options.append(bw_ctx.options)
  153. bw_ctx.success = True
  154. return HookSignal.DONE
  155. @strategy
  156. def battle_wait(self, *args, **kwargs):
  157. return self.battle_wait_with_strategy(*args, **kwargs)
  158. battle_wait = object.__new__(OptionBattleWait)
  159. # 装饰器覆盖全局: 调用方拿到装饰器的整份 options
  160. assert battle_wait.battle_wait() is True
  161. assert received_options[-1] == decorator_options
  162. with strategy.with_options(context_options):
  163. # with 是整份覆盖, 不是合并: 装饰器的 completion 键也被覆盖掉
  164. assert battle_wait.battle_wait() is True
  165. assert received_options[-1] == context_options
  166. strategy_text = str(strategy)
  167. assert 'options=' in strategy_text
  168. assert 'C_END_MESSAGE_RIGHT_TOP' in strategy_text
  169. # 退出 with 后还原为装饰器覆盖的那份(需要 __exit__ 还原 _previous_options)
  170. assert battle_wait.battle_wait() is True
  171. assert received_options[-1] == decorator_options
  172. assert 'C_REWARD_1' in str(strategy)
  173. # 跨任务(两个装饰器)场景。复现 script.py 的调度时序: 每个任务运行前用 load_module
  174. # 重新执行自己的 script_task.py, 即"装饰器在任务运行时才生效, 后加载的任务覆盖前者"。
  175. # 契约: options 与 battle_wait_plan 语义完全一致 —— 都是全局槽位, 后加载装饰器整份覆盖,
  176. # 正在运行的任务拿到的正是自己模块装饰器声明的配置。
  177. def test_cross_task_decorators_switch_plan_and_keep_own_options():
  178. # 任务 A 的装饰器: 默认 success hook + 自己的 options
  179. strategy_a = battle_wait_strategy(
  180. 'success_default',
  181. options={'success': {'market': 'task_a'}},
  182. )
  183. @strategy_a
  184. def battle_wait_a(owner, *, battle_wait_plan, options=None):
  185. # 被装饰后 wrapper 会注入当前生效的 battle_wait_plan / options, 这里原样抛回来断言
  186. return battle_wait_plan, options
  187. # 任务 A 运行: 生效的应是它自己的 plan 和 options
  188. plan_a, options_a = battle_wait_a(object())
  189. assert plan_a.success == 'default'
  190. assert options_a['success']['market'] == 'task_a'
  191. # A 装饰(模块加载)后, 全局生效 plan 和 options 都是 A 的
  192. assert battle_wait_strategy.battle_wait_plan is plan_a
  193. assert battle_wait_strategy.options['success']['market'] == 'task_a'
  194. # 任务 B 后加载并装饰: 不同 success hook + 不同的 options → 整份覆盖全局
  195. strategy_b = battle_wait_strategy(
  196. success='activity',
  197. options={'success': {'market': 'task_b'}},
  198. )
  199. @strategy_b
  200. def battle_wait_b(owner, *, battle_wait_plan, options=None):
  201. return battle_wait_plan, options
  202. # 核心断言 1: B 装饰(加载)后, 全局生效 plan 和 options 都切成 B 自己的
  203. assert battle_wait_strategy.battle_wait_plan is not plan_a
  204. plan_b, options_b = battle_wait_b(object())
  205. assert plan_b.success == 'activity'
  206. assert battle_wait_strategy.battle_wait_plan is plan_b
  207. assert options_b['success']['market'] == 'task_b'
  208. assert battle_wait_strategy.options['success']['market'] == 'task_b'
  209. # 观察 A 的"滞后"行为: 此刻全局已被 B 覆盖, A 再次调用跟随全局(B 的),
  210. # options 和 plan 一样是"后加载者胜", 不再保留 A 自己的。
  211. plan_a2, options_a2 = battle_wait_a(object())
  212. assert plan_a2 is plan_b
  213. assert options_a2['success']['market'] == 'task_b'
  214. # 未声明 options 的装饰器应重置回 _DEFAULT_OPTIONS, 而不是继承上一个任务的 options。
  215. # 这是"后加载者覆盖 + 未声明回默认"的兜底, 避免任务链里的 options 漂移。
  216. def test_cross_task_options_do_not_leak_between_tasks():
  217. # 任务 A: 声明 options → 覆盖全局
  218. strategy_a = battle_wait_strategy(
  219. 'success_default',
  220. options={'success': {'market': 'task_a'}},
  221. )
  222. @strategy_a
  223. def battle_wait_a(owner, *, battle_wait_plan, options=None):
  224. return battle_wait_plan, options
  225. _, options_a = battle_wait_a(object())
  226. assert options_a['success']['market'] == 'task_a'
  227. # 任务 B: 不声明 options → __call__ 重置回 _DEFAULT_OPTIONS, 不残留 A 的
  228. strategy_b = battle_wait_strategy(success='activity')
  229. @strategy_b
  230. def battle_wait_b(owner, *, battle_wait_plan, options=None):
  231. return battle_wait_plan, options
  232. plan_b, options_b = battle_wait_b(object())
  233. assert plan_b.success == 'activity'
  234. assert 'task_a' not in options_b.get('success', {})
  235. # 全局槽位也跟着回到默认(与 plan 的"后加载者覆盖"一致)
  236. assert battle_wait_strategy.options == battle_wait_strategy._DEFAULT_OPTIONS
  237. assert options_b == battle_wait_strategy._DEFAULT_OPTIONS