test_touch_fish.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """Replay the real weekly storage workflow without a device or OCR engine."""
  2. from types import SimpleNamespace
  3. from unittest.mock import Mock
  4. import pytest
  5. from module.exception import GameStuckError
  6. from tasks.WeeklyTrifles import script_task
  7. from tasks.WeeklyTrifles.script_task import ScriptTask
  8. @pytest.fixture(autouse=True)
  9. def advance_time_without_sleeping(monkeypatch):
  10. clock = [0.0]
  11. class ReplayTimer:
  12. def __init__(self, limit):
  13. self.limit = limit
  14. def start(self):
  15. self.deadline = clock[0] + self.limit
  16. return self
  17. def reached(self):
  18. return clock[0] >= self.deadline
  19. def sleep(seconds):
  20. clock[0] += seconds
  21. monkeypatch.setattr(script_task, 'Timer', ReplayTimer)
  22. monkeypatch.setattr(script_task, 'sleep', sleep)
  23. class StorageReplay(ScriptTask):
  24. def __init__(self, frames, *, failed_save_clicks=0, tickets=10, cost=5,
  25. menu_opens=True):
  26. # BaseTask initialization requires a running emulator; keep its workflow.
  27. self.frames = list(frames)
  28. self.frame = set()
  29. self.stage = None
  30. self.menu_opens = menu_opens
  31. self.failed_save_clicks = failed_save_clicks
  32. self.save_attempts = 0
  33. self.clicks = []
  34. self.destinations = []
  35. self.screenshots = 0
  36. self.device = SimpleNamespace(image=None)
  37. self.O_WT_LUCKY_TICKETS = SimpleNamespace(ocr=Mock(return_value=(tickets, 0, tickets)))
  38. self.O_WT_SAVE_COST = SimpleNamespace(ocr=Mock(return_value=cost))
  39. self.config = SimpleNamespace(weekly_trifles=SimpleNamespace(trifles=SimpleNamespace(
  40. share_collect=False, share_area_boss=False, share_secret=False,
  41. save_touch_fish=True, broken_amulet=0,
  42. )))
  43. self.set_next_run = Mock()
  44. def ui_get_current_page(self):
  45. pass
  46. def ui_goto(self, page):
  47. self.destinations.append(page)
  48. self.stage = page
  49. return True
  50. def screenshot(self):
  51. self.screenshots += 1
  52. assert self.screenshots < 200, 'Storage workflow did not respect its timeout'
  53. if self.stage is script_task.page_guild:
  54. self.frame = {'I_WT_FOLD_WINDOW' if self.menu_opens else 'I_WT_OPEN_FOLD_WINDOW'}
  55. elif self.frames:
  56. self.frame = set(self.frames.pop(0))
  57. self.device.image = frozenset(self.frame)
  58. def appear(self, asset, **kwargs):
  59. return any(asset is getattr(self, name) for name in self.frame)
  60. def appear_then_click(self, asset, **kwargs):
  61. if not self.appear(asset):
  62. return False
  63. if asset is self.I_WT_SAVE_ALL:
  64. self.save_attempts += 1
  65. if self.failed_save_clicks:
  66. self.failed_save_clicks -= 1
  67. return False
  68. self.clicks.append(asset)
  69. return True
  70. def click(self, asset, **kwargs):
  71. self.clicks.append(asset)
  72. return True
  73. def test_storage_dismisses_obscuring_popups_before_reading_tickets():
  74. task = StorageReplay([
  75. {'I_WT_LAST_SAVE'},
  76. {'I_WT_HAPPY_GET'},
  77. {'I_WT_SAVE_ALL'},
  78. {'I_WT_TF_CONFIRM'},
  79. {'I_WT_TF_SAVE_SUCCESS'},
  80. ])
  81. task._save_touch_fish()
  82. task.O_WT_LUCKY_TICKETS.ocr.assert_called_once_with(frozenset({'I_WT_SAVE_ALL'}))
  83. assert task.I_WT_HAPPY_GET in task.clicks
  84. assert task.I_WT_TF_CONFIRM in task.clicks
  85. assert task.save_attempts == 1
  86. assert task.destinations[-1] is script_task.page_main
  87. def test_storage_retries_a_save_button_that_was_not_clicked():
  88. task = StorageReplay([
  89. {'I_WT_SAVE_ALL'}, {'I_WT_SAVE_ALL'},
  90. {'I_WT_TF_CONFIRM'}, {'I_WT_TF_SAVE_SUCCESS'},
  91. ], failed_save_clicks=1)
  92. task._save_touch_fish()
  93. assert task.save_attempts == 2
  94. assert task.destinations[-1] is script_task.page_main
  95. @pytest.mark.parametrize('stuck_frame', [
  96. {'I_WT_LAST_SAVE'}, {'I_WT_HAPPY_GET'}, {'I_WT_TF_CONFIRM'}, set(),
  97. ])
  98. def test_storage_timeout_cannot_mark_the_week_complete(stuck_frame):
  99. task = StorageReplay([stuck_frame])
  100. with pytest.raises(GameStuckError, match='Touch fish save'):
  101. task.run()
  102. task.set_next_run.assert_not_called()
  103. assert task.destinations[-1] is script_task.page_touch_fish
  104. def test_guild_menu_has_a_bounded_wait():
  105. task = StorageReplay([], menu_opens=False)
  106. with pytest.raises(GameStuckError, match='guild menu'):
  107. task._save_touch_fish()
  108. assert script_task.page_touch_fish not in task.destinations
  109. def test_already_stored_week_needs_no_ocr_or_new_save():
  110. task = StorageReplay([{'I_WT_TF_SAVE_SUCCESS'}])
  111. task._save_touch_fish()
  112. task.O_WT_LUCKY_TICKETS.ocr.assert_not_called()
  113. assert task.save_attempts == 0
  114. assert task.destinations[-1] is script_task.page_main
  115. def test_insufficient_tickets_does_not_click_save():
  116. task = StorageReplay([{'I_WT_SAVE_ALL'}], tickets=1, cost=5)
  117. task._save_touch_fish()
  118. assert task.save_attempts == 0
  119. assert task.destinations[-1] is script_task.page_main