Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AttackingOrDefending authored Dec 2, 2024
1 parent 6ad7976 commit 6e9530a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
11 changes: 7 additions & 4 deletions test_bot/test_lichess.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Tests for the lichess communication."""

from lib import lichess
import logging
import os
import pytest


def test_lichess():
def test_lichess() -> None:
"""Test the lichess communication."""
token = os.getenv("LICHESS_BOT_TEST_TOKEN")
if token is None:
pytest.skip("Lichess-bot test token must be set.")
Expand All @@ -18,9 +21,9 @@ def test_lichess():
'createdAt': 1627834995597, 'followable': True, 'following': False, 'id': 'badsunfish',
'perfs': {'blitz': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'bullet': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'classical': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'correspondence': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'rapid': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500}},
'classical': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'correspondence': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500},
'rapid': {'games': 0, 'prog': 0, 'prov': True, 'rating': 1500, 'rd': 500}},
'playTime': {'total': 1873, 'tv': 0}, 'seenAt': 1700000000000, 'title': 'BOT',
'url': 'https://lichess.org/@/BadSunfish', 'username': 'BadSunfish'}
assert li.get_ongoing_games() == []
Expand Down
97 changes: 60 additions & 37 deletions test_bot/test_model.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
"""Tests for the models."""

import datetime
from lib import model
import yaml
from lib import config
from collections import defaultdict
from lib.timer import Timer
from lib.types import ChallengeType, UserProfileType, GameEventType, PlayerType


def test_model():
challenge = {"id": "zzzzzzzz", "url": "https://lichess.org/zzzzzzzz", "status": "created",
"challenger": {"id": "c", "name": "c", "rating": 2000, "title": None, "online": True},
"destUser": {"id": "b", "name": "b", "rating": 3000, "title": "BOT", "online": True},
"variant": {"key": "standard", "name": "Standard", "short": "Std"}, "rated": False, "speed": "bullet",
"timeControl": {"type": "clock", "limit": 90, "increment": 1, "show": "1.5+1"}, "color": "random",
"finalColor": "white", "perf": {"icon": "\ue032", "name": "Bullet"}}
user_profile = {"id": "b", "username": "b",
"perfs": {"bullet": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"blitz": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"rapid": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"classical": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"correspondence": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"antichess": {"games": 100, "rating": 3000, "rd": 150, "prog": -10, "prov": True}},
"title": "BOT", "createdAt": 1500000000000, "profile": {
"bio": "This is my bio",
"links": "https://github.com/lichess-bot-devs/lichess-bot"},
"seenAt": 1700000000000, "playTime": {"total": 1000000, "tv": 10000},
"url": "https://lichess.org/@/b",
"count": {"all": 600, "rated": 500, "ai": 50, "draw": 200, "drawH": 50, "loss": 50,
"lossH": 50, "win": 250, "winH": 200, "bookmark": 0, "playing": 0, "import": 0, "me": 0},
"followable": True, "following": False, "blocking": False}
player1 = {"id": "c", "name": "c", "rating": 2000, "title": None, "online": True}
player2 = {"id": "b", "name": "b", "rating": 3000, "title": "BOT", "online": True}
game = {"id": "sAoJVsG3", "variant": {"key": "standard", "name": "Standard", "short": "Std"}, "speed": "bullet",
"perf": {"name": "Bullet"}, "rated": False, "createdAt": 1700000000000,
"white": {"id": "c", "name": "c", "title": None, "rating": 2000},
"black": {"id": "b", "name": "b", "title": "BOT", "rating": 3000},
"initialFen": "startpos", "clock": {"initial": 90000, "increment": 1000}, "type": "gameFull",
"state": {"type": "gameState", "moves": "", "wtime": 90000, "btime": 90000, "winc": 1000, "binc": 1000,
"status": "started"}}
username = "b"
base_url = "https://lichess.org/"
abort_time = datetime.timedelta(seconds=30)

def test_challenge() -> None:
"""Test the challenge model."""
challenge: ChallengeType = {"id": "zzzzzzzz", "url": "https://lichess.org/zzzzzzzz", "status": "created",
"challenger": {"id": "c", "name": "c", "rating": 2000, "title": None, "online": True},
"destUser": {"id": "b", "name": "b", "rating": 3000, "title": "BOT", "online": True},
"variant": {"key": "standard", "name": "Standard", "short": "Std"}, "rated": False,
"speed": "bullet",
"timeControl": {"type": "clock", "limit": 90, "increment": 1, "show": "1.5+1"},
"color": "random", "finalColor": "white", "perf": {"icon": "\ue032", "name": "Bullet"}}
user_profile: UserProfileType = {"id": "b", "username": "b",
"perfs": {"bullet": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"blitz": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"rapid": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"classical": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"correspondence": {"games": 100, "rating": 3000, "rd": 150, "prog": -10},
"antichess": {"games": 100, "rating": 3000, "rd": 150, "prog": -10,
"prov": True}},
"title": "BOT", "createdAt": 1500000000000,
"profile": {"bio": "This is my bio",
"links": "https://github.com/lichess-bot-devs/lichess-bot"},
"seenAt": 1700000000000, "playTime": {"total": 1000000, "tv": 10000},
"url": "https://lichess.org/@/b",
"count": {"all": 600, "rated": 500, "ai": 50, "draw": 200, "drawH": 50, "loss": 50,
"lossH": 50, "win": 250, "winH": 200, "bookmark": 0, "playing": 0,
"import": 0, "me": 0},
"followable": True, "following": False, "blocking": False}

with open("./config.yml.default") as file:
CONFIG = yaml.safe_load(file)
CONFIG["token"] = ""
CONFIG["challenge"]["allow_list"] = []
CONFIG["challenge"]["block_list"] = []
configuration = config.Configuration(CONFIG).challenge
recent_challenges = defaultdict()
recent_challenges: defaultdict[str, list[Timer]] = defaultdict()
recent_challenges["c"] = []

challenge_model = model.Challenge(challenge, user_profile)
assert challenge_model.id == "zzzzzzzz"
assert challenge_model.rated is False
Expand All @@ -60,3 +56,30 @@ def test_model():

CONFIG["challenge"]["min_base"] = 120
assert challenge_model.is_supported(configuration, recent_challenges) == (False, "timeControl")


def test_game() -> None:
"""Test the game model."""
game: GameEventType = {"id": "zzzzzzzz", "variant": {"key": "standard", "name": "Standard", "short": "Std"},
"speed": "bullet", "perf": {"name": "Bullet"}, "rated": False, "createdAt": 1700000000000,
"white": {"id": "c", "name": "c", "title": None, "rating": 2000},
"black": {"id": "b", "name": "b", "title": "BOT", "rating": 3000},
"initialFen": "startpos", "clock": {"initial": 90000, "increment": 1000}, "type": "gameFull",
"state": {"type": "gameState", "moves": "", "wtime": 90000, "btime": 90000, "winc": 1000,
"binc": 1000, "status": "started"}}
username = "b"
base_url = "https://lichess.org/"
abort_time = datetime.timedelta(seconds=30)

game_model = model.Game(game, username, base_url, abort_time)
assert game_model.id == "zzzzzzzz"
assert game_model.mode == "casual"
assert game_model.is_white is False


def test_player() -> None:
"""Test the player model."""
player: PlayerType = {"id": "b", "name": "b", "rating": 3000, "title": "BOT", "online": True}
player_model = model.Player(player)
assert player_model.is_bot is True
assert str(player_model) == "BOT b (3000)"

0 comments on commit 6e9530a

Please sign in to comment.