| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import unittest
- from types import SimpleNamespace
- from unittest.mock import Mock, patch
- from module.exception import GameStuckError, TaskEnd
- from tasks.FrogBoss.config import Strategy
- from tasks.FrogBoss.script_task import ScriptTask
- class FrameTimer:
- """Bound waits by simulated frames without sleeping or using a device."""
- def __init__(self, *_args):
- self.frames = 0
- def start(self):
- return self
- def reset(self):
- self.frames = 0
- return self
- def reached(self):
- self.frames += 1
- return self.frames > 12
- class BettingTask(ScriptTask):
- def __init__(self, frames):
- self.frames = iter(frames)
- self.visible = set()
- self.clicks = []
- def screenshot(self):
- self.visible = next(self.frames, self.visible)
- def appear(self, rule):
- return rule.name in self.visible
- def click(self, rule, interval=None):
- self.clicks.append(rule)
- return True
- def appear_then_click(self, rule, interval=None):
- if not self.appear(rule):
- return False
- return self.click(rule, interval=interval)
- @patch('tasks.FrogBoss.script_task.Timer', FrameTimer)
- class BettingFlowTests(unittest.TestCase):
- def test_reward_overlay_and_confirmation_hide_clickable_background(self):
- panel = {ScriptTask.I_GOLD_30.name, ScriptTask.I_BET_SURE.name}
- task = BettingTask([
- panel | {ScriptTask.I_GOLD_30_CHECK.name},
- panel,
- panel,
- panel | {ScriptTask.I_UI_CONFIRM.name},
- {ScriptTask.I_BETTED.name},
- ])
- task.confirm_bet()
- self.assertEqual([rule.name for rule in task.clicks], [
- ScriptTask.C_REWARD_2.name,
- 'FB_GOLD_30_SELECT',
- ScriptTask.I_BET_SURE.name,
- ScriptTask.I_UI_CONFIRM.name,
- ])
- selection = task.clicks[1]
- x, y, width, height = ScriptTask.I_GOLD_30.roi_front
- sx, sy, sw, sh = selection.roi_front
- self.assertGreaterEqual(sx, x)
- self.assertLessEqual(sx + sw, x + width)
- self.assertGreaterEqual(sy, y)
- self.assertLess(sy + sh, y + height // 2)
- def test_cannot_submit_before_selecting_amount(self):
- task = BettingTask([{ScriptTask.I_BET_SURE.name}])
- with self.assertRaisesRegex(GameStuckError, 'gold_selected=False'):
- task.confirm_bet()
- self.assertEqual(task.clicks, [])
- def test_no_response_limits_submit_retries_without_reselecting_amount(self):
- panel = {ScriptTask.I_GOLD_30.name, ScriptTask.I_BET_SURE.name}
- task = BettingTask([panel])
- with self.assertRaisesRegex(GameStuckError, 'submit_attempts=3'):
- task.confirm_bet()
- self.assertEqual([rule.name for rule in task.clicks], [
- 'FB_GOLD_30_SELECT',
- ScriptTask.I_BET_SURE.name,
- ScriptTask.I_BET_SURE.name,
- ScriptTask.I_BET_SURE.name,
- ])
- def test_already_betted_and_rest_end_without_clicking(self):
- for marker in (ScriptTask.I_BETTED, ScriptTask.I_FROG_BOSS_REST):
- with self.subTest(marker=marker.name):
- task = BettingTask([{marker.name}])
- task.confirm_bet()
- self.assertEqual(task.clicks, [])
- def test_non_oas_strategy_can_resume_from_record_page(self):
- class HistoryTask(BettingTask):
- def __init__(self):
- super().__init__([])
- self.visible = {self.I_FROG_LOG_CHECK.name, self.I_FROG_LOG_CLOSE.name}
- self.config = SimpleNamespace(model=SimpleNamespace(frog_boss=SimpleNamespace(
- frog_boss_config=SimpleNamespace(strategy_frog=Strategy.Majority))))
- self.next_run = Mock()
- self.frame_count = 0
- def screenshot(self):
- self.frame_count += 1
- assert self.frame_count < 30, 'Record page was never closed'
- def click(self, rule, interval=None):
- if rule is self.I_FROG_LOG_CLOSE:
- self.visible = {self.I_FROG_CHECK.name, self.I_BETTED.name}
- return super().click(rule, interval)
- task = HistoryTask()
- with self.assertRaises(TaskEnd):
- task.run()
- self.assertEqual([rule.name for rule in task.clicks], [task.I_FROG_LOG_CLOSE.name])
- task.next_run.assert_called_once_with()
- def test_record_page_close_is_bounded(self):
- task = BettingTask([{ScriptTask.I_FROG_LOG_CHECK.name, ScriptTask.I_FROG_LOG_CLOSE.name}])
- with self.assertRaisesRegex(GameStuckError, 'record page did not close'):
- task.close_frog_history_page()
- if __name__ == '__main__':
- unittest.main()
|