import json

import pytest

from scripts.serve_web_with_logs import parse_record


def test_parse_record_accepts_analysis_record():
    record = {"format": "lost-cities-web-game-v1", "gameId": "game-1", "moves": []}
    assert parse_record(json.dumps(record).encode()) == record


@pytest.mark.parametrize("record", [{}, {"format": "other"}, {"format": "lost-cities-web-game-v1"}])
def test_parse_record_rejects_invalid_input(record):
    with pytest.raises(ValueError):
        parse_record(json.dumps(record).encode())


def test_parse_record_accepts_the_match_schema():
    # v2 carries which model played and the three-round match; v1 had neither.
    record = {
        "format": "lost-cities-web-game-v2",
        "gameId": "game-2",
        "opponent": {"codename": "borealis", "hash": "13a25243de1c"},
        "mode": 3,
        "moves": [],
    }
    assert parse_record(json.dumps(record).encode()) == record


def test_parse_record_still_accepts_v1():
    # 111 games were recorded under v1; refusing them now would orphan them.
    record = {"format": "lost-cities-web-game-v1", "gameId": "game-1", "moves": []}
    assert parse_record(json.dumps(record).encode()) == record
