fix(plex): a stray byte keeps a UTF-8 subtitle UTF-8; untagged and Serbian read as cp1250

One invalid byte sent a whole UTF-8 sidecar to a legacy code page, an untagged
file fell to Windows-1252 (ő/ű became õ/û) and Serbian was read as Cyrillic.
The cache key moves to sub3_ so a track the old rules mis-decoded is read again.
This commit is contained in:
2026-09-27 13:58:23 +02:00
parent 3411265dc1
commit 7987bd8a37
2 changed files with 78 additions and 9 deletions
@@ -79,3 +79,37 @@ def test_a_track_cached_before_the_encoding_fix_is_not_served(tmp_path, monkeypa
"WEBVTT\n\n1\n00:00:01.000 --> 00:00:02.000\nAz�rt\n", "utf-8"
)
assert subtitles.cached_track("14881", 1)[0] is None
def test_one_stray_byte_does_not_send_a_utf8_file_to_a_legacy_code_page(monkeypatch):
"""A UTF-8 release with ONE bad byte (a cut-and-paste Windows-1252 quote) failed strict UTF-8 as a
whole, and the whole file was then read as Windows-1250: every correct ő/é became mojibake. Nearly
all of it is valid UTF-8, so it stays UTF-8 and only the one byte is replaced."""
raw = (HU * 20).encode("utf-8") + b"\x93quoted\x94\r\n"
vtt = _sidecar(monkeypatch, raw, "hun")
assert "Miért nem szóltál? Ősz, űr." in vtt
assert "Miért" not in vtt
def test_an_untagged_legacy_sidecar_is_read_as_central_european(monkeypatch):
"""No language tag: the fallback was Windows-1252, which turns a Hungarian ő/ű into õ/û."""
vtt = _sidecar(monkeypatch, HU.encode("cp1250"), None)
assert "Ősz, űr." in vtt
def test_a_latin_serbian_sidecar_is_read_as_central_european(monkeypatch):
"""Serbian releases are overwhelmingly Latin script in Windows-1250 (like hrv/bos); read as
Windows-1251 every č/ć/š/ž/đ turned into Cyrillic noise."""
sr = "1\r\n00:00:01,000 --> 00:00:02,000\r\nŠta ćeš, đače? Čekaj.\r\n"
vtt = _sidecar(monkeypatch, sr.encode("cp1250"), "srp")
assert "Šta ćeš, đače? Čekaj." in vtt
def test_a_track_cached_under_the_previous_decoder_is_not_served(tmp_path, monkeypatch):
"""The decoder changed again (stray-byte UTF-8, cp1250 fallbacks): the key is versioned once more
so a track the old rules mis-decoded is read again instead of served for good."""
monkeypatch.setattr(subtitles, "CACHE_DIR", tmp_path)
(tmp_path / "sub2_14881_1.vtt").write_text(
"WEBVTT\n\n1\n00:00:01.000 --> 00:00:02.000\nAzõrt\n", "utf-8"
)
assert subtitles.cached_track("14881", 1)[0] is None