feat(lyrics): add selectable LRCLIB results (#102)
This commit is contained in:
@@ -7,6 +7,118 @@ import urllib.error
|
||||
import lyric_sources
|
||||
|
||||
|
||||
class LrclibAdapterTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.track = {
|
||||
"title": "Can't Stop",
|
||||
"artist": "Red Hot Chili Peppers",
|
||||
"album": "By the Way",
|
||||
"duration": 269_000_000,
|
||||
}
|
||||
self.results = [
|
||||
{
|
||||
"id": 10,
|
||||
"trackName": "Can't Stop",
|
||||
"artistName": "Red Hot Chili Peppers",
|
||||
"albumName": "By the Way",
|
||||
"duration": 269,
|
||||
"plainLyrics": "Plain line",
|
||||
"syncedLyrics": "",
|
||||
},
|
||||
{
|
||||
"id": 20,
|
||||
"trackName": "Can't Stop",
|
||||
"artistName": "Red Hot Chili Peppers",
|
||||
"albumName": "By the Way",
|
||||
"duration": 269,
|
||||
"plainLyrics": "Synced line",
|
||||
"syncedLyrics": "[00:01.00]Synced line",
|
||||
},
|
||||
]
|
||||
|
||||
@mock.patch("lyric_sources.itunes_cover", return_value="")
|
||||
@mock.patch("lyric_sources.request_json")
|
||||
def test_prefers_synced_candidate_and_returns_metadata(self, request_json, _itunes_cover):
|
||||
request_json.return_value = self.results
|
||||
|
||||
result = lyric_sources.adapter_lrclib(self.track, {}, {})
|
||||
|
||||
self.assertEqual(result["selected_candidate_id"], "20")
|
||||
self.assertEqual(result["lines"][0]["time"], 1000)
|
||||
self.assertEqual([item["id"] for item in result["candidates"]], ["20", "10"])
|
||||
self.assertNotIn("plainLyrics", result["candidates"][0])
|
||||
self.assertTrue(result["candidates"][0]["synced"])
|
||||
|
||||
@mock.patch("lyric_sources.itunes_cover", return_value="")
|
||||
@mock.patch("lyric_sources.request_json")
|
||||
def test_honors_requested_candidate(self, request_json, _itunes_cover):
|
||||
request_json.return_value = self.results
|
||||
|
||||
result = lyric_sources.adapter_lrclib(
|
||||
self.track, {}, {"lyrics_candidate_id": "10"}
|
||||
)
|
||||
|
||||
self.assertEqual(result["selected_candidate_id"], "10")
|
||||
self.assertEqual(result["lines"][0]["time"], -1)
|
||||
self.assertEqual(result["lines"][0]["text"], "Plain line")
|
||||
|
||||
@mock.patch("lyric_sources.request_json")
|
||||
def test_rejects_stale_requested_candidate(self, request_json):
|
||||
request_json.return_value = self.results
|
||||
|
||||
result = lyric_sources.adapter_lrclib(
|
||||
self.track, {}, {"lyrics_candidate_id": "missing"}
|
||||
)
|
||||
|
||||
self.assertEqual(result["type"], "none")
|
||||
self.assertEqual(result["diag"], ["lrclib: requested match unavailable"])
|
||||
|
||||
def test_rejects_wrong_artist_even_when_synced(self):
|
||||
results = [
|
||||
{
|
||||
"id": 30,
|
||||
"trackName": "Can't Stop",
|
||||
"artistName": "Unrelated Artist",
|
||||
"duration": 269,
|
||||
"syncedLyrics": "[00:01.00]Wrong",
|
||||
},
|
||||
self.results[0],
|
||||
]
|
||||
|
||||
ranked = lyric_sources.lrclib_candidates(results, self.track)
|
||||
|
||||
self.assertEqual([item["id"] for item in ranked], [10])
|
||||
|
||||
def test_prefers_duration_bucket_before_sync_status(self):
|
||||
results = [
|
||||
{
|
||||
"id": 30,
|
||||
"trackName": "Can't Stop",
|
||||
"artistName": "Red Hot Chili Peppers",
|
||||
"albumName": "By the Way",
|
||||
"duration": 400,
|
||||
"syncedLyrics": "[00:01.00]Wrong version",
|
||||
},
|
||||
self.results[0],
|
||||
]
|
||||
|
||||
ranked = lyric_sources.lrclib_candidates(results, self.track)
|
||||
|
||||
self.assertEqual([item["id"] for item in ranked], [10, 30])
|
||||
|
||||
def test_filters_missing_and_duplicate_candidate_ids(self):
|
||||
duplicate = dict(self.results[1])
|
||||
duplicate["plainLyrics"] = "Duplicate"
|
||||
missing = dict(self.results[0])
|
||||
missing.pop("id")
|
||||
|
||||
ranked = lyric_sources.lrclib_candidates(
|
||||
[missing, self.results[1], duplicate], self.track
|
||||
)
|
||||
|
||||
self.assertEqual([item["id"] for item in ranked], [20])
|
||||
|
||||
|
||||
class SPlayerLinesTest(unittest.TestCase):
|
||||
def test_preserves_timing_layers_and_markers(self):
|
||||
lines = lyric_sources.splayer_transmitted_lines({
|
||||
|
||||
Reference in New Issue
Block a user