import json from pathlib import Path from ai_chess.chess_game import ChessGame from ai_chess.persistence import save_game, list_games, load_game from ai_chess.sides import Side def make_game() -> ChessGame: g = ChessGame(game_id="01HZZZTEST", agents={ "white": {"agent": "claude", "model": "opus", "effort": "max"}, "black": {"agent": "codex", "model": "gpt-5.5", "effort": "xhigh"}, }) g.try_move(Side.WHITE, "e4", comment="го") g.try_move(Side.BLACK, "c5", comment="да") g.resign(Side.BLACK) return g def test_save_creates_files(tmp_path: Path): g = make_game() save_game(tmp_path, g) assert (tmp_path / "01HZZZTEST.pgn").exists() assert (tmp_path / "01HZZZTEST.json").exists() meta = json.loads((tmp_path / "01HZZZTEST.json").read_text()) assert meta["id"] == "01HZZZTEST" assert meta["result"] == "1-0" assert meta["termination"] == "resignation" assert meta["agents"]["white"]["agent"] == "claude" def test_list_games_sorted_newest_first(tmp_path: Path): g1 = make_game() g1.game_id = "AAA1" save_game(tmp_path, g1) g2 = make_game() g2.game_id = "ZZZ9" save_game(tmp_path, g2) games = list_games(tmp_path) assert [g["id"] for g in games] == ["ZZZ9", "AAA1"] def test_load_game_returns_meta_and_pgn(tmp_path: Path): g = make_game() save_game(tmp_path, g) loaded = load_game(tmp_path, "01HZZZTEST") assert loaded["meta"]["id"] == "01HZZZTEST" assert "[White " in loaded["pgn"]