From 66f52f7b9c0457cebc7c6301547f30e5697aff6d Mon Sep 17 00:00:00 2001 From: ai-chess Date: Thu, 14 May 2026 23:09:46 +0300 Subject: [PATCH] feat(persistence): save/list/load games (PGN + JSON) --- src/ai_chess/persistence.py | 44 ++++++++++++++++++++++++++++++++++ tests/test_persistence.py | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/ai_chess/persistence.py create mode 100644 tests/test_persistence.py diff --git a/src/ai_chess/persistence.py b/src/ai_chess/persistence.py new file mode 100644 index 0000000..1f1f99e --- /dev/null +++ b/src/ai_chess/persistence.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +from ai_chess.chess_game import ChessGame + + +def save_game(games_dir: Path, game: ChessGame) -> None: + games_dir = Path(games_dir) + games_dir.mkdir(parents=True, exist_ok=True) + (games_dir / f"{game.game_id}.pgn").write_text(game.to_pgn()) + meta = { + "id": game.game_id, + "started_at": game.started_at, + "ended_at": game.ended_at, + "result": game.result, + "termination": game.termination, + "agents": game.agents, + "moves_count": len([h for h in game.history if h.uci not in ("resign", "agreement")]), + } + (games_dir / f"{game.game_id}.json").write_text(json.dumps(meta, indent=2)) + + +def list_games(games_dir: Path) -> list[dict[str, Any]]: + games_dir = Path(games_dir) + if not games_dir.exists(): + return [] + out: list[dict[str, Any]] = [] + for p in games_dir.glob("*.json"): + try: + out.append(json.loads(p.read_text())) + except Exception: + continue + out.sort(key=lambda g: g.get("id", ""), reverse=True) + return out + + +def load_game(games_dir: Path, game_id: str) -> dict[str, Any]: + games_dir = Path(games_dir) + meta = json.loads((games_dir / f"{game_id}.json").read_text()) + pgn = (games_dir / f"{game_id}.pgn").read_text() + return {"meta": meta, "pgn": pgn} diff --git a/tests/test_persistence.py b/tests/test_persistence.py new file mode 100644 index 0000000..bca1a2d --- /dev/null +++ b/tests/test_persistence.py @@ -0,0 +1,48 @@ +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"]