添加 Musicfox Waybar 歌词模块
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import unittest
|
||||
|
||||
from musicfox_lyrics import (
|
||||
LyricLine,
|
||||
PlayerState,
|
||||
candidate_score,
|
||||
extract_song_id,
|
||||
lyric_at,
|
||||
parse_lrc,
|
||||
)
|
||||
|
||||
|
||||
class LyricsTest(unittest.TestCase):
|
||||
def test_parses_and_sorts_lrc_with_offset(self) -> None:
|
||||
lines = parse_lrc(
|
||||
"[offset:500]\n[00:10.000]第二句\n[00:01.250][00:05.000]第一句\n[00:20.000]"
|
||||
)
|
||||
self.assertEqual(
|
||||
lines,
|
||||
[
|
||||
LyricLine(1.75, "第一句"),
|
||||
LyricLine(5.5, "第一句"),
|
||||
LyricLine(10.5, "第二句"),
|
||||
],
|
||||
)
|
||||
|
||||
def test_selects_line_for_playback_position(self) -> None:
|
||||
lines = [LyricLine(1, "一"), LyricLine(3, "二"), LyricLine(8, "三")]
|
||||
self.assertEqual(lyric_at(lines, 0.5), "")
|
||||
self.assertEqual(lyric_at(lines, 3.2), "二")
|
||||
self.assertEqual(lyric_at(lines, 20), "三")
|
||||
|
||||
def test_extracts_netease_song_id(self) -> None:
|
||||
self.assertEqual(extract_song_id("https://music.163.com/#/song?id=347230"), 347230)
|
||||
self.assertEqual(extract_song_id("/org/mpris/MediaPlayer2/song_347230"), 347230)
|
||||
self.assertIsNone(extract_song_id("https://example.com/track/347230"))
|
||||
|
||||
def test_song_match_prefers_title_artist_and_duration(self) -> None:
|
||||
state = PlayerState("musicfox", "Playing", 10, "海阔天空", "Beyond", 326, "", "")
|
||||
correct = {
|
||||
"id": 347230,
|
||||
"name": "海阔天空",
|
||||
"artists": [{"name": "Beyond"}],
|
||||
"duration": 326000,
|
||||
}
|
||||
wrong_version = {
|
||||
"id": 1,
|
||||
"name": "海阔天空",
|
||||
"artists": [{"name": "Beyond"}],
|
||||
"duration": 239000,
|
||||
}
|
||||
self.assertGreater(candidate_score(correct, state), candidate_score(wrong_version, state))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user