"""Round 106: Compress lossless imports to MP3 (importers/lossless.py, gui/compress_import.py).""" import hashlib import shutil import subprocess import time from pathlib import Path import pytest from lintunes import tagging from lintunes.importers import lossless from lintunes.library_manager import LibraryManager from lintunes.models import Library, Playlist from lintunes.preferences import Preferences from tests.conftest import _generate_audio @pytest.fixture def isolated_config(tmp_path, monkeypatch): monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) return tmp_path / "config" @pytest.fixture def fake_trash(tmp_path, monkeypatch): """Cancel trashes the copies it made; never into the real trash.""" bin_ = tmp_path / "trash-bin" bin_.mkdir() def send(path): dest = bin_ / f"{len(list(bin_.iterdir()))}-{Path(path).name}" shutil.move(str(path), dest) return dest monkeypatch.setattr("lintunes.library_manager.trash.send_to_trash", send) return bin_ def _digest(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def _tagged_flac(path: Path, title: str, art: bytes | None = None, seconds: int = 1) -> Path: if shutil.which("ffmpeg") is None: pytest.skip("ffmpeg not available") subprocess.run(["ffmpeg", "-y", "-f", "lavfi", "-i", f"anoisesrc=d={seconds}:a=0.3", "-ac", "2", "-c:a", "flac", str(path)], capture_output=True, check=True) tagging.write_tags(path, {"name": title, "artist": "Nissa", "album": "Big Files", "year": 2019, "track_number": 3, "track_count": 9}) if art: tagging.write_artwork(path, art) return path def _manager(tmp_path, pid_tracks=None): (tmp_path / "media").mkdir(exist_ok=True) # or the window asks for one library = Library(music_folder=str(tmp_path / "media")) if pid_tracks is not None: library.playlists["P"] = Playlist(name="Mix", persistent_id="P", track_ids=list(pid_tracks)) return LibraryManager(library, tmp_path / "data") def _wait(qapp, batch, seconds=20): deadline = time.time() + seconds while batch.worker.busy() and time.time() < deadline: qapp.processEvents() time.sleep(0.01) for _ in range(5): qapp.processEvents() # ---- pure parts ---- def test_off_by_default(tmp_path): prefs = Preferences(tmp_path / "data") assert lossless.settings(prefs) == (False, 256) prefs.set("import_compress_kbps", 999) assert lossless.settings(prefs)[1] == lossless.DEFAULT_BITRATE def test_the_five_bitrates(): assert lossless.BITRATES == (320, 256, 192, 160, 128) def test_what_counts_as_lossless(tmp_path, mp3_file, m4a_file, flac_file): assert lossless.is_lossless(flac_file) assert not lossless.is_lossless(mp3_file) assert not lossless.is_lossless(m4a_file) # plain AAC alac = _generate_audio(tmp_path / "alac.m4a", ["-c:a", "alac"]) assert lossless.is_lossless(alac) for name in ("a.wav", "a.aiff", "a.AIF"): assert lossless.is_lossless(Path(name)) def test_progress_lines(): assert lossless.parse_progress("out_time_us=5000000", 10_000) == 50 assert lossless.parse_progress("out_time_us=99000000", 10_000) == 99 assert lossless.parse_progress("progress=end", 10_000) == 100 assert lossless.parse_progress("out_time_us=N/A", 10_000) is None assert lossless.parse_progress("bitrate=1.0kbits/s", 10_000) is None assert lossless.parse_progress("out_time_us=5", 0) is None # ---- the conversion, through the real ffmpeg ---- def test_convert_keeps_tags_and_art_and_never_touches_the_original( tmp_path, jpeg_bytes): src = _tagged_flac(tmp_path / "song.flac", "Huge", jpeg_bytes, seconds=3) before = _digest(src) dest = tmp_path / "out" / "song.mp3" dest.parent.mkdir() seen = [] assert lossless.convert(src, dest, 128, 3000, seen.append) assert _digest(src) == before fields = tagging.read_tags(dest) assert fields["kind"] == "MPEG audio file" assert fields["name"] == "Huge" and fields["artist"] == "Nissa" assert fields["album"] == "Big Files" and fields["year"] == 2019 assert (fields["track_number"], fields["track_count"]) == (3, 9) assert 120 <= fields["bit_rate"] <= 136 assert tagging.read_embedded_artwork(dest) == jpeg_bytes assert seen and seen[-1] == 100 assert dest.stat().st_size < src.stat().st_size def test_a_failed_conversion_says_why_and_leaves_nothing(tmp_path): bad = tmp_path / "broken.flac" bad.write_bytes(b"not audio at all") dest = tmp_path / "broken.mp3" with pytest.raises(OSError, match="ffmpeg failed on broken.flac"): lossless.convert(bad, dest, 256) assert not dest.exists() # ---- a batch ---- def _batch(qapp, tmp_path, files, pid="", position=None, manager=None): from lintunes.gui.compress_import import CompressImport, plan_items manager = manager or _manager(tmp_path) music = tmp_path / "media" / "Music" items = plan_items(files, manager, music) batch = CompressImport(manager, music, items, 192, pid, position) return manager, batch def test_a_batch_imports_in_order_into_the_playlist(qapp, tmp_path, mp3_file): a = _tagged_flac(tmp_path / "a.flac", "Alpha") b = _tagged_flac(tmp_path / "b.flac", "Bravo") sums = {a: _digest(a), b: _digest(b)} manager = _manager(tmp_path) old = manager.add_track(tagging_track(tmp_path)) manager.library.playlists["P"] = Playlist(name="Mix", persistent_id="P", track_ids=[old, old]) manager, batch = _batch(qapp, tmp_path, [a, mp3_file, b], "P", 1, manager) assert batch.window is not None over = [] batch.over.connect(over.append) batch.start() _wait(qapp, batch) playlist = manager.library.playlists["P"] names = [manager.library.tracks[t].name for t in playlist.track_ids] assert names == ["Old", "Alpha", "test", "Bravo", "Old"] alpha = manager.library.tracks[playlist.track_ids[1]] assert alpha.location.endswith(".mp3") and "media/Music/Nissa" in alpha.location assert alpha.kind == "MPEG audio file" assert all(_digest(p) == d for p, d in sums.items()) assert batch.window.done_button.isEnabled() assert not batch.worker.tmp_dir.exists() # Not undoable piecemeal: Cancel is this import's undo. assert not manager.undo_stack.can_undo() batch.window.done_button.click() assert over and "3 songs" in over[0] and "2 converted to 192 kbps" in over[0] def tagging_track(tmp_path): from lintunes.models import Track return Track(name="Old", location=str(tmp_path / "old.mp3")) def test_cancel_after_finishing_takes_every_song_back( qapp, tmp_path, mp3_file, fake_trash): a = _tagged_flac(tmp_path / "a.flac", "Alpha") a_sum = _digest(a) manager, batch = _batch(qapp, tmp_path, [a, mp3_file], "P", manager=_manager(tmp_path, pid_tracks=[])) batch.start() _wait(qapp, batch) assert len(manager.library.tracks) == 2 batch.request_cancel(confirm=False) assert manager.library.tracks == {} assert manager.library.playlists["P"].track_ids == [] assert list((tmp_path / "media").rglob("*.mp3")) == [] assert len(list(fake_trash.iterdir())) == 2 # copies, not originals assert _digest(a) == a_sum and mp3_file.exists() def test_cancel_never_trashes_a_file_imported_in_place(qapp, tmp_path, fake_trash): music = tmp_path / "media" / "Music" music.mkdir(parents=True) in_place = _generate_audio(music / "already.mp3", ["-codec:a", "libmp3lame", "-b:a", "64k"]) a = _tagged_flac(tmp_path / "a.flac", "Alpha") manager, batch = _batch(qapp, tmp_path, [in_place, a]) batch.start() _wait(qapp, batch) batch.request_cancel(confirm=False) assert manager.library.tracks == {} assert in_place.exists() assert len(list(fake_trash.iterdir())) == 1 def test_one_song_has_no_window(qapp, tmp_path): a = _tagged_flac(tmp_path / "a.flac", "Alpha") manager, batch = _batch(qapp, tmp_path, [a]) assert batch.window is None statuses, over = [], [] batch.status.connect(lambda *s: statuses.append(s)) batch.over.connect(over.append) batch.start() _wait(qapp, batch) assert statuses[0][0] == "Converting a.flac" assert over == ["Imported 1 song (1 converted to 192 kbps MP3)"] def test_a_lossless_file_already_in_the_library_is_not_converted_again( qapp, tmp_path): from lintunes.gui.compress_import import plan_items from lintunes.importers import file_importer music = tmp_path / "media" / "Music" music.mkdir(parents=True) kept = _tagged_flac(music / "kept.flac", "Kept") manager = _manager(tmp_path) file_importer.import_paths([kept], music, manager) [(source, convert, _)] = plan_items([kept], manager, music) assert not convert # ---- the window ---- def test_import_files_hands_lossless_files_to_the_converter( qapp, tmp_path, isolated_config, mp3_file): from lintunes.gui.main_window import MainWindow a = _tagged_flac(tmp_path / "a.flac", "Alpha") manager = _manager(tmp_path) prefs = Preferences(tmp_path / "data") window = MainWindow(manager, prefs) try: # Off: the old synchronous import, FLAC as it is. [track] = window.import_files([a]) assert track.location.endswith(".flac") manager.delete_tracks([track.track_id]) # On, but nothing lossless: unchanged too. prefs.set("import_compress", True) assert len(window.import_files([mp3_file])) == 1 # On, with a FLAC: in the background, into the status bar. assert window.import_files([tmp_path / "a.flac"]) == [] batch = window._compress assert batch is not None and window._busy_worker() is batch.worker assert window._sync_label.isVisibleTo(window) _wait(qapp, batch) assert window._compress is None assert not window._sync_label.isVisibleTo(window) assert any(t.location.endswith("a.mp3") for t in manager.library.tracks.values()) finally: window.close() def test_preferences_has_the_setting(qapp, tmp_path): from unittest.mock import MagicMock from lintunes.gui.preferences_dialog import PreferencesDialog prefs = Preferences(tmp_path / "data") dialog = PreferencesDialog(prefs, MagicMock()) assert not dialog._compress_check.isChecked() assert not dialog._compress_kbps.isEnabled() dialog._compress_check.setChecked(True) assert prefs.get("import_compress") is True dialog._compress_kbps.setCurrentIndex(lossless.BITRATES.index(160)) assert prefs.get("import_compress_kbps") == 160