from __future__ import annotations import json from pathlib import Path import pytest from audiobooksorter.ingest import IngestError, load_library def test_load_library_from_json(samples_dir: Path) -> None: records = load_library(samples_dir / "library.json") assert len(records) == 5 assert records[0].title == "The Final Empire" assert records[0].source_path is None def test_load_library_from_directory(tmp_path: Path) -> None: library = tmp_path / "library" book_one = library / "Mistborn1" book_two = library / "Wayfarers" book_one.mkdir(parents=True) book_two.mkdir(parents=True) (book_one / "metadata.json").write_text( json.dumps( {"title": "The Final Empire", "series": "Mistborn", "seriesIndex": 1, "author": "Brandon"} ), encoding="utf-8", ) (book_two / "metadata.json").write_text( json.dumps({"title": "Wayfarers", "author": "Becky Chambers"}), encoding="utf-8", ) records = load_library(library) sources = {record.title: record.source_path for record in records} assert sources["The Final Empire"] == book_one assert sources["Wayfarers"] == book_two def test_missing_library_raises(tmp_path: Path) -> None: with pytest.raises(IngestError): load_library(tmp_path / "missing")