test_login.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from types import SimpleNamespace
  2. from unittest.mock import Mock
  3. import pytest
  4. from module.atom.image import RuleImage
  5. from module.atom.ocr import RuleOcr
  6. from module.base import timer
  7. from tasks.Restart.login import LoginHandler
  8. from module.exception import GameStuckError
  9. def test_desktop_reappearing_login_popup_is_bounded(monkeypatch):
  10. task = LoginReplay(monkeypatch, lambda task: task.courtyard())
  11. task.device.is_desktop = True
  12. task.device.find_desktop_login_popup = lambda: 123
  13. task.device.desktop_confirm_login_popup = Mock(return_value=True)
  14. with pytest.raises(GameStuckError, match='reappeared'):
  15. task._app_handle_login()
  16. assert task.device.desktop_confirm_login_popup.call_count == 5
  17. assert task.frames == 0
  18. def test_desktop_popup_arriving_during_screenshot_is_handled_first(monkeypatch):
  19. task = LoginReplay(monkeypatch, lambda task: task.courtyard())
  20. task.device.is_desktop = True
  21. closed = []
  22. task.device.find_desktop_login_popup = lambda: 123 if task.frames and not closed else 0
  23. task.device.desktop_confirm_login_popup = lambda: closed.append(task.frames) or True
  24. assert task._app_handle_login() is True
  25. assert closed == [1]
  26. def test_desktop_popup_that_cannot_close_stops_login(monkeypatch):
  27. task = LoginReplay(monkeypatch, lambda task: task.courtyard())
  28. task.device.is_desktop = True
  29. task.device.find_desktop_login_popup = lambda: 123
  30. task.device.desktop_confirm_login_popup = lambda: False
  31. with pytest.raises(GameStuckError, match='cannot be closed'):
  32. task._app_handle_login()
  33. assert task.frames == 0
  34. class LoginReplay(LoginHandler):
  35. """Run the real login loop with deterministic screenshots and a fake clock."""
  36. def __init__(self, monkeypatch, next_screen):
  37. self.interval_timer = {}
  38. self.now = 100.0
  39. self.frames = 0
  40. self.clicks = []
  41. self.next_screen = next_screen
  42. self.device = SimpleNamespace(
  43. is_desktop=False,
  44. image=set(),
  45. screenshot=self.take_screenshot,
  46. click=self.record_click,
  47. stuck_record_add=lambda name: None,
  48. get_orientation=lambda: None,
  49. check_screen_size_sample=lambda: True,
  50. )
  51. monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
  52. monkeypatch.setattr(
  53. RuleImage, 'match', lambda asset, image, **kwargs: asset.name in image
  54. )
  55. def ocr(asset, image, **kwargs):
  56. if asset.name not in image:
  57. return (0, 0, 0, 0) if asset is self.O_LOGIN_SPECIFIC_SERVE else ''
  58. if asset is self.O_LOGIN_SPECIFIC_SERVE:
  59. return (110, 120, 100, 30)
  60. return asset.keyword
  61. monkeypatch.setattr(RuleOcr, 'ocr', ocr)
  62. def take_screenshot(self):
  63. self.frames += 1
  64. assert self.frames <= 80, 'Login did not reach the courtyard'
  65. self.now += 0.5
  66. self.device.image = {asset.name for asset in self.next_screen(self)}
  67. def record_click(self, x, y, control_name=None):
  68. self.clicks.append((control_name, self.frames, self.now))
  69. def courtyard(self):
  70. return [self.I_BUFF_1, self.I_MAIN_GOTO_SHIKIGAMI_RECORDS]
  71. def login_screen(self, alternate=False):
  72. button = self.O_LOGIN_ENTER_GAME if alternate else self.O_LOGIN_ENTER_GAME_ORIGIN
  73. return [self.I_LOGIN_8, button]
  74. @pytest.mark.parametrize('alternate_on_retry', [False, True])
  75. def test_ignored_enter_game_click_is_retried_on_a_new_frame(monkeypatch, alternate_on_retry):
  76. def next_screen(task):
  77. if len(task.clicks) >= 2:
  78. return task.courtyard()
  79. return task.login_screen(alternate=bool(task.clicks) and alternate_on_retry)
  80. task = LoginReplay(monkeypatch, next_screen)
  81. assert task._app_handle_login() is True
  82. assert len(task.clicks) == 2
  83. first, second = task.clicks
  84. assert first[0] == task.O_LOGIN_ENTER_GAME_ORIGIN.name
  85. expected_retry = task.O_LOGIN_ENTER_GAME if alternate_on_retry else task.O_LOGIN_ENTER_GAME_ORIGIN
  86. assert second[0] == expected_retry.name
  87. assert second[1] > first[1]
  88. assert second[2] - first[2] >= 3
  89. def test_direct_courtyard_entry_does_not_wait_for_character_selection(monkeypatch):
  90. task = LoginReplay(
  91. monkeypatch,
  92. lambda task: task.courtyard() if task.clicks else task.login_screen(),
  93. )
  94. assert task._app_handle_login() is True
  95. assert len(task.clicks) == 1
  96. # The courtyard is immediately available; only its stability check is needed.
  97. assert task.now - task.clicks[0][2] < 3
  98. def test_character_selection_is_handled_after_enter_game(monkeypatch):
  99. def next_screen(task):
  100. clicked = [click[0] for click in task.clicks]
  101. if task.C_LOGIN_ENSURE_LOGIN_CHARACTER_IN_SAME_SVR.name in clicked:
  102. return task.courtyard()
  103. if task.O_LOGIN_ENTER_GAME_ORIGIN.name in clicked:
  104. return [task.I_LOGIN_SPECIFIC_SERVE, task.O_LOGIN_SPECIFIC_SERVE]
  105. return task.login_screen()
  106. task = LoginReplay(monkeypatch, next_screen)
  107. assert task._app_handle_login() is True
  108. assert [click[0] for click in task.clicks] == [
  109. task.O_LOGIN_ENTER_GAME_ORIGIN.name,
  110. task.O_LOGIN_SPECIFIC_SERVE.name,
  111. task.C_LOGIN_ENSURE_LOGIN_CHARACTER_IN_SAME_SVR.name,
  112. ]
  113. def test_server_selection_is_left_before_processing_a_new_login_frame(monkeypatch):
  114. def next_screen(task):
  115. if not task.clicks:
  116. return [task.I_CHARACTARS, *task.login_screen()]
  117. if len(task.clicks) == 1:
  118. return task.login_screen()
  119. return task.courtyard()
  120. task = LoginReplay(monkeypatch, next_screen)
  121. assert task._app_handle_login() is True
  122. assert len(task.clicks) == 2
  123. assert task.clicks[1][0] == task.O_LOGIN_ENTER_GAME_ORIGIN.name
  124. assert task.clicks[1][1] > task.clicks[0][1]
  125. def test_loading_frames_do_not_trigger_another_enter_game_click(monkeypatch):
  126. def next_screen(task):
  127. if not task.clicks:
  128. return task.login_screen()
  129. if task.now - task.clicks[0][2] < 6:
  130. return []
  131. return task.courtyard()
  132. task = LoginReplay(monkeypatch, next_screen)
  133. assert task._app_handle_login() is True
  134. assert len(task.clicks) == 1