feat(music): redraw the album header with the quality of the first song's chosen audio track

This commit is contained in:
imSp4rky
2026-12-02 17:44:15 -07:00
parent 0c93f1dc63
commit a4bc8b7efa
4 changed files with 198 additions and 14 deletions
+131
View File
@@ -1,3 +1,4 @@
from collections.abc import Callable
from typing import Any
import jsonpickle
@@ -7,6 +8,7 @@ from tests.lang_selection.test_kind_only_selection import FakeService, run
from unshackle.core.console import console
from unshackle.core.music.renderer import MusicRenderer
from unshackle.core.titles.music import Album, Music, Song
from unshackle.core.tracks import Audio, Tracks
class DummyService:
@@ -225,3 +227,132 @@ def test_the_track_line_shows_the_song_as_given(
)
def test_is_hires_quality_reads_sample_rate_not_bitrate(label: str, hires: bool) -> None:
assert MusicRenderer.is_hires_quality(label) is hires
def flac_release(**data: Any) -> tuple[Album, Tracks]:
"""One release of two songs with `data` on its first song, and one 24-bit / 96 kHz FLAC track."""
music = Album([song(1, "Album 1", "Artist"), song(2, "Album 1", "Artist")])
music[0].data = {"duration": 60, **data}
tracks = Tracks()
tracks.add(
Audio(
id_="a",
url="https://x/a",
language="en",
codec=Audio.Codec.FLAC,
bitrate=3_075_000,
bit_depth=24,
sample_rate=96000,
)
)
return music, tracks
def run_release(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
music: Music,
tracks: Tracks | Callable[[Song], Tracks],
**flags: Any,
) -> str:
service = FakeService({})
service.get_titles = lambda: music # type: ignore[assignment]
service.get_tracks = tracks if callable(tracks) else lambda title: tracks # type: ignore[method-assign, assignment]
with console.capture() as out:
run(monkeypatch, caplog, {}, service=service, lang=["en"], **flags)
return out.get()
@pytest.mark.parametrize("list_", [False, True])
@pytest.mark.parametrize("data", [{}, {"bit_depth": 16, "sample_rate": 44100}], ids=["missing", "lower"])
def test_the_header_is_drawn_again_with_the_quality_of_the_first_chosen_track(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, data: dict, list_: bool
) -> None:
music, tracks = flac_release(**data)
out = run_release(monkeypatch, caplog, music, tracks, list_=list_)
assert out.count("Hi-Res Lossless") == 1
assert out.count("Length") == 2
assert out.count("Tracklist") == 1
assert music[0].data | {"bit_depth": 24, "sample_rate": 96000, "lossless": True} == music[0].data
def test_the_header_is_not_drawn_again_when_its_quality_stays_the_same(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
music, tracks = flac_release(bit_depth=24, sample_rate=192000, lossless=True)
out = run_release(monkeypatch, caplog, music, tracks)
assert out.count("Length") == 1
assert music[0].data["sample_rate"] == 192000
def test_a_collection_of_different_releases_is_not_drawn_again(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
_, tracks = flac_release()
music = mixed(artist="Own Artist")
out = run_release(monkeypatch, caplog, music, tracks)
assert out.count("Length") == 1
assert "Quality" not in out
def test_the_header_is_drawn_again_once_per_release(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
music, tracks = flac_release()
higher = Tracks()
higher.add(
Audio(id_="b", url="https://x/b", language="en", codec=Audio.Codec.FLAC, bit_depth=24, sample_rate=192000)
)
out = run_release(monkeypatch, caplog, music, lambda title: tracks if title is music[0] else higher, to_end=True)
assert out.count("Length") == 2
assert music[0].data["sample_rate"] == 96000
def test_a_later_first_song_fills_the_release_song(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
music, tracks = flac_release()
out = run_release(monkeypatch, caplog, music, tracks, wanted=["1x2"])
assert out.count("Hi-Res Lossless") == 1
assert music[0].data["bit_depth"] == 24
def test_the_best_of_several_chosen_tracks_fills_the_header() -> None:
music, tracks = flac_release()
lossy = Audio(id_="e", url="https://x/e", language="en", codec=Audio.Codec.EC3, sample_rate=48000)
assert MusicRenderer().redraw_header(music, [lossy, *tracks.audio]) is not None
assert music[0].data | {"bit_depth": 24, "sample_rate": 96000, "lossless": True} == music[0].data
def test_a_lossless_flag_the_service_set_stays() -> None:
music, tracks = flac_release(bit_depth=24, sample_rate=96000, lossless=False)
assert MusicRenderer().redraw_header(music, tracks.audio) is None
assert music[0].data["lossless"] is False
def test_a_lossy_first_track_draws_no_header_again(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
music, _ = flac_release()
lossy = Tracks()
lossy.add(
Audio(id_="e", url="https://x/e", language="en", codec=Audio.Codec.EC3, bitrate=768_000, sample_rate=48000)
)
out = run_release(monkeypatch, caplog, music, lossy)
assert out.count("Length") == 1
assert "Quality" not in out
assert music[0].data == {"duration": 60}