|
| 1 | +"""Compare the installed wheel's remote wrappers with a real local server.""" |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +import re |
| 5 | +import selectors |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from gamesforai import NativeEnv, connect, make |
| 11 | + |
| 12 | +with tempfile.TemporaryDirectory() as directory: |
| 13 | + process = subprocess.Popen([ |
| 14 | + str(Path("target/debug/gfa").resolve()), "serve", "--sqlite", |
| 15 | + str(Path(directory) / "remote.sqlite"), "--port", "0", |
| 16 | + ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 17 | + try: |
| 18 | + with selectors.DefaultSelector() as selector: |
| 19 | + selector.register(process.stdout, selectors.EVENT_READ) |
| 20 | + if not selector.select(timeout=30): |
| 21 | + raise RuntimeError("server startup timed out") |
| 22 | + line = process.stdout.readline() |
| 23 | + match = re.search(r"http://127\.0\.0\.1:\d+", line) |
| 24 | + assert match, "server did not publish a local address" |
| 25 | + client = connect(match.group(), timeout=15) |
| 26 | + for game in ["tictactoe", "connect4", "chess"]: |
| 27 | + remote, local = client.native(game, seed=42), NativeEnv(game, seed=42) |
| 28 | + for _ in range(12): |
| 29 | + assert json.loads(remote.get_state()) == json.loads(local.get_state()) |
| 30 | + if any(local.flags()): |
| 31 | + break |
| 32 | + seat = local.current_players()[0] |
| 33 | + left, right = remote.frame(seat), local.frame(seat) |
| 34 | + np.testing.assert_array_equal(left["observation"], right["observation"]) |
| 35 | + np.testing.assert_array_equal(left["action_mask"], right["action_mask"]) |
| 36 | + action = int(np.flatnonzero(right["action_mask"])[0]) |
| 37 | + assert remote.step(seat, action) == local.step(seat, action) |
| 38 | + for seat in [0, 1]: |
| 39 | + remote = client.make("tictactoe", seat=seat, opponent="minimax:3") |
| 40 | + local = make("tictactoe", seat=seat, opponent="minimax:3") |
| 41 | + left, _ = remote.reset(seed=19) |
| 42 | + right, _ = local.reset(seed=19) |
| 43 | + np.testing.assert_array_equal(left, right) |
| 44 | + while not any(remote.native.flags()): |
| 45 | + action = int(np.flatnonzero(local.action_masks())[0]) |
| 46 | + a, b = remote.step(action), local.step(action) |
| 47 | + np.testing.assert_array_equal(a[0], b[0]) |
| 48 | + assert a[1:4] == b[1:4] |
| 49 | + env = client.aec("tictactoe") |
| 50 | + env.reset(seed=0) |
| 51 | + checkpoint = env.get_state() |
| 52 | + env.step(0) |
| 53 | + env.set_state(checkpoint) |
| 54 | + for action in [0, 3, 1, 4, 2]: |
| 55 | + env.step(action) |
| 56 | + assert all(env.terminations.values()) |
| 57 | + assert env.rewards == {"player_0": 1.0, "player_1": -1.0} |
| 58 | + assert isinstance(env.render(), str) |
| 59 | + env = client.make("connect4") |
| 60 | + obs, info = env.reset(seed=42, options={"position_set": "connect4-solved-positions@1"}) |
| 61 | + assert info["curriculum"]["position_set"] == "connect4-solved-positions@1" |
| 62 | + finally: |
| 63 | + process.terminate() |
| 64 | + try: |
| 65 | + process.wait(timeout=15) |
| 66 | + except subprocess.TimeoutExpired: |
| 67 | + process.kill() |
| 68 | + process.wait() |
| 69 | + raise |
| 70 | + assert process.returncode == 0, "server shutdown failed" |
| 71 | +print("Remote/native checkpoints, frames, Gym/AEC rewards and curricula verified") |
0 commit comments