66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import unittest
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from automation.engine import Matcher, Region, load_flow
|
|
from automation.idle import IdleRunner
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
class IdleDesktop:
|
|
def __init__(self, images):
|
|
self.images = images
|
|
self.index = 0
|
|
self.clicks = []
|
|
|
|
def screenshot(self):
|
|
return self.images[min(self.index, len(self.images) - 1)]
|
|
|
|
def click(self, point, region, size, hold=0.2):
|
|
self.clicks.append((point, hold))
|
|
self.index += 1
|
|
|
|
|
|
class IdleTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.flow = load_flow(ROOT / 'workflows/idle_claim.json')
|
|
self.matcher = Matcher(ROOT / 'assets')
|
|
self.region = Region(0, 0, 562, 1002)
|
|
|
|
def image(self, state):
|
|
image = Image.new('RGB', (562, 1002), (42, 58, 73))
|
|
for spec in self.flow['states'][state]:
|
|
template = Image.open(ROOT / 'assets' / spec['template']).convert('RGB')
|
|
x, y, _, _ = spec['search']
|
|
image.paste(template, (x + 5, y + 5))
|
|
return image
|
|
|
|
def runner(self, states):
|
|
desktop = IdleDesktop([self.image(state) for state in states])
|
|
return desktop, IdleRunner(desktop, self.matcher, self.flow,
|
|
lambda _: self.region, log=lambda _: None, timeout=0.8)
|
|
|
|
def test_claims_reward_with_long_press_and_returns_home(self):
|
|
desktop, runner = self.runner(['home', 'available', 'reward', 'claimed', 'home'])
|
|
self.assertEqual(runner.run(), 'claimed')
|
|
self.assertEqual(desktop.clicks, [
|
|
(self.flow['clicks']['open_pig'], 0.2),
|
|
(self.flow['clicks']['claim'], 0.8),
|
|
(self.flow['clicks']['dismiss_reward'], 0.2),
|
|
(self.flow['clicks']['close'], 0.2),
|
|
])
|
|
|
|
def test_grey_button_skips_claim_and_returns_home(self):
|
|
desktop, runner = self.runner(['home', 'claimed', 'home'])
|
|
self.assertEqual(runner.run(), 'no_reward')
|
|
self.assertEqual(desktop.clicks, [
|
|
(self.flow['clicks']['open_pig'], 0.2),
|
|
(self.flow['clicks']['close'], 0.2),
|
|
])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|