test_switch_onmyoji.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 types import SimpleNamespace
  13. import module.base.timer as timer_module
  14. from module.exception import ScriptError
  15. from tasks.Component.SwitchOnmyoji.config import Onmyoji
  16. from tasks.Component.SwitchOnmyoji.switch_onmyoji import SwitchOnmyoji
  17. @pytest.fixture
  18. def clock(monkeypatch):
  19. """Advance only when a screenshot is taken; never depend on wall time."""
  20. clock = SimpleNamespace(now=100.0)
  21. monkeypatch.setattr(timer_module, 'time', SimpleNamespace(time=lambda: clock.now))
  22. return clock
  23. class ScriptedSwitch(SwitchOnmyoji):
  24. """Double that replays a fixed sequence of `appear` results.
  25. `screenshot` and every click are no-ops, so the test exercises the real
  26. loop control flow of `switch_role` without a Config, a Device or a game.
  27. """
  28. def __init__(self, appear_results, clock, timeout=20):
  29. # deliberately skip BaseTask.__init__: it needs a real Config/Device
  30. self._appear_results = list(appear_results)
  31. self._appear_calls = 0
  32. self._clock = clock
  33. self.switch_clicks = 0
  34. self.battle_clicks = 0
  35. self.ui_clicks = 0
  36. # The fake clock makes both real 20s guards run without waiting.
  37. self.SWITCH_TAB_TIMEOUT = timeout
  38. self.SWITCH_BATTLE_TIMEOUT = timeout
  39. def _next_appear(self, button):
  40. self._appear_calls += 1
  41. if not self._appear_results:
  42. return False
  43. return self._appear_results.pop(0)
  44. def screenshot(self):
  45. self._clock.now += 1
  46. def appear(self, button, interval=None, **kwargs):
  47. return self._next_appear(button)
  48. def appear_then_click(self, button, interval=None, **kwargs):
  49. self.switch_clicks += 1
  50. return True
  51. def click(self, button, interval=None, **kwargs):
  52. self.battle_clicks += 1
  53. return True
  54. def ui_click(self, button1, button2, interval=None, **kwargs):
  55. self.ui_clicks += 1
  56. return True
  57. def test_switch_role_raises_when_battle_list_never_appears(clock):
  58. task = ScriptedSwitch(appear_results=[], clock=clock)
  59. with pytest.raises(ScriptError, match='cannot reach the role list'):
  60. task.switch_role(role=None, battle_dict={'x': object()}, check_img=object())
  61. assert task.switch_clicks == 21
  62. assert task.battle_clicks == 0
  63. def test_switch_role_raises_when_check_icon_never_appears(clock):
  64. # battle icon visible, confirm icon never does
  65. task = ScriptedSwitch(appear_results=[True], clock=clock)
  66. with pytest.raises(ScriptError, match='cannot select'):
  67. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  68. assert task.switch_clicks == 0
  69. assert task.battle_clicks == 21
  70. def test_switch_role_still_switches_when_the_flow_is_healthy(clock):
  71. # appear order: battle icon, then confirm icon -> no extra clicks needed
  72. task = ScriptedSwitch(appear_results=[True, True], clock=clock)
  73. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  74. assert task.switch_clicks == 0
  75. assert task.battle_clicks == 0
  76. assert task.ui_clicks == 0
  77. def test_switch_role_clicks_the_battle_icon_when_confirm_icon_is_absent(clock):
  78. # appear order: battle icon, not-confirm, battle icon -> ui_click back path
  79. task = ScriptedSwitch(appear_results=[True, False, True], clock=clock)
  80. task.switch_role(role=Onmyoji.KAGURA, battle_dict={Onmyoji.KAGURA: object()}, check_img=object())
  81. assert task.switch_clicks == 0
  82. assert task.ui_clicks == 1
  83. def test_timeouts_are_not_shared_between_instances(clock):
  84. a = ScriptedSwitch(appear_results=[], clock=clock)
  85. b = ScriptedSwitch(appear_results=[], clock=clock)
  86. assert a.SWITCH_TAB_TIMEOUT is not None
  87. assert b.SWITCH_TAB_TIMEOUT is not None