| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- from types import SimpleNamespace
- from unittest.mock import Mock
- import pytest
- from module.atom.image import RuleImage
- from module.atom.ocr import RuleOcr
- from module.base import timer
- from tasks.Restart.login import LoginHandler
- from module.exception import GameStuckError
- def test_desktop_reappearing_login_popup_is_bounded(monkeypatch):
- task = LoginReplay(monkeypatch, lambda task: task.courtyard())
- task.device.is_desktop = True
- task.device.find_desktop_login_popup = lambda: 123
- task.device.desktop_confirm_login_popup = Mock(return_value=True)
- with pytest.raises(GameStuckError, match='reappeared'):
- task._app_handle_login()
- assert task.device.desktop_confirm_login_popup.call_count == 5
- assert task.frames == 0
- def test_desktop_popup_arriving_during_screenshot_is_handled_first(monkeypatch):
- task = LoginReplay(monkeypatch, lambda task: task.courtyard())
- task.device.is_desktop = True
- closed = []
- task.device.find_desktop_login_popup = lambda: 123 if task.frames and not closed else 0
- task.device.desktop_confirm_login_popup = lambda: closed.append(task.frames) or True
- assert task._app_handle_login() is True
- assert closed == [1]
- def test_desktop_popup_that_cannot_close_stops_login(monkeypatch):
- task = LoginReplay(monkeypatch, lambda task: task.courtyard())
- task.device.is_desktop = True
- task.device.find_desktop_login_popup = lambda: 123
- task.device.desktop_confirm_login_popup = lambda: False
- with pytest.raises(GameStuckError, match='cannot be closed'):
- task._app_handle_login()
- assert task.frames == 0
- class LoginReplay(LoginHandler):
- """Run the real login loop with deterministic screenshots and a fake clock."""
- def __init__(self, monkeypatch, next_screen):
- self.interval_timer = {}
- self.now = 100.0
- self.frames = 0
- self.clicks = []
- self.next_screen = next_screen
- self.device = SimpleNamespace(
- is_desktop=False,
- image=set(),
- screenshot=self.take_screenshot,
- click=self.record_click,
- stuck_record_add=lambda name: None,
- get_orientation=lambda: None,
- check_screen_size_sample=lambda: True,
- )
- monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
- monkeypatch.setattr(
- RuleImage, 'match', lambda asset, image, **kwargs: asset.name in image
- )
- def ocr(asset, image, **kwargs):
- if asset.name not in image:
- return (0, 0, 0, 0) if asset is self.O_LOGIN_SPECIFIC_SERVE else ''
- if asset is self.O_LOGIN_SPECIFIC_SERVE:
- return (110, 120, 100, 30)
- return asset.keyword
- monkeypatch.setattr(RuleOcr, 'ocr', ocr)
- def take_screenshot(self):
- self.frames += 1
- assert self.frames <= 80, 'Login did not reach the courtyard'
- self.now += 0.5
- self.device.image = {asset.name for asset in self.next_screen(self)}
- def record_click(self, x, y, control_name=None):
- self.clicks.append((control_name, self.frames, self.now))
- def courtyard(self):
- return [self.I_BUFF_1, self.I_MAIN_GOTO_SHIKIGAMI_RECORDS]
- def login_screen(self, alternate=False):
- button = self.O_LOGIN_ENTER_GAME if alternate else self.O_LOGIN_ENTER_GAME_ORIGIN
- return [self.I_LOGIN_8, button]
- @pytest.mark.parametrize('alternate_on_retry', [False, True])
- def test_ignored_enter_game_click_is_retried_on_a_new_frame(monkeypatch, alternate_on_retry):
- def next_screen(task):
- if len(task.clicks) >= 2:
- return task.courtyard()
- return task.login_screen(alternate=bool(task.clicks) and alternate_on_retry)
- task = LoginReplay(monkeypatch, next_screen)
- assert task._app_handle_login() is True
- assert len(task.clicks) == 2
- first, second = task.clicks
- assert first[0] == task.O_LOGIN_ENTER_GAME_ORIGIN.name
- expected_retry = task.O_LOGIN_ENTER_GAME if alternate_on_retry else task.O_LOGIN_ENTER_GAME_ORIGIN
- assert second[0] == expected_retry.name
- assert second[1] > first[1]
- assert second[2] - first[2] >= 3
- def test_direct_courtyard_entry_does_not_wait_for_character_selection(monkeypatch):
- task = LoginReplay(
- monkeypatch,
- lambda task: task.courtyard() if task.clicks else task.login_screen(),
- )
- assert task._app_handle_login() is True
- assert len(task.clicks) == 1
- # The courtyard is immediately available; only its stability check is needed.
- assert task.now - task.clicks[0][2] < 3
- def test_character_selection_is_handled_after_enter_game(monkeypatch):
- def next_screen(task):
- clicked = [click[0] for click in task.clicks]
- if task.C_LOGIN_ENSURE_LOGIN_CHARACTER_IN_SAME_SVR.name in clicked:
- return task.courtyard()
- if task.O_LOGIN_ENTER_GAME_ORIGIN.name in clicked:
- return [task.I_LOGIN_SPECIFIC_SERVE, task.O_LOGIN_SPECIFIC_SERVE]
- return task.login_screen()
- task = LoginReplay(monkeypatch, next_screen)
- assert task._app_handle_login() is True
- assert [click[0] for click in task.clicks] == [
- task.O_LOGIN_ENTER_GAME_ORIGIN.name,
- task.O_LOGIN_SPECIFIC_SERVE.name,
- task.C_LOGIN_ENSURE_LOGIN_CHARACTER_IN_SAME_SVR.name,
- ]
- def test_server_selection_is_left_before_processing_a_new_login_frame(monkeypatch):
- def next_screen(task):
- if not task.clicks:
- return [task.I_CHARACTARS, *task.login_screen()]
- if len(task.clicks) == 1:
- return task.login_screen()
- return task.courtyard()
- task = LoginReplay(monkeypatch, next_screen)
- assert task._app_handle_login() is True
- assert len(task.clicks) == 2
- assert task.clicks[1][0] == task.O_LOGIN_ENTER_GAME_ORIGIN.name
- assert task.clicks[1][1] > task.clicks[0][1]
- def test_loading_frames_do_not_trigger_another_enter_game_click(monkeypatch):
- def next_screen(task):
- if not task.clicks:
- return task.login_screen()
- if task.now - task.clicks[0][2] < 6:
- return []
- return task.courtyard()
- task = LoginReplay(monkeypatch, next_screen)
- assert task._app_handle_login() is True
- assert len(task.clicks) == 1
|