test_general_battle.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. from types import SimpleNamespace
  2. import numpy as np
  3. import pytest
  4. from module.config.config import Config
  5. from module.config.config_model import ConfigModel
  6. from tasks.ActivityShikigami.config import GeneralBattleConfig as ActivityBattleConfig
  7. from tasks.Component.GeneralBattle import general_battle as battle_module
  8. from tasks.Component.GeneralBattle.battle import Battle
  9. from tasks.Component.GeneralBattle.config_general_battle import (
  10. GeneralBattleConfig, GreenMarkEnum, GreenMarkType,
  11. )
  12. from tasks.OtherWorldTwilight.config import OtherWorldTwilight, OWTBattleConfig
  13. def test_buff_panel_closes_before_preparation_starts(monkeypatch):
  14. monkeypatch.setattr(battle_module.time, 'sleep', lambda duration: None)
  15. task = object.__new__(battle_module.GeneralBattle)
  16. events = []
  17. panel = [True]
  18. task.screenshot = lambda: None
  19. task.ui_click = lambda *args, **kwargs: events.append('open')
  20. task.appear = lambda asset, **kwargs: asset is task.I_CLOUD and panel[0]
  21. task.soul = lambda enable: events.append(('soul', enable))
  22. def close(asset, **kwargs):
  23. assert asset is task.I_BUFF
  24. events.append('close')
  25. panel[0] = False
  26. return True
  27. def prepare(asset, interval):
  28. assert not panel[0]
  29. assert asset is task.I_PREPARE_HIGHLIGHT
  30. assert interval == 0.8
  31. events.append('prepare')
  32. task.appear_then_click = close
  33. task.ui_click_until_disappear = prepare
  34. task.check_and_open_buff(battle_module.BuffClass.SOUL)
  35. assert events == ['open', ('soul', True), 'close', 'prepare']
  36. def test_general_battle_forwards_name_mark_configuration():
  37. config = OWTBattleConfig(green_enable=True, green_mark_name='缘结神')
  38. calls = []
  39. battle = SimpleNamespace(
  40. current_count=0,
  41. battle_before=lambda *args: None,
  42. is_in_battle=lambda screenshot: True,
  43. green_mark=lambda *args, **kwargs: calls.append((args, kwargs)),
  44. battle_wait=lambda **kwargs: True,
  45. )
  46. assert battle_module.GeneralBattle.run_general_battle(battle, config=config) is True
  47. assert calls == [((True, GreenMarkType.GREEN_LEFT1), {
  48. 'green_mark_type': GreenMarkEnum.NAME, 'green_mark_name': '缘结神',
  49. })]
  50. def test_new_battle_loadout_keeps_name_mark_configuration():
  51. battle = object.__new__(Battle)
  52. config = OWTBattleConfig(green_enable=True, green_mark_name='缘结神')
  53. _, options = battle.loadout_from_config(config)
  54. assert options['green'].green_mark_type == GreenMarkEnum.NAME
  55. assert options['green'].green_mark_name == '缘结神'
  56. def test_green_mark_name_uses_ocr_box_and_roi_offsets():
  57. clicks = []
  58. battle = object.__new__(battle_module.GeneralBattle)
  59. battle.screenshot = lambda: None
  60. battle.device = SimpleNamespace(
  61. image=None, click=lambda *args, **kwargs: clicks.append((args, kwargs)),
  62. )
  63. battle.O_GREEN_MARK_AREA = SimpleNamespace(
  64. roi=(84, 256, 1116, 308),
  65. detect_and_ocr=lambda image: [SimpleNamespace(
  66. ocr_text='缘结神', box=np.array([[120, 30], [160, 30], [160, 50], [120, 50]]),
  67. )],
  68. )
  69. battle.green_mark_name('缘结神')
  70. assert clicks == [((209, 316), {'control_name': '缘结神'})]
  71. def _real_config(task_name='OtherWorldTwilight'):
  72. config = object.__new__(Config)
  73. config.model = ConfigModel.model_construct(
  74. running_task=task_name,
  75. other_world_twilight=OtherWorldTwilight(
  76. general_battle_config=OWTBattleConfig(green_enable=True, green_mark_name='缘结神'),
  77. ),
  78. )
  79. return config
  80. def test_navigation_battle_uses_real_task_config_and_existing_task_runner():
  81. config = _real_config()
  82. assert config.general_battle_config is None
  83. calls = []
  84. task = SimpleNamespace(config=config, device=object(), current_count=3)
  85. def run_general_battle(*, config):
  86. calls.append(config)
  87. task.current_count += 1
  88. return True
  89. task.run_general_battle = run_general_battle
  90. assert battle_module.run_task_or_default_general_battle(task) is True
  91. assert calls == [config.other_world_twilight.general_battle_config]
  92. assert task.current_count == 4
  93. def test_navigation_battle_uses_initialized_conf_before_scheduler_config():
  94. conf = OtherWorldTwilight()
  95. calls = []
  96. task = SimpleNamespace(
  97. config=_real_config(), conf=conf,
  98. run_general_battle=lambda **kwargs: calls.append(kwargs['config']) or True,
  99. )
  100. assert battle_module.run_task_or_default_general_battle(task) is True
  101. assert calls == [conf.general_battle_config]
  102. def test_navigation_battle_uses_session_category_without_scheduler_task():
  103. config = _real_config(task_name='')
  104. calls = []
  105. task = SimpleNamespace(
  106. config=config, navigator=SimpleNamespace(task_category='other_world_twilight'),
  107. run_general_battle=lambda **kwargs: calls.append(kwargs['config']) or True,
  108. )
  109. assert battle_module.run_task_or_default_general_battle(task) is True
  110. assert calls == [config.other_world_twilight.general_battle_config]
  111. @pytest.mark.parametrize('value', [None, ActivityBattleConfig(), {'green_enable': True}])
  112. def test_navigation_battle_rejects_nonstandard_config(value):
  113. calls = []
  114. task = SimpleNamespace(
  115. config=SimpleNamespace(general_battle_config=value),
  116. conf=SimpleNamespace(general_battle_config=value),
  117. run_general_battle=lambda **kwargs: calls.append(kwargs['config']) or True,
  118. )
  119. assert battle_module.run_task_or_default_general_battle(task) is True
  120. assert calls == [None]
  121. def test_navigation_battle_preserves_direct_config_compatibility():
  122. config = GeneralBattleConfig(lock_team_enable=True)
  123. calls = []
  124. task = SimpleNamespace(
  125. config=SimpleNamespace(general_battle_config=config),
  126. run_general_battle=lambda **kwargs: calls.append(kwargs['config']) or True,
  127. )
  128. assert battle_module.run_task_or_default_general_battle(task) is True
  129. assert calls == [config]
  130. def test_navigation_battle_falls_back_for_tasks_without_battle_runner(monkeypatch):
  131. calls = []
  132. config, device = SimpleNamespace(general_battle_config=None), object()
  133. class DefaultBattle:
  134. def __init__(self, **kwargs):
  135. calls.append(kwargs)
  136. def run_general_battle(self, **kwargs):
  137. calls.append(kwargs)
  138. return False
  139. monkeypatch.setattr(battle_module, 'GeneralBattle', DefaultBattle)
  140. assert battle_module.run_task_or_default_general_battle(
  141. SimpleNamespace(config=config, device=device)
  142. ) is False
  143. assert calls == [{'config': config, 'device': device}, {'config': None}]
  144. def test_navigation_battle_does_not_retry_exceptions_from_task_runner():
  145. calls = []
  146. def broken(**kwargs):
  147. calls.append(kwargs)
  148. raise AttributeError('battle failure')
  149. task = SimpleNamespace(config=SimpleNamespace(), run_general_battle=broken)
  150. with pytest.raises(AttributeError, match='battle failure'):
  151. battle_module.run_task_or_default_general_battle(task)
  152. assert len(calls) == 1
  153. def test_default_navigation_battle_keeps_scheduler_identity(monkeypatch):
  154. config = _real_config()
  155. calls = []
  156. def run_battle(self, **kwargs):
  157. calls.append((self.get_task_name(), kwargs['config']))
  158. return True
  159. monkeypatch.setattr(battle_module.GeneralBattle, 'run_general_battle', run_battle)
  160. task = SimpleNamespace(
  161. config=config, device=object(), get_task_name=lambda: 'OtherWorldTwilight',
  162. )
  163. assert battle_module.run_task_or_default_general_battle(task) is True
  164. assert calls == [('OtherWorldTwilight', config.other_world_twilight.general_battle_config)]