/
/
/
1"""Tests for cleaning radio streamtitle."""
2
3from music_assistant.helpers.util import clean_stream_title, parse_quoted_stream_title
4
5
6def test_cleaning_streamtitle() -> None:
7 """Tests for cleaning radio streamtitle."""
8 tstm = "Thirty Seconds To Mars - Closer to the Edge"
9 advert = "Advert"
10
11 line = "Advertisement_Start_Length=00:00:29.960"
12 stream_title = clean_stream_title(line)
13 assert stream_title == advert
14
15 line = "Advertisement_Stop"
16 stream_title = clean_stream_title(line)
17 assert stream_title == advert
18
19 line = "START_AD_BREAK_6000"
20 stream_title = clean_stream_title(line)
21 assert stream_title == advert
22
23 line = "STOP ADBREAK 1"
24 stream_title = clean_stream_title(line)
25 assert stream_title == advert
26
27 line = "AD 2"
28 stream_title = clean_stream_title(line)
29 assert stream_title == advert
30
31 line = 'title="Thirty Seconds To Mars - Closer to the Edge",artist="Thirty Seconds To Mars - Closer to the Edge",url="https://nowplaying.scahw.com.au/c/fd8ee07bed6a5e4e9824a11aa02dd34a.jpg?t=1714568458&l=250"'
32 stream_title = clean_stream_title(line)
33 assert stream_title == tstm
34
35 line = 'title="https://listenapi.planetradio.co.uk/api9.2/eventdata/247801912",url="https://listenapi.planetradio.co.uk/api9.2/eventdata/247801912"'
36 stream_title = clean_stream_title(line)
37 assert stream_title == ""
38
39 line = 'title="Thirty Seconds To Mars - Closer to the Edge https://nowplaying.scahw.com.au/",artist="Thirty Seconds To Mars - Closer to the Edge",url="https://nowplaying.scahw.com.au/c/fd8ee07bed6a5e4e9824a11aa02dd34a.jpg?t=1714568458&l=250"'
40 stream_title = clean_stream_title(line)
41 assert stream_title == tstm
42
43 line = 'title="Closer to the Edge",artist="Thirty Seconds To Mars",url="https://nowplaying.scahw.com.au/c/fd8ee07bed6a5e4e9824a11aa02dd34a.jpg?t=1714568458&l=250"'
44 stream_title = clean_stream_title(line)
45 assert stream_title == tstm
46
47 line = 'title="Thirty Seconds To Mars - Closer to the Edge"'
48 stream_title = clean_stream_title(line)
49 assert stream_title == tstm
50
51 line = "Thirty Seconds To Mars - Closer to the Edge https://nowplaying.scahw.com.au/"
52 stream_title = clean_stream_title(line)
53 assert stream_title == tstm
54
55 line = "Lonely Street By: Andy Williams - WALMRadio.com"
56 stream_title = clean_stream_title(line)
57 assert stream_title == "Andy Williams - Lonely Street"
58
59 line = "Bye Bye Blackbird By: Sammy Davis Jr. - WALMRadio.com"
60 stream_title = clean_stream_title(line)
61 assert stream_title == "Sammy Davis Jr. - Bye Bye Blackbird"
62
63 line = (
64 "Asha Bhosle, Mohd Rafi (mp3yaar.com) - Gunguna Rahe Hain Bhanwre - Araadhna (mp3yaar.com)"
65 )
66 stream_title = clean_stream_title(line)
67 assert stream_title == "Asha Bhosle, Mohd Rafi - Gunguna Rahe Hain Bhanwre - Araadhna"
68
69 line = "Mohammed Rafi(Jatt.fm) - Rang Aur Noor Ki Baraat (Ghazal)(Jatt.fm)"
70 stream_title = clean_stream_title(line)
71 assert stream_title == "Mohammed Rafi - Rang Aur Noor Ki Baraat (Ghazal)"
72
73
74def test_cleaning_streamtitle_german_format() -> None:
75 """Test normalisation of the German "Track" von Artist format."""
76 line = '"Cornflake Girl" von Tori Amos'
77 stream_title = clean_stream_title(line)
78 assert stream_title == "Tori Amos - Cornflake Girl"
79
80 line = '"Closer to the Edge" von "Thirty Seconds To Mars"'
81 stream_title = clean_stream_title(line)
82 assert stream_title == "Thirty Seconds To Mars - Closer to the Edge"
83
84
85def test_cleaning_streamtitle_english_format() -> None:
86 """Test normalisation of the English "Track" by Artist from "Album" format."""
87 line = '"Nickel Wound" by Texas Is The Reason from "Do You Know Who You Are?"'
88 assert clean_stream_title(line) == "Texas Is The Reason - Nickel Wound"
89
90 # album is optional
91 line = '"Cornflake Girl" by Tori Amos'
92 assert clean_stream_title(line) == "Tori Amos - Cornflake Girl"
93
94
95def test_parse_quoted_stream_title() -> None:
96 """Test structured parsing of quoted stream titles, including album extraction."""
97 # English format with album
98 assert parse_quoted_stream_title(
99 '"Nickel Wound" by Texas Is The Reason from "Do You Know Who You Are?: '
100 'The Complete Collection"'
101 ) == (
102 "Nickel Wound",
103 "Texas Is The Reason",
104 "Do You Know Who You Are?: The Complete Collection",
105 )
106
107 # English format without album
108 assert parse_quoted_stream_title('"Cornflake Girl" by Tori Amos') == (
109 "Cornflake Girl",
110 "Tori Amos",
111 None,
112 )
113
114 # "by" and "from" appearing inside the artist name must not be mis-split
115 assert parse_quoted_stream_title('"Romeo" by Death From Above 1979 from "Heads Up"') == (
116 "Romeo",
117 "Death From Above 1979",
118 "Heads Up",
119 )
120 assert parse_quoted_stream_title('"Romeo" by Death From Above 1979') == (
121 "Romeo",
122 "Death From Above 1979",
123 None,
124 )
125
126 # German format yields no album
127 assert parse_quoted_stream_title('"Cornflake Girl" von Tori Amos') == (
128 "Cornflake Girl",
129 "Tori Amos",
130 None,
131 )
132
133 # conventional "Artist - Track" is not a quoted format
134 assert parse_quoted_stream_title("Tori Amos - Cornflake Girl") is None
135