"""Round 109 — importing a playlist link can make a playlist of it. A YouTube or SoundCloud playlist, or a Spotify album or playlist, has a name. Once the link turns out to be a list, the import dialog offers "Make a new playlist of these songs, named [its name]" (off unless ticked, the name editable). The playlist is made when the first song lands, so a link that downloads nothing leaves no empty playlist, and the songs go in in the link's order. yt-dlp itself is never run: round 75's fake, taught to print the list name. """ import os import stat import sys import pytest from lintunes import spotify_link, url_import from lintunes.gui.url_import_dialog import UrlImportDialog from lintunes.library_manager import LibraryManager from lintunes.models import Library from lintunes.preferences import Preferences from lintunes.spotify_link import SpotifyTrack from tests.test_round75 import FAKE_YTDLP, MIX, _pump PLAYLIST = "https://www.youtube.com/playlist?list=PLx" # Every listed entry is preceded by the name of its list, as yt-dlp's # "%(playlist_title|)s" prints it ("" for a lone song). FAKE_WITH_NAME = FAKE_YTDLP.replace( ' print(f"LTENTRY {vid}', ' print("LTLIST " + os.environ.get("FAKE_LISTNAME", ""), ' 'flush=True)\n print(f"LTENTRY {vid}') @pytest.fixture def fake(tmp_path, monkeypatch): assert FAKE_WITH_NAME != FAKE_YTDLP bin_dir = tmp_path / "bin" bin_dir.mkdir() script = bin_dir / "yt-dlp" script.write_text(FAKE_WITH_NAME.replace("/usr/bin/env python3", sys.executable)) script.chmod(script.stat().st_mode | stat.S_IEXEC) monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}") monkeypatch.setenv("FAKE_YTDLP_LOG", str(tmp_path / "argv.log")) monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) monkeypatch.setenv("FAKE_LISTNAME", "Road Trip") def _dialog(qapp, url): qapp.clipboard().setText(url) return UrlImportDialog("Adds to the end of “Chill”") # -------------------------------------------------------------------------- # A. the name, from yt-dlp and from Spotify def test_list_name_lines(): assert url_import.parse_line("LTLIST Road Trip\n") == ("list", "Road Trip") assert url_import.parse_line("LTLIST \n") is None # a lone song assert url_import.parse_line("LTLIST NA\n") is None assert "LTLIST %(playlist_title|)s" in url_import.build_probe_command( PLAYLIST) def test_the_probe_names_its_list_once(qapp, fake): probe = url_import.PlaylistProbe(PLAYLIST) names, done = [], [] probe.named.connect(names.append) probe.finished.connect(done.append) probe.start() _pump(qapp, lambda: done) assert names == ["Road Trip"] @pytest.mark.parametrize("link, named", [ ("https://open.spotify.com/album/abc", ["Fresh Album"]), ("https://open.spotify.com/track/abc", []), ]) def test_spotify_names_albums_and_playlists(qapp, monkeypatch, link, named): monkeypatch.setattr(spotify_link, "fetch", lambda kind, sid: ( "Fresh Album", [SpotifyTrack(id="1", title="One", artist="A")])) probe = url_import.SpotifyProbe(link) names, done = [], [] probe.named.connect(names.append) probe.finished.connect(done.append) probe.start() _pump(qapp, lambda: done) assert names == named # -------------------------------------------------------------------------- # B. the dialog def test_a_named_list_offers_a_new_playlist(qapp, fake): dialog = _dialog(qapp, PLAYLIST) assert not dialog._make.isVisibleTo(dialog) dialog._on_import() _pump(qapp, lambda: dialog._probe is None) assert dialog._make.isVisibleTo(dialog) assert dialog._make_name.text() == "Road Trip" assert not dialog._make.isChecked() # off unless ticked assert dialog.new_playlist_name() == "" dialog._make.setChecked(True) assert dialog.new_playlist_name() == "Road Trip" dialog._make_name.setText("") assert not dialog._ok.isEnabled() # a playlist needs a name dialog._make_name.setText(" Summer ") assert dialog._ok.isEnabled() dialog._on_import() assert dialog.new_playlist_name() == "Summer" def test_no_name_no_offer(qapp, fake, monkeypatch): monkeypatch.setenv("FAKE_LISTNAME", "") dialog = _dialog(qapp, PLAYLIST) dialog._on_import() _pump(qapp, lambda: dialog._probe is None) assert dialog._picking assert not dialog._make.isVisibleTo(dialog) dialog._make.setChecked(True) assert dialog.new_playlist_name() == "" def test_just_this_song_makes_no_playlist(qapp, fake): dialog = _dialog(qapp, MIX) dialog._on_import() _pump(qapp, lambda: dialog._probe is None) dialog._make.setChecked(True) dialog._just.click() assert dialog.new_playlist_name() == "" # -------------------------------------------------------------------------- # C. through the window @pytest.fixture def window(qapp, tmp_path, fake, monkeypatch): from lintunes.gui.main_window import MainWindow library = Library(music_folder=str(tmp_path / "media")) (tmp_path / "media").mkdir() manager = LibraryManager(library, tmp_path / "data") win = MainWindow(manager, Preferences(tmp_path / "data")) monkeypatch.setattr(win, "_enqueue_identify", lambda tracks: None) monkeypatch.setattr(win, "_show_batch_review", lambda analysis: None) yield win if win._url_batch is not None: win._url_batch.cancel() win.close() def _import(window, qapp, monkeypatch, ids, name="Road Trip"): from lintunes.gui import main_window as mw chosen = [{"id": i, "title": f"Song {i}", "url": f"https://www.youtube.com/watch?v={i}"} for i in ids] class _Dialog: cookies_used = None def __init__(self, *a, **kw): pass def exec(self): return True def add_to_playlist(self): return False def new_playlist_name(self): return name def chosen_entries(self): return chosen def spotify_tracks(self): return [] def download_urls(self): return [c["url"] for c in chosen] monkeypatch.setattr(mw, "UrlImportDialog", _Dialog) window._import_from_url() worker = window._url_worker _pump(qapp, lambda: worker.temp_dir is None) def _named(manager, name): return [p for p in manager.library.playlists.values() if p.name == name] def test_the_new_playlist_holds_every_song_in_order(window, qapp, fake, mp3_file, monkeypatch): monkeypatch.setenv("FAKE_YTDLP_AUDIO", str(mp3_file)) manager = window._manager _import(window, qapp, monkeypatch, ["ccc", "bad1", "aaa"]) [playlist] = _named(manager, "Road Trip") titles = [manager.library.tracks[t].name for t in playlist.track_ids] assert titles == ["Song ccc [ccc]", "Song aaa [aaa]"] # not the failed one manager.undo_stack.undo() # one step takes it all away assert _named(manager, "Road Trip") == [] assert len(manager.library.tracks) == 2 # the songs stay imported def test_nothing_downloaded_makes_no_playlist(window, qapp, fake, mp3_file, monkeypatch): from lintunes.gui import main_window as mw warned = [] monkeypatch.setattr(mw.QMessageBox, "warning", lambda *a, **kw: warned.append(a)) monkeypatch.setenv("FAKE_YTDLP_AUDIO", str(mp3_file)) _import(window, qapp, monkeypatch, ["bad1", "bad2"]) _pump(qapp, lambda: warned) # "Couldn't import from that link" assert _named(window._manager, "Road Trip") == [] def test_unticked_makes_no_playlist(window, qapp, fake, mp3_file, monkeypatch): monkeypatch.setenv("FAKE_YTDLP_AUDIO", str(mp3_file)) before = set(window._manager.library.playlists) _import(window, qapp, monkeypatch, ["aaa", "bbb"], name="") assert set(window._manager.library.playlists) == before