"""Consistency tests: parse_fit_bytes == parse_fit_file."""

from .conftest import SAMPLE_FIT, SESSION_NUMERIC_FIELDS, messages_by_type, message_type_counts


def test_bytes_and_file_return_same_message_types():
    from pyfitparsernative import parse_fit_bytes, parse_fit_file

    with open(SAMPLE_FIT, "rb") as f:
        data = f.read()

    from_file = message_type_counts(parse_fit_file(SAMPLE_FIT))
    from_bytes = message_type_counts(parse_fit_bytes(data))

    assert from_file == from_bytes


def test_bytes_and_file_return_same_record_counts():
    from pyfitparsernative import parse_fit_bytes, parse_fit_file

    with open(SAMPLE_FIT, "rb") as f:
        data = f.read()

    from_file = message_type_counts(parse_fit_file(SAMPLE_FIT))
    from_bytes = message_type_counts(parse_fit_bytes(data))

    for msg_type in from_file:
        assert from_file[msg_type] == from_bytes[msg_type], (
            f"'{msg_type}': from_file={from_file[msg_type]}, "
            f"from_bytes={from_bytes[msg_type]}"
        )


def test_bytes_and_file_session_fields_equal():
    from pyfitparsernative import parse_fit_bytes, parse_fit_file

    with open(SAMPLE_FIT, "rb") as f:
        data = f.read()

    from_file = messages_by_type(parse_fit_file(SAMPLE_FIT))["session"][0]
    from_bytes = messages_by_type(parse_fit_bytes(data))["session"][0]

    for field in SESSION_NUMERIC_FIELDS:
        assert from_file[field] == from_bytes[field], (
            f"'{field}': from_file={from_file[field]}, from_bytes={from_bytes[field]}"
        )


def test_bytes_and_file_message_order_matches():
    """parse_fit_bytes and parse_fit_file return messages in the same order."""
    from pyfitparsernative import parse_fit_bytes, parse_fit_file

    with open(SAMPLE_FIT, "rb") as f:
        data = f.read()

    file_types = [m["message_type"] for m in parse_fit_file(SAMPLE_FIT)]
    bytes_types = [m["message_type"] for m in parse_fit_bytes(data)]

    assert file_types == bytes_types
