def test_serialization_returns_correct_serialization_for_boards():
    p1 = Player.ONE
    p1_row = PlayerRow.get_new_player_row()
    p2 = Player.TWO
    p2_row = PlayerRow.get_new_player_row()
    board = Board({p1: p1_row, p2: p2_row})
    assert to_serializable(board) == {
        to_serializable(p1): to_serializable(p1_row),
        to_serializable(p2): to_serializable(p2_row),
    }
def test_simulation_loop_serialization():
    p1 = AlwaysMinimumPlayerStrategy()
    p2 = AlwaysMaximumPlayerStrategy()
    loop = SimulationLoop(player_one=p1, player_two=p2)
    expected_serialization_keys = {
        "player_strategies",
        "starting_player",
        "winning_player",
        "turns",
        "boards",
    }

    # Pre-run
    prerun_serialization = json.loads(loop.serialize())
    assert prerun_serialization.keys() == expected_serialization_keys
    assert prerun_serialization["player_strategies"].keys() == {"one", "two"}
    assert prerun_serialization["player_strategies"]["one"] == p1.strategy_name
    assert prerun_serialization["player_strategies"]["two"] == p2.strategy_name
    assert prerun_serialization["starting_player"] in ["one", "two"]
    assert prerun_serialization["winning_player"] is None
    assert len(prerun_serialization["turns"]) == 0
    assert len(prerun_serialization["boards"]) == 1
    assert prerun_serialization["boards"][0] == {
        "one": {
            "bins": [4, 4, 4, 4, 4, 4],
            "goal": 0,
        },
        "two": {
            "bins": [4, 4, 4, 4, 4, 4],
            "goal": 0,
        },
    }

    # Post-run
    loop.run()
    postrun_serialization = json.loads(loop.serialize())
    assert postrun_serialization.keys() == expected_serialization_keys
    assert postrun_serialization["player_strategies"].keys() == {"one", "two"}
    assert postrun_serialization["player_strategies"][
        "one"] == p1.strategy_name
    assert postrun_serialization["player_strategies"][
        "two"] == p2.strategy_name
    assert postrun_serialization["starting_player"] in ["one", "two"]
    assert (postrun_serialization["winning_player"] == "one"
            )  # minimum strategy always wins
    assert len(postrun_serialization["turns"]) > 0
    assert postrun_serialization["turns"] == [
        to_serializable(turn) for turn in loop.turns
    ]
    assert len(postrun_serialization["boards"]) > 1
    assert postrun_serialization["boards"] == [
        to_serializable(board) for board in loop.boards
    ]
 def serialize(self) -> str:
     p1 = Player.ONE
     p2 = Player.TWO
     return json.dumps({
         "player_strategies": {
             to_serializable(p1): self._strategies[p1].strategy_name,
             to_serializable(p2): self._strategies[p2].strategy_name,
         },
         "starting_player":
         to_serializable(self._starting_player),
         "winning_player":
         to_serializable(self._winning_player),
         "turns": [to_serializable(turn) for turn in self.turns],
         "boards": [to_serializable(board) for board in self.boards],
     })
def test_serialize_returns_type_error_for_unregistered_type():
    class NewType:
        pass

    with pytest.raises(TypeError):
        to_serializable(NewType())
def test_serialize_returns_correct_serialization(serializable, serialized):
    assert to_serializable(serializable) == serialized