#!/usr/bin/env python3
"""RAMSES RF - Test the Schema processor."""

import json
from pathlib import Path

import pytest

from ramses_rf import Gateway
from ramses_rf.helpers import shrink

from .helpers import (
    TEST_DIR,
    gwy,  # noqa: F401
    shuffle_dict,
)

WORK_DIR = f"{TEST_DIR}/schemas"


@pytest.mark.parametrize(
    "f_name", [f.stem for f in Path(f"{WORK_DIR}/log_files").glob("*.log")]
)
async def test_schema_discover_from_log(f_name: Path) -> None:
    path = f"{WORK_DIR}/log_files/{f_name}.log"
    gwy = Gateway(None, input_file=path, config={})  # noqa: F811
    await gwy.start()  # this is what we're testing

    with open(f"{WORK_DIR}/log_files/{f_name}.json") as f:
        schema = json.load(f)

        assert shrink(await gwy.schema()) == shrink(schema)

        gwy.ser_name = "/dev/null"  # HACK: needed to pause engine
        schema, packets = await gwy.get_state(include_expired=True)
        packets = shuffle_dict(packets)
        await gwy._restore_cached_packets(packets)

        assert shrink(await gwy.schema()) == shrink(schema)

    await gwy.stop()


# def test_schema_load_from_json(f_name: Path) -> None:
#     path = f"{WORK_DIR}/jsn_files/{f_name}.json"
#     gwy = Gateway(None, input_file=path, config={})  # noqa: F811

#     with open(f"{WORK_DIR}/jsn_files/{f_name}.json") as f:
#         schema = json.load(f)

#     load_schema(gwy, schema)
