"""Round 120: Connections ▸ Full Rabbit Sync. - "Sync to Rabbit" is now "Sync Music to Rabbit", and does what it did. - "Full Rabbit Sync" runs that music sync and then Import Shazams, adding them to the playlist that was current when it was clicked. - A cancelled music sync stops it; a failed one or nothing to sync still goes on to the Shazams (they come over adb, not the MTP mount). """ import time import pytest from lintunes import device_sync from lintunes.library_manager import LibraryManager from lintunes.models import Library from lintunes.preferences import Preferences def _pump(qapp, until, timeout=3.0): deadline = time.monotonic() + timeout while time.monotonic() < deadline: qapp.processEvents() if until(): return True time.sleep(0.01) return until() @pytest.fixture def window(qapp, tmp_path, monkeypatch): from lintunes.gui.main_window import MainWindow manager = LibraryManager(Library(), tmp_path / "data") win = MainWindow(manager, Preferences(tmp_path / "data")) calls = [] monkeypatch.setattr(win, "_import_shazams", lambda target=None: calls.append(target)) monkeypatch.setattr(win, "_when_app_active", lambda show: show()) win.shazam_calls = calls yield win win.close() def _summary(): return {"playlists": 1, "device": "Rabbit", "tracks": 1, "copied": 1, "kept": 0, "removed": 0, "skipped": 0, "total": 1} def test_menu_names(window): window._refresh_device_actions() assert window._andtunes_action.text() == "Sync Music to Rabbit" assert window._full_sync_action.text() == "Full Rabbit Sync" # No Rabbit plugged in: neither can start. assert not window._full_sync_action.isEnabled() def test_full_sync_with_nothing_ticked_goes_straight_to_shazams( window, monkeypatch): target = object() monkeypatch.setattr(window, "_url_import_target", lambda: target) monkeypatch.setattr(window._devices, "when_ready", lambda cb: cb(object(), device_sync.ABSENT)) window._full_rabbit_sync() assert window.shazam_calls == [target] def test_shazams_follow_a_finished_music_sync(window, qapp): target = object() window._full_sync_pending, window._full_sync_target = True, target window._on_andtunes_finished(_summary()) assert _pump(qapp, lambda: window.shazam_calls) assert window.shazam_calls == [target] assert not window._full_sync_pending def test_a_cancelled_music_sync_stops_there(window, qapp): window._full_sync_pending = True window._on_sync_cancelled(_summary()) window._then_shazams() qapp.processEvents() assert window.shazam_calls == [] def test_a_failed_music_sync_still_imports_shazams(window, qapp, monkeypatch): from lintunes.gui import main_window as mw monkeypatch.setattr(mw.QMessageBox, "warning", lambda *a, **k: None) window._full_sync_pending = True window._on_sync_failed("mount wedged") assert window.shazam_calls == [None] def test_plain_music_sync_never_imports_shazams(window, qapp): window._on_andtunes_finished(_summary()) qapp.processEvents() assert window.shazam_calls == [] def test_waits_for_the_worker_to_let_go(window, qapp): class _Busy: busy_for = 3 def busy(self): self.busy_for -= 1 return self.busy_for > 0 window._andtunes_worker = _Busy() window._full_sync_pending = True window._then_shazams() assert window.shazam_calls == [] assert _pump(qapp, lambda: window.shazam_calls)