|
| 1 | +import copy |
| 2 | +import json |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from gamesforai import make |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("opponent", ["random", "minimax:1", "minimax:3", "mcts:2"]) |
| 11 | +def test_resume_repeats_policy_decisions_and_rng(opponent): |
| 12 | + env = make("connect4", opponent=opponent) |
| 13 | + env.reset(seed=87) |
| 14 | + restored = make("connect4", opponent=opponent) |
| 15 | + restored.set_state(json.loads(json.dumps(env.get_state()))) |
| 16 | + for _ in range(4): |
| 17 | + action = int(np.flatnonzero(env.action_masks())[0]) |
| 18 | + first, second = env.step(action), restored.step(action) |
| 19 | + np.testing.assert_array_equal(first[0], second[0]) |
| 20 | + assert first[1:4] == second[1:4] |
| 21 | + assert env.get_state() == restored.get_state() |
| 22 | + if first[2] or first[3]: |
| 23 | + break |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize("opponent", ["random", "minimax:2", "mcts:3"]) |
| 27 | +def test_wrong_opponent_is_rejected_without_state_changes(opponent): |
| 28 | + original = make("connect4", opponent="minimax:3") |
| 29 | + original.reset(seed=3) |
| 30 | + other = make("connect4", opponent=opponent) |
| 31 | + other.reset(seed=5) |
| 32 | + before = other.get_state() |
| 33 | + with pytest.raises(ValueError, match="opponent"): |
| 34 | + other.set_state(original.get_state()) |
| 35 | + assert other.get_state() == before |
| 36 | + |
| 37 | + |
| 38 | +def test_legacy_checkpoints_bind_only_to_random_opponent(): |
| 39 | + random = make("connect4") |
| 40 | + random.reset(seed=81) |
| 41 | + legacy = random.get_state() |
| 42 | + del legacy["opponent"] |
| 43 | + for version in (2, 1): |
| 44 | + legacy["version"] = version |
| 45 | + if version == 1: |
| 46 | + del legacy["curriculum"] |
| 47 | + random.set_state(legacy) |
| 48 | + search = make("connect4", opponent="minimax:1") |
| 49 | + with pytest.raises(ValueError, match="opponent"): |
| 50 | + search.set_state(legacy) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("field,value", [ |
| 54 | + ("version", True), ("seat", False), ("rng_seed", True), ("rng_seed", -2), |
| 55 | +]) |
| 56 | +def test_malformed_metadata_is_atomic(field, value): |
| 57 | + env = make("connect4") |
| 58 | + env.reset(seed=4) |
| 59 | + before = env.get_state() |
| 60 | + invalid = copy.deepcopy(before) |
| 61 | + invalid[field] = value |
| 62 | + with pytest.raises(ValueError): |
| 63 | + env.set_state(invalid) |
| 64 | + assert env.get_state() == before |
| 65 | + |
| 66 | + |
| 67 | +def test_python_policy_state_remains_explicitly_caller_owned(): |
| 68 | + def policy(obs, info): |
| 69 | + return int(np.flatnonzero(info["action_mask"])[0]) |
| 70 | + |
| 71 | + env = make("connect4", opponent=policy) |
| 72 | + env.reset(seed=2) |
| 73 | + state = env.get_state() |
| 74 | + assert state["opponent"] == "python" |
| 75 | + restored = make("connect4", opponent=policy) |
| 76 | + restored.set_state(state) |
| 77 | + assert restored.get_state() == state |
| 78 | + with pytest.raises(ValueError, match="opponent"): |
| 79 | + make("connect4").set_state(state) |
0 commit comments