"""The free-space preflight warns before the downloads when the temp volume cannot hold a title's tracks.""" from pathlib import Path from types import SimpleNamespace from typing import Any import pytest from tests.lang_selection import test_kind_only_selection as harness from unshackle.commands import dl as dl_module from unshackle.commands.dl import low_disk_warning from unshackle.core.config import config from unshackle.core.manifests import DASH, HLS, ISM from unshackle.core.tracks import Tracks from unshackle.core.utils.bitrate import manifest_seconds GB = 1_000_000_000 def mpd(periods: str, total: str = "PT1H") -> str: return ( f'{periods}' ) def period(attrs: str = "") -> str: return ( f"" '' '' '' "" '' '' '' "" "" ) def dash_tracks(manifest: str) -> Tracks: return DASH.from_text(manifest, "https://cdn.example/x.mpd").to_tracks("en") @pytest.fixture def free_space(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> list[int]: """Report the bytes in the returned list as the free space of the temp volume.""" monkeypatch.setattr(config.directories, "temp", tmp_path) free = [100 * GB] monkeypatch.setattr(dl_module.shutil, "disk_usage", lambda path: SimpleNamespace(free=free[0])) return free def test_a_temp_volume_too_small_for_twice_the_tracks_gets_one_warning_line( free_space: list[int], tmp_path: Path ) -> None: free_space[0] = 3_100_000_000 line = low_disk_warning(dash_tracks(mpd(period()))) assert line == f"Temp volume {tmp_path} has 3.1 GB free, the selected tracks need about 15.0 GB" def test_enough_free_space_gives_no_warning(free_space: list[int]) -> None: free_space[0] = 15 * GB assert low_disk_warning(dash_tracks(mpd(period()))) is None def test_hls_tracks_have_no_estimate_and_read_no_disk(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(dl_module.shutil, "disk_usage", lambda path: pytest.fail("read the disk")) master = '#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=16000000,RESOLUTION=3840x2160,CODECS="hvc1.2.4.L153"\nv.m3u8\n' tracks = HLS.from_text(master, "https://cdn.example/x.m3u8").to_tracks("en") assert tracks.videos and manifest_seconds(tracks.videos[0]) is None assert low_disk_warning(tracks) is None def test_a_dash_track_counts_only_its_content_periods() -> None: manifest = mpd(period('id="ad" duration="PT10M"') + period('id="main" duration="PT50M"')) track = DASH.from_text(manifest, "https://cdn.example/x.mpd").to_tracks("en").videos[0] track.data["dash"]["filtered_period_ids"] = ["ad"] assert manifest_seconds(track) == 3000 def test_a_dash_track_skips_a_period_that_does_not_carry_it() -> None: other = period('id="b" duration="PT30M"').replace('width="3840" height="2160"', 'width="1920" height="1080"') manifest = mpd(period('id="a" duration="PT30M"') + other) video = dash_tracks(manifest).videos[0] assert video.height == 2160 assert manifest_seconds(video) == 1800 def test_a_duration_the_parser_cannot_read_gives_no_warning(free_space: list[int]) -> None: free_space[0] = 0 assert low_disk_warning(dash_tracks(mpd(period(), total="P0DT1H"))) is None def test_a_dash_period_of_unknown_length_takes_the_presentation_length() -> None: manifest = mpd(period('id="a"') + period('id="b"'), total="PT1H") assert manifest_seconds(dash_tracks(manifest).videos[0]) == 3600 def test_a_dash_manifest_with_no_length_gives_no_estimate() -> None: manifest = '' + period() + "" assert manifest_seconds(dash_tracks(manifest).videos[0]) is None def test_an_ism_track_takes_the_manifest_duration() -> None: manifest = ( '' '' '' '' "" ) video = ISM.from_text(manifest, url="https://cdn.example/x/manifest").to_tracks(language="en").videos[0] assert manifest_seconds(video) == 3600 def test_an_unreadable_temp_volume_gives_no_warning(free_space: list[int], monkeypatch: pytest.MonkeyPatch) -> None: def broken(path: Any) -> Any: raise PermissionError(13, "Access is denied") monkeypatch.setattr(dl_module.shutil, "disk_usage", broken) assert low_disk_warning(dash_tracks(mpd(period()))) is None def test_dl_logs_the_warning_once_per_title_and_not_with_skip_dl( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture ) -> None: monkeypatch.setattr(dl_module, "low_disk_warning", lambda tracks: "Temp volume is short") harness.run(monkeypatch, caplog, {1: {}, 2: {}}, to_end=True, no_mux=True) assert caplog.text.count("Temp volume is short") == 2 caplog.clear() harness.run(monkeypatch, caplog, {1: {}}, to_end=True, skip_dl=True) assert "Temp volume is short" not in caplog.text