feat(music): list and select playlist songs by their position

This commit is contained in:
imSp4rky
2026-11-30 12:39:51 -07:00
parent 40114fff19
commit 70ace41201
16 changed files with 222 additions and 38 deletions
+63 -3
View File
@@ -7,12 +7,19 @@ click type makes of them, and the titles the download loop keeps.
from __future__ import annotations
import re
import click
import jsonpickle
import pytest
from tests.lang_selection.test_kind_only_selection import FakeService, run
from unshackle.commands.dl import title_wanted
from unshackle.core.console import console
from unshackle.core.music.renderer import MusicRenderer
from unshackle.core.music.tagger import build_tag_values
from unshackle.core.titles.movie import Movie
from unshackle.core.titles.music import Song
from unshackle.core.titles.music import Album, Song
from unshackle.core.utils.click_types import SeasonRange
@@ -20,15 +27,16 @@ class DummyService:
pass
def make_song(track: int = 1, disc: int = 1) -> Song:
def make_song(track: int = 1, disc: int = 1, position: int | None = None, album: str = "Example Album") -> Song:
return Song(
id_=f"track-{disc:02}{track:04}",
service=DummyService,
name=f"Example Track {track}",
artist="Example Artist",
album="Example Album",
album=album,
track=track,
disc=disc,
position=position,
year=2025,
album_artist="Example Artist",
)
@@ -124,3 +132,55 @@ def test_song_matches_only_its_own_key():
song = make_song(track=3, disc=2)
assert song.matches_wanted(["2x3"]) is True
assert song.matches_wanted(["1x3", "2x4"]) is False
def test_a_playlist_lists_and_selects_by_position_but_tags_by_album_track():
songs = [make_song(1, position=1, album="Z"), make_song(6, 2, 2, "B"), make_song(1, position=3, album="A")]
playlist = Album(reversed(songs), kind="playlist")
assert list(playlist) == songs
assert select("2", playlist) == [6]
assert not songs[1].matches_wanted(["2x6"])
assert [MusicRenderer().song_line(s, playlist).plain[:2] for s in playlist] == ["01", "02", "03"]
tags = build_tag_values(songs[1])
assert (tags["TRACKNUMBER"], tags["DISCNUMBER"]) == ("6", "2")
def test_a_playlist_shows_no_disc_count():
playlist = Album([make_song(6, 2, position=1), make_song(1, 3, position=2)], kind="playlist")
assert "Discs" not in list(MusicRenderer().render_header(playlist).columns[0].cells)
assert "Discs" in list(MusicRenderer().render_header(Album([make_song(6, 2)])).columns[0].cells)
@pytest.mark.parametrize(("position", "key", "rule"), [(9, "1x9", "Track 09"), (None, "2x6", "Track 06")])
def test_the_download_loop_names_the_number_that_selected_the_song(monkeypatch, caplog, position, key, rule):
service = FakeService({1: {}})
tracks = service.get_tracks(service.get_titles()[0])
service.get_titles = lambda: Album([make_song(6, 2, position, "B"), make_song(1)])
service.get_tracks = lambda title: tracks
with console.capture() as out:
run(monkeypatch, caplog, {}, service=service, wanted=[key])
assert re.findall(r"Track \d+: Example Track \d+", out.get()) == [f"{rule}: Example Track 6"]
def test_a_release_without_positions_keeps_its_order_and_numbers():
songs = [make_song(1, album="A"), make_song(6, 2, album="B"), make_song(1, album="Z")]
release = Album(reversed(songs))
assert list(release) == songs
assert [MusicRenderer().song_line(s, release).plain[:4].strip() for s in release] == ["01", "2.06", "01"]
def test_a_release_from_an_older_title_cache_reads_as_having_no_positions():
"""jsonpickle restores a Song without its __init__, so an older entry has no 'position' key."""
songs = [make_song(6, 2, album="B"), make_song(1, album="Z")]
for song in songs:
del song.__dict__["position"]
restored = jsonpickle.loads(jsonpickle.dumps(songs))
assert ["position" in vars(song) for song in restored] == [False, False]
assert [song.listing_number for song in restored] == [(2, 6), (1, 1)]
assert [song.album for song in Album(reversed(restored))] == ["B", "Z"]