"""Test idempotentności hashowania raw payloadu."""
from app.ingest import _canonical_json, _hash_raw


def test_hash_is_stable_across_key_order() -> None:
    a = {"id": "x", "title": "T", "performers": [{"name": "Mia", "id": "1"}]}
    b = {"performers": [{"id": "1", "name": "Mia"}], "title": "T", "id": "x"}
    assert _hash_raw(a) == _hash_raw(b)


def test_hash_changes_when_value_changes() -> None:
    a = {"id": "x", "title": "T"}
    b = {"id": "x", "title": "Tt"}
    assert _hash_raw(a) != _hash_raw(b)


def test_canonical_json_is_compact_and_sorted() -> None:
    raw = _canonical_json({"b": 1, "a": 2})
    assert raw == b'{"a":2,"b":1}'


def test_hash_handles_dates() -> None:
    from datetime import date

    a = {"d": date(2024, 1, 1)}
    b = {"d": "2024-01-01"}
    # default=str converts date to string identical to the literal version
    assert _hash_raw(a) == _hash_raw(b)
