test_switch_onmyoji.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """SwitchOnmyoji must have a bounded exit path.
  2. Both loops in `SwitchOnmyoji.switch_role` used to be unconditional `while True`.
  3. When the onmyoji/hero tab cannot be reached (stale asset coordinates after a
  4. game UI update, emulator showing a different page, ...), they kept clicking
  5. forever until the global `GameStuckError` watchdog fired roughly two minutes
  6. later, discarding all context about what actually went wrong.
  7. These tests pin the exit conditions: a loop that never finds its target has to
  8. raise a named, catchable `ScriptError` instead of spinning, and the happy path
  9. must keep working unchanged.
  10. """
  11. import pytest
  12. from module.base.timer import Timer
  13. from module.exception import ScriptError
  14. from tasks.Component.SwitchOnmyoji.config import Onmyoji
  15. from tasks.Component.SwitchOnmyoji.switch_onmyoji import SwitchOnmyoji
  16. class ScriptedSwitch(SwitchOnmyoji):
  17. """Double that replays a fixed sequence of `appear` results.
  18. `screenshot` and every click are no-ops, so the test exercises the real
  19. loop control flow of `switch_role` without a Config, a Device or a game.
  20. """
  21. def __init__(self, appear_results, timeout=0):
  22. # deliberately skip BaseTask.__init__: it needs a real Config/Device
  23. self._appear_results = list(appear_results)
  24. self._appear_calls = 0
  25. self.switch_clicks = 0
  26. self.battle_clicks = 0
  27. self.ui_clicks = 0
  28. # override the real 20s guards so the test does not have to wait
  29. self.SWITCH_TAB_TIMEOUT = timeout
  30. self.SWITCH_BATTLE_TIMEOUT = timeout
  31. def _next_appear(self, button):
  32. self._appear_calls += 1
  33. if not self._appear_results:
  34. return False
  35. return self._appear_results.pop(0)
  36. def screenshot(self):
  37. pass
  38. def appear(self, button, interval=None, **kwargs):
  39. return self._next_appear(button)
  40. def appear_then_click(self, button, interval=None, **kwargs):
  41. self.switch_clicks += 1
  42. return True
  43. def click(self, button, interval=None, **kwargs):
  44. self.battle_clicks += 1
  45. return True
  46. def ui_click(self, button1, button2, interval=None, **kwargs):
  47. self.ui_clicks += 1
  48. return True
  49. def test_switch_role_raises_when_battle_list_never_appears():
  50. task = ScriptedSwitch(appear_results=[])
  51. with pytest.raises(ScriptError):
  52. task.switch_role(role=None, battle_dict={'x': object()}, check_img=object())
  53. def test_switch_role_raises_when_check_icon_never_appears():
  54. # battle icon visible, confirm icon never does
  55. task = ScriptedSwitch(appear_results=[True])
  56. with pytest.raises(ScriptError):
  57. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  58. def test_switch_role_still_switches_when_the_flow_is_healthy():
  59. # appear order: battle icon, then confirm icon -> no extra clicks needed
  60. task = ScriptedSwitch(appear_results=[True, True])
  61. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  62. assert task.switch_clicks == 0
  63. assert task.battle_clicks == 0
  64. assert task.ui_clicks == 0
  65. def test_switch_role_clicks_the_battle_icon_when_confirm_icon_is_absent():
  66. # appear order: battle icon, not-confirm, battle icon -> ui_click back path
  67. task = ScriptedSwitch(appear_results=[True, False, True])
  68. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  69. assert task.switch_clicks == 0
  70. assert task.ui_clicks == 1
  71. def test_timeouts_are_not_shared_between_instances():
  72. a = ScriptedSwitch(appear_results=[])
  73. b = ScriptedSwitch(appear_results=[])
  74. assert a.SWITCH_TAB_TIMEOUT is not None
  75. assert b.SWITCH_TAB_TIMEOUT is not None