Skip to content

Commit

Permalink
run the sf process directly
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoknjas committed Aug 29, 2024
1 parent 6c6e391 commit 863f35f
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions tests/stockfish/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


0 comments on commit 863f35f

Please sign in to comment.