From 863f35f421804dddae5cd6f80b49376f63dd5040 Mon Sep 17 00:00:00 2001 From: johndoknjas Date: Thu, 29 Aug 2024 13:45:44 -0700 Subject: [PATCH] run the sf process directly --- tests/stockfish/test_models.py | 69 ++++++++++++++++------------------ 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/tests/stockfish/test_models.py b/tests/stockfish/test_models.py index 6abaff4..a55dcd0 100644 --- a/tests/stockfish/test_models.py +++ b/tests/stockfish/test_models.py @@ -2,45 +2,40 @@ from timeit import default_timer import time from typing import List, Optional, Dict +import subprocess +from subprocess import Popen +import os -from stockfish import Stockfish, StockfishException +def send_command(process: Popen, command): + process.stdin.write(command + "\n") + process.stdin.flush() + response = process.stdout.readline() + return response class TestStockfish: - @pytest.fixture - def stockfish(self) -> Stockfish: - return Stockfish() - - # change to `autouse=True` to have the below fixture called before each test function, and then - # the code after the 'yield' to run after each test. - @pytest.fixture(autouse=False) - def autouse_fixture(self, stockfish: Stockfish): - yield stockfish - # Some assert statement testing something about the stockfish object here. - - def test_constructor_defaults(self): - sf = Stockfish() - assert sf is not None and sf._path == "stockfish" - assert sf._parameters == sf._DEFAULT_STOCKFISH_PARAMS - assert sf._depth == 15 and sf._num_nodes == 1000000 - assert sf._turn_perspective is True - - def test_constructor_options(self): - sf = Stockfish( - depth=20, - num_nodes=1000, - turn_perspective=False, - parameters={"Threads": 2, "UCI_Elo": 1500}, + + def test_sf_process(self): + # Get the path to the current directory + current_dir = os.path.dirname(os.path.abspath(__file__)) + + # Path to the stockfish executable + stockfish_path = os.path.join(current_dir, "stockfish") + + # Start the stockfish process + process = subprocess.Popen( + [stockfish_path], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True ) - assert sf._depth == 20 and sf._num_nodes == 1000 - assert sf._turn_perspective is False - assert sf._parameters["Threads"] == 2 and sf._parameters["UCI_Elo"] == 1500 - - @pytest.mark.slow - def test_get_best_move_remaining_time_not_first_move(self, stockfish: Stockfish): - stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1", False) - stockfish.set_fen_position("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2", False) - best_move = stockfish.get_best_move(wtime=1000) - assert best_move in ("d2d4", "a2a3", "d1e2", "b1c3") - best_move = stockfish.get_best_move(btime=1000) - assert best_move in ("d2d4", "b1c3") + + send_command(process, "uci") + send_command(process, "isready") + send_command(process, "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 moves e2e4 e7e6") + print(send_command(process, "go wtime 1000")) + print(send_command(process, "go btime 1000")) + send_command(process, "quit") + +