Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Create test.py
Browse files Browse the repository at this point in the history
Signed-off-by: Đinh Hoàng Việt <134517889+M-DinhHoangViet@users.noreply.github.com>
  • Loading branch information
M-DinhHoangViet authored Sep 10, 2023
1 parent 343a356 commit 425984e
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import pytest
import pytest_timeout
import requests
import time
import zipfile
import yaml
import shutil
import importlib
lishogi_bot = importlib.import_module("lishogi-bot")

TOKEN = os.environ['BOT_TOKEN']


def test_nothing():
assert True


def run_bot(CONFIG, logging_level):
lichess_bot.logger.info(lichess_bot.intro())
li = lichess_bot.lichess.Lichess(CONFIG["token"], CONFIG["url"], lichess_bot.__version__, logging_level)

user_profile = li.get_profile()
username = user_profile["username"]
is_bot = user_profile.get("title") == "BOT"
lichess_bot.logger.info(f"Welcome BOT {username}!")

if not is_bot:
is_bot = lichess_bot.upgrade_account(li)

if is_bot:
games = li.get_ongoing_games()
game_ids = list(map(lambda game: game["gameId"], games))
for game in game_ids:
try:
li.abort(game)
except:
pass
time.sleep(2)
while li.get_ongoing_games():
time.sleep(60)
game_id = li.challenge_ai()["id"]
time.sleep(2)

@pytest.mark.timeout(300)
def run_test():
lichess_bot.start(li, user_profile, CONFIG, logging_level, None, one_game=True)
headers = {"Accept": "application/json"}
response = requests.get(f"https://lichess.org/game/export/{game_id}?moves=false", headers=headers)
json = response.json()
winner = json["winner"]
assert "user" in json["players"][winner]

run_test()
else:
lichess_bot.logger.error(f"{username} is not a bot account. Please upgrade it to a bot account!")
games = li.get_ongoing_games()
game_ids = list(map(lambda game: game["gameId"], games))
for game in game_ids:
try:
li.abort(game)
except:
pass
time.sleep(2)


def test_bot():
logging_level = lichess_bot.logging.INFO
lichess_bot.logging_configurer(logging_level, None)
with open("./config.yml") as file:
CONFIG = yaml.safe_load(file)
CONFIG["token"] = TOKEN
CONFIG["engine"]["name"] = "fairy-stockfish"
CONFIG["engine"]["protocol"] = "uci"
run_bot(CONFIG, logging_level)


if __name__ == "__main__":
test_bot()

0 comments on commit 425984e

Please sign in to comment.