| … | … | @@ -0,0 +1,235 @@ |
| 1 | """A library copy plays with ITS OWN subtitles, not wyzie's. |
| 2 | |
| 3 | 2026-10-18: No Other Choice's English drifted in Kodi although vsembed had |
| 4 | synced it to the copy's audio on 10-04. The synced file sat beside the mp4, |
| 5 | where only Jellyfin looks; Jacktook fetched wyzie itself into a per-TMDB cache |
| 6 | and reused it silently. Proven by md5: the Pi's English.srt (025fc304...) was |
| 7 | byte-identical to the UNSYNCED original, not the corrected sidecar (37840b79...). |
| 8 | |
| 9 | A per-TMDB cache cannot be right anyway -- two copies of one film are two |
| 10 | different cuts. So when the stream is vsembed's `/mp4/<kind>/<ident>.mp4`, the |
| 11 | subtitles come from `/subs/<kind>/<ident>.json` on the same host: the copy's |
| 12 | own sidecars, after the server measured them against its audio. Anything else |
| 13 | -- a live stream, the server unreachable, a copy with none -- falls through to |
| 14 | wyzie exactly as before. |
| 15 | |
| 16 | Run: python3 -m pytest plugin.video.jacktook/tests/unit/test_library_subtitles.py -q |
| 17 | """ |
| 18 | |
| 19 | import os |
| 20 | from unittest.mock import MagicMock, patch |
| 21 | |
| 22 | import pytest |
| 23 | |
| 24 | from lib.clients.subtitle.library import LibrarySubtitleClient, library_list_url |
| 25 | |
| 26 | |
| 27 | HOST = "http://192.168.50.170:7220" |
| 28 | MOVIE = HOST + "/mp4/movie/tmdb:639988.mp4?h=1080" |
| 29 | EPISODE = HOST + "/mp4/series/tmdb:138502:1:1.mp4?h=1080" |
| 30 | |
| 31 | |
| 32 | def _resp(payload=None, status=200, body=b""): |
| 33 | r = MagicMock() |
| 34 | r.status_code = status |
| 35 | r.json.return_value = payload |
| 36 | r.content = body |
| 37 | return r |
| 38 | |
| 39 | |
| 40 | def _listing(*entries): |
| 41 | return {"subtitles": [ |
| 42 | {"id": sid, "lang": sid.split(".")[0], |
| 43 | "url": "{0}/subs/movie/tmdb:639988/{1}.srt".format(HOST, sid), |
| 44 | "identity": identity} |
| 45 | for sid, identity in entries]} |
| 46 | |
| 47 | |
| 48 | class FakeServer: |
| 49 | """Answers the list URL with `listing`, each file URL with its id.""" |
| 50 | |
| 51 | def __init__(self, listing): |
| 52 | self.listing = listing |
| 53 | self.files = [] |
| 54 | |
| 55 | def __call__(self, url, timeout=None, headers=None): |
| 56 | if url.endswith(".json"): |
| 57 | return _resp(self.listing) |
| 58 | self.files.append(url) |
| 59 | sid = url.rsplit("/", 1)[1][:-len(".srt")] |
| 60 | return _resp(body="1\n00:00:01,000 --> 00:00:02,000\n{0}\n".format(sid).encode()) |
| 61 | |
| 62 | |
| 63 | # ------------------------------------------------------------ which streams |
| 64 | |
| 65 | class TestListUrl: |
| 66 | def test_a_library_movie(self): |
| 67 | assert library_list_url(MOVIE) == HOST + "/subs/movie/tmdb:639988.json" |
| 68 | |
| 69 | def test_a_library_episode(self): |
| 70 | assert library_list_url(EPISODE) == HOST + "/subs/series/tmdb:138502:1:1.json" |
| 71 | |
| 72 | def test_an_imdb_keyed_copy(self): |
| 73 | assert library_list_url(HOST + "/mp4/movie/tt31967670.mp4") == \ |
| 74 | HOST + "/subs/movie/tt31967670.json" |
| 75 | |
| 76 | @pytest.mark.parametrize("url", [ |
| 77 | HOST + "/vidcore/movie/tmdb:639988.m3u8", |
| 78 | HOST + "/hdhub/movie/tmdb:639988.mkv", |
| 79 | "https://cdn.example/mp4/movie/x/y.mp4", |
| 80 | "magnet:?xt=urn:btih:abc", |
| 81 | "", |
| 82 | None, |
| 83 | ]) |
| 84 | def test_anything_else_is_not_a_library_copy(self, url): |
| 85 | assert library_list_url(url) is None |
| 86 | |
| 87 | |
| 88 | # ----------------------------------------------------------------- fetching |
| 89 | |
| 90 | @pytest.fixture |
| 91 | def client(tmp_path): |
| 92 | return LibrarySubtitleClient(str(tmp_path)) |
| 93 | |
| 94 | |
| 95 | def _get(server): |
| 96 | return patch("lib.clients.subtitle.library.requests.get", side_effect=server) |
| 97 | |
| 98 | |
| 99 | class TestFetch: |
| 100 | def test_each_listed_track_is_written_and_returned(self, client): |
| 101 | server = FakeServer(_listing(("en", [99078, 1]), ("ko", [5, 1]))) |
| 102 | with _get(server): |
| 103 | paths = client.get_subtitles(MOVIE) |
| 104 | assert sorted(os.path.basename(p) for p in paths) == ["en.srt", "ko.srt"] |
| 105 | for p in paths: |
| 106 | sid = os.path.basename(p)[:-len(".srt")] |
| 107 | with open(p, "rb") as f: |
| 108 | assert sid.encode() in f.read() |
| 109 | |
| 110 | def test_a_second_track_in_one_language_is_kept_apart(self, client): |
| 111 | server = FakeServer(_listing(("en", [1, 1]), ("en.2", [2, 1]))) |
| 112 | with _get(server): |
| 113 | paths = client.get_subtitles(MOVIE) |
| 114 | assert sorted(os.path.basename(p) for p in paths) == ["en.2.srt", "en.srt"] |
| 115 | |
| 116 | def test_an_unchanged_track_is_not_downloaded_again(self, client): |
| 117 | server = FakeServer(_listing(("en", [99078, 1]))) |
| 118 | with _get(server): |
| 119 | client.get_subtitles(MOVIE) |
| 120 | client.get_subtitles(MOVIE) |
| 121 | assert len(server.files) == 1 |
| 122 | |
| 123 | def test_a_track_the_server_resynced_is_downloaded_again(self, client): |
| 124 | """The whole point: the server corrects a file in place, and the copy |
| 125 | on the Pi must follow it rather than outlive it.""" |
| 126 | server = FakeServer(_listing(("en", [99078, 1]))) |
| 127 | with _get(server): |
| 128 | client.get_subtitles(MOVIE) |
| 129 | server.listing = _listing(("en", [99100, 2])) |
| 130 | client.get_subtitles(MOVIE) |
| 131 | assert len(server.files) == 2 |
| 132 | |
| 133 | def test_a_track_the_server_no_longer_lists_is_not_offered(self, client): |
| 134 | server = FakeServer(_listing(("en", [1, 1]), ("de", [2, 1]))) |
| 135 | with _get(server): |
| 136 | client.get_subtitles(MOVIE) |
| 137 | server.listing = _listing(("en", [1, 1])) |
| 138 | paths = client.get_subtitles(MOVIE) |
| 139 | assert [os.path.basename(p) for p in paths] == ["en.srt"] |
| 140 | |
| 141 | def test_two_copies_do_not_share_a_folder(self, client): |
| 142 | server = FakeServer(_listing(("en", [1, 1]))) |
| 143 | with _get(server): |
| 144 | a = client.get_subtitles(MOVIE) |
| 145 | b = client.get_subtitles(EPISODE) |
| 146 | assert os.path.dirname(a[0]) != os.path.dirname(b[0]) |
| 147 | |
| 148 | def test_one_failed_file_costs_only_that_track(self, client): |
| 149 | server = FakeServer(_listing(("en", [1, 1]), ("ko", [2, 1]))) |
| 150 | |
| 151 | def flaky(url, timeout=None, headers=None): |
| 152 | if url.endswith("/ko.srt"): |
| 153 | return _resp(status=503) |
| 154 | return server(url, timeout, headers) |
| 155 | with _get(flaky): |
| 156 | paths = client.get_subtitles(MOVIE) |
| 157 | assert [os.path.basename(p) for p in paths] == ["en.srt"] |
| 158 | |
| 159 | def test_an_id_cannot_escape_the_folder(self, client, tmp_path): |
| 160 | server = FakeServer({"subtitles": [ |
| 161 | {"id": "../../evil", "lang": "en", "url": HOST + "/subs/x/evil.srt", |
| 162 | "identity": [1, 1]}]}) |
| 163 | with _get(server): |
| 164 | paths = client.get_subtitles(MOVIE) |
| 165 | for p in paths: |
| 166 | assert os.path.realpath(p).startswith(os.path.realpath(str(tmp_path))) |
| 167 | |
| 168 | |
| 169 | class TestFallsThrough: |
| 170 | """None means 'not mine to answer' -- the caller carries on to wyzie.""" |
| 171 | |
| 172 | def test_not_a_library_stream(self, client): |
| 173 | with patch("lib.clients.subtitle.library.requests.get") as g: |
| 174 | assert client.get_subtitles(HOST + "/vidcore/movie/tmdb:1.m3u8") is None |
| 175 | g.assert_not_called() |
| 176 | |
| 177 | def test_the_server_is_unreachable(self, client): |
| 178 | with patch("lib.clients.subtitle.library.requests.get", |
| 179 | side_effect=OSError("no route")): |
| 180 | assert client.get_subtitles(MOVIE) is None |
| 181 | |
| 182 | def test_an_older_server_without_the_route(self, client): |
| 183 | with patch("lib.clients.subtitle.library.requests.get", |
| 184 | return_value=_resp(status=404)): |
| 185 | assert client.get_subtitles(MOVIE) is None |
| 186 | |
| 187 | def test_a_copy_with_no_subtitles(self, client): |
| 188 | with _get(FakeServer({"subtitles": []})): |
| 189 | assert client.get_subtitles(MOVIE) is None |
| 190 | |
| 191 | |
| 192 | # ------------------------------------------------- wired into SubtitleManager |
| 193 | |
| 194 | from lib.clients.subtitle.submanager import SubtitleManager # noqa: E402 |
| 195 | |
| 196 | |
| 197 | def _settings(**overrides): |
| 198 | values = {"wyzie_subtitle_enabled": True, "stremio_subtitle_enabled": False, |
| 199 | "deepl_enabled": False, "auto_subtitle_download": False} |
| 200 | values.update(overrides) |
| 201 | return lambda key, *a, **k: values.get(key, False) |
| 202 | |
| 203 | |
| 204 | def _manager(url): |
| 205 | return SubtitleManager({"title": "in library — seekable\nlocal copy", |
| 206 | "mode": "movies", "url": url, |
| 207 | "ids": {"tmdb_id": "", "imdb_id": "", |
| 208 | "original_id": "fxl:639988"}}, MagicMock()) |
| 209 | |
| 210 | |
| 211 | class TestManager: |
| 212 | def test_a_library_copy_uses_its_own_and_never_asks_wyzie(self, tmp_path): |
| 213 | m = _manager(MOVIE) |
| 214 | m.library_client.get_subtitles = MagicMock(return_value=["/lib/en.srt"]) |
| 215 | m.wyzie_client.get_subtitles = MagicMock() |
| 216 | with patch("lib.clients.subtitle.submanager.get_setting", _settings()): |
| 217 | got = m.fetch_subtitles(folder_path=str(tmp_path)) |
| 218 | assert got == ["/lib/en.srt"] |
| 219 | m.wyzie_client.get_subtitles.assert_not_called() |
| 220 | |
| 221 | def test_it_wins_over_a_stale_per_title_cache(self, tmp_path): |
| 222 | """The Pi's cache held the unsynced English for this exact title.""" |
| 223 | (tmp_path / "English.srt").write_text("stale") |
| 224 | m = _manager(MOVIE) |
| 225 | m.library_client.get_subtitles = MagicMock(return_value=["/lib/en.srt"]) |
| 226 | with patch("lib.clients.subtitle.submanager.get_setting", _settings()): |
| 227 | assert m.fetch_subtitles(folder_path=str(tmp_path)) == ["/lib/en.srt"] |
| 228 | |
| 229 | def test_nothing_from_the_library_falls_through_to_wyzie(self, tmp_path): |
| 230 | m = _manager(HOST + "/vidcore/movie/tmdb:639988.m3u8") |
| 231 | m.library_client.get_subtitles = MagicMock(return_value=None) |
| 232 | m.wyzie_client.get_subtitles = MagicMock(return_value=[{"url": "x"}]) |
| 233 | m.wyzie_client.download_subtitles_batch = MagicMock(return_value=["/w/a.srt"]) |
| 234 | with patch("lib.clients.subtitle.submanager.get_setting", _settings()): |
| 235 | assert m.fetch_subtitles(folder_path=str(tmp_path)) == ["/w/a.srt"] |