test_touch_fish.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 goto_page(self, page):
  45. self.destinations.append(page)
  46. self.stage = page
  47. return True
  48. def screenshot(self):
  49. self.screenshots += 1
  50. assert self.screenshots < 200, 'Storage workflow did not respect its timeout'
  51. if self.stage is script_task.page_guild:
  52. self.frame = {'I_WT_FOLD_WINDOW' if self.menu_opens else 'I_WT_OPEN_FOLD_WINDOW'}
  53. elif self.frames:
  54. self.frame = set(self.frames.pop(0))
  55. self.device.image = frozenset(self.frame)
  56. def appear(self, asset, **kwargs):
  57. return any(asset is getattr(self, name) for name in self.frame)
  58. def appear_then_click(self, asset, **kwargs):
  59. if not self.appear(asset):
  60. return False
  61. if asset is self.I_WT_SAVE_ALL:
  62. self.save_attempts += 1
  63. if self.failed_save_clicks:
  64. self.failed_save_clicks -= 1
  65. return False
  66. self.clicks.append(asset)
  67. return True
  68. def click(self, asset, **kwargs):
  69. self.clicks.append(asset)
  70. return True
  71. def test_storage_dismisses_obscuring_popups_before_reading_tickets():
  72. task = StorageReplay([
  73. {'I_WT_LAST_SAVE'},
  74. {'I_WT_HAPPY_GET'},
  75. {'I_WT_SAVE_ALL'},
  76. {'I_WT_TF_CONFIRM'},
  77. {'I_WT_TF_SAVE_SUCCESS'},
  78. ])
  79. task._save_touch_fish()
  80. task.O_WT_LUCKY_TICKETS.ocr.assert_called_once_with(frozenset({'I_WT_SAVE_ALL'}))
  81. assert task.I_WT_HAPPY_GET in task.clicks
  82. assert task.I_WT_TF_CONFIRM in task.clicks
  83. assert task.save_attempts == 1
  84. assert task.destinations[-1] is script_task.page_main
  85. def test_storage_retries_a_save_button_that_was_not_clicked():
  86. task = StorageReplay([
  87. {'I_WT_SAVE_ALL'}, {'I_WT_SAVE_ALL'},
  88. {'I_WT_TF_CONFIRM'}, {'I_WT_TF_SAVE_SUCCESS'},
  89. ], failed_save_clicks=1)
  90. task._save_touch_fish()
  91. assert task.save_attempts == 2
  92. assert task.destinations[-1] is script_task.page_main
  93. @pytest.mark.parametrize('stuck_frame', [
  94. {'I_WT_LAST_SAVE'}, {'I_WT_HAPPY_GET'}, {'I_WT_TF_CONFIRM'}, set(),
  95. ])
  96. def test_storage_timeout_cannot_mark_the_week_complete(stuck_frame):
  97. task = StorageReplay([stuck_frame])
  98. with pytest.raises(GameStuckError, match='Touch fish save'):
  99. task.run()
  100. task.set_next_run.assert_not_called()
  101. assert task.destinations[-1] is script_task.page_touch_fish
  102. def test_guild_menu_has_a_bounded_wait():
  103. task = StorageReplay([], menu_opens=False)
  104. with pytest.raises(GameStuckError, match='guild menu'):
  105. task._save_touch_fish()
  106. assert script_task.page_touch_fish not in task.destinations
  107. def test_already_stored_week_needs_no_ocr_or_new_save():
  108. task = StorageReplay([{'I_WT_TF_SAVE_SUCCESS'}])
  109. task._save_touch_fish()
  110. task.O_WT_LUCKY_TICKETS.ocr.assert_not_called()
  111. assert task.save_attempts == 0
  112. assert task.destinations[-1] is script_task.page_main
  113. def test_insufficient_tickets_does_not_click_save():
  114. task = StorageReplay([{'I_WT_SAVE_ALL'}], tickets=1, cost=5)
  115. task._save_touch_fish()
  116. assert task.save_attempts == 0
  117. assert task.destinations[-1] is script_task.page_main