Commit 34fe74d2 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Fix synced lyrics being silently skipped during tagging

The synced-lyrics lookup was only attempted when the identification
candidate had no plain lyrics of its own - but Shazam and Gemini can
both independently return a plain snippet as part of identification,
which was short-circuiting the sync fetch entirely and silently
discarding real timing data that was actually available. Now always
attempted, with synced data (when found) always winning over whatever
plain-text source identification provided.

Also fixes the Discovery "give from shared library" path, which
copied a source song's plain lyrics but not its synced_lyrics -
copies given via Discovery were silently losing sync data even when
the source had it.

Existing tests never caught this because they always constructed a
candidate with lyrics already stripped out; also fixes a shared
mutable test fixture (FAKE_CANDIDATE) that the code under test
mutates in place, which was silently leaking state between tests.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent ae06a8fd
Pipeline #1149 failed with stage
in 153 minutes
...@@ -69,6 +69,7 @@ def _give_from_shared_library(db: Session, current_user: User, title: str, artis ...@@ -69,6 +69,7 @@ def _give_from_shared_library(db: Session, current_user: User, title: str, artis
genre=source_song.genre, genre=source_song.genre,
coverart=source_song.coverart, coverart=source_song.coverart,
lyrics=source_song.lyrics, lyrics=source_song.lyrics,
synced_lyrics=source_song.synced_lyrics,
file_path=source_song.file_path, file_path=source_song.file_path,
fingerprint=source_song.fingerprint, fingerprint=source_song.fingerprint,
) )
......
...@@ -118,10 +118,20 @@ async def _identify_tag_and_store( ...@@ -118,10 +118,20 @@ async def _identify_tag_and_store(
gemini_result = await identify_with_gemini(raw_file_path, language_preference) gemini_result = await identify_with_gemini(raw_file_path, language_preference)
candidate = gemini_result if gemini_result else {} candidate = gemini_result if gemini_result else {}
if not candidate.get("lyrics"): # Always attempted, not gated on "candidate already has plain lyrics" - Shazam and
# Gemini can both independently return a plain lyrics snippet as part of
# identification, which used to short-circuit this entirely and silently lose real
# synced timing that was actually available. Synced data, when found, always wins
# over whichever plain-text source the identification step provided, since the
# parsed-from-LRC text is guaranteed to match the timing exactly - a different
# plain-text source sitting next to it would drift from what's highlighted.
lyrics_result = await fetch_lyrics(candidate.get("title", ""), candidate.get("artist", "")) lyrics_result = await fetch_lyrics(candidate.get("title", ""), candidate.get("artist", ""))
if lyrics_result["synced_lyrics"]:
candidate["lyrics"] = lyrics_result["lyrics"] candidate["lyrics"] = lyrics_result["lyrics"]
candidate["synced_lyrics"] = lyrics_result["synced_lyrics"] candidate["synced_lyrics"] = lyrics_result["synced_lyrics"]
elif not candidate.get("lyrics"):
candidate["lyrics"] = lyrics_result["lyrics"]
candidate["synced_lyrics"] = None
if not candidate.get("genre") or candidate.get("genre") == "Unknown Genre": if not candidate.get("genre") or candidate.get("genre") == "Unknown Genre":
title, artist = candidate.get("title", ""), candidate.get("artist", "") title, artist = candidate.get("title", ""), candidate.get("artist", "")
......
...@@ -157,6 +157,28 @@ def test_discovery_listen_gives_a_song_already_in_someone_elses_library(client, ...@@ -157,6 +157,28 @@ def test_discovery_listen_gives_a_song_already_in_someone_elses_library(client,
assert given.fingerprint == "abc123" assert given.fingerprint == "abc123"
def test_discovery_listen_give_copies_synced_lyrics_from_the_source_song(client, db_session, auth_as, monkeypatch):
headers, finder = auth_as()
_, contributor = auth_as()
db_session.add(LibrarySong(
user_id=contributor.id, title="Timed Song", artist="Timed Artist", genre="Pop",
file_path="/data/timed-song.mp3", fingerprint="def456",
synced_lyrics='[[0.0, "la"], [1.5, "la la"]]',
))
db_session.commit()
async def _fake_identify_hit(path):
return [{"title": "Timed Song", "artist": "Timed Artist", "genre": "Pop", "coverart": None}]
monkeypatch.setattr(discovery_router, "identify_audio", _fake_identify_hit)
response = client.post("/discovery/listen", headers=headers, files={"file": _dummy_audio_file()})
body = response.json()
given = db_session.query(LibrarySong).filter(LibrarySong.id == body["library_song_id"]).first()
assert given.user_id == finder.id
assert given.synced_lyrics == '[[0.0, "la"], [1.5, "la la"]]'
def test_discovery_listen_does_not_duplicate_a_song_the_finder_already_has(client, db_session, auth_as, monkeypatch): def test_discovery_listen_does_not_duplicate_a_song_the_finder_already_has(client, db_session, auth_as, monkeypatch):
headers, finder = auth_as() headers, finder = auth_as()
existing = LibrarySong(user_id=finder.id, title="Already Mine", artist="Someone", genre="Pop", file_path="/data/mine.mp3") existing = LibrarySong(user_id=finder.id, title="Already Mine", artist="Someone", genre="Pop", file_path="/data/mine.mp3")
......
...@@ -13,7 +13,11 @@ FAKE_CANDIDATE = { ...@@ -13,7 +13,11 @@ FAKE_CANDIDATE = {
def _mock_pipeline(mocker, final_path="/tmp/does-not-need-to-exist.mp3", url_path="/downloads/1/x.mp3", fingerprint=None): def _mock_pipeline(mocker, final_path="/tmp/does-not-need-to-exist.mp3", url_path="/downloads/1/x.mp3", fingerprint=None):
mocker.patch("src.api.routers.music.compute_fingerprint", return_value=fingerprint) mocker.patch("src.api.routers.music.compute_fingerprint", return_value=fingerprint)
mocker.patch("src.api.routers.music.identify_audio", return_value=[FAKE_CANDIDATE]) # A fresh copy every call, not the shared FAKE_CANDIDATE object itself -
# _identify_tag_and_store mutates the candidate dict in place (setting lyrics,
# synced_lyrics, genre, ...), so handing out the same object across tests would let one
# test's mutations silently leak into every later test in the same pytest session.
mocker.patch("src.api.routers.music.identify_audio", return_value=[dict(FAKE_CANDIDATE)])
mocker.patch("src.api.routers.music.tag_and_convert_audio", return_value="/tmp/tagged.mp3") mocker.patch("src.api.routers.music.tag_and_convert_audio", return_value="/tmp/tagged.mp3")
mocker.patch("src.api.routers.music.storage.store_final", return_value=(final_path, url_path)) mocker.patch("src.api.routers.music.storage.store_final", return_value=(final_path, url_path))
...@@ -120,6 +124,34 @@ def test_music_upload_persists_synced_lyrics(client, db_session, auth_as, mocker ...@@ -120,6 +124,34 @@ def test_music_upload_persists_synced_lyrics(client, db_session, auth_as, mocker
assert song.synced_lyrics == '[[0.0, "la"], [1.5, "la la"]]' assert song.synced_lyrics == '[[0.0, "la"], [1.5, "la la"]]'
def test_music_upload_synced_lyrics_override_candidates_own_plain_lyrics(client, db_session, auth_as, mocker):
"""Regression: Shazam/Gemini can both independently return a plain `lyrics` snippet as part
of identification (FAKE_CANDIDATE has one) - that must not short-circuit the synced-lyrics
lookup entirely, or real available sync timing gets silently dropped on the floor."""
headers, user = auth_as()
_mock_pipeline(mocker) # identify_audio returns FAKE_CANDIDATE, which already has lyrics set
fetch_lyrics = mocker.patch(
"src.api.routers.music.fetch_lyrics",
return_value={"lyrics": "real\nsynced lyrics", "synced_lyrics": [[0.0, "real"], [2.0, "synced lyrics"]]},
)
response = client.post(
"/music/upload",
headers=headers,
files={"file": ("test.mp3", b"fake mp3 bytes", "audio/mpeg")},
)
assert response.status_code == 200
fetch_lyrics.assert_called_once()
assert response.json()["metadata"]["synced_lyrics"] == [[0.0, "real"], [2.0, "synced lyrics"]]
assert response.json()["metadata"]["lyrics"] == "real\nsynced lyrics"
song = db_session.query(LibrarySong).filter(LibrarySong.user_id == user.id).first()
assert song.synced_lyrics == '[[0.0, "real"], [2.0, "synced lyrics"]]'
# The synced source's own text replaces the candidate's plain lyrics too, so the two never
# disagree with what's actually highlighted.
assert song.lyrics == "real\nsynced lyrics"
def test_music_upload_no_synced_lyrics_stays_null(client, db_session, auth_as, mocker): def test_music_upload_no_synced_lyrics_stays_null(client, db_session, auth_as, mocker):
headers, user = auth_as() headers, user = auth_as()
candidate_without_lyrics = {**FAKE_CANDIDATE, "lyrics": None} candidate_without_lyrics = {**FAKE_CANDIDATE, "lyrics": None}
...@@ -138,6 +170,27 @@ def test_music_upload_no_synced_lyrics_stays_null(client, db_session, auth_as, m ...@@ -138,6 +170,27 @@ def test_music_upload_no_synced_lyrics_stays_null(client, db_session, auth_as, m
assert song.synced_lyrics is None assert song.synced_lyrics is None
def test_music_upload_keeps_candidates_own_lyrics_when_no_synced_source_found(client, db_session, auth_as, mocker):
"""When identification already provided plain lyrics and no synced source exists anywhere,
that plain lyrics must survive rather than getting wiped by fetch_lyrics's own empty
fallback result."""
headers, user = auth_as()
_mock_pipeline(mocker) # FAKE_CANDIDATE's lyrics is "la la la"
mocker.patch("src.api.routers.music.fetch_lyrics", return_value={"lyrics": None, "synced_lyrics": None})
response = client.post(
"/music/upload",
headers=headers,
files={"file": ("test.mp3", b"fake mp3 bytes", "audio/mpeg")},
)
assert response.json()["metadata"]["synced_lyrics"] is None
assert response.json()["metadata"]["lyrics"] == "la la la"
song = db_session.query(LibrarySong).filter(LibrarySong.user_id == user.id).first()
assert song.synced_lyrics is None
assert song.lyrics == "la la la"
def test_music_upload_new_fingerprint_populates_cache(client, db_session, auth_as, mocker): def test_music_upload_new_fingerprint_populates_cache(client, db_session, auth_as, mocker):
headers, _ = auth_as() headers, _ = auth_as()
fingerprint = "fp-new-song-abc123" fingerprint = "fp-new-song-abc123"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment