v0.79.3: editing the playing song's tags or art no longer glitches playback

This commit is contained in:
2026-10-25 20:09:55 -07:00
parent b8f2325f0b
commit eba27e7b8c
5 changed files with 179 additions and 46 deletions
+8
View File
@@ -26,6 +26,14 @@ def _no_real_device(monkeypatch):
monkeypatch.setattr(device_sync, "find_device", lambda: None)
@pytest.fixture(autouse=True)
def _no_file_in_use(monkeypatch):
"""A MainWindow points tagging at its player (Round 111); one a test left
behind mustn't decide how a later test's tag writes are saved."""
from lintunes import tagging
monkeypatch.setattr(tagging, "in_use", lambda: ())
@pytest.fixture(autouse=True)
def _no_dj_sound(monkeypatch):
"""DJ mode's sink never opens in a test: the mixer would play the decks
+88 -43
View File
@@ -1,53 +1,98 @@
"""Round 111: the visualizer page's sliders are tall and jump where clicked;
the radial dot size reads as a share of the spacing between dots."""
from unittest.mock import MagicMock
"""Round 111: a tag or art save on the playing song doesn't glitch playback.
from PyQt6.QtCore import QPoint, Qt
from PyQt6.QtTest import QTest
mutagen saves in place, shifting the audio inside the file when a tag
outgrows its padding; a decoder holding the file open then reads from the
wrong offsets. The playing file is instead edited as a copy and renamed
over it, so what's already open stays byte for byte what it was."""
import os
from lintunes.gui import preferences_dialog as pd
from lintunes.gui.preferences_dialog import PreferencesDialog
from lintunes.gui.transport import ClickJumpSlider
from lintunes.preferences import Preferences
import pytest
from lintunes import tagging
def _dialog(tmp_path):
dialog = PreferencesDialog(Preferences(tmp_path), MagicMock())
dialog.resize(900, 700)
dialog.pages_list.setCurrentRow(2)
dialog._viz_radios["radial"].setChecked(True)
dialog.show()
QTest.qWait(20)
return dialog
def _big_art():
# Far more than any padding: the in-place save would move the audio.
return b"\xff\xd8\xff\xe0" + os.urandom(300_000)
def test_visualizer_sliders_are_tall_click_jump_sliders(qapp, tmp_path):
dialog = _dialog(tmp_path)
sliders = [dialog._radial_dot_slider, dialog._fade_slider,
dialog._sync_sliders["radial"], dialog._dot_slider]
for slider in sliders:
assert isinstance(slider, ClickJumpSlider)
assert slider.property("tall")
dialog.close()
def _strays(path):
return [p for p in path.parent.iterdir() if p.name.startswith(".lintunes-tag-")]
def test_a_click_puts_the_handle_where_it_lands(qapp, tmp_path):
dialog = _dialog(tmp_path)
slider = dialog._radial_dot_slider
y = slider.height() // 2
QTest.mouseClick(slider, Qt.MouseButton.LeftButton, pos=QPoint(0, y))
assert slider.value() == slider.minimum()
QTest.mouseClick(slider, Qt.MouseButton.LeftButton,
pos=QPoint(slider.width() - 1, y))
assert slider.value() == slider.maximum()
QTest.mouseClick(slider, Qt.MouseButton.LeftButton,
pos=QPoint(slider.width() // 2, y))
middle = (slider.minimum() + slider.maximum()) / 2
assert abs(slider.value() - middle) <= 2 # not one page step from the end
dialog.close()
@pytest.mark.parametrize("fixture", ["mp3_file", "flac_file", "m4a_file"])
def test_the_playing_file_is_replaced_not_rewritten(request, monkeypatch, fixture):
path = request.getfixturevalue(fixture)
original = path.read_bytes()
monkeypatch.setattr(tagging, "in_use", lambda: (str(path),))
with open(path, "rb") as playing: # the decoder's open file
inode = os.fstat(playing.fileno()).st_ino
tagging.write_artwork(path, _big_art())
tagging.write_tags(path, {"name": "New Name", "artist": "Someone"})
assert playing.read() == original # nothing moved under it
assert os.stat(path).st_ino != inode
assert tagging.read_tags(path)["name"] == "New Name"
assert tagging.read_embedded_artwork(path) is not None
assert _strays(path) == []
def test_dot_size_is_a_share_of_the_spacing():
# The automatic size (1×) is 70% of the gap; dots touch at 100%.
assert pd._dot_text(1.0) == "70%"
assert pd._dot_text(1 / 0.7) == "100%"
def test_mode_is_kept(mp3_file, monkeypatch):
os.chmod(mp3_file, 0o640)
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
tagging.write_tags(mp3_file, {"name": "x"})
assert os.stat(mp3_file).st_mode & 0o777 == 0o640
def test_other_files_are_still_saved_in_place(mp3_file):
inode = os.stat(mp3_file).st_ino
tagging.write_tags(mp3_file, {"name": "In Place"})
assert os.stat(mp3_file).st_ino == inode
assert tagging.read_tags(mp3_file)["name"] == "In Place"
def test_a_failed_save_leaves_the_file_and_no_copy(mp3_file, monkeypatch):
original = mp3_file.read_bytes()
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
real = tagging._write_tags
def broken(work, fields, path):
real(work, fields, path)
raise RuntimeError("disk on fire")
monkeypatch.setattr(tagging, "_write_tags", broken)
with pytest.raises(RuntimeError):
tagging.write_tags(mp3_file, {"name": "never"})
assert mp3_file.read_bytes() == original
assert _strays(mp3_file) == []
def test_no_room_for_a_copy_saves_in_place(mp3_file, monkeypatch):
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
def full(*_a, **_k):
raise OSError(28, "No space left on device")
monkeypatch.setattr(tagging.shutil, "copyfile", full)
tagging.write_tags(mp3_file, {"name": "Squeezed"})
assert tagging.read_tags(mp3_file)["name"] == "Squeezed"
assert _strays(mp3_file) == []
def test_the_window_names_the_playing_song(qapp, tmp_path):
from lintunes.gui.main_window import MainWindow
from lintunes.library_manager import LibraryManager
from lintunes.models.library import Library
from lintunes.models.track import Track
from lintunes.preferences import Preferences
library = Library()
library.tracks[1] = Track(track_id=1, name="Song", location="/music/song.mp3")
window = MainWindow(LibraryManager(library, tmp_path / "data"),
Preferences(tmp_path / "data"))
try:
assert tagging.in_use == window._files_in_use
assert tagging.in_use() == ()
window.player._current_track = library.tracks[1]
assert tagging.in_use() == ("/music/song.mp3",)
finally:
window.player._current_track = None
window.close()