MIL-008: smarter purchase rule #49
@@ -102,6 +102,7 @@ The bot plays for 5 minutes, then prints one line on standard output, for exampl
|
|||||||
| `--duration SECONDS` | How long to play | 300 |
|
| `--duration SECONDS` | How long to play | 300 |
|
||||||
| `--interval SECONDS` | Time between upgrade checks (greater than 0) | 5 |
|
| `--interval SECONDS` | Time between upgrade checks (greater than 0) | 5 |
|
||||||
| `--buy-all` | Buy every affordable upgrade, most expensive first, instead of one | off |
|
| `--buy-all` | Buy every affordable upgrade, most expensive first, instead of one | off |
|
||||||
|
| `--best-value` | Buy the affordable upgrade with the lowest price per cookie per second (not together with `--buy-all`) | off |
|
||||||
| `--headless` | Run Chromium without a window | off (on in Docker) |
|
| `--headless` | Run Chromium without a window | off (on in Docker) |
|
||||||
| `--screenshot PATH` | Save a screenshot when the run ends | none |
|
| `--screenshot PATH` | Save a screenshot when the run ends | none |
|
||||||
| `--screenshot-start PATH` | Save a screenshot before the first click | none |
|
| `--screenshot-start PATH` | Save a screenshot before the first click | none |
|
||||||
|
|||||||
@@ -1,96 +1,116 @@
|
|||||||
"""@file game_loop.py
|
"""@file game_loop.py
|
||||||
@brief The game loop of the bot, written against the game port.
|
@brief The game loop of the bot, written against the game port.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from cookie_clicker_bot.application.ports import GamePort
|
from cookie_clicker_bot.application.ports import GamePort
|
||||||
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
||||||
from cookie_clicker_bot.domain.upgrades import (
|
from cookie_clicker_bot.domain.upgrades import (
|
||||||
Upgrade,
|
Upgrade,
|
||||||
choose_affordable_upgrades,
|
choose_affordable_upgrades,
|
||||||
choose_most_expensive_affordable,
|
choose_best_value,
|
||||||
)
|
choose_most_expensive_affordable,
|
||||||
|
)
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
|
||||||
class PlayResult:
|
@dataclass(frozen=True, slots=True)
|
||||||
"""@brief What a run did."""
|
class PlayResult:
|
||||||
|
"""@brief What a run did."""
|
||||||
## Number of clicks on the big cookie.
|
|
||||||
clicks: int
|
## Number of clicks on the big cookie.
|
||||||
## Names of the upgrades bought, in the order they were bought.
|
clicks: int
|
||||||
purchases: tuple[str, ...]
|
## Names of the upgrades bought, in the order they were bought.
|
||||||
## Seconds the run lasted, measured with the clock given to `click_and_buy`.
|
purchases: tuple[str, ...]
|
||||||
elapsed_seconds: float
|
## Seconds the run lasted, measured with the clock given to `click_and_buy`.
|
||||||
|
elapsed_seconds: float
|
||||||
|
|
||||||
def click_cookie(game: GamePort) -> None:
|
|
||||||
"""@brief Click the big cookie once (lecture step 2).
|
def click_cookie(game: GamePort) -> None:
|
||||||
|
"""@brief Click the big cookie once (lecture step 2).
|
||||||
@param game The game to play.
|
|
||||||
"""
|
@param game The game to play.
|
||||||
game.click_cookie()
|
"""
|
||||||
|
game.click_cookie()
|
||||||
|
|
||||||
def buy_most_expensive_upgrade(game: GamePort) -> Upgrade | None:
|
|
||||||
"""@brief Buy the most expensive upgrade the player can afford (lecture step 3).
|
def buy_most_expensive_upgrade(game: GamePort) -> Upgrade | None:
|
||||||
|
"""@brief Buy the most expensive upgrade the player can afford (lecture step 3).
|
||||||
@param game The game to play.
|
|
||||||
@return The upgrade that was bought, or None when nothing was affordable or
|
@param game The game to play.
|
||||||
the click did not reach the product.
|
@return The upgrade that was bought, or None when nothing was affordable or
|
||||||
"""
|
the click did not reach the product.
|
||||||
chosen = choose_most_expensive_affordable(game.upgrades(), game.cookie_count())
|
"""
|
||||||
if chosen is not None and game.buy(chosen):
|
chosen = choose_most_expensive_affordable(game.upgrades(), game.cookie_count())
|
||||||
return chosen
|
if chosen is not None and game.buy(chosen):
|
||||||
return None
|
return chosen
|
||||||
|
return None
|
||||||
|
|
||||||
def buy_upgrades(game: GamePort, strategy: BuyStrategy) -> list[Upgrade]:
|
|
||||||
"""@brief Do one purchase round with the given strategy (lecture step 5).
|
def buy_best_value_upgrade(game: GamePort) -> Upgrade | None:
|
||||||
|
"""@brief Buy the affordable upgrade with the lowest price per cookie per second.
|
||||||
@param game The game to play.
|
|
||||||
@param strategy BUY_ONE buys the most expensive affordable upgrade, BUY_ALL buys
|
@param game The game to play.
|
||||||
every upgrade the cookies pay for.
|
@return The upgrade that was bought, or None when nothing was affordable or
|
||||||
@return The upgrades really bought, in order; a click that missed is left out.
|
the click did not reach the product.
|
||||||
"""
|
"""
|
||||||
if strategy is BuyStrategy.BUY_ONE:
|
chosen = choose_best_value(game.upgrades(), game.cookie_count())
|
||||||
bought = buy_most_expensive_upgrade(game)
|
if chosen is not None and game.buy(chosen):
|
||||||
return [] if bought is None else [bought]
|
return chosen
|
||||||
plan = choose_affordable_upgrades(game.upgrades(), game.cookie_count())
|
return None
|
||||||
return [upgrade for upgrade in plan if game.buy(upgrade)]
|
|
||||||
|
|
||||||
|
def buy_upgrades(game: GamePort, strategy: BuyStrategy) -> list[Upgrade]:
|
||||||
def click_and_buy(
|
"""@brief Do one purchase round with the given strategy (lecture step 5).
|
||||||
game: GamePort,
|
|
||||||
duration_seconds: float,
|
@param game The game to play.
|
||||||
check_interval_seconds: float,
|
@param strategy BUY_ONE buys the most expensive affordable upgrade, BUY_ALL buys
|
||||||
clock: Callable[[], float] = time.monotonic,
|
every upgrade the cookies pay for, BEST_VALUE buys the one with the lowest
|
||||||
strategy: BuyStrategy = BuyStrategy.BUY_ONE,
|
price per cookie per second.
|
||||||
) -> PlayResult:
|
@return The upgrades really bought, in order; a click that missed is left out.
|
||||||
"""@brief Click the cookie nonstop and check the upgrades at every interval.
|
"""
|
||||||
|
if strategy is not BuyStrategy.BUY_ALL:
|
||||||
@param game The game to play.
|
buy_one = (
|
||||||
@param duration_seconds How long to play.
|
buy_best_value_upgrade
|
||||||
@param check_interval_seconds Time between two upgrade checks; the first check
|
if strategy is BuyStrategy.BEST_VALUE
|
||||||
comes one interval after the start.
|
else buy_most_expensive_upgrade
|
||||||
@param clock Monotonic clock in seconds; a test passes a fake one.
|
)
|
||||||
@param strategy What to buy at each check.
|
bought = buy_one(game)
|
||||||
@return The clicks, the names of the upgrades bought in order, and the time spent.
|
return [] if bought is None else [bought]
|
||||||
"""
|
plan = choose_affordable_upgrades(game.upgrades(), game.cookie_count())
|
||||||
start = clock()
|
return [upgrade for upgrade in plan if game.buy(upgrade)]
|
||||||
deadline = start + duration_seconds
|
|
||||||
next_check = start + check_interval_seconds
|
|
||||||
clicks = 0
|
def click_and_buy(
|
||||||
purchases: list[str] = []
|
game: GamePort,
|
||||||
while (now := clock()) < deadline:
|
duration_seconds: float,
|
||||||
click_cookie(game)
|
check_interval_seconds: float,
|
||||||
clicks += 1
|
clock: Callable[[], float] = time.monotonic,
|
||||||
if now >= next_check:
|
strategy: BuyStrategy = BuyStrategy.BUY_ONE,
|
||||||
next_check = now + check_interval_seconds
|
) -> PlayResult:
|
||||||
purchases.extend(upgrade.name for upgrade in buy_upgrades(game, strategy))
|
"""@brief Click the cookie nonstop and check the upgrades at every interval.
|
||||||
return PlayResult(
|
|
||||||
clicks=clicks, purchases=tuple(purchases), elapsed_seconds=clock() - start
|
@param game The game to play.
|
||||||
)
|
@param duration_seconds How long to play.
|
||||||
|
@param check_interval_seconds Time between two upgrade checks; the first check
|
||||||
|
comes one interval after the start.
|
||||||
|
@param clock Monotonic clock in seconds; a test passes a fake one.
|
||||||
|
@param strategy What to buy at each check.
|
||||||
|
@return The clicks, the names of the upgrades bought in order, and the time spent.
|
||||||
|
"""
|
||||||
|
start = clock()
|
||||||
|
deadline = start + duration_seconds
|
||||||
|
next_check = start + check_interval_seconds
|
||||||
|
clicks = 0
|
||||||
|
purchases: list[str] = []
|
||||||
|
while (now := clock()) < deadline:
|
||||||
|
click_cookie(game)
|
||||||
|
clicks += 1
|
||||||
|
if now >= next_check:
|
||||||
|
next_check = now + check_interval_seconds
|
||||||
|
purchases.extend(upgrade.name for upgrade in buy_upgrades(game, strategy))
|
||||||
|
return PlayResult(
|
||||||
|
clicks=clicks, purchases=tuple(purchases), elapsed_seconds=clock() - start
|
||||||
|
)
|
||||||
|
|||||||
+161
-139
@@ -1,139 +1,161 @@
|
|||||||
"""@file cli.py
|
"""@file cli.py
|
||||||
@brief Command-line entry point (the composition root of the bot).
|
@brief Command-line entry point (the composition root of the bot).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from cookie_clicker_bot import constants
|
from cookie_clicker_bot import constants
|
||||||
from cookie_clicker_bot.application.game_loop import click_and_buy
|
from cookie_clicker_bot.application.game_loop import click_and_buy
|
||||||
from cookie_clicker_bot.application.ports import GamePort
|
from cookie_clicker_bot.application.ports import GamePort
|
||||||
from cookie_clicker_bot.application.report import format_result
|
from cookie_clicker_bot.application.report import format_result
|
||||||
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
||||||
from cookie_clicker_bot.infrastructure.browser import create_driver
|
from cookie_clicker_bot.infrastructure.browser import create_driver
|
||||||
from cookie_clicker_bot.infrastructure.selenium_game import SeleniumGame
|
from cookie_clicker_bot.infrastructure.selenium_game import SeleniumGame
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create_game(headless: bool) -> GamePort:
|
def create_game(headless: bool) -> GamePort:
|
||||||
"""@brief Build the Selenium game.
|
"""@brief Build the Selenium game.
|
||||||
|
|
||||||
@param headless True to run Chromium without a window.
|
@param headless True to run Chromium without a window.
|
||||||
@return The game, ready to open.
|
@return The game, ready to open.
|
||||||
"""
|
"""
|
||||||
return SeleniumGame(create_driver(headless))
|
return SeleniumGame(create_driver(headless))
|
||||||
|
|
||||||
|
|
||||||
def positive_float(text: str) -> float:
|
def positive_float(text: str) -> float:
|
||||||
"""@brief Read a number greater than zero from the command line.
|
"""@brief Read a number greater than zero from the command line.
|
||||||
|
|
||||||
@param text The argument text.
|
@param text The argument text.
|
||||||
@return The number.
|
@return The number.
|
||||||
@throws argparse.ArgumentTypeError When the text is not a number above zero.
|
@throws argparse.ArgumentTypeError When the text is not a number above zero.
|
||||||
"""
|
"""
|
||||||
message = constants.POSITIVE_NUMBER_ERROR_TEMPLATE.format(value=text)
|
message = constants.POSITIVE_NUMBER_ERROR_TEMPLATE.format(value=text)
|
||||||
try:
|
try:
|
||||||
value = float(text)
|
value = float(text)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise argparse.ArgumentTypeError(message) from None
|
raise argparse.ArgumentTypeError(message) from None
|
||||||
if not value > 0:
|
if not value > 0:
|
||||||
raise argparse.ArgumentTypeError(message)
|
raise argparse.ArgumentTypeError(message)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
|
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
|
||||||
"""@brief Read the command line.
|
"""@brief Read the command line.
|
||||||
|
|
||||||
@param argv Arguments without the program name; None reads `sys.argv`.
|
@param argv Arguments without the program name; None reads `sys.argv`.
|
||||||
@return The parsed options.
|
@return The parsed options.
|
||||||
"""
|
"""
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="cookie-clicker-bot",
|
prog="cookie-clicker-bot",
|
||||||
description=(
|
description=(
|
||||||
"Plays Cookie Clicker: clicks the big cookie as fast as it can "
|
"Plays Cookie Clicker: clicks the big cookie as fast as it can "
|
||||||
"and buys upgrades."
|
"and buys upgrades."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--duration",
|
"--duration",
|
||||||
type=float,
|
type=float,
|
||||||
default=constants.RUN_DURATION_SECONDS,
|
default=constants.RUN_DURATION_SECONDS,
|
||||||
help="seconds to play (default: %(default)s)",
|
help="seconds to play (default: %(default)s)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--interval",
|
"--interval",
|
||||||
type=positive_float,
|
type=positive_float,
|
||||||
default=constants.UPGRADE_CHECK_INTERVAL_SECONDS,
|
default=constants.UPGRADE_CHECK_INTERVAL_SECONDS,
|
||||||
help="seconds between upgrade checks, greater than 0 (default: %(default)s)",
|
help="seconds between upgrade checks, greater than 0 (default: %(default)s)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
choice = parser.add_mutually_exclusive_group()
|
||||||
"--buy-all",
|
choice.add_argument(
|
||||||
action="store_true",
|
"--buy-all",
|
||||||
help=(
|
action="store_true",
|
||||||
"at each check buy every affordable upgrade, most expensive first "
|
help=(
|
||||||
"(default: buy only the most expensive affordable one)"
|
"at each check buy every affordable upgrade, most expensive first "
|
||||||
),
|
"(default: buy only the most expensive affordable one)"
|
||||||
)
|
),
|
||||||
parser.add_argument(
|
)
|
||||||
"--headless",
|
choice.add_argument(
|
||||||
action="store_true",
|
"--best-value",
|
||||||
help="run Chromium without a window (the Docker image does this)",
|
action="store_true",
|
||||||
)
|
help=(
|
||||||
parser.add_argument(
|
"at each check buy the affordable upgrade with the lowest price per "
|
||||||
"--screenshot-start",
|
"cookie per second"
|
||||||
type=Path,
|
),
|
||||||
help="write a screenshot of the game before the first click",
|
)
|
||||||
)
|
parser.add_argument(
|
||||||
parser.add_argument(
|
"--headless",
|
||||||
"--screenshot",
|
action="store_true",
|
||||||
type=Path,
|
help="run Chromium without a window (the Docker image does this)",
|
||||||
help="write a screenshot of the game when the run ends",
|
)
|
||||||
)
|
parser.add_argument(
|
||||||
return parser.parse_args(argv)
|
"--screenshot-start",
|
||||||
|
type=Path,
|
||||||
|
help="write a screenshot of the game before the first click",
|
||||||
def main(
|
)
|
||||||
argv: Sequence[str] | None = None,
|
parser.add_argument(
|
||||||
game_factory: Callable[[bool], GamePort] = create_game,
|
"--screenshot",
|
||||||
) -> int:
|
type=Path,
|
||||||
"""@brief Start the bot.
|
help="write a screenshot of the game when the run ends",
|
||||||
|
)
|
||||||
@param argv Arguments without the program name; None reads `sys.argv`.
|
return parser.parse_args(argv)
|
||||||
@param game_factory Builds the game; a test passes a fake.
|
|
||||||
@return Process exit code, 0 when the run finished.
|
|
||||||
"""
|
def choose_strategy(args: argparse.Namespace) -> BuyStrategy:
|
||||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
"""@brief Turn the purchase options into a strategy.
|
||||||
args = parse_args(argv)
|
|
||||||
game = game_factory(args.headless)
|
@param args The parsed options.
|
||||||
try:
|
@return BUY_ALL, BEST_VALUE or, by default, BUY_ONE.
|
||||||
game.open_game()
|
"""
|
||||||
game.dismiss_language()
|
if args.buy_all:
|
||||||
if args.screenshot_start is not None:
|
return BuyStrategy.BUY_ALL
|
||||||
game.save_screenshot(args.screenshot_start)
|
if args.best_value:
|
||||||
strategy = BuyStrategy.BUY_ALL if args.buy_all else BuyStrategy.BUY_ONE
|
return BuyStrategy.BEST_VALUE
|
||||||
logger.info(
|
return BuyStrategy.BUY_ONE
|
||||||
"checking upgrades every %s seconds, strategy %s",
|
|
||||||
args.interval,
|
|
||||||
strategy.value,
|
def main(
|
||||||
)
|
argv: Sequence[str] | None = None,
|
||||||
result = click_and_buy(game, args.duration, args.interval, strategy=strategy)
|
game_factory: Callable[[bool], GamePort] = create_game,
|
||||||
for name in result.purchases:
|
) -> int:
|
||||||
logger.info("bought %s", name)
|
"""@brief Start the bot.
|
||||||
logger.info(
|
|
||||||
"clicked the cookie %d times and bought %d upgrades in %.1f seconds",
|
@param argv Arguments without the program name; None reads `sys.argv`.
|
||||||
result.clicks,
|
@param game_factory Builds the game; a test passes a fake.
|
||||||
len(result.purchases),
|
@return Process exit code, 0 when the run finished.
|
||||||
result.elapsed_seconds,
|
"""
|
||||||
)
|
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||||
cookies_per_second = game.cookies_per_second()
|
args = parse_args(argv)
|
||||||
logger.info("%s", format_result(cookies_per_second))
|
game = game_factory(args.headless)
|
||||||
if args.screenshot is not None:
|
try:
|
||||||
game.save_screenshot(args.screenshot)
|
game.open_game()
|
||||||
finally:
|
game.dismiss_language()
|
||||||
game.close()
|
if args.screenshot_start is not None:
|
||||||
# The program's required result: the only line written to stdout (logs use stderr).
|
game.save_screenshot(args.screenshot_start)
|
||||||
print(format_result(cookies_per_second))
|
strategy = choose_strategy(args)
|
||||||
return 0
|
logger.info(
|
||||||
|
"checking upgrades every %s seconds, strategy %s",
|
||||||
|
args.interval,
|
||||||
|
strategy.value,
|
||||||
|
)
|
||||||
|
result = click_and_buy(game, args.duration, args.interval, strategy=strategy)
|
||||||
|
for name in result.purchases:
|
||||||
|
logger.info("bought %s", name)
|
||||||
|
logger.info(
|
||||||
|
"clicked the cookie %d times and bought %d upgrades in %.1f seconds",
|
||||||
|
result.clicks,
|
||||||
|
len(result.purchases),
|
||||||
|
result.elapsed_seconds,
|
||||||
|
)
|
||||||
|
cookies_per_second = game.cookies_per_second()
|
||||||
|
logger.info("%s", format_result(cookies_per_second))
|
||||||
|
if args.screenshot is not None:
|
||||||
|
game.save_screenshot(args.screenshot)
|
||||||
|
finally:
|
||||||
|
game.close()
|
||||||
|
# The program's required result: the only line written to stdout (logs use stderr).
|
||||||
|
print(format_result(cookies_per_second))
|
||||||
|
return 0
|
||||||
|
|||||||
@@ -90,6 +90,12 @@ WINDOW_SIZE: Final[str] = "1366,900"
|
|||||||
## Script that clicks an element from inside the page, past anything that covers it.
|
## Script that clicks an element from inside the page, past anything that covers it.
|
||||||
JS_CLICK_SCRIPT: Final[str] = "arguments[0].click();"
|
JS_CLICK_SCRIPT: Final[str] = "arguments[0].click();"
|
||||||
|
|
||||||
|
## Script that reads the cookies per second of one unit of each product, by index.
|
||||||
|
JS_PRODUCTION_SCRIPT: Final[str] = "return Game.ObjectsById.map(o => o.storedCps);"
|
||||||
|
|
||||||
|
## Prefix of a product element id; the rest is the index into the script's list.
|
||||||
|
PRODUCT_ID_PREFIX: Final[str] = "product"
|
||||||
|
|
||||||
## Chromium arguments needed to run headless inside a container.
|
## Chromium arguments needed to run headless inside a container.
|
||||||
HEADLESS_BROWSER_ARGUMENTS: Final[tuple[str, ...]] = (
|
HEADLESS_BROWSER_ARGUMENTS: Final[tuple[str, ...]] = (
|
||||||
"--headless=new",
|
"--headless=new",
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
"""@file buy_strategy.py
|
"""@file buy_strategy.py
|
||||||
@brief How many upgrades the bot buys at each check.
|
@brief How many upgrades the bot buys at each check.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class BuyStrategy(Enum):
|
class BuyStrategy(Enum):
|
||||||
"""@brief The purchase strategy of one upgrade check (lecture step 5)."""
|
"""@brief The purchase strategy of one upgrade check (lecture step 5)."""
|
||||||
|
|
||||||
## Buy only the most expensive affordable upgrade (the assignment).
|
## Buy only the most expensive affordable upgrade (the assignment).
|
||||||
BUY_ONE = "buy-one"
|
BUY_ONE = "buy-one"
|
||||||
## Buy every upgrade the cookies pay for, most expensive first.
|
## Buy every upgrade the cookies pay for, most expensive first.
|
||||||
BUY_ALL = "buy-all"
|
BUY_ALL = "buy-all"
|
||||||
|
## Buy the affordable upgrade with the lowest price per cookie per second.
|
||||||
|
BEST_VALUE = "best-value"
|
||||||
|
|||||||
@@ -1,52 +1,81 @@
|
|||||||
"""@file upgrades.py
|
"""@file upgrades.py
|
||||||
@brief The rule for which upgrade to buy.
|
@brief The rule for which upgrade to buy.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
@dataclass(frozen=True, slots=True)
|
||||||
class Upgrade:
|
class Upgrade:
|
||||||
"""@brief A product of the right-hand pane that can be bought."""
|
"""@brief A product of the right-hand pane that can be bought."""
|
||||||
|
|
||||||
## Id of the product element on the page (product0 .. product19).
|
## Id of the product element on the page (product0 .. product19).
|
||||||
key: str
|
key: str
|
||||||
## Name shown in the store, for example Cursor.
|
## Name shown in the store, for example Cursor.
|
||||||
name: str
|
name: str
|
||||||
## Price in cookies.
|
## Price in cookies.
|
||||||
price: int
|
price: int
|
||||||
|
## Cookies per second one unit makes; None when the page did not tell.
|
||||||
|
cookies_per_second: float | None = None
|
||||||
def choose_affordable_upgrades(
|
|
||||||
upgrades: Sequence[Upgrade], cookies: int
|
|
||||||
) -> list[Upgrade]:
|
def choose_affordable_upgrades(
|
||||||
"""@brief Plan a buy-all round: as many upgrades as the cookies pay for.
|
upgrades: Sequence[Upgrade], cookies: int
|
||||||
|
) -> list[Upgrade]:
|
||||||
Walks the upgrades from most expensive to cheapest (the list order on a tie) and
|
"""@brief Plan a buy-all round: as many upgrades as the cookies pay for.
|
||||||
takes each one whose price still fits in the cookies left.
|
|
||||||
|
Walks the upgrades from most expensive to cheapest (the list order on a tie) and
|
||||||
@param upgrades The visible upgrades, in store order.
|
takes each one whose price still fits in the cookies left.
|
||||||
@param cookies The cookies the player has.
|
|
||||||
@return The upgrades to buy, most expensive first; empty when none is affordable.
|
@param upgrades The visible upgrades, in store order.
|
||||||
"""
|
@param cookies The cookies the player has.
|
||||||
plan: list[Upgrade] = []
|
@return The upgrades to buy, most expensive first; empty when none is affordable.
|
||||||
remaining = cookies
|
"""
|
||||||
for upgrade in sorted(upgrades, key=lambda candidate: -candidate.price):
|
plan: list[Upgrade] = []
|
||||||
if upgrade.price <= remaining:
|
remaining = cookies
|
||||||
plan.append(upgrade)
|
for upgrade in sorted(upgrades, key=lambda candidate: -candidate.price):
|
||||||
remaining -= upgrade.price
|
if upgrade.price <= remaining:
|
||||||
return plan
|
plan.append(upgrade)
|
||||||
|
remaining -= upgrade.price
|
||||||
|
return plan
|
||||||
def choose_most_expensive_affordable(
|
|
||||||
upgrades: Sequence[Upgrade], cookies: int
|
|
||||||
) -> Upgrade | None:
|
def choose_best_value(upgrades: Sequence[Upgrade], cookies: int) -> Upgrade | None:
|
||||||
"""@brief Pick the most expensive upgrade the player can pay for.
|
"""@brief Pick the affordable upgrade that pays for itself fastest.
|
||||||
|
|
||||||
@param upgrades The visible upgrades, in store order.
|
The value of an upgrade is its price divided by the cookies per second it makes,
|
||||||
@param cookies The cookies the player has.
|
so the lowest ratio wins. Upgrades with unknown or zero production are skipped;
|
||||||
@return The chosen upgrade (the first on a tie), or None when none is affordable.
|
when none of the affordable ones has a known production the rule falls back to
|
||||||
"""
|
the most expensive affordable upgrade.
|
||||||
plan = choose_affordable_upgrades(upgrades, cookies)
|
|
||||||
return plan[0] if plan else None
|
@param upgrades The visible upgrades, in store order.
|
||||||
|
@param cookies The cookies the player has.
|
||||||
|
@return The chosen upgrade (the first on a tie), or None when none is affordable.
|
||||||
|
"""
|
||||||
|
producing = [
|
||||||
|
upgrade
|
||||||
|
for upgrade in upgrades
|
||||||
|
if upgrade.price <= cookies
|
||||||
|
and upgrade.cookies_per_second is not None
|
||||||
|
and upgrade.cookies_per_second > 0
|
||||||
|
]
|
||||||
|
if not producing:
|
||||||
|
return choose_most_expensive_affordable(upgrades, cookies)
|
||||||
|
return min(
|
||||||
|
producing,
|
||||||
|
key=lambda upgrade: upgrade.price / (upgrade.cookies_per_second or 1.0),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def choose_most_expensive_affordable(
|
||||||
|
upgrades: Sequence[Upgrade], cookies: int
|
||||||
|
) -> Upgrade | None:
|
||||||
|
"""@brief Pick the most expensive upgrade the player can pay for.
|
||||||
|
|
||||||
|
@param upgrades The visible upgrades, in store order.
|
||||||
|
@param cookies The cookies the player has.
|
||||||
|
@return The chosen upgrade (the first on a tie), or None when none is affordable.
|
||||||
|
"""
|
||||||
|
plan = choose_affordable_upgrades(upgrades, cookies)
|
||||||
|
return plan[0] if plan else None
|
||||||
|
|||||||
@@ -1,229 +1,256 @@
|
|||||||
"""@file selenium_game.py
|
"""@file selenium_game.py
|
||||||
@brief Selenium adapter that implements the game port.
|
@brief Selenium adapter that implements the game port.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Protocol, TypeVar
|
from typing import Any, Protocol, TypeVar
|
||||||
|
|
||||||
from selenium.common.exceptions import (
|
from selenium.common.exceptions import (
|
||||||
ElementClickInterceptedException,
|
ElementClickInterceptedException,
|
||||||
NoSuchElementException,
|
JavascriptException,
|
||||||
StaleElementReferenceException,
|
NoSuchElementException,
|
||||||
)
|
StaleElementReferenceException,
|
||||||
from selenium.webdriver.common.by import By
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
from cookie_clicker_bot import constants
|
|
||||||
from cookie_clicker_bot.domain.parsing import (
|
from cookie_clicker_bot import constants
|
||||||
parse_amount,
|
from cookie_clicker_bot.domain.parsing import (
|
||||||
parse_cookie_count,
|
parse_amount,
|
||||||
parse_cookies_per_second,
|
parse_cookie_count,
|
||||||
)
|
parse_cookies_per_second,
|
||||||
from cookie_clicker_bot.domain.upgrades import Upgrade
|
)
|
||||||
|
from cookie_clicker_bot.domain.upgrades import Upgrade
|
||||||
T = TypeVar("T")
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
class ElementLike(Protocol):
|
|
||||||
"""@brief The part of a Selenium element that the adapter uses."""
|
class ElementLike(Protocol):
|
||||||
|
"""@brief The part of a Selenium element that the adapter uses."""
|
||||||
@property
|
|
||||||
def text(self) -> str:
|
@property
|
||||||
"""@brief Visible text of the element."""
|
def text(self) -> str:
|
||||||
...
|
"""@brief Visible text of the element."""
|
||||||
|
...
|
||||||
def click(self) -> None:
|
|
||||||
"""@brief Click the element."""
|
def click(self) -> None:
|
||||||
|
"""@brief Click the element."""
|
||||||
def is_displayed(self) -> bool:
|
|
||||||
"""@brief True when the element is visible."""
|
def is_displayed(self) -> bool:
|
||||||
...
|
"""@brief True when the element is visible."""
|
||||||
|
...
|
||||||
def get_attribute(self, name: str) -> str | None:
|
|
||||||
"""@brief Read an attribute of the element."""
|
def get_attribute(self, name: str) -> str | None:
|
||||||
...
|
"""@brief Read an attribute of the element."""
|
||||||
|
...
|
||||||
def find_element(self, by: str, value: str) -> "ElementLike":
|
|
||||||
"""@brief Find one element inside this element."""
|
def find_element(self, by: str, value: str) -> "ElementLike":
|
||||||
...
|
"""@brief Find one element inside this element."""
|
||||||
|
...
|
||||||
|
|
||||||
class DriverLike(Protocol):
|
|
||||||
"""@brief The part of a Selenium driver that the adapter uses."""
|
class DriverLike(Protocol):
|
||||||
|
"""@brief The part of a Selenium driver that the adapter uses."""
|
||||||
def get(self, url: str) -> None:
|
|
||||||
"""@brief Load a page."""
|
def get(self, url: str) -> None:
|
||||||
|
"""@brief Load a page."""
|
||||||
def find_element(self, by: str, value: str) -> ElementLike:
|
|
||||||
"""@brief Find one element."""
|
def find_element(self, by: str, value: str) -> ElementLike:
|
||||||
...
|
"""@brief Find one element."""
|
||||||
|
...
|
||||||
def find_elements(self, by: str, value: str) -> Sequence[ElementLike]:
|
|
||||||
"""@brief Find every element that matches."""
|
def find_elements(self, by: str, value: str) -> Sequence[ElementLike]:
|
||||||
...
|
"""@brief Find every element that matches."""
|
||||||
|
...
|
||||||
def save_screenshot(self, filename: str) -> bool:
|
|
||||||
"""@brief Write a PNG screenshot."""
|
def save_screenshot(self, filename: str) -> bool:
|
||||||
...
|
"""@brief Write a PNG screenshot."""
|
||||||
|
...
|
||||||
def execute_script(self, script: str, *args: Any) -> Any: # noqa: ANN401
|
|
||||||
"""@brief Run JavaScript in the page; the arguments are `arguments[n]`."""
|
def execute_script(self, script: str, *args: Any) -> Any: # noqa: ANN401
|
||||||
...
|
"""@brief Run JavaScript in the page; the arguments are `arguments[n]`."""
|
||||||
|
...
|
||||||
def quit(self) -> None:
|
|
||||||
"""@brief Close the browser."""
|
def quit(self) -> None:
|
||||||
|
"""@brief Close the browser."""
|
||||||
|
|
||||||
class SeleniumGame:
|
|
||||||
"""@brief Plays the game page through a Selenium driver."""
|
class SeleniumGame:
|
||||||
|
"""@brief Plays the game page through a Selenium driver."""
|
||||||
def __init__(
|
|
||||||
self,
|
def __init__(
|
||||||
driver: DriverLike,
|
self,
|
||||||
timeout_seconds: float = constants.WAIT_TIMEOUT_SECONDS,
|
driver: DriverLike,
|
||||||
clock: Callable[[], float] = time.monotonic,
|
timeout_seconds: float = constants.WAIT_TIMEOUT_SECONDS,
|
||||||
sleep: Callable[[float], None] = time.sleep,
|
clock: Callable[[], float] = time.monotonic,
|
||||||
) -> None:
|
sleep: Callable[[float], None] = time.sleep,
|
||||||
"""@brief Wrap a driver.
|
) -> None:
|
||||||
|
"""@brief Wrap a driver.
|
||||||
@param driver The Selenium driver (or a fake in tests).
|
|
||||||
@param timeout_seconds Longest wait for an element.
|
@param driver The Selenium driver (or a fake in tests).
|
||||||
@param clock Monotonic clock in seconds.
|
@param timeout_seconds Longest wait for an element.
|
||||||
@param sleep Pause function used between polls.
|
@param clock Monotonic clock in seconds.
|
||||||
"""
|
@param sleep Pause function used between polls.
|
||||||
self._driver = driver
|
"""
|
||||||
self._timeout = timeout_seconds
|
self._driver = driver
|
||||||
self._clock = clock
|
self._timeout = timeout_seconds
|
||||||
self._sleep = sleep
|
self._clock = clock
|
||||||
|
self._sleep = sleep
|
||||||
def open_game(self) -> None:
|
|
||||||
"""@brief Load the game page."""
|
def open_game(self) -> None:
|
||||||
self._driver.get(constants.GAME_URL)
|
"""@brief Load the game page."""
|
||||||
|
self._driver.get(constants.GAME_URL)
|
||||||
def dismiss_language(self) -> None:
|
|
||||||
"""@brief Pick English, then wait until the big cookie can be clicked."""
|
def dismiss_language(self) -> None:
|
||||||
english = self._wait_for(lambda: self._visible(constants.LANGUAGE_ENGLISH_ID))
|
"""@brief Pick English, then wait until the big cookie can be clicked."""
|
||||||
english.click()
|
english = self._wait_for(lambda: self._visible(constants.LANGUAGE_ENGLISH_ID))
|
||||||
self._wait_for(lambda: self._visible(constants.BIG_COOKIE_ID))
|
english.click()
|
||||||
|
self._wait_for(lambda: self._visible(constants.BIG_COOKIE_ID))
|
||||||
def click_cookie(self) -> None:
|
|
||||||
"""@brief Click the big cookie once."""
|
def click_cookie(self) -> None:
|
||||||
try:
|
"""@brief Click the big cookie once."""
|
||||||
self._driver.find_element(By.ID, constants.BIG_COOKIE_ID).click()
|
try:
|
||||||
except (ElementClickInterceptedException, StaleElementReferenceException):
|
self._driver.find_element(By.ID, constants.BIG_COOKIE_ID).click()
|
||||||
# A banner or a re-render covered the cookie for a moment; try again.
|
except (ElementClickInterceptedException, StaleElementReferenceException):
|
||||||
return
|
# A banner or a re-render covered the cookie for a moment; try again.
|
||||||
|
return
|
||||||
def cookie_count(self) -> int:
|
|
||||||
"""@brief Read how many cookies the player has.
|
def cookie_count(self) -> int:
|
||||||
|
"""@brief Read how many cookies the player has.
|
||||||
@return The cookie count.
|
|
||||||
"""
|
@return The cookie count.
|
||||||
return self._read_again_if_stale(
|
"""
|
||||||
lambda: parse_cookie_count(
|
return self._read_again_if_stale(
|
||||||
self._driver.find_element(By.ID, constants.COOKIE_COUNT_ID).text
|
lambda: parse_cookie_count(
|
||||||
)
|
self._driver.find_element(By.ID, constants.COOKIE_COUNT_ID).text
|
||||||
)
|
)
|
||||||
|
)
|
||||||
def cookies_per_second(self) -> float:
|
|
||||||
"""@brief Read how many cookies the player makes each second.
|
def cookies_per_second(self) -> float:
|
||||||
|
"""@brief Read how many cookies the player makes each second.
|
||||||
@return The cookies per second.
|
|
||||||
"""
|
@return The cookies per second.
|
||||||
return self._read_again_if_stale(
|
"""
|
||||||
lambda: parse_cookies_per_second(
|
return self._read_again_if_stale(
|
||||||
self._driver.find_element(By.ID, constants.COOKIES_PER_SECOND_ID).text
|
lambda: parse_cookies_per_second(
|
||||||
)
|
self._driver.find_element(By.ID, constants.COOKIES_PER_SECOND_ID).text
|
||||||
)
|
)
|
||||||
|
)
|
||||||
def upgrades(self) -> list[Upgrade]:
|
|
||||||
"""@brief Read the upgrades the player can see in the right-hand pane.
|
def upgrades(self) -> list[Upgrade]:
|
||||||
|
"""@brief Read the upgrades the player can see in the right-hand pane.
|
||||||
@return The visible upgrades, affordable or not, in store order.
|
|
||||||
"""
|
@return The visible upgrades, affordable or not, in store order.
|
||||||
return self._read_again_if_stale(self._read_upgrades)
|
"""
|
||||||
|
return self._read_again_if_stale(self._read_upgrades)
|
||||||
def _read_upgrades(self) -> list[Upgrade]:
|
|
||||||
"""@brief Find the visible products and read each one."""
|
def _read_upgrades(self) -> list[Upgrade]:
|
||||||
products = self._driver.find_elements(
|
"""@brief Find the visible products and read each one."""
|
||||||
By.CSS_SELECTOR, constants.VISIBLE_PRODUCT_SELECTOR
|
products = self._driver.find_elements(
|
||||||
)
|
By.CSS_SELECTOR, constants.VISIBLE_PRODUCT_SELECTOR
|
||||||
return [self._read_upgrade(product) for product in products]
|
)
|
||||||
|
production = self._read_production()
|
||||||
def _read_again_if_stale(self, read: Callable[[], T]) -> T:
|
return [self._read_upgrade(product, production) for product in products]
|
||||||
"""@brief Run a read; repeat it while the page re-renders under it.
|
|
||||||
|
def _read_production(self) -> list[float]:
|
||||||
@throws StaleElementReferenceException When every attempt went stale.
|
"""@brief Read the cookies per second of one unit of every product.
|
||||||
"""
|
|
||||||
for _ in range(constants.STALE_READ_ATTEMPTS - 1):
|
@return The numbers by product index; empty when the page does not tell.
|
||||||
try:
|
"""
|
||||||
return read()
|
try:
|
||||||
except StaleElementReferenceException:
|
production = self._driver.execute_script(constants.JS_PRODUCTION_SCRIPT)
|
||||||
self._sleep(constants.POLL_INTERVAL_SECONDS)
|
except JavascriptException:
|
||||||
return read()
|
return []
|
||||||
|
return production if isinstance(production, list) else []
|
||||||
def buy(self, upgrade: Upgrade) -> bool:
|
|
||||||
"""@brief Buy an upgrade by clicking its product.
|
def _read_again_if_stale(self, read: Callable[[], T]) -> T:
|
||||||
|
"""@brief Run a read; repeat it while the page re-renders under it.
|
||||||
@param upgrade The upgrade to buy.
|
|
||||||
@return True only when a click really reached the product.
|
@throws StaleElementReferenceException When every attempt went stale.
|
||||||
"""
|
"""
|
||||||
try:
|
for _ in range(constants.STALE_READ_ATTEMPTS - 1):
|
||||||
element = self._driver.find_element(By.ID, upgrade.key)
|
try:
|
||||||
try:
|
return read()
|
||||||
element.click()
|
except StaleElementReferenceException:
|
||||||
except ElementClickInterceptedException:
|
self._sleep(constants.POLL_INTERVAL_SECONDS)
|
||||||
# The cookie-consent banner covers the lower products. Click from
|
return read()
|
||||||
# inside the page instead; the banner itself is never clicked.
|
|
||||||
self._driver.execute_script(constants.JS_CLICK_SCRIPT, element)
|
def buy(self, upgrade: Upgrade) -> bool:
|
||||||
except StaleElementReferenceException:
|
"""@brief Buy an upgrade by clicking its product.
|
||||||
# The pane re-rendered under us; the next check retries.
|
|
||||||
return False
|
@param upgrade The upgrade to buy.
|
||||||
return True
|
@return True only when a click really reached the product.
|
||||||
|
"""
|
||||||
def save_screenshot(self, path: Path) -> None:
|
try:
|
||||||
"""@brief Write a screenshot of the game to a PNG file.
|
element = self._driver.find_element(By.ID, upgrade.key)
|
||||||
|
try:
|
||||||
@param path Target file; its folder is created when missing.
|
element.click()
|
||||||
"""
|
except ElementClickInterceptedException:
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
# The cookie-consent banner covers the lower products. Click from
|
||||||
self._driver.save_screenshot(str(path))
|
# inside the page instead; the banner itself is never clicked.
|
||||||
|
self._driver.execute_script(constants.JS_CLICK_SCRIPT, element)
|
||||||
def close(self) -> None:
|
except StaleElementReferenceException:
|
||||||
"""@brief Close the browser."""
|
# The pane re-rendered under us; the next check retries.
|
||||||
self._driver.quit()
|
return False
|
||||||
|
return True
|
||||||
@staticmethod
|
|
||||||
def _read_upgrade(product: ElementLike) -> Upgrade:
|
def save_screenshot(self, path: Path) -> None:
|
||||||
"""@brief Read the id, name and price of one product element."""
|
"""@brief Write a screenshot of the game to a PNG file.
|
||||||
name = product.find_element(By.CSS_SELECTOR, constants.PRODUCT_NAME_SELECTOR)
|
|
||||||
price = product.find_element(By.CSS_SELECTOR, constants.PRODUCT_PRICE_SELECTOR)
|
@param path Target file; its folder is created when missing.
|
||||||
key = product.get_attribute(constants.ID_ATTRIBUTE) or ""
|
"""
|
||||||
return Upgrade(key=key, name=name.text.strip(), price=parse_amount(price.text))
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._driver.save_screenshot(str(path))
|
||||||
def _visible(self, element_id: str) -> ElementLike | None:
|
|
||||||
"""@brief Return the element with this id when it exists and is displayed."""
|
def close(self) -> None:
|
||||||
try:
|
"""@brief Close the browser."""
|
||||||
element = self._driver.find_element(By.ID, element_id)
|
self._driver.quit()
|
||||||
return element if element.is_displayed() else None
|
|
||||||
except (NoSuchElementException, StaleElementReferenceException):
|
@staticmethod
|
||||||
# Not there yet, or the page re-rendered under us: poll again.
|
def _read_upgrade(product: ElementLike, production: Sequence[float]) -> Upgrade:
|
||||||
return None
|
"""@brief Read the id, name, price and production of one product element."""
|
||||||
|
name = product.find_element(By.CSS_SELECTOR, constants.PRODUCT_NAME_SELECTOR)
|
||||||
def _wait_for(self, locate: Callable[[], T | None]) -> T:
|
price = product.find_element(By.CSS_SELECTOR, constants.PRODUCT_PRICE_SELECTOR)
|
||||||
"""@brief Poll until `locate` returns a value.
|
key = product.get_attribute(constants.ID_ATTRIBUTE) or ""
|
||||||
|
return Upgrade(
|
||||||
@param locate Function that returns the value, or None while not ready.
|
key=key,
|
||||||
@return The value.
|
name=name.text.strip(),
|
||||||
@throws TimeoutError When the value is not there within the timeout.
|
price=parse_amount(price.text),
|
||||||
"""
|
cookies_per_second=SeleniumGame._production_of(key, production),
|
||||||
deadline = self._clock() + self._timeout
|
)
|
||||||
while True:
|
|
||||||
found = locate()
|
@staticmethod
|
||||||
if found is not None:
|
def _production_of(key: str, production: Sequence[float]) -> float | None:
|
||||||
return found
|
"""@brief Look up the production of the product with this element id."""
|
||||||
if self._clock() >= deadline:
|
index = key.removeprefix(constants.PRODUCT_ID_PREFIX)
|
||||||
message = f"the game page was not ready within {self._timeout} s"
|
if not index.isdigit() or int(index) >= len(production):
|
||||||
raise TimeoutError(message)
|
return None
|
||||||
self._sleep(constants.POLL_INTERVAL_SECONDS)
|
value = production[int(index)]
|
||||||
|
return float(value) if isinstance(value, int | float) else None
|
||||||
|
|
||||||
|
def _visible(self, element_id: str) -> ElementLike | None:
|
||||||
|
"""@brief Return the element with this id when it exists and is displayed."""
|
||||||
|
try:
|
||||||
|
element = self._driver.find_element(By.ID, element_id)
|
||||||
|
return element if element.is_displayed() else None
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
# Not there yet, or the page re-rendered under us: poll again.
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _wait_for(self, locate: Callable[[], T | None]) -> T:
|
||||||
|
"""@brief Poll until `locate` returns a value.
|
||||||
|
|
||||||
|
@param locate Function that returns the value, or None while not ready.
|
||||||
|
@return The value.
|
||||||
|
@throws TimeoutError When the value is not there within the timeout.
|
||||||
|
"""
|
||||||
|
deadline = self._clock() + self._timeout
|
||||||
|
while True:
|
||||||
|
found = locate()
|
||||||
|
if found is not None:
|
||||||
|
return found
|
||||||
|
if self._clock() >= deadline:
|
||||||
|
message = f"the game page was not ready within {self._timeout} s"
|
||||||
|
raise TimeoutError(message)
|
||||||
|
self._sleep(constants.POLL_INTERVAL_SECONDS)
|
||||||
|
|||||||
@@ -1,429 +1,490 @@
|
|||||||
"""@file test_selenium_game.py
|
"""@file test_selenium_game.py
|
||||||
@brief Tests of the Selenium adapter against a fake driver (no browser).
|
@brief Tests of the Selenium adapter against a fake driver (no browser).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from selenium.common.exceptions import (
|
from selenium.common.exceptions import (
|
||||||
ElementClickInterceptedException,
|
ElementClickInterceptedException,
|
||||||
NoSuchElementException,
|
JavascriptException,
|
||||||
StaleElementReferenceException,
|
NoSuchElementException,
|
||||||
)
|
StaleElementReferenceException,
|
||||||
|
)
|
||||||
from cookie_clicker_bot import constants
|
|
||||||
from cookie_clicker_bot.domain.upgrades import Upgrade
|
from cookie_clicker_bot import constants
|
||||||
from cookie_clicker_bot.infrastructure.selenium_game import SeleniumGame
|
from cookie_clicker_bot.domain.upgrades import Upgrade
|
||||||
|
from cookie_clicker_bot.infrastructure.selenium_game import SeleniumGame
|
||||||
|
|
||||||
class FakeElement:
|
|
||||||
"""An element that records clicks and may raise on click."""
|
class FakeElement:
|
||||||
|
"""An element that records clicks and may raise on click."""
|
||||||
def __init__(
|
|
||||||
self,
|
def __init__(
|
||||||
displayed: Callable[[], bool] = lambda: True,
|
self,
|
||||||
click_error: Exception | None = None,
|
displayed: Callable[[], bool] = lambda: True,
|
||||||
text: str = "",
|
click_error: Exception | None = None,
|
||||||
attributes: dict[str, str] | None = None,
|
text: str = "",
|
||||||
children: dict[str, "FakeElement"] | None = None,
|
attributes: dict[str, str] | None = None,
|
||||||
) -> None:
|
children: dict[str, "FakeElement"] | None = None,
|
||||||
self.text = text
|
) -> None:
|
||||||
self.clicks = 0
|
self.text = text
|
||||||
self._displayed = displayed
|
self.clicks = 0
|
||||||
self._click_error = click_error
|
self._displayed = displayed
|
||||||
self._attributes = attributes or {}
|
self._click_error = click_error
|
||||||
self._children = children or {}
|
self._attributes = attributes or {}
|
||||||
|
self._children = children or {}
|
||||||
def get_attribute(self, name: str) -> str | None:
|
|
||||||
return self._attributes.get(name)
|
def get_attribute(self, name: str) -> str | None:
|
||||||
|
return self._attributes.get(name)
|
||||||
def find_element(self, by: str, value: str) -> "FakeElement":
|
|
||||||
if by != "css selector" or value not in self._children:
|
def find_element(self, by: str, value: str) -> "FakeElement":
|
||||||
raise NoSuchElementException(value)
|
if by != "css selector" or value not in self._children:
|
||||||
return self._children[value]
|
raise NoSuchElementException(value)
|
||||||
|
return self._children[value]
|
||||||
def click(self) -> None:
|
|
||||||
if self._click_error is not None:
|
def click(self) -> None:
|
||||||
raise self._click_error
|
if self._click_error is not None:
|
||||||
self.clicks += 1
|
raise self._click_error
|
||||||
|
self.clicks += 1
|
||||||
def is_displayed(self) -> bool:
|
|
||||||
return self._displayed()
|
def is_displayed(self) -> bool:
|
||||||
|
return self._displayed()
|
||||||
|
|
||||||
class FakeDriver:
|
|
||||||
"""A driver with elements by id."""
|
class FakeDriver:
|
||||||
|
"""A driver with elements by id."""
|
||||||
def __init__(self, elements: dict[str, FakeElement] | None = None) -> None:
|
|
||||||
self.elements = elements or {}
|
def __init__(self, elements: dict[str, FakeElement] | None = None) -> None:
|
||||||
self.groups: dict[str, list[FakeElement]] = {}
|
self.elements = elements or {}
|
||||||
self.loaded: list[str] = []
|
self.groups: dict[str, list[FakeElement]] = {}
|
||||||
self.screenshots: list[str] = []
|
self.loaded: list[str] = []
|
||||||
self.quit_called = False
|
self.screenshots: list[str] = []
|
||||||
self.scripts: list[tuple[str, FakeElement]] = []
|
self.quit_called = False
|
||||||
|
self.scripts: list[tuple[str, FakeElement]] = []
|
||||||
def execute_script(self, script: str, *args: Any) -> None: # noqa: ANN401
|
self.production: Any = []
|
||||||
self.scripts.append((script, args[0]))
|
|
||||||
|
def execute_script(self, script: str, *args: Any) -> Any: # noqa: ANN401
|
||||||
def get(self, url: str) -> None:
|
if script == constants.JS_PRODUCTION_SCRIPT:
|
||||||
self.loaded.append(url)
|
if isinstance(self.production, Exception):
|
||||||
|
raise self.production
|
||||||
def find_element(self, by: str, value: str) -> FakeElement:
|
return self.production
|
||||||
if by != "id" or value not in self.elements:
|
self.scripts.append((script, args[0]))
|
||||||
raise NoSuchElementException(value)
|
|
||||||
return self.elements[value]
|
def get(self, url: str) -> None:
|
||||||
|
self.loaded.append(url)
|
||||||
def find_elements(self, by: str, value: str) -> list[FakeElement]:
|
|
||||||
if by != "css selector":
|
def find_element(self, by: str, value: str) -> FakeElement:
|
||||||
return []
|
if by != "id" or value not in self.elements:
|
||||||
return self.groups.get(value, [])
|
raise NoSuchElementException(value)
|
||||||
|
return self.elements[value]
|
||||||
def save_screenshot(self, filename: str) -> bool:
|
|
||||||
self.screenshots.append(filename)
|
def find_elements(self, by: str, value: str) -> list[FakeElement]:
|
||||||
return True
|
if by != "css selector":
|
||||||
|
return []
|
||||||
def quit(self) -> None:
|
return self.groups.get(value, [])
|
||||||
self.quit_called = True
|
|
||||||
|
def save_screenshot(self, filename: str) -> bool:
|
||||||
|
self.screenshots.append(filename)
|
||||||
class FakeTime:
|
return True
|
||||||
"""A clock that only moves when the fake sleep is called."""
|
|
||||||
|
def quit(self) -> None:
|
||||||
def __init__(self) -> None:
|
self.quit_called = True
|
||||||
self.now = 0.0
|
|
||||||
self.sleeps: list[float] = []
|
|
||||||
|
class FakeTime:
|
||||||
def clock(self) -> float:
|
"""A clock that only moves when the fake sleep is called."""
|
||||||
return self.now
|
|
||||||
|
def __init__(self) -> None:
|
||||||
def sleep(self, seconds: float) -> None:
|
self.now = 0.0
|
||||||
self.sleeps.append(seconds)
|
self.sleeps: list[float] = []
|
||||||
self.now += seconds
|
|
||||||
|
def clock(self) -> float:
|
||||||
|
return self.now
|
||||||
def make_game(driver: FakeDriver, fake_time: FakeTime | None = None) -> SeleniumGame:
|
|
||||||
fake_time = fake_time or FakeTime()
|
def sleep(self, seconds: float) -> None:
|
||||||
return SeleniumGame(
|
self.sleeps.append(seconds)
|
||||||
driver, timeout_seconds=1.0, clock=fake_time.clock, sleep=fake_time.sleep
|
self.now += seconds
|
||||||
)
|
|
||||||
|
|
||||||
|
def make_game(driver: FakeDriver, fake_time: FakeTime | None = None) -> SeleniumGame:
|
||||||
def ready_driver() -> FakeDriver:
|
fake_time = fake_time or FakeTime()
|
||||||
return FakeDriver(
|
return SeleniumGame(
|
||||||
{
|
driver, timeout_seconds=1.0, clock=fake_time.clock, sleep=fake_time.sleep
|
||||||
constants.LANGUAGE_ENGLISH_ID: FakeElement(),
|
)
|
||||||
constants.BIG_COOKIE_ID: FakeElement(),
|
|
||||||
}
|
|
||||||
)
|
def ready_driver() -> FakeDriver:
|
||||||
|
return FakeDriver(
|
||||||
|
{
|
||||||
def test_open_game_loads_the_game_address() -> None:
|
constants.LANGUAGE_ENGLISH_ID: FakeElement(),
|
||||||
driver = FakeDriver()
|
constants.BIG_COOKIE_ID: FakeElement(),
|
||||||
|
}
|
||||||
make_game(driver).open_game()
|
)
|
||||||
|
|
||||||
assert driver.loaded == [constants.GAME_URL]
|
|
||||||
|
def test_open_game_loads_the_game_address() -> None:
|
||||||
|
driver = FakeDriver()
|
||||||
def test_dismiss_language_clicks_english_and_waits_for_the_cookie() -> None:
|
|
||||||
driver = ready_driver()
|
make_game(driver).open_game()
|
||||||
|
|
||||||
make_game(driver).dismiss_language()
|
assert driver.loaded == [constants.GAME_URL]
|
||||||
|
|
||||||
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
|
||||||
|
def test_dismiss_language_clicks_english_and_waits_for_the_cookie() -> None:
|
||||||
|
driver = ready_driver()
|
||||||
def test_dismiss_language_polls_until_the_button_is_visible() -> None:
|
|
||||||
fake_time = FakeTime()
|
make_game(driver).dismiss_language()
|
||||||
driver = ready_driver()
|
|
||||||
driver.elements[constants.LANGUAGE_ENGLISH_ID] = FakeElement(
|
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
||||||
displayed=lambda: fake_time.now >= 0.4
|
|
||||||
)
|
|
||||||
|
def test_dismiss_language_polls_until_the_button_is_visible() -> None:
|
||||||
make_game(driver, fake_time).dismiss_language()
|
fake_time = FakeTime()
|
||||||
|
driver = ready_driver()
|
||||||
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
driver.elements[constants.LANGUAGE_ENGLISH_ID] = FakeElement(
|
||||||
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * 2
|
displayed=lambda: fake_time.now >= 0.4
|
||||||
|
)
|
||||||
|
|
||||||
def stale_once() -> Callable[[], bool]:
|
make_game(driver, fake_time).dismiss_language()
|
||||||
"""Return an `is_displayed` that goes stale on the first call only."""
|
|
||||||
calls: list[int] = []
|
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
||||||
|
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * 2
|
||||||
def displayed() -> bool:
|
|
||||||
calls.append(1)
|
|
||||||
if len(calls) == 1:
|
def stale_once() -> Callable[[], bool]:
|
||||||
raise StaleElementReferenceException
|
"""Return an `is_displayed` that goes stale on the first call only."""
|
||||||
return True
|
calls: list[int] = []
|
||||||
|
|
||||||
return displayed
|
def displayed() -> bool:
|
||||||
|
calls.append(1)
|
||||||
|
if len(calls) == 1:
|
||||||
def test_dismiss_language_survives_a_cookie_that_goes_stale_once() -> None:
|
raise StaleElementReferenceException
|
||||||
driver = ready_driver()
|
return True
|
||||||
driver.elements[constants.BIG_COOKIE_ID] = FakeElement(displayed=stale_once())
|
|
||||||
|
return displayed
|
||||||
make_game(driver).dismiss_language()
|
|
||||||
|
|
||||||
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
def test_dismiss_language_survives_a_cookie_that_goes_stale_once() -> None:
|
||||||
|
driver = ready_driver()
|
||||||
|
driver.elements[constants.BIG_COOKIE_ID] = FakeElement(displayed=stale_once())
|
||||||
def test_dismiss_language_survives_a_button_that_goes_stale_once() -> None:
|
|
||||||
driver = ready_driver()
|
make_game(driver).dismiss_language()
|
||||||
driver.elements[constants.LANGUAGE_ENGLISH_ID] = FakeElement(displayed=stale_once())
|
|
||||||
|
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
||||||
make_game(driver).dismiss_language()
|
|
||||||
|
|
||||||
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
def test_dismiss_language_survives_a_button_that_goes_stale_once() -> None:
|
||||||
|
driver = ready_driver()
|
||||||
|
driver.elements[constants.LANGUAGE_ENGLISH_ID] = FakeElement(displayed=stale_once())
|
||||||
def test_dismiss_language_survives_a_lookup_that_goes_stale_once() -> None:
|
|
||||||
driver = ready_driver()
|
make_game(driver).dismiss_language()
|
||||||
real_find = driver.find_element
|
|
||||||
calls: list[int] = []
|
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
||||||
|
|
||||||
def find_element(by: str, value: str) -> FakeElement:
|
|
||||||
calls.append(1)
|
def test_dismiss_language_survives_a_lookup_that_goes_stale_once() -> None:
|
||||||
if len(calls) == 1:
|
driver = ready_driver()
|
||||||
raise StaleElementReferenceException
|
real_find = driver.find_element
|
||||||
return real_find(by, value)
|
calls: list[int] = []
|
||||||
|
|
||||||
driver.find_element = find_element # type: ignore[method-assign]
|
def find_element(by: str, value: str) -> FakeElement:
|
||||||
|
calls.append(1)
|
||||||
make_game(driver).dismiss_language()
|
if len(calls) == 1:
|
||||||
|
raise StaleElementReferenceException
|
||||||
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
return real_find(by, value)
|
||||||
|
|
||||||
|
driver.find_element = find_element # type: ignore[method-assign]
|
||||||
def test_dismiss_language_times_out_when_the_button_never_shows() -> None:
|
|
||||||
fake_time = FakeTime()
|
make_game(driver).dismiss_language()
|
||||||
|
|
||||||
with pytest.raises(TimeoutError):
|
assert driver.elements[constants.LANGUAGE_ENGLISH_ID].clicks == 1
|
||||||
make_game(FakeDriver(), fake_time).dismiss_language()
|
|
||||||
|
|
||||||
assert fake_time.now >= 1.0
|
def test_dismiss_language_times_out_when_the_button_never_shows() -> None:
|
||||||
|
fake_time = FakeTime()
|
||||||
|
|
||||||
def test_dismiss_language_times_out_when_the_cookie_never_shows() -> None:
|
with pytest.raises(TimeoutError):
|
||||||
driver = FakeDriver({constants.LANGUAGE_ENGLISH_ID: FakeElement()})
|
make_game(FakeDriver(), fake_time).dismiss_language()
|
||||||
|
|
||||||
with pytest.raises(TimeoutError):
|
assert fake_time.now >= 1.0
|
||||||
make_game(driver).dismiss_language()
|
|
||||||
|
|
||||||
|
def test_dismiss_language_times_out_when_the_cookie_never_shows() -> None:
|
||||||
def test_click_cookie_clicks_the_big_cookie() -> None:
|
driver = FakeDriver({constants.LANGUAGE_ENGLISH_ID: FakeElement()})
|
||||||
driver = ready_driver()
|
|
||||||
|
with pytest.raises(TimeoutError):
|
||||||
make_game(driver).click_cookie()
|
make_game(driver).dismiss_language()
|
||||||
|
|
||||||
assert driver.elements[constants.BIG_COOKIE_ID].clicks == 1
|
|
||||||
|
def test_click_cookie_clicks_the_big_cookie() -> None:
|
||||||
|
driver = ready_driver()
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"error",
|
make_game(driver).click_cookie()
|
||||||
[ElementClickInterceptedException(), StaleElementReferenceException()],
|
|
||||||
)
|
assert driver.elements[constants.BIG_COOKIE_ID].clicks == 1
|
||||||
def test_click_cookie_ignores_a_covered_or_stale_cookie(error: Exception) -> None:
|
|
||||||
driver = FakeDriver({constants.BIG_COOKIE_ID: FakeElement(click_error=error)})
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
make_game(driver).click_cookie()
|
"error",
|
||||||
|
[ElementClickInterceptedException(), StaleElementReferenceException()],
|
||||||
|
)
|
||||||
def make_product(key: str, name: str, price: str) -> FakeElement:
|
def test_click_cookie_ignores_a_covered_or_stale_cookie(error: Exception) -> None:
|
||||||
return FakeElement(
|
driver = FakeDriver({constants.BIG_COOKIE_ID: FakeElement(click_error=error)})
|
||||||
attributes={"id": key},
|
|
||||||
children={
|
make_game(driver).click_cookie()
|
||||||
constants.PRODUCT_NAME_SELECTOR: FakeElement(text=name),
|
|
||||||
constants.PRODUCT_PRICE_SELECTOR: FakeElement(text=price),
|
|
||||||
},
|
def make_product(key: str, name: str, price: str) -> FakeElement:
|
||||||
)
|
return FakeElement(
|
||||||
|
attributes={"id": key},
|
||||||
|
children={
|
||||||
def test_cookie_count_reads_the_cookie_counter() -> None:
|
constants.PRODUCT_NAME_SELECTOR: FakeElement(text=name),
|
||||||
driver = FakeDriver(
|
constants.PRODUCT_PRICE_SELECTOR: FakeElement(text=price),
|
||||||
{
|
},
|
||||||
constants.COOKIE_COUNT_ID: FakeElement(
|
)
|
||||||
text="1.235\nmillion cookies\nper second: 0"
|
|
||||||
)
|
|
||||||
}
|
def test_cookie_count_reads_the_cookie_counter() -> None:
|
||||||
)
|
driver = FakeDriver(
|
||||||
|
{
|
||||||
assert make_game(driver).cookie_count() == 1_235_000
|
constants.COOKIE_COUNT_ID: FakeElement(
|
||||||
|
text="1.235\nmillion cookies\nper second: 0"
|
||||||
|
)
|
||||||
def test_cookies_per_second_reads_its_element() -> None:
|
}
|
||||||
driver = FakeDriver(
|
)
|
||||||
{constants.COOKIES_PER_SECOND_ID: FakeElement(text="per second: 1,234.5")}
|
|
||||||
)
|
assert make_game(driver).cookie_count() == 1_235_000
|
||||||
|
|
||||||
assert make_game(driver).cookies_per_second() == 1234.5
|
|
||||||
|
def test_cookies_per_second_reads_its_element() -> None:
|
||||||
|
driver = FakeDriver(
|
||||||
def test_upgrades_reads_the_visible_products() -> None:
|
{constants.COOKIES_PER_SECOND_ID: FakeElement(text="per second: 1,234.5")}
|
||||||
driver = FakeDriver()
|
)
|
||||||
driver.groups[constants.VISIBLE_PRODUCT_SELECTOR] = [
|
|
||||||
make_product("product0", "Cursor", "15"),
|
assert make_game(driver).cookies_per_second() == 1234.5
|
||||||
make_product("product2", "Farm", "1,100"),
|
|
||||||
]
|
|
||||||
|
def test_upgrades_reads_the_visible_products() -> None:
|
||||||
assert make_game(driver).upgrades() == [
|
driver = FakeDriver()
|
||||||
Upgrade("product0", "Cursor", 15),
|
driver.groups[constants.VISIBLE_PRODUCT_SELECTOR] = [
|
||||||
Upgrade("product2", "Farm", 1100),
|
make_product("product0", "Cursor", "15"),
|
||||||
]
|
make_product("product2", "Farm", "1,100"),
|
||||||
|
]
|
||||||
|
|
||||||
class StaleOnceText(FakeElement):
|
assert make_game(driver).upgrades() == [
|
||||||
"""An element whose text goes stale on the first read."""
|
Upgrade("product0", "Cursor", 15),
|
||||||
|
Upgrade("product2", "Farm", 1100),
|
||||||
def __init__(self, text: str) -> None:
|
]
|
||||||
super().__init__()
|
|
||||||
self._text = text
|
|
||||||
self._reads = 0
|
class StaleOnceText(FakeElement):
|
||||||
|
"""An element whose text goes stale on the first read."""
|
||||||
@property
|
|
||||||
def text(self) -> str:
|
def __init__(self, text: str) -> None:
|
||||||
self._reads += 1
|
super().__init__()
|
||||||
if self._reads == 1:
|
self._text = text
|
||||||
raise StaleElementReferenceException
|
self._reads = 0
|
||||||
return self._text
|
|
||||||
|
@property
|
||||||
@text.setter
|
def text(self) -> str:
|
||||||
def text(self, value: str) -> None:
|
self._reads += 1
|
||||||
self._text = value
|
if self._reads == 1:
|
||||||
|
raise StaleElementReferenceException
|
||||||
|
return self._text
|
||||||
def test_cookie_count_retries_once_when_the_counter_goes_stale() -> None:
|
|
||||||
driver = FakeDriver({constants.COOKIE_COUNT_ID: StaleOnceText("42 cookies")})
|
@text.setter
|
||||||
|
def text(self, value: str) -> None:
|
||||||
assert make_game(driver).cookie_count() == 42
|
self._text = value
|
||||||
|
|
||||||
|
|
||||||
def test_cookies_per_second_retries_once_when_the_element_goes_stale() -> None:
|
def test_cookie_count_retries_once_when_the_counter_goes_stale() -> None:
|
||||||
driver = FakeDriver(
|
driver = FakeDriver({constants.COOKIE_COUNT_ID: StaleOnceText("42 cookies")})
|
||||||
{constants.COOKIES_PER_SECOND_ID: StaleOnceText("per second: 3.5")}
|
|
||||||
)
|
assert make_game(driver).cookie_count() == 42
|
||||||
|
|
||||||
assert make_game(driver).cookies_per_second() == 3.5
|
|
||||||
|
def test_cookies_per_second_retries_once_when_the_element_goes_stale() -> None:
|
||||||
|
driver = FakeDriver(
|
||||||
def test_upgrades_retries_once_when_a_product_goes_stale() -> None:
|
{constants.COOKIES_PER_SECOND_ID: StaleOnceText("per second: 3.5")}
|
||||||
product = make_product("product0", "Cursor", "15")
|
)
|
||||||
product.get_attribute = _stale_once_then(product.get_attribute) # type: ignore[assignment]
|
|
||||||
driver = FakeDriver()
|
assert make_game(driver).cookies_per_second() == 3.5
|
||||||
driver.groups[constants.VISIBLE_PRODUCT_SELECTOR] = [product]
|
|
||||||
|
|
||||||
assert make_game(driver).upgrades() == [Upgrade("product0", "Cursor", 15)]
|
def test_upgrades_retries_once_when_a_product_goes_stale() -> None:
|
||||||
|
product = make_product("product0", "Cursor", "15")
|
||||||
|
product.get_attribute = _stale_once_then(product.get_attribute) # type: ignore[assignment]
|
||||||
def test_cookies_per_second_succeeds_after_four_stale_attempts() -> None:
|
driver = FakeDriver()
|
||||||
driver = FakeDriver()
|
driver.groups[constants.VISIBLE_PRODUCT_SELECTOR] = [product]
|
||||||
attempts = _stale_for(4, FakeElement(text="per second: 3.5"))
|
|
||||||
driver.find_element = attempts.find_element # type: ignore[method-assign]
|
assert make_game(driver).upgrades() == [Upgrade("product0", "Cursor", 15)]
|
||||||
fake_time = FakeTime()
|
|
||||||
|
|
||||||
assert make_game(driver, fake_time).cookies_per_second() == 3.5
|
def test_cookies_per_second_succeeds_after_four_stale_attempts() -> None:
|
||||||
assert attempts.calls == 5
|
driver = FakeDriver()
|
||||||
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * 4
|
attempts = _stale_for(4, FakeElement(text="per second: 3.5"))
|
||||||
|
driver.find_element = attempts.find_element # type: ignore[method-assign]
|
||||||
|
fake_time = FakeTime()
|
||||||
def test_cookie_count_gives_up_when_the_counter_stays_stale() -> None:
|
|
||||||
driver = FakeDriver()
|
assert make_game(driver, fake_time).cookies_per_second() == 3.5
|
||||||
attempts = _stale_for(100, FakeElement(text="42 cookies"))
|
assert attempts.calls == 5
|
||||||
driver.find_element = attempts.find_element # type: ignore[method-assign]
|
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * 4
|
||||||
fake_time = FakeTime()
|
|
||||||
|
|
||||||
with pytest.raises(StaleElementReferenceException):
|
def test_cookie_count_gives_up_when_the_counter_stays_stale() -> None:
|
||||||
make_game(driver, fake_time).cookie_count()
|
driver = FakeDriver()
|
||||||
|
attempts = _stale_for(100, FakeElement(text="42 cookies"))
|
||||||
assert attempts.calls == constants.STALE_READ_ATTEMPTS
|
driver.find_element = attempts.find_element # type: ignore[method-assign]
|
||||||
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * (
|
fake_time = FakeTime()
|
||||||
constants.STALE_READ_ATTEMPTS - 1
|
|
||||||
)
|
with pytest.raises(StaleElementReferenceException):
|
||||||
|
make_game(driver, fake_time).cookie_count()
|
||||||
|
|
||||||
class _StaleFor:
|
assert attempts.calls == constants.STALE_READ_ATTEMPTS
|
||||||
"""A `find_element` that goes stale for the first `stale` calls."""
|
assert fake_time.sleeps == [constants.POLL_INTERVAL_SECONDS] * (
|
||||||
|
constants.STALE_READ_ATTEMPTS - 1
|
||||||
def __init__(self, stale: int, element: FakeElement) -> None:
|
)
|
||||||
self._stale = stale
|
|
||||||
self._element = element
|
|
||||||
self.calls = 0
|
class _StaleFor:
|
||||||
|
"""A `find_element` that goes stale for the first `stale` calls."""
|
||||||
def find_element(self, by: str, value: str) -> FakeElement:
|
|
||||||
self.calls += 1
|
def __init__(self, stale: int, element: FakeElement) -> None:
|
||||||
if self.calls <= self._stale:
|
self._stale = stale
|
||||||
raise StaleElementReferenceException
|
self._element = element
|
||||||
return self._element
|
self.calls = 0
|
||||||
|
|
||||||
|
def find_element(self, by: str, value: str) -> FakeElement:
|
||||||
def _stale_for(stale: int, element: FakeElement) -> _StaleFor:
|
self.calls += 1
|
||||||
return _StaleFor(stale, element)
|
if self.calls <= self._stale:
|
||||||
|
raise StaleElementReferenceException
|
||||||
|
return self._element
|
||||||
def _stale_once_then(
|
|
||||||
original: Callable[[str], str | None],
|
|
||||||
) -> Callable[[str], str | None]:
|
def _stale_for(stale: int, element: FakeElement) -> _StaleFor:
|
||||||
calls: list[int] = []
|
return _StaleFor(stale, element)
|
||||||
|
|
||||||
def get_attribute(name: str) -> str | None:
|
|
||||||
calls.append(1)
|
def _stale_once_then(
|
||||||
if len(calls) == 1:
|
original: Callable[[str], str | None],
|
||||||
raise StaleElementReferenceException
|
) -> Callable[[str], str | None]:
|
||||||
return original(name)
|
calls: list[int] = []
|
||||||
|
|
||||||
return get_attribute
|
def get_attribute(name: str) -> str | None:
|
||||||
|
calls.append(1)
|
||||||
|
if len(calls) == 1:
|
||||||
def test_upgrades_is_empty_when_no_product_is_visible() -> None:
|
raise StaleElementReferenceException
|
||||||
assert make_game(FakeDriver()).upgrades() == []
|
return original(name)
|
||||||
|
|
||||||
|
return get_attribute
|
||||||
def test_buy_clicks_the_product_with_the_upgrade_key() -> None:
|
|
||||||
driver = FakeDriver({"product1": FakeElement()})
|
|
||||||
|
def test_upgrades_is_empty_when_no_product_is_visible() -> None:
|
||||||
bought = make_game(driver).buy(Upgrade("product1", "Grandma", 100))
|
assert make_game(FakeDriver()).upgrades() == []
|
||||||
|
|
||||||
assert bought is True
|
|
||||||
assert driver.elements["product1"].clicks == 1
|
def test_buy_clicks_the_product_with_the_upgrade_key() -> None:
|
||||||
assert driver.scripts == []
|
driver = FakeDriver({"product1": FakeElement()})
|
||||||
|
|
||||||
|
bought = make_game(driver).buy(Upgrade("product1", "Grandma", 100))
|
||||||
def test_buy_clicks_through_javascript_when_a_banner_covers_the_product() -> None:
|
|
||||||
element = FakeElement(click_error=ElementClickInterceptedException())
|
assert bought is True
|
||||||
driver = FakeDriver({"product1": element})
|
assert driver.elements["product1"].clicks == 1
|
||||||
|
assert driver.scripts == []
|
||||||
bought = make_game(driver).buy(Upgrade("product1", "Grandma", 100))
|
|
||||||
|
|
||||||
assert bought is True
|
def test_buy_clicks_through_javascript_when_a_banner_covers_the_product() -> None:
|
||||||
assert driver.scripts == [(constants.JS_CLICK_SCRIPT, element)]
|
element = FakeElement(click_error=ElementClickInterceptedException())
|
||||||
|
driver = FakeDriver({"product1": element})
|
||||||
|
|
||||||
def test_buy_returns_false_for_a_stale_product() -> None:
|
bought = make_game(driver).buy(Upgrade("product1", "Grandma", 100))
|
||||||
driver = FakeDriver(
|
|
||||||
{"product1": FakeElement(click_error=StaleElementReferenceException())}
|
assert bought is True
|
||||||
)
|
assert driver.scripts == [(constants.JS_CLICK_SCRIPT, element)]
|
||||||
|
|
||||||
assert make_game(driver).buy(Upgrade("product1", "Grandma", 100)) is False
|
|
||||||
assert driver.scripts == []
|
def test_buy_returns_false_for_a_stale_product() -> None:
|
||||||
|
driver = FakeDriver(
|
||||||
|
{"product1": FakeElement(click_error=StaleElementReferenceException())}
|
||||||
def test_save_screenshot_creates_the_folder_and_calls_the_driver(
|
)
|
||||||
tmp_path: Path,
|
|
||||||
) -> None:
|
assert make_game(driver).buy(Upgrade("product1", "Grandma", 100)) is False
|
||||||
driver = FakeDriver()
|
assert driver.scripts == []
|
||||||
target = tmp_path / "images" / "shot.png"
|
|
||||||
|
|
||||||
make_game(driver).save_screenshot(target)
|
def test_save_screenshot_creates_the_folder_and_calls_the_driver(
|
||||||
|
tmp_path: Path,
|
||||||
assert target.parent.is_dir()
|
) -> None:
|
||||||
assert driver.screenshots == [str(target)]
|
driver = FakeDriver()
|
||||||
|
target = tmp_path / "images" / "shot.png"
|
||||||
|
|
||||||
def test_close_quits_the_driver() -> None:
|
make_game(driver).save_screenshot(target)
|
||||||
driver = FakeDriver()
|
|
||||||
|
assert target.parent.is_dir()
|
||||||
make_game(driver).close()
|
assert driver.screenshots == [str(target)]
|
||||||
|
|
||||||
assert driver.quit_called
|
|
||||||
|
def test_close_quits_the_driver() -> None:
|
||||||
|
driver = FakeDriver()
|
||||||
|
|
||||||
|
make_game(driver).close()
|
||||||
|
|
||||||
|
assert driver.quit_called
|
||||||
|
|
||||||
|
|
||||||
|
def _driver_with_two_products() -> FakeDriver:
|
||||||
|
driver = FakeDriver()
|
||||||
|
driver.groups[constants.VISIBLE_PRODUCT_SELECTOR] = [
|
||||||
|
make_product("product0", "Cursor", "15"),
|
||||||
|
make_product("product1", "Grandma", "100"),
|
||||||
|
]
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
def test_upgrades_reads_the_production_of_each_product() -> None:
|
||||||
|
driver = _driver_with_two_products()
|
||||||
|
driver.production = [0.1, 1]
|
||||||
|
|
||||||
|
assert make_game(driver).upgrades() == [
|
||||||
|
Upgrade("product0", "Cursor", 15, 0.1),
|
||||||
|
Upgrade("product1", "Grandma", 100, 1.0),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_upgrades_leaves_the_production_unknown_when_the_script_fails() -> None:
|
||||||
|
driver = _driver_with_two_products()
|
||||||
|
driver.production = JavascriptException("Game is not defined")
|
||||||
|
|
||||||
|
upgrades = make_game(driver).upgrades()
|
||||||
|
|
||||||
|
assert [upgrade.cookies_per_second for upgrade in upgrades] == [None, None]
|
||||||
|
|
||||||
|
|
||||||
|
def test_upgrades_leaves_the_production_unknown_for_a_product_the_list_lacks() -> None:
|
||||||
|
driver = _driver_with_two_products()
|
||||||
|
driver.production = [0.1]
|
||||||
|
|
||||||
|
upgrades = make_game(driver).upgrades()
|
||||||
|
|
||||||
|
assert [upgrade.cookies_per_second for upgrade in upgrades] == [0.1, None]
|
||||||
|
|
||||||
|
|
||||||
|
def test_upgrades_leaves_a_production_that_is_not_a_number_unknown() -> None:
|
||||||
|
driver = _driver_with_two_products()
|
||||||
|
driver.production = [{}, 1]
|
||||||
|
|
||||||
|
upgrades = make_game(driver).upgrades()
|
||||||
|
|
||||||
|
assert [upgrade.cookies_per_second for upgrade in upgrades] == [None, 1.0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_upgrades_leaves_the_production_unknown_for_an_unexpected_answer() -> None:
|
||||||
|
driver = _driver_with_two_products()
|
||||||
|
driver.production = None
|
||||||
|
|
||||||
|
upgrades = make_game(driver).upgrades()
|
||||||
|
|
||||||
|
assert [upgrade.cookies_per_second for upgrade in upgrades] == [None, None]
|
||||||
|
|||||||
+241
-216
@@ -1,216 +1,241 @@
|
|||||||
"""@file test_cli.py
|
"""@file test_cli.py
|
||||||
@brief Tests of the command line and the run sequence with a fake game.
|
@brief Tests of the command line and the run sequence with a fake game.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from cookie_clicker_bot import constants
|
from cookie_clicker_bot import constants
|
||||||
from cookie_clicker_bot.application.ports import GamePort
|
from cookie_clicker_bot.application.ports import GamePort
|
||||||
from cookie_clicker_bot.cli import main, parse_args
|
from cookie_clicker_bot.cli import main, parse_args
|
||||||
from cookie_clicker_bot.domain.upgrades import Upgrade
|
from cookie_clicker_bot.domain.upgrades import Upgrade
|
||||||
|
|
||||||
|
|
||||||
class RecordingGame:
|
class RecordingGame:
|
||||||
"""A game that records every call in order."""
|
"""A game that records every call in order."""
|
||||||
|
|
||||||
def __init__(self, fail_on_open: bool = False, buy_succeeds: bool = True) -> None:
|
def __init__(self, fail_on_open: bool = False, buy_succeeds: bool = True) -> None:
|
||||||
self._buy_succeeds = buy_succeeds
|
self._buy_succeeds = buy_succeeds
|
||||||
self.calls: list[str] = []
|
self.calls: list[str] = []
|
||||||
self.screenshots: list[Path] = []
|
self.screenshots: list[Path] = []
|
||||||
self._fail_on_open = fail_on_open
|
self._fail_on_open = fail_on_open
|
||||||
|
|
||||||
def open_game(self) -> None:
|
def open_game(self) -> None:
|
||||||
self.calls.append("open_game")
|
self.calls.append("open_game")
|
||||||
if self._fail_on_open:
|
if self._fail_on_open:
|
||||||
message = "boom"
|
message = "boom"
|
||||||
raise RuntimeError(message)
|
raise RuntimeError(message)
|
||||||
|
|
||||||
def dismiss_language(self) -> None:
|
def dismiss_language(self) -> None:
|
||||||
self.calls.append("dismiss_language")
|
self.calls.append("dismiss_language")
|
||||||
|
|
||||||
def click_cookie(self) -> None:
|
def click_cookie(self) -> None:
|
||||||
self.calls.append("click_cookie")
|
self.calls.append("click_cookie")
|
||||||
|
|
||||||
def cookie_count(self) -> int:
|
def cookie_count(self) -> int:
|
||||||
return 1000
|
return 1000
|
||||||
|
|
||||||
def cookies_per_second(self) -> float:
|
def cookies_per_second(self) -> float:
|
||||||
return 8.5
|
return 8.5
|
||||||
|
|
||||||
def upgrades(self) -> list[Upgrade]:
|
def upgrades(self) -> list[Upgrade]:
|
||||||
return [Upgrade("product0", "Cursor", 15)]
|
return [Upgrade("product0", "Cursor", 15)]
|
||||||
|
|
||||||
def buy(self, upgrade: Upgrade) -> bool:
|
def buy(self, upgrade: Upgrade) -> bool:
|
||||||
self.calls.append("buy")
|
self.calls.append("buy")
|
||||||
return self._buy_succeeds
|
return self._buy_succeeds
|
||||||
|
|
||||||
def save_screenshot(self, path: Path) -> None:
|
def save_screenshot(self, path: Path) -> None:
|
||||||
self.calls.append("save_screenshot")
|
self.calls.append("save_screenshot")
|
||||||
self.screenshots.append(path)
|
self.screenshots.append(path)
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
self.calls.append("close")
|
self.calls.append("close")
|
||||||
|
|
||||||
|
|
||||||
def test_parse_args_uses_defaults() -> None:
|
def test_parse_args_uses_defaults() -> None:
|
||||||
args = parse_args([])
|
args = parse_args([])
|
||||||
|
|
||||||
assert args.duration == constants.RUN_DURATION_SECONDS == 300
|
assert args.duration == constants.RUN_DURATION_SECONDS == 300
|
||||||
assert args.interval == constants.UPGRADE_CHECK_INTERVAL_SECONDS == 5
|
assert args.interval == constants.UPGRADE_CHECK_INTERVAL_SECONDS == 5
|
||||||
assert args.headless is False
|
assert args.headless is False
|
||||||
assert args.screenshot_start is None
|
assert args.screenshot_start is None
|
||||||
assert args.screenshot is None
|
assert args.screenshot is None
|
||||||
|
|
||||||
|
|
||||||
def test_parse_args_reads_every_option() -> None:
|
def test_parse_args_reads_every_option() -> None:
|
||||||
args = parse_args(
|
args = parse_args(
|
||||||
[
|
[
|
||||||
"--headless",
|
"--headless",
|
||||||
"--duration",
|
"--duration",
|
||||||
"10",
|
"10",
|
||||||
"--interval",
|
"--interval",
|
||||||
"2.5",
|
"2.5",
|
||||||
"--screenshot-start",
|
"--screenshot-start",
|
||||||
"a.png",
|
"a.png",
|
||||||
"--screenshot",
|
"--screenshot",
|
||||||
"b.png",
|
"b.png",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
assert args.headless is True
|
assert args.headless is True
|
||||||
assert args.duration == 10
|
assert args.duration == 10
|
||||||
assert args.interval == 2.5
|
assert args.interval == 2.5
|
||||||
assert args.screenshot_start == Path("a.png")
|
assert args.screenshot_start == Path("a.png")
|
||||||
assert args.screenshot == Path("b.png")
|
assert args.screenshot == Path("b.png")
|
||||||
|
|
||||||
|
|
||||||
def test_main_opens_dismisses_and_closes() -> None:
|
def test_main_opens_dismisses_and_closes() -> None:
|
||||||
game = RecordingGame()
|
game = RecordingGame()
|
||||||
|
|
||||||
code = main(["--duration", "0"], lambda _headless: game)
|
code = main(["--duration", "0"], lambda _headless: game)
|
||||||
|
|
||||||
assert code == 0
|
assert code == 0
|
||||||
assert game.calls == ["open_game", "dismiss_language", "close"]
|
assert game.calls == ["open_game", "dismiss_language", "close"]
|
||||||
|
|
||||||
|
|
||||||
def test_main_passes_headless_to_the_factory() -> None:
|
def test_main_passes_headless_to_the_factory() -> None:
|
||||||
seen: list[bool] = []
|
seen: list[bool] = []
|
||||||
|
|
||||||
def factory(headless: bool) -> GamePort:
|
def factory(headless: bool) -> GamePort:
|
||||||
seen.append(headless)
|
seen.append(headless)
|
||||||
return RecordingGame()
|
return RecordingGame()
|
||||||
|
|
||||||
main(["--headless", "--duration", "0"], factory)
|
main(["--headless", "--duration", "0"], factory)
|
||||||
|
|
||||||
assert seen == [True]
|
assert seen == [True]
|
||||||
|
|
||||||
|
|
||||||
def test_main_takes_both_screenshots_when_asked() -> None:
|
def test_main_takes_both_screenshots_when_asked() -> None:
|
||||||
game = RecordingGame()
|
game = RecordingGame()
|
||||||
|
|
||||||
main(
|
main(
|
||||||
["--duration", "0", "--screenshot-start", "s.png", "--screenshot", "e.png"],
|
["--duration", "0", "--screenshot-start", "s.png", "--screenshot", "e.png"],
|
||||||
lambda _headless: game,
|
lambda _headless: game,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert game.screenshots == [Path("s.png"), Path("e.png")]
|
assert game.screenshots == [Path("s.png"), Path("e.png")]
|
||||||
assert game.calls[-1] == "close"
|
assert game.calls[-1] == "close"
|
||||||
|
|
||||||
|
|
||||||
def test_main_takes_no_screenshot_by_default() -> None:
|
def test_main_takes_no_screenshot_by_default() -> None:
|
||||||
game = RecordingGame()
|
game = RecordingGame()
|
||||||
|
|
||||||
main(["--duration", "0"], lambda _headless: game)
|
main(["--duration", "0"], lambda _headless: game)
|
||||||
|
|
||||||
assert game.screenshots == []
|
assert game.screenshots == []
|
||||||
|
|
||||||
|
|
||||||
def test_main_closes_the_game_when_an_error_is_raised() -> None:
|
def test_main_closes_the_game_when_an_error_is_raised() -> None:
|
||||||
game = RecordingGame(fail_on_open=True)
|
game = RecordingGame(fail_on_open=True)
|
||||||
|
|
||||||
with pytest.raises(RuntimeError, match="boom"):
|
with pytest.raises(RuntimeError, match="boom"):
|
||||||
main(["--duration", "0"], lambda _headless: game)
|
main(["--duration", "0"], lambda _headless: game)
|
||||||
|
|
||||||
assert game.calls == ["open_game", "close"]
|
assert game.calls == ["open_game", "close"]
|
||||||
|
|
||||||
|
|
||||||
def test_main_prints_exactly_one_result_line_on_stdout(
|
def test_main_prints_exactly_one_result_line_on_stdout(
|
||||||
capsys: pytest.CaptureFixture[str], caplog: pytest.LogCaptureFixture
|
capsys: pytest.CaptureFixture[str], caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
caplog.set_level("INFO")
|
caplog.set_level("INFO")
|
||||||
|
|
||||||
main(["--duration", "0"], lambda _headless: RecordingGame())
|
main(["--duration", "0"], lambda _headless: RecordingGame())
|
||||||
|
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
assert out == "cookies/second: 8.5" + chr(10)
|
assert out == "cookies/second: 8.5" + chr(10)
|
||||||
assert "clicked the cookie" not in out
|
assert "clicked the cookie" not in out
|
||||||
assert "clicked the cookie" in caplog.text
|
assert "clicked the cookie" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_main_logs_bought_only_for_real_purchases(
|
def test_main_logs_bought_only_for_real_purchases(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
caplog.set_level("INFO")
|
caplog.set_level("INFO")
|
||||||
|
|
||||||
main(["--duration", "0.05", "--interval", "0.001"], lambda _h: RecordingGame())
|
main(["--duration", "0.05", "--interval", "0.001"], lambda _h: RecordingGame())
|
||||||
assert "bought Cursor" in caplog.text
|
assert "bought Cursor" in caplog.text
|
||||||
|
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
game = RecordingGame(buy_succeeds=False)
|
game = RecordingGame(buy_succeeds=False)
|
||||||
main(["--duration", "0.05", "--interval", "0.001"], lambda _h: game)
|
main(["--duration", "0.05", "--interval", "0.001"], lambda _h: game)
|
||||||
assert "buy" in game.calls
|
assert "buy" in game.calls
|
||||||
assert "bought Cursor" not in caplog.text
|
assert "bought Cursor" not in caplog.text
|
||||||
assert "bought 0 upgrades" in caplog.text
|
assert "bought 0 upgrades" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_main_prints_nothing_on_stdout_when_the_run_fails(
|
def test_main_prints_nothing_on_stdout_when_the_run_fails(
|
||||||
capsys: pytest.CaptureFixture[str],
|
capsys: pytest.CaptureFixture[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
with pytest.raises(RuntimeError, match="boom"):
|
with pytest.raises(RuntimeError, match="boom"):
|
||||||
main(["--duration", "0"], lambda _headless: RecordingGame(fail_on_open=True))
|
main(["--duration", "0"], lambda _headless: RecordingGame(fail_on_open=True))
|
||||||
|
|
||||||
assert capsys.readouterr().out == ""
|
assert capsys.readouterr().out == ""
|
||||||
|
|
||||||
|
|
||||||
def test_parse_args_buys_one_upgrade_unless_buy_all_is_given() -> None:
|
def test_parse_args_buys_one_upgrade_unless_buy_all_is_given() -> None:
|
||||||
assert parse_args([]).buy_all is False
|
assert parse_args([]).buy_all is False
|
||||||
assert parse_args(["--buy-all"]).buy_all is True
|
assert parse_args(["--buy-all"]).buy_all is True
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("value", ["0", "-1", "abc", "nan"])
|
def test_parse_args_best_value_is_off_by_default() -> None:
|
||||||
def test_parse_args_rejects_an_interval_that_is_not_positive(
|
assert parse_args([]).best_value is False
|
||||||
value: str, capsys: pytest.CaptureFixture[str]
|
assert parse_args(["--best-value"]).best_value is True
|
||||||
) -> None:
|
|
||||||
with pytest.raises(SystemExit) as exit_info:
|
|
||||||
parse_args(["--interval", value])
|
def test_parse_args_rejects_buy_all_together_with_best_value(
|
||||||
|
capsys: pytest.CaptureFixture[str],
|
||||||
assert exit_info.value.code == 2
|
) -> None:
|
||||||
assert "greater than 0" in capsys.readouterr().err
|
with pytest.raises(SystemExit) as exit_info:
|
||||||
|
parse_args(["--buy-all", "--best-value"])
|
||||||
|
|
||||||
def test_main_logs_the_interval_and_the_strategy(
|
assert exit_info.value.code == 2
|
||||||
caplog: pytest.LogCaptureFixture,
|
assert "not allowed with" in capsys.readouterr().err
|
||||||
) -> None:
|
|
||||||
caplog.set_level("INFO")
|
|
||||||
|
def test_main_logs_the_best_value_strategy(
|
||||||
main(["--duration", "0"], lambda _h: RecordingGame())
|
caplog: pytest.LogCaptureFixture,
|
||||||
assert "every 5 seconds, strategy buy-one" in caplog.text
|
) -> None:
|
||||||
|
caplog.set_level("INFO")
|
||||||
caplog.clear()
|
|
||||||
main(
|
main(["--duration", "0", "--best-value"], lambda _h: RecordingGame())
|
||||||
["--duration", "0", "--interval", "2", "--buy-all"], lambda _h: RecordingGame()
|
|
||||||
)
|
assert "strategy best-value" in caplog.text
|
||||||
assert "every 2.0 seconds, strategy buy-all" in caplog.text
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", ["0", "-1", "abc", "nan"])
|
||||||
def test_main_buy_all_buys_every_affordable_upgrade_per_check(
|
def test_parse_args_rejects_an_interval_that_is_not_positive(
|
||||||
caplog: pytest.LogCaptureFixture,
|
value: str, capsys: pytest.CaptureFixture[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
caplog.set_level("INFO")
|
with pytest.raises(SystemExit) as exit_info:
|
||||||
game = RecordingGame()
|
parse_args(["--interval", value])
|
||||||
|
|
||||||
main(["--duration", "0.05", "--interval", "0.001", "--buy-all"], lambda _h: game)
|
assert exit_info.value.code == 2
|
||||||
|
assert "greater than 0" in capsys.readouterr().err
|
||||||
assert "bought Cursor" in caplog.text
|
|
||||||
|
|
||||||
|
def test_main_logs_the_interval_and_the_strategy(
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
caplog.set_level("INFO")
|
||||||
|
|
||||||
|
main(["--duration", "0"], lambda _h: RecordingGame())
|
||||||
|
assert "every 5 seconds, strategy buy-one" in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
|
main(
|
||||||
|
["--duration", "0", "--interval", "2", "--buy-all"], lambda _h: RecordingGame()
|
||||||
|
)
|
||||||
|
assert "every 2.0 seconds, strategy buy-all" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_buy_all_buys_every_affordable_upgrade_per_check(
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
caplog.set_level("INFO")
|
||||||
|
game = RecordingGame()
|
||||||
|
|
||||||
|
main(["--duration", "0.05", "--interval", "0.001", "--buy-all"], lambda _h: game)
|
||||||
|
|
||||||
|
assert "bought Cursor" in caplog.text
|
||||||
|
|||||||
+282
-250
@@ -1,250 +1,282 @@
|
|||||||
"""@file test_game_loop.py
|
"""@file test_game_loop.py
|
||||||
@brief Tests of the game loop with a fake game and a fake clock.
|
@brief Tests of the game loop with a fake game and a fake clock.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from cookie_clicker_bot.application.game_loop import (
|
from cookie_clicker_bot.application.game_loop import (
|
||||||
PlayResult,
|
PlayResult,
|
||||||
buy_most_expensive_upgrade,
|
buy_best_value_upgrade,
|
||||||
buy_upgrades,
|
buy_most_expensive_upgrade,
|
||||||
click_and_buy,
|
buy_upgrades,
|
||||||
click_cookie,
|
click_and_buy,
|
||||||
)
|
click_cookie,
|
||||||
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
)
|
||||||
from cookie_clicker_bot.domain.upgrades import Upgrade
|
from cookie_clicker_bot.domain.buy_strategy import BuyStrategy
|
||||||
|
from cookie_clicker_bot.domain.upgrades import Upgrade
|
||||||
CURSOR = Upgrade("product0", "Cursor", 15)
|
|
||||||
GRANDMA = Upgrade("product1", "Grandma", 100)
|
VALUE_CURSOR = Upgrade("product0", "Cursor", 15, 0.1)
|
||||||
|
VALUE_GRANDMA = Upgrade("product1", "Grandma", 100, 1.0)
|
||||||
|
|
||||||
class FakeClock:
|
CURSOR = Upgrade("product0", "Cursor", 15)
|
||||||
"""A clock that only moves when the fake game is clicked."""
|
GRANDMA = Upgrade("product1", "Grandma", 100)
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.now = 0.0
|
class FakeClock:
|
||||||
|
"""A clock that only moves when the fake game is clicked."""
|
||||||
def __call__(self) -> float:
|
|
||||||
return self.now
|
def __init__(self) -> None:
|
||||||
|
self.now = 0.0
|
||||||
|
|
||||||
class FakeGame:
|
def __call__(self) -> float:
|
||||||
"""A game with a cookie count and upgrades; each click takes one second."""
|
return self.now
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
class FakeGame:
|
||||||
cookies: int = 0,
|
"""A game with a cookie count and upgrades; each click takes one second."""
|
||||||
upgrades: list[Upgrade] | None = None,
|
|
||||||
clock: FakeClock | None = None,
|
def __init__(
|
||||||
buy_succeeds: bool = True,
|
self,
|
||||||
) -> None:
|
cookies: int = 0,
|
||||||
self.buy_succeeds = buy_succeeds
|
upgrades: list[Upgrade] | None = None,
|
||||||
self.clicks = 0
|
clock: FakeClock | None = None,
|
||||||
self.cookies = cookies
|
buy_succeeds: bool = True,
|
||||||
self.available = upgrades or []
|
) -> None:
|
||||||
self.bought: list[Upgrade] = []
|
self.buy_succeeds = buy_succeeds
|
||||||
self.buy_times: list[float] = []
|
self.clicks = 0
|
||||||
self._clock = clock
|
self.cookies = cookies
|
||||||
|
self.available = upgrades or []
|
||||||
def open_game(self) -> None:
|
self.bought: list[Upgrade] = []
|
||||||
pass
|
self.buy_times: list[float] = []
|
||||||
|
self._clock = clock
|
||||||
def dismiss_language(self) -> None:
|
|
||||||
pass
|
def open_game(self) -> None:
|
||||||
|
pass
|
||||||
def click_cookie(self) -> None:
|
|
||||||
self.clicks += 1
|
def dismiss_language(self) -> None:
|
||||||
if self._clock is not None:
|
pass
|
||||||
self._clock.now += 1.0
|
|
||||||
|
def click_cookie(self) -> None:
|
||||||
def cookie_count(self) -> int:
|
self.clicks += 1
|
||||||
return self.cookies
|
if self._clock is not None:
|
||||||
|
self._clock.now += 1.0
|
||||||
def cookies_per_second(self) -> float:
|
|
||||||
return 0.0
|
def cookie_count(self) -> int:
|
||||||
|
return self.cookies
|
||||||
def upgrades(self) -> list[Upgrade]:
|
|
||||||
return self.available
|
def cookies_per_second(self) -> float:
|
||||||
|
return 0.0
|
||||||
def buy(self, upgrade: Upgrade) -> bool:
|
|
||||||
self.bought.append(upgrade)
|
def upgrades(self) -> list[Upgrade]:
|
||||||
if self._clock is not None:
|
return self.available
|
||||||
self.buy_times.append(self._clock.now)
|
|
||||||
return self.buy_succeeds
|
def buy(self, upgrade: Upgrade) -> bool:
|
||||||
|
self.bought.append(upgrade)
|
||||||
def save_screenshot(self, path: Path) -> None:
|
if self._clock is not None:
|
||||||
pass
|
self.buy_times.append(self._clock.now)
|
||||||
|
return self.buy_succeeds
|
||||||
def close(self) -> None:
|
|
||||||
pass
|
def save_screenshot(self, path: Path) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_click_cookie_delegates_to_the_game() -> None:
|
def close(self) -> None:
|
||||||
game = FakeGame()
|
pass
|
||||||
|
|
||||||
click_cookie(game)
|
|
||||||
|
def test_click_cookie_delegates_to_the_game() -> None:
|
||||||
assert game.clicks == 1
|
game = FakeGame()
|
||||||
|
|
||||||
|
click_cookie(game)
|
||||||
def test_buy_most_expensive_upgrade_buys_the_best_affordable() -> None:
|
|
||||||
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA])
|
assert game.clicks == 1
|
||||||
|
|
||||||
bought = buy_most_expensive_upgrade(game)
|
|
||||||
|
def test_buy_most_expensive_upgrade_buys_the_best_affordable() -> None:
|
||||||
assert bought == GRANDMA
|
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA])
|
||||||
assert game.bought == [GRANDMA]
|
|
||||||
|
bought = buy_most_expensive_upgrade(game)
|
||||||
|
|
||||||
def test_buy_most_expensive_upgrade_buys_nothing_when_too_poor() -> None:
|
assert bought == GRANDMA
|
||||||
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA])
|
assert game.bought == [GRANDMA]
|
||||||
|
|
||||||
assert buy_most_expensive_upgrade(game) is None
|
|
||||||
assert game.bought == []
|
def test_buy_most_expensive_upgrade_buys_nothing_when_too_poor() -> None:
|
||||||
|
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA])
|
||||||
|
|
||||||
def test_click_and_buy_checks_the_upgrades_every_interval() -> None:
|
assert buy_most_expensive_upgrade(game) is None
|
||||||
clock = FakeClock()
|
assert game.bought == []
|
||||||
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 12, 5, clock)
|
def test_click_and_buy_checks_the_upgrades_every_interval() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result == PlayResult(
|
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA], clock=clock)
|
||||||
clicks=12, purchases=("Grandma", "Grandma"), elapsed_seconds=12.0
|
|
||||||
)
|
result = click_and_buy(game, 12, 5, clock)
|
||||||
# The checks fall in the iterations that start at 5 s and 10 s; each click then
|
|
||||||
# moves the fake clock on by one second.
|
assert result == PlayResult(
|
||||||
assert game.buy_times == [6.0, 11.0]
|
clicks=12, purchases=("Grandma", "Grandma"), elapsed_seconds=12.0
|
||||||
|
)
|
||||||
|
# The checks fall in the iterations that start at 5 s and 10 s; each click then
|
||||||
def test_click_and_buy_does_not_check_before_the_first_interval() -> None:
|
# moves the fake clock on by one second.
|
||||||
clock = FakeClock()
|
assert game.buy_times == [6.0, 11.0]
|
||||||
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 4, 5, clock)
|
def test_click_and_buy_does_not_check_before_the_first_interval() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result == PlayResult(clicks=4, purchases=(), elapsed_seconds=4.0)
|
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
||||||
assert game.bought == []
|
|
||||||
|
result = click_and_buy(game, 4, 5, clock)
|
||||||
|
|
||||||
def test_click_and_buy_records_only_real_purchases() -> None:
|
assert result == PlayResult(clicks=4, purchases=(), elapsed_seconds=4.0)
|
||||||
clock = FakeClock()
|
assert game.bought == []
|
||||||
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 11, 5, clock)
|
def test_click_and_buy_records_only_real_purchases() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result.clicks == 11
|
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA], clock=clock)
|
||||||
assert result.purchases == ()
|
|
||||||
|
result = click_and_buy(game, 11, 5, clock)
|
||||||
|
|
||||||
def test_buy_most_expensive_upgrade_returns_none_when_the_click_fails() -> None:
|
assert result.clicks == 11
|
||||||
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA], buy_succeeds=False)
|
assert result.purchases == ()
|
||||||
|
|
||||||
assert buy_most_expensive_upgrade(game) is None
|
|
||||||
assert game.bought == [GRANDMA]
|
def test_buy_most_expensive_upgrade_returns_none_when_the_click_fails() -> None:
|
||||||
|
game = FakeGame(cookies=105, upgrades=[CURSOR, GRANDMA], buy_succeeds=False)
|
||||||
|
|
||||||
def test_click_and_buy_does_not_record_a_purchase_when_the_click_fails() -> None:
|
assert buy_most_expensive_upgrade(game) is None
|
||||||
clock = FakeClock()
|
assert game.bought == [GRANDMA]
|
||||||
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock, buy_succeeds=False)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 12, 5, clock)
|
def test_click_and_buy_does_not_record_a_purchase_when_the_click_fails() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert game.buy_times == [6.0, 11.0]
|
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock, buy_succeeds=False)
|
||||||
assert result.purchases == ()
|
|
||||||
|
result = click_and_buy(game, 12, 5, clock)
|
||||||
|
|
||||||
def test_click_and_buy_does_not_click_when_duration_is_zero() -> None:
|
assert game.buy_times == [6.0, 11.0]
|
||||||
clock = FakeClock()
|
assert result.purchases == ()
|
||||||
game = FakeGame(clock=clock)
|
|
||||||
|
|
||||||
assert click_and_buy(game, 0, 5, clock) == PlayResult(
|
def test_click_and_buy_does_not_click_when_duration_is_zero() -> None:
|
||||||
clicks=0, purchases=(), elapsed_seconds=0.0
|
clock = FakeClock()
|
||||||
)
|
game = FakeGame(clock=clock)
|
||||||
|
|
||||||
|
assert click_and_buy(game, 0, 5, clock) == PlayResult(
|
||||||
def test_click_and_buy_stops_at_the_deadline_and_reports_elapsed_time() -> None:
|
clicks=0, purchases=(), elapsed_seconds=0.0
|
||||||
clock = FakeClock()
|
)
|
||||||
game = FakeGame(clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 300, 5, clock)
|
def test_click_and_buy_stops_at_the_deadline_and_reports_elapsed_time() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result.clicks == 300
|
game = FakeGame(clock=clock)
|
||||||
assert result.elapsed_seconds == 300.0
|
|
||||||
|
result = click_and_buy(game, 300, 5, clock)
|
||||||
|
|
||||||
def test_buy_upgrades_buy_one_buys_only_the_best_affordable() -> None:
|
assert result.clicks == 300
|
||||||
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA])
|
assert result.elapsed_seconds == 300.0
|
||||||
|
|
||||||
assert buy_upgrades(game, BuyStrategy.BUY_ONE) == [GRANDMA]
|
|
||||||
assert game.bought == [GRANDMA]
|
def test_buy_upgrades_buy_one_buys_only_the_best_affordable() -> None:
|
||||||
|
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA])
|
||||||
|
|
||||||
def test_buy_upgrades_buy_all_buys_every_affordable_upgrade() -> None:
|
assert buy_upgrades(game, BuyStrategy.BUY_ONE) == [GRANDMA]
|
||||||
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA])
|
assert game.bought == [GRANDMA]
|
||||||
|
|
||||||
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == [GRANDMA, CURSOR]
|
|
||||||
assert game.bought == [GRANDMA, CURSOR]
|
def test_buy_upgrades_buy_all_buys_every_affordable_upgrade() -> None:
|
||||||
|
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA])
|
||||||
|
|
||||||
def test_buy_upgrades_buy_all_buys_nothing_when_too_poor() -> None:
|
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == [GRANDMA, CURSOR]
|
||||||
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA])
|
assert game.bought == [GRANDMA, CURSOR]
|
||||||
|
|
||||||
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == []
|
|
||||||
assert game.bought == []
|
def test_buy_upgrades_buy_all_buys_nothing_when_too_poor() -> None:
|
||||||
|
game = FakeGame(cookies=10, upgrades=[CURSOR, GRANDMA])
|
||||||
|
|
||||||
def test_buy_upgrades_buy_all_leaves_out_failed_buys() -> None:
|
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == []
|
||||||
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], buy_succeeds=False)
|
assert game.bought == []
|
||||||
|
|
||||||
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == []
|
|
||||||
assert game.bought == [GRANDMA, CURSOR]
|
def test_buy_upgrades_buy_all_leaves_out_failed_buys() -> None:
|
||||||
|
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], buy_succeeds=False)
|
||||||
|
|
||||||
def test_click_and_buy_defaults_to_buying_one_upgrade() -> None:
|
assert buy_upgrades(game, BuyStrategy.BUY_ALL) == []
|
||||||
clock = FakeClock()
|
assert game.bought == [GRANDMA, CURSOR]
|
||||||
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 6, 5, clock)
|
def test_click_and_buy_defaults_to_buying_one_upgrade() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result.purchases == ("Grandma",)
|
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock)
|
||||||
|
|
||||||
|
result = click_and_buy(game, 6, 5, clock)
|
||||||
def test_click_and_buy_buy_all_records_every_purchase_in_order() -> None:
|
|
||||||
clock = FakeClock()
|
assert result.purchases == ("Grandma",)
|
||||||
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 6, 5, clock, BuyStrategy.BUY_ALL)
|
def test_click_and_buy_buy_all_records_every_purchase_in_order() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert result.purchases == ("Grandma", "Cursor")
|
game = FakeGame(cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock)
|
||||||
|
|
||||||
|
result = click_and_buy(game, 6, 5, clock, BuyStrategy.BUY_ALL)
|
||||||
def test_click_and_buy_buy_all_does_not_record_failed_buys() -> None:
|
|
||||||
clock = FakeClock()
|
assert result.purchases == ("Grandma", "Cursor")
|
||||||
game = FakeGame(
|
|
||||||
cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock, buy_succeeds=False
|
|
||||||
)
|
def test_click_and_buy_buy_all_does_not_record_failed_buys() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
result = click_and_buy(game, 6, 5, clock, BuyStrategy.BUY_ALL)
|
game = FakeGame(
|
||||||
|
cookies=120, upgrades=[CURSOR, GRANDMA], clock=clock, buy_succeeds=False
|
||||||
assert game.bought == [GRANDMA, CURSOR]
|
)
|
||||||
assert result.purchases == ()
|
|
||||||
|
result = click_and_buy(game, 6, 5, clock, BuyStrategy.BUY_ALL)
|
||||||
|
|
||||||
def test_click_and_buy_checks_every_second_with_a_one_second_interval() -> None:
|
assert game.bought == [GRANDMA, CURSOR]
|
||||||
clock = FakeClock()
|
assert result.purchases == ()
|
||||||
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
click_and_buy(game, 4, 1, clock)
|
def test_click_and_buy_checks_every_second_with_a_one_second_interval() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert game.buy_times == [2.0, 3.0, 4.0]
|
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
||||||
|
|
||||||
|
click_and_buy(game, 4, 1, clock)
|
||||||
def test_click_and_buy_checks_every_ten_seconds_with_a_ten_second_interval() -> None:
|
|
||||||
clock = FakeClock()
|
assert game.buy_times == [2.0, 3.0, 4.0]
|
||||||
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
|
||||||
|
|
||||||
result = click_and_buy(game, 25, 10, clock)
|
def test_click_and_buy_checks_every_ten_seconds_with_a_ten_second_interval() -> None:
|
||||||
|
clock = FakeClock()
|
||||||
assert game.buy_times == [11.0, 21.0]
|
game = FakeGame(cookies=105, upgrades=[GRANDMA], clock=clock)
|
||||||
assert result.purchases == ("Grandma", "Grandma")
|
|
||||||
|
result = click_and_buy(game, 25, 10, clock)
|
||||||
|
|
||||||
|
assert game.buy_times == [11.0, 21.0]
|
||||||
|
assert result.purchases == ("Grandma", "Grandma")
|
||||||
|
|
||||||
|
|
||||||
|
def test_buy_best_value_upgrade_buys_the_cheapest_per_cookie_per_second() -> None:
|
||||||
|
game = FakeGame(cookies=500, upgrades=[VALUE_CURSOR, VALUE_GRANDMA])
|
||||||
|
|
||||||
|
bought = buy_best_value_upgrade(game)
|
||||||
|
|
||||||
|
assert bought == VALUE_GRANDMA
|
||||||
|
assert game.bought == [VALUE_GRANDMA]
|
||||||
|
|
||||||
|
|
||||||
|
def test_buy_best_value_upgrade_buys_nothing_when_too_poor() -> None:
|
||||||
|
game = FakeGame(cookies=10, upgrades=[VALUE_CURSOR, VALUE_GRANDMA])
|
||||||
|
|
||||||
|
assert buy_best_value_upgrade(game) is None
|
||||||
|
assert game.bought == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_buy_best_value_upgrade_leaves_out_a_click_that_missed() -> None:
|
||||||
|
game = FakeGame(cookies=500, upgrades=[VALUE_GRANDMA], buy_succeeds=False)
|
||||||
|
|
||||||
|
assert buy_best_value_upgrade(game) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_buy_upgrades_with_best_value_buys_one_upgrade() -> None:
|
||||||
|
game = FakeGame(cookies=500, upgrades=[VALUE_CURSOR, VALUE_GRANDMA])
|
||||||
|
|
||||||
|
assert buy_upgrades(game, BuyStrategy.BEST_VALUE) == [VALUE_GRANDMA]
|
||||||
|
|||||||
+114
-70
@@ -1,70 +1,114 @@
|
|||||||
"""@file test_upgrades.py
|
"""@file test_upgrades.py
|
||||||
@brief Tests of the rule that picks the upgrade to buy.
|
@brief Tests of the rule that picks the upgrade to buy.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from cookie_clicker_bot.domain.upgrades import (
|
from cookie_clicker_bot.domain.upgrades import (
|
||||||
Upgrade,
|
Upgrade,
|
||||||
choose_affordable_upgrades,
|
choose_affordable_upgrades,
|
||||||
choose_most_expensive_affordable,
|
choose_best_value,
|
||||||
)
|
choose_most_expensive_affordable,
|
||||||
|
)
|
||||||
CURSOR = Upgrade("product0", "Cursor", 15)
|
|
||||||
GRANDMA = Upgrade("product1", "Grandma", 100)
|
CURSOR = Upgrade("product0", "Cursor", 15)
|
||||||
FARM = Upgrade("product2", "Farm", 1100)
|
GRANDMA = Upgrade("product1", "Grandma", 100)
|
||||||
|
FARM = Upgrade("product2", "Farm", 1100)
|
||||||
|
|
||||||
def test_choose_picks_the_most_expensive_affordable() -> None:
|
|
||||||
assert choose_most_expensive_affordable([CURSOR, GRANDMA, FARM], 105) == GRANDMA
|
def test_choose_picks_the_most_expensive_affordable() -> None:
|
||||||
|
assert choose_most_expensive_affordable([CURSOR, GRANDMA, FARM], 105) == GRANDMA
|
||||||
|
|
||||||
def test_choose_returns_none_when_nothing_is_affordable() -> None:
|
|
||||||
assert choose_most_expensive_affordable([CURSOR, GRANDMA], 10) is None
|
def test_choose_returns_none_when_nothing_is_affordable() -> None:
|
||||||
|
assert choose_most_expensive_affordable([CURSOR, GRANDMA], 10) is None
|
||||||
|
|
||||||
def test_choose_returns_none_without_upgrades() -> None:
|
|
||||||
assert choose_most_expensive_affordable([], 1000) is None
|
def test_choose_returns_none_without_upgrades() -> None:
|
||||||
|
assert choose_most_expensive_affordable([], 1000) is None
|
||||||
|
|
||||||
def test_choose_accepts_a_price_equal_to_the_cookies() -> None:
|
|
||||||
assert choose_most_expensive_affordable([CURSOR, GRANDMA], 100) == GRANDMA
|
def test_choose_accepts_a_price_equal_to_the_cookies() -> None:
|
||||||
|
assert choose_most_expensive_affordable([CURSOR, GRANDMA], 100) == GRANDMA
|
||||||
|
|
||||||
def test_choose_takes_the_first_on_a_tie() -> None:
|
|
||||||
twin = Upgrade("product9", "Twin", 100)
|
def test_choose_takes_the_first_on_a_tie() -> None:
|
||||||
|
twin = Upgrade("product9", "Twin", 100)
|
||||||
assert choose_most_expensive_affordable([GRANDMA, twin], 500) == GRANDMA
|
|
||||||
|
assert choose_most_expensive_affordable([GRANDMA, twin], 500) == GRANDMA
|
||||||
|
|
||||||
def test_choose_ignores_the_order_of_the_list() -> None:
|
|
||||||
assert choose_most_expensive_affordable([GRANDMA, CURSOR], 105) == GRANDMA
|
def test_choose_ignores_the_order_of_the_list() -> None:
|
||||||
|
assert choose_most_expensive_affordable([GRANDMA, CURSOR], 105) == GRANDMA
|
||||||
|
|
||||||
def test_plan_skips_an_upgrade_the_remaining_cookies_cannot_pay() -> None:
|
|
||||||
assert choose_affordable_upgrades([CURSOR, GRANDMA, FARM], 105) == [GRANDMA]
|
def test_plan_skips_an_upgrade_the_remaining_cookies_cannot_pay() -> None:
|
||||||
|
assert choose_affordable_upgrades([CURSOR, GRANDMA, FARM], 105) == [GRANDMA]
|
||||||
|
|
||||||
def test_plan_buys_several_upgrades_most_expensive_first() -> None:
|
|
||||||
assert choose_affordable_upgrades([CURSOR, GRANDMA, FARM], 120) == [GRANDMA, CURSOR]
|
def test_plan_buys_several_upgrades_most_expensive_first() -> None:
|
||||||
|
assert choose_affordable_upgrades([CURSOR, GRANDMA, FARM], 120) == [GRANDMA, CURSOR]
|
||||||
|
|
||||||
def test_plan_is_empty_when_nothing_is_affordable() -> None:
|
|
||||||
assert choose_affordable_upgrades([CURSOR, GRANDMA], 10) == []
|
def test_plan_is_empty_when_nothing_is_affordable() -> None:
|
||||||
|
assert choose_affordable_upgrades([CURSOR, GRANDMA], 10) == []
|
||||||
|
|
||||||
def test_plan_is_empty_without_upgrades() -> None:
|
|
||||||
assert choose_affordable_upgrades([], 1000) == []
|
def test_plan_is_empty_without_upgrades() -> None:
|
||||||
|
assert choose_affordable_upgrades([], 1000) == []
|
||||||
|
|
||||||
def test_plan_spends_the_whole_budget_on_equal_prices() -> None:
|
|
||||||
twin = Upgrade("product9", "Twin", 100)
|
def test_plan_spends_the_whole_budget_on_equal_prices() -> None:
|
||||||
|
twin = Upgrade("product9", "Twin", 100)
|
||||||
assert choose_affordable_upgrades([GRANDMA, twin], 250) == [GRANDMA, twin]
|
|
||||||
assert choose_affordable_upgrades([twin, GRANDMA], 150) == [twin]
|
assert choose_affordable_upgrades([GRANDMA, twin], 250) == [GRANDMA, twin]
|
||||||
|
assert choose_affordable_upgrades([twin, GRANDMA], 150) == [twin]
|
||||||
|
|
||||||
def test_plan_does_not_change_the_given_list() -> None:
|
|
||||||
upgrades = [CURSOR, GRANDMA]
|
def test_plan_does_not_change_the_given_list() -> None:
|
||||||
|
upgrades = [CURSOR, GRANDMA]
|
||||||
choose_affordable_upgrades(upgrades, 1000)
|
|
||||||
|
choose_affordable_upgrades(upgrades, 1000)
|
||||||
assert upgrades == [CURSOR, GRANDMA]
|
|
||||||
|
assert upgrades == [CURSOR, GRANDMA]
|
||||||
|
|
||||||
|
|
||||||
|
VALUE_CURSOR = Upgrade("product0", "Cursor", 15, 0.1)
|
||||||
|
VALUE_GRANDMA = Upgrade("product1", "Grandma", 100, 1.0)
|
||||||
|
VALUE_FARM = Upgrade("product2", "Farm", 1100, 8.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_picks_the_lowest_price_per_cookie_per_second() -> None:
|
||||||
|
upgrades = [VALUE_CURSOR, VALUE_GRANDMA, VALUE_FARM]
|
||||||
|
|
||||||
|
assert choose_best_value(upgrades, 2000) == VALUE_GRANDMA
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_ignores_what_the_cookies_cannot_pay() -> None:
|
||||||
|
upgrades = [VALUE_CURSOR, VALUE_GRANDMA, VALUE_FARM]
|
||||||
|
|
||||||
|
assert choose_best_value(upgrades, 50) == VALUE_CURSOR
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_returns_none_when_nothing_is_affordable() -> None:
|
||||||
|
assert choose_best_value([VALUE_CURSOR, VALUE_GRANDMA], 10) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_returns_none_without_upgrades() -> None:
|
||||||
|
assert choose_best_value([], 1000) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_takes_the_first_on_a_tie() -> None:
|
||||||
|
twin = Upgrade("product9", "Twin", 100, 1.0)
|
||||||
|
|
||||||
|
assert choose_best_value([VALUE_GRANDMA, twin], 500) == VALUE_GRANDMA
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_skips_an_upgrade_with_zero_production() -> None:
|
||||||
|
idle = Upgrade("product9", "Idle", 10, 0.0)
|
||||||
|
|
||||||
|
assert choose_best_value([idle, VALUE_GRANDMA], 500) == VALUE_GRANDMA
|
||||||
|
|
||||||
|
|
||||||
|
def test_best_value_falls_back_to_the_most_expensive_when_production_is_unknown() -> (
|
||||||
|
None
|
||||||
|
):
|
||||||
|
assert choose_best_value([CURSOR, GRANDMA, FARM], 1200) == FARM
|
||||||
|
|||||||
Reference in New Issue
Block a user