This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Đinh Hoàng Việt <134517889+M-DinhHoangViet@users.noreply.github.com>
- Loading branch information
1 parent
343a356
commit 425984e
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |