| … | … | @@ -0,0 +1,139 @@ |
| 1 | """Daily retry for fetched copies that never got English subtitles (2026-10-18). |
| 2 | |
| 3 | An ingest asks for subtitles once, when the copy lands. On 10-18 wyzie |
| 4 | answered 503 for every file all evening, and 43 fetched copies had none -- |
| 5 | fixed by a one-off sweep, but nothing would have retried them. So once a day |
| 6 | the service runs the ingest's own subtitle step (_fetch_subtitles_for: wyzie, |
| 7 | sync, OpenSubtitles, sync) for every fetched copy that has never had an |
| 8 | English track. |
| 9 | |
| 10 | "Never had" is deliberate: a copy whose English was measured and withdrawn |
| 11 | (a different cut) already had both sources tried, and fetching the same bad |
| 12 | track again every day would only refill the originals store. A copy being |
| 13 | written is left to its own ingest. |
| 14 | |
| 15 | Run: .venv/bin/python3 -m pytest test_subtitle_retry.py -q |
| 16 | """ |
| 17 | |
| 18 | import json |
| 19 | import os |
| 20 | |
| 21 | import pytest |
| 22 | |
| 23 | import addon |
| 24 | |
| 25 | |
| 26 | @pytest.fixture |
| 27 | def lib(tmp_path, monkeypatch): |
| 28 | root = tmp_path / "lib" |
| 29 | monkeypatch.setattr(addon, "LIBRARY_ROOT", str(root)) |
| 30 | monkeypatch.setattr(addon, "_mp4_jobs", {}) |
| 31 | copies = {} |
| 32 | |
| 33 | def hold(name, key, subs=()): |
| 34 | path = root / name |
| 35 | path.parent.mkdir(parents=True, exist_ok=True) |
| 36 | path.write_bytes(b"video") |
| 37 | for code in subs: |
| 38 | (root / (os.path.splitext(name)[0] + f".{code}.srt")).write_text("x") |
| 39 | copies[os.path.abspath(path)] = [key] |
| 40 | return str(path) |
| 41 | |
| 42 | monkeypatch.setattr(addon, "_fetched_dests", lambda: dict(copies)) |
| 43 | monkeypatch.setattr(addon, "_library_meta", |
| 44 | lambda kind, ident, s=0, e=0: {"tmdb_id": ident.split(":")[1], |
| 45 | "title": "T", "year": "2020", |
| 46 | "episode_title": ""}) |
| 47 | calls = [] |
| 48 | |
| 49 | def fetch(dest, meta, season, episode, kind="movie"): |
| 50 | calls.append((os.path.basename(dest), kind, meta["tmdb_id"], season, episode)) |
| 51 | monkeypatch.setattr(addon, "_fetch_subtitles_for", fetch) |
| 52 | return hold, calls |
| 53 | |
| 54 | |
| 55 | def test_a_copy_with_no_subtitles_is_retried_with_its_own_ids(lib): |
| 56 | hold, calls = lib |
| 57 | hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 58 | hold("Shows/Show - S02E06.mp4", "series:tmdb:1:2:6:1080") |
| 59 | addon._subtitle_retry_pass() |
| 60 | assert sorted(calls) == [("Film (2025).mp4", "movie", "2", 0, 0), |
| 61 | ("Show - S02E06.mp4", "series", "1", 2, 6)] |
| 62 | |
| 63 | |
| 64 | def test_a_copy_with_english_is_left_alone(lib): |
| 65 | hold, calls = lib |
| 66 | hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080", subs=("en",)) |
| 67 | addon._subtitle_retry_pass() |
| 68 | assert calls == [] |
| 69 | |
| 70 | |
| 71 | def test_other_languages_alone_still_count_as_no_english(lib): |
| 72 | hold, calls = lib |
| 73 | hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080", subs=("fr", "de")) |
| 74 | addon._subtitle_retry_pass() |
| 75 | assert len(calls) == 1 |
| 76 | |
| 77 | |
| 78 | def test_english_that_was_withdrawn_is_not_fetched_again(lib): |
| 79 | hold, calls = lib |
| 80 | dest = hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 81 | en = os.path.splitext(dest)[0] + ".en.srt" |
| 82 | with open(addon.SUBTITLE_SYNC_FILE, "w") as f: |
| 83 | json.dump({os.path.abspath(en): {"status": "unfixable", "ref": 2, |
| 84 | "offset": 30.0, "scale": 1.0}}, f) |
| 85 | addon._subtitle_retry_pass() |
| 86 | assert calls == [] |
| 87 | |
| 88 | |
| 89 | def test_a_copy_being_written_is_skipped(lib, monkeypatch): |
| 90 | hold, calls = lib |
| 91 | hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 92 | monkeypatch.setattr(addon, "_mp4_jobs", {"movie:tmdb:2:0:0:1080": "running"}) |
| 93 | addon._subtitle_retry_pass() |
| 94 | assert calls == [] |
| 95 | |
| 96 | |
| 97 | def test_a_copy_gone_from_disk_is_skipped(lib): |
| 98 | hold, calls = lib |
| 99 | dest = hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 100 | os.remove(dest) |
| 101 | addon._subtitle_retry_pass() |
| 102 | assert calls == [] |
| 103 | |
| 104 | |
| 105 | def test_the_pass_reports_what_it_did(lib, monkeypatch): |
| 106 | hold, calls = lib |
| 107 | hold("Movies/A (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 108 | hold("Movies/B (2025).mp4", "movie:tmdb:3:0:0:1080", subs=("en",)) |
| 109 | hold("Movies/C (2025).mp4", "movie:tmdb:4:0:0:1080") |
| 110 | |
| 111 | def fetch(dest, meta, season, episode, kind="movie"): |
| 112 | if "A (2025)" in dest: |
| 113 | open(os.path.splitext(dest)[0] + ".en.srt", "w").close() |
| 114 | monkeypatch.setattr(addon, "_fetch_subtitles_for", fetch) |
| 115 | assert addon._subtitle_retry_pass() == {"checked": 3, "retried": 2, "gained": 1} |
| 116 | |
| 117 | |
| 118 | def test_one_copy_failing_does_not_stop_the_pass(lib, monkeypatch): |
| 119 | hold, calls = lib |
| 120 | hold("Movies/A (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 121 | hold("Movies/B (2025).mp4", "movie:tmdb:3:0:0:1080") |
| 122 | seen = [] |
| 123 | |
| 124 | def fetch(dest, meta, season, episode, kind="movie"): |
| 125 | seen.append(os.path.basename(dest)) |
| 126 | if "A (2025)" in dest: |
| 127 | raise RuntimeError("boom") |
| 128 | monkeypatch.setattr(addon, "_fetch_subtitles_for", fetch) |
| 129 | addon._subtitle_retry_pass() |
| 130 | assert sorted(seen) == ["A (2025).mp4", "B (2025).mp4"] |
| 131 | |
| 132 | |
| 133 | def test_an_unreadable_sync_record_touches_nothing(lib): |
| 134 | hold, calls = lib |
| 135 | hold("Movies/Film (2025).mp4", "movie:tmdb:2:0:0:1080") |
| 136 | with open(addon.SUBTITLE_SYNC_FILE, "w") as f: |
| 137 | f.write("{not json") |
| 138 | addon._subtitle_retry_pass() |
| 139 | assert calls == [] |