feat(settings): Settings model + load/save

This commit is contained in:
ai-chess
2026-05-14 23:05:09 +03:00
parent 5fcb620ac2
commit 83b68305be
2 changed files with 89 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
import json
from pathlib import Path
import pytest
from ai_chess.settings import Settings, load_settings, save_settings, DEFAULTS
def test_defaults():
s = Settings()
assert s.allow_draw_resign is True
assert s.allow_illegal_moves is False
assert s.theme_mode == "by_side"
assert s.watchdog_seconds == 180
assert s.watchdog_max_nudges == 3
assert s.stockfish_depth == 15
def test_load_creates_file_if_missing(tmp_path: Path):
cfg = tmp_path / "settings.json"
s = load_settings(cfg)
assert cfg.exists()
assert s == Settings()
assert json.loads(cfg.read_text()) == DEFAULTS
def test_load_round_trip(tmp_path: Path):
cfg = tmp_path / "settings.json"
cfg.write_text(json.dumps({"allow_illegal_moves": True, "stockfish_depth": 22}))
s = load_settings(cfg)
assert s.allow_illegal_moves is True
assert s.stockfish_depth == 22
assert s.allow_draw_resign is True # default preserved
def test_save_partial(tmp_path: Path):
cfg = tmp_path / "settings.json"
s = load_settings(cfg)
s2 = save_settings(cfg, s, {"allow_illegal_moves": True})
assert s2.allow_illegal_moves is True
assert s2.allow_draw_resign is True
assert json.loads(cfg.read_text())["allow_illegal_moves"] is True
def test_save_rejects_unknown(tmp_path: Path):
cfg = tmp_path / "settings.json"
s = load_settings(cfg)
with pytest.raises(ValueError):
save_settings(cfg, s, {"nonsense": 1})