"""The GENERATED catalog: derived facts ⊕ authored overlay, validated + merged."""
import build_catalog as bc


def test_catalog_validates_and_merges_overlay():
    out = bc.build()
    from models import Catalog
    cat = Catalog.model_validate(out)              # DRY contract
    assert cat.n_tracks == len(cat.tracks)
    # the authored overlay must actually merge onto the derived rows
    by_id = {t.id: t for t in cat.tracks}
    sept = by_id.get("live/collab/baba/sept1.tidal")
    assert sept and "rhadamanthe" in sept.collab   # authored collab credit
    assert sept.sounds                             # derived sounds still present
    assert sept.authored_prov is not None          # provenance carried through


def test_no_authored_entry_still_yields_a_row():
    out = bc.build()
    # a track with no overlay must still appear, just without authored fields
    rows = [t for t in out["tracks"] if not t["authored_prov"]]
    assert rows and all(r["title"] for r in rows)
