"""The ISM merge: the rolling path ends the download phase, and a text subtitle merges behind its init as UTF-8.""" from __future__ import annotations from tests.core.test_rolling_merge_hls_ism import base, ism_track, make_ctx from tests.core.test_rolling_merge_hls_ism import no_cancel as no_cancel # noqa: F401 from tests.core.test_rolling_merge_hls_ism import server as server # noqa: F401 from unshackle.core.config import config from unshackle.core.manifests import ISM from unshackle.core.manifests import ism as ism_module from unshackle.core.tracks.segment_store import SegmentStore CUES = [b"1\n00:00:00,000 --> 00:00:01,000\n‎one\n\n", b"2\n00:00:02,000 --> 00:00:03,000\n‏two\n\n"] def test_a_text_subtitle_merges_behind_its_init_with_the_entities_as_characters(server, tmp_path, monkeypatch): monkeypatch.setattr(config, "merge_segments", False) firsts: list[bytes | None] = [] def init_segment(track, session_drm, first_segment=None): firsts.append(first_segment) return b"INIT" monkeypatch.setattr(ISM, "init_segment", staticmethod(init_segment)) for i, cue in enumerate(CUES): server.routes[f"/x.ism/text{i * 20000000}.srt"] = cue manifest = f""" """ track = ISM.from_text(manifest, f"{base(server)}x.ism/manifest").to_tracks().subtitles[0] ctx = make_ctx(tmp_path, "subtitle.srt") ISM.download_track(track, ctx) assert firsts == [CUES[0]] assert ctx.save_path.read_bytes() == b"INIT" + b"".join(CUES).replace(b"‎", "\u200e".encode()).replace( b"‏", "\u200f".encode() ) def test_the_rolling_merge_ends_the_download_phase_before_it_checks_the_count(server, tmp_path, monkeypatch): monkeypatch.setattr(config, "merge_segments", True) calls: list[str] = [] monkeypatch.setattr(SegmentStore, "merging", lambda self: calls.append("merging")) monkeypatch.setattr(ism_module, "check_merged", lambda *args: calls.append("check_merged")) ISM.download_track(ism_track(server), make_ctx(tmp_path, "audio.mp4")) assert calls == ["merging", "check_merged"]