1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# deck-it - tests for music
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import unittest
import deckit.modules.music as music
class Music(unittest.TestCase):
def test_parses_metadata(self):
sep = "\x1f"
fake = lambda cmd, timeout=2: {
"metadata": sep.join(["Playing", "Song", "Artist", "Album", "200000000",
"1000000", "", "spotify"]),
"get-volume": "Volume: 0.42",
}[cmd[1]]
m = music.make({"run": fake})
st = m.state()
self.assertEqual(st["title"], "Song")
self.assertEqual(st["status"], "Playing")
self.assertEqual(st["volume"]["level"], 42)
def test_float_length(self):
# Haruna reports mpris:length as a float.
sep = "\x1f"
fake = lambda cmd, timeout=2: {
"metadata": sep.join(["Playing", "clip.mp4", "", "", "65816666.666666664",
"14583353", "", "haruna"]),
"get-volume": "Volume: 0.42",
}[cmd[1]]
st = music.make({"run": fake}).state()
self.assertAlmostEqual(st["length"], 65.82, places=2)
def _state(self, art, url, frame=None):
out = "\x1f".join(["Playing", "t", "", "", "1", "0", art, "haruna", url])
fake = lambda cmd, timeout=2: {"metadata": out, "get-volume": ""}[cmd[1]]
m = music.make({"run": fake, "frame": frame or (lambda path: b"")})
return m, m.state()
def test_embedded_cover_served_decoded(self):
m, st = self._state("data:image/png;base64,aGVsbG8=", "file:///a/song.flac")
key = st["art"].rsplit("/", 1)[1]
self.assertEqual(m.image(key), (b"hello", "image/png"))
self.assertIsNone(m.image("other"))
def test_video_gets_a_frame(self):
paths = []
m, st = self._state("", "file:///v/a%20clip.mp4",
lambda path: paths.append(path) or b"jpeg")
self.assertEqual(m.image(st["art"].rsplit("/", 1)[1]), (b"jpeg", "image/jpeg"))
self.assertEqual(paths, ["/v/a clip.mp4"])
def test_audio_without_cover_has_no_art(self):
self.assertIsNone(self._state("", "file:///a/song.flac")[1]["art"])
def test_action_commands(self):
self.assertEqual(music.BASE_ACTIONS["next"], ["playerctl", "next"])
def test_seek_runs_playerctl(self):
calls = []
m = music.make({"run": lambda *a, **k: "", "popen": calls.append})
m.actions["seek"]({"position": 12.34})
self.assertEqual(calls[0], ["playerctl", "position", "12.3"])
def test_seek_rejects_bad_positions(self):
m = music.make({"run": lambda *a, **k: "", "popen": lambda cmd: None})
for bad in ({}, {"position": -1}, {"position": "x"}):
with self.assertRaises(music.ModuleError, msg=bad):
m.actions["seek"](bad)
def test_volume_sets_the_sink(self):
calls = []
m = music.make({"run": lambda *a, **k: "", "popen": calls.append})
m.actions["volume"]({"level": 45})
self.assertEqual(calls[0], ["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "0.45"])
for bad in ({}, {"level": 101}, {"level": -1}, {"level": "5"}, {"level": True}):
with self.assertRaises(music.ModuleError, msg=bad):
m.actions["volume"](bad)
if __name__ == "__main__":
unittest.main()
|