import pytest

from hef.catalog import Record, validate_catalog, index_by_id, CatalogError


def make_record(**o):
    base = dict(
        id="a",
        title="t",
        source_url="u",
        source_archive="nasa",
        license="public_domain",
        mode="video",
        left=0,
        right=0,
        dark=0,
        light=0,
        duration_s=1,
        file_path="p",
    )
    base.update(o)
    return Record(**base)


def test_validate_catalog_accepts_unique_ids():
    validate_catalog([make_record(id="a"), make_record(id="b")])  # no raise


def test_validate_catalog_rejects_duplicate_id():
    with pytest.raises(CatalogError) as e:
        validate_catalog([make_record(id="dup"), make_record(id="dup")])
    assert "dup" in str(e.value)


def test_validate_catalog_validates_each_record():
    with pytest.raises(CatalogError):
        validate_catalog([make_record(left=9)])


def test_index_by_id_round_trips():
    recs = [make_record(id="a"), make_record(id="b")]
    idx = index_by_id(recs)
    assert idx["a"].id == "a" and idx["b"].id == "b"


def test_index_by_id_rejects_duplicates():
    with pytest.raises(CatalogError):
        index_by_id([make_record(id="x"), make_record(id="x")])
