"""Tests for recovering ČSFD link / title / year from pre-index layouts.""" import json from src.core.legacy_meta import LegacyMeta, read_legacy_meta def _video(tmp_path, name="Matrix (1999).mkv"): v = tmp_path / name v.write_bytes(b"x") return v def test_reads_tagger_era_sidecar(tmp_path): video = _video(tmp_path) (tmp_path / f".{video.name}.!tag").write_text( json.dumps( { "title": "Matrix", "csfd_link": "https://www.csfd.cz/film/9499-matrix/", "csfd_cache": {"year": 1999, "rating": 90}, } ), encoding="utf-8", ) meta = read_legacy_meta(video) assert meta == LegacyMeta( csfd_url="https://www.csfd.cz/film/9499-matrix/", title="Matrix", year="1999", source="sidecar", ) def test_sidecar_new_key_and_meta_block(tmp_path): video = _video(tmp_path) (tmp_path / f".{video.name}.!tag").write_text( json.dumps( { "title": "Matrix", "csfd_url": "https://www.csfd.cz/film/9499/", "meta": {"year": "1999"}, } ), encoding="utf-8", ) meta = read_legacy_meta(video) assert meta.csfd_url == "https://www.csfd.cz/film/9499/" assert meta.year == "1999" def test_reads_old_pool_index_by_relative_key(tmp_path): pool = tmp_path / "old-pool" movies_dir = pool / "Filmy" movies_dir.mkdir(parents=True) video = _video(movies_dir) (pool / ".Curator.!index").write_text( json.dumps( { "settings": {}, "movies": { "Filmy/Matrix (1999).mkv": { "title": "Matrix", "csfd_link": "https://www.csfd.cz/film/9499-matrix/", "csfd_cache": {"year": 1999}, } }, } ), encoding="utf-8", ) meta = read_legacy_meta(video) assert meta.csfd_url == "https://www.csfd.cz/film/9499-matrix/" assert meta.title == "Matrix" assert meta.year == "1999" assert meta.source == "index" def test_index_fallback_to_unique_basename_match(tmp_path): pool = tmp_path / "pool" pool.mkdir() elsewhere = tmp_path / "loose" elsewhere.mkdir() video = _video(elsewhere) (pool.parent / ".Curator.!index").write_text( json.dumps( { "movies": { "whatever/Matrix (1999).mkv": { "csfd_url": "https://www.csfd.cz/film/9499/" } }, } ), encoding="utf-8", ) meta = read_legacy_meta(video) assert meta is not None assert meta.csfd_url == "https://www.csfd.cz/film/9499/" def test_sidecar_wins_over_index(tmp_path): video = _video(tmp_path) (tmp_path / f".{video.name}.!tag").write_text( json.dumps({"csfd_link": "https://www.csfd.cz/film/SIDECAR/"}), encoding="utf-8" ) (tmp_path / ".Curator.!index").write_text( json.dumps( { "movies": { "Matrix (1999).mkv": { "csfd_link": "https://www.csfd.cz/film/INDEX/" } }, } ), encoding="utf-8", ) assert read_legacy_meta(video).csfd_url == "https://www.csfd.cz/film/SIDECAR/" def test_none_when_no_metadata(tmp_path): assert read_legacy_meta(_video(tmp_path)) is None def test_none_on_broken_sidecar(tmp_path): video = _video(tmp_path) (tmp_path / f".{video.name}.!tag").write_text("{not json", encoding="utf-8") assert read_legacy_meta(video) is None def test_record_without_useful_fields_is_none(tmp_path): video = _video(tmp_path) (tmp_path / f".{video.name}.!tag").write_text( json.dumps({"new": True, "ignored": False, "tags": []}), encoding="utf-8" ) assert read_legacy_meta(video) is None