test_betting_flow.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import unittest
  2. from types import SimpleNamespace
  3. from unittest.mock import Mock, patch
  4. from module.exception import GameStuckError, TaskEnd
  5. from tasks.FrogBoss.config import Strategy
  6. from tasks.FrogBoss.script_task import ScriptTask
  7. class FrameTimer:
  8. """Bound waits by simulated frames without sleeping or using a device."""
  9. def __init__(self, *_args):
  10. self.frames = 0
  11. def start(self):
  12. return self
  13. def reset(self):
  14. self.frames = 0
  15. return self
  16. def reached(self):
  17. self.frames += 1
  18. return self.frames > 12
  19. class BettingTask(ScriptTask):
  20. def __init__(self, frames):
  21. self.frames = iter(frames)
  22. self.visible = set()
  23. self.clicks = []
  24. def screenshot(self):
  25. self.visible = next(self.frames, self.visible)
  26. def appear(self, rule):
  27. return rule.name in self.visible
  28. def click(self, rule, interval=None):
  29. self.clicks.append(rule)
  30. return True
  31. def appear_then_click(self, rule, interval=None):
  32. if not self.appear(rule):
  33. return False
  34. return self.click(rule, interval=interval)
  35. @patch('tasks.FrogBoss.script_task.Timer', FrameTimer)
  36. class BettingFlowTests(unittest.TestCase):
  37. def test_reward_overlay_and_confirmation_hide_clickable_background(self):
  38. panel = {ScriptTask.I_GOLD_30.name, ScriptTask.I_BET_SURE.name}
  39. task = BettingTask([
  40. panel | {ScriptTask.I_GOLD_30_CHECK.name},
  41. panel,
  42. panel,
  43. panel | {ScriptTask.I_UI_CONFIRM.name},
  44. {ScriptTask.I_BETTED.name},
  45. ])
  46. task.confirm_bet()
  47. self.assertEqual([rule.name for rule in task.clicks], [
  48. ScriptTask.C_REWARD_2.name,
  49. 'FB_GOLD_30_SELECT',
  50. ScriptTask.I_BET_SURE.name,
  51. ScriptTask.I_UI_CONFIRM.name,
  52. ])
  53. selection = task.clicks[1]
  54. x, y, width, height = ScriptTask.I_GOLD_30.roi_front
  55. sx, sy, sw, sh = selection.roi_front
  56. self.assertGreaterEqual(sx, x)
  57. self.assertLessEqual(sx + sw, x + width)
  58. self.assertGreaterEqual(sy, y)
  59. self.assertLess(sy + sh, y + height // 2)
  60. def test_cannot_submit_before_selecting_amount(self):
  61. task = BettingTask([{ScriptTask.I_BET_SURE.name}])
  62. with self.assertRaisesRegex(GameStuckError, 'gold_selected=False'):
  63. task.confirm_bet()
  64. self.assertEqual(task.clicks, [])
  65. def test_no_response_limits_submit_retries_without_reselecting_amount(self):
  66. panel = {ScriptTask.I_GOLD_30.name, ScriptTask.I_BET_SURE.name}
  67. task = BettingTask([panel])
  68. with self.assertRaisesRegex(GameStuckError, 'submit_attempts=3'):
  69. task.confirm_bet()
  70. self.assertEqual([rule.name for rule in task.clicks], [
  71. 'FB_GOLD_30_SELECT',
  72. ScriptTask.I_BET_SURE.name,
  73. ScriptTask.I_BET_SURE.name,
  74. ScriptTask.I_BET_SURE.name,
  75. ])
  76. def test_already_betted_and_rest_end_without_clicking(self):
  77. for marker in (ScriptTask.I_BETTED, ScriptTask.I_FROG_BOSS_REST):
  78. with self.subTest(marker=marker.name):
  79. task = BettingTask([{marker.name}])
  80. task.confirm_bet()
  81. self.assertEqual(task.clicks, [])
  82. def test_non_oas_strategy_can_resume_from_record_page(self):
  83. class HistoryTask(BettingTask):
  84. def __init__(self):
  85. super().__init__([])
  86. self.visible = {self.I_FROG_LOG_CHECK.name, self.I_FROG_LOG_CLOSE.name}
  87. self.config = SimpleNamespace(model=SimpleNamespace(frog_boss=SimpleNamespace(
  88. frog_boss_config=SimpleNamespace(strategy_frog=Strategy.Majority))))
  89. self.next_run = Mock()
  90. self.frame_count = 0
  91. def screenshot(self):
  92. self.frame_count += 1
  93. assert self.frame_count < 30, 'Record page was never closed'
  94. def click(self, rule, interval=None):
  95. if rule is self.I_FROG_LOG_CLOSE:
  96. self.visible = {self.I_FROG_CHECK.name, self.I_BETTED.name}
  97. return super().click(rule, interval)
  98. task = HistoryTask()
  99. with self.assertRaises(TaskEnd):
  100. task.run()
  101. self.assertEqual([rule.name for rule in task.clicks], [task.I_FROG_LOG_CLOSE.name])
  102. task.next_run.assert_called_once_with()
  103. def test_record_page_close_is_bounded(self):
  104. task = BettingTask([{ScriptTask.I_FROG_LOG_CHECK.name, ScriptTask.I_FROG_LOG_CLOSE.name}])
  105. with self.assertRaisesRegex(GameStuckError, 'record page did not close'):
  106. task.close_frog_history_page()
  107. if __name__ == '__main__':
  108. unittest.main()