Debian Codegraph @

tests/test_plugin_blink1.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 import logging
29 import sys
30 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import pytest
34
35 from apprise import Apprise, NotifyType
36 from apprise.plugins.blink1 import (
37 BLINK1_DEFAULT_DURATION_MS,
38 BLINK1_DEFAULT_FADE_MS,
39 BLINK1_MAX_DURATION_MS,
40 BLINK1_MAX_FADE_MS,
41 BLINK1_REPORT_SIZE,
42 Blink1LED,
43 NotifyBlink1,
44 _blink1_fade_buf,
45 )
46
47 logging.disable(logging.CRITICAL)
48
49 # ---------------------------------------------------------------------------
50 # Helpers
51 # ---------------------------------------------------------------------------
52
53
54 def _mk_device(report_size=BLINK1_REPORT_SIZE, open_exc=None):
55 """Return a mock hidapi device that succeeds by default."""
56 dev = mock.Mock()
57 dev.send_feature_report.return_value = report_size
58 if open_exc is not None:
59 dev.open.side_effect = open_exc
60 return dev
61
62
63 # ---------------------------------------------------------------------------
64 # URL matrix used by AppriseURLTester
65 # ---------------------------------------------------------------------------
66
67 apprise_url_tests = (
68 # First available device, all defaults
69 (
70 "blink1://",
71 {
72 "instance": NotifyBlink1,
73 },
74 ),
75 # Explicit underscore placeholder for first device
76 (
77 "blink1://_/",
78 {
79 "instance": NotifyBlink1,
80 },
81 ),
82 # Specific device by serial number
83 (
84 "blink1://ABCD1234/",
85 {
86 "instance": NotifyBlink1,
87 },
88 ),
89 # Minimum allowed parameter values
90 (
91 "blink1://?duration=0&fade=0&ledn=0",
92 {
93 "instance": NotifyBlink1,
94 },
95 ),
96 # LED 1 (first LED only)
97 (
98 "blink1://?ledn=1",
99 {
100 "instance": NotifyBlink1,
101 },
102 ),
103 # LED 2 (second LED only)
104 (
105 "blink1://?ledn=2",
106 {
107 "instance": NotifyBlink1,
108 },
109 ),
110 # Maximum allowed duration
111 (
112 f"blink1://?duration={BLINK1_MAX_DURATION_MS}",
113 {
114 "instance": NotifyBlink1,
115 },
116 ),
117 # Maximum allowed fade
118 (
119 f"blink1://?fade={BLINK1_MAX_FADE_MS}",
120 {
121 "instance": NotifyBlink1,
122 },
123 ),
124 # Non-numeric duration -> TypeError
125 (
126 "blink1://?duration=abc",
127 {
128 "instance": TypeError,
129 },
130 ),
131 # Negative duration -> TypeError
132 (
133 "blink1://?duration=-1",
134 {
135 "instance": TypeError,
136 },
137 ),
138 # Duration exceeds maximum -> TypeError
139 (
140 f"blink1://?duration={BLINK1_MAX_DURATION_MS + 1}",
141 {
142 "instance": TypeError,
143 },
144 ),
145 # Non-numeric fade -> TypeError
146 (
147 "blink1://?fade=abc",
148 {
149 "instance": TypeError,
150 },
151 ),
152 # Negative fade -> TypeError
153 (
154 "blink1://?fade=-1",
155 {
156 "instance": TypeError,
157 },
158 ),
159 # Fade exceeds maximum -> TypeError
160 (
161 f"blink1://?fade={BLINK1_MAX_FADE_MS + 1}",
162 {
163 "instance": TypeError,
164 },
165 ),
166 )
167
168
169 # ---------------------------------------------------------------------------
170 # Import-error path (run only when hidapi is NOT installed)
171 # ---------------------------------------------------------------------------
172
173
174 @pytest.mark.skipif(
175 "hid" in sys.modules,
176 reason="Requires that hidapi NOT be installed",
177 )
178 def test_plugin_blink1_import_error():
179 """NotifyBlink1() returns None when hidapi is absent."""
180
181 obj = Apprise.instantiate("blink1://")
182 assert obj is None
183
184
185 # ---------------------------------------------------------------------------
186 # All remaining tests require hidapi to be installed
187 # ---------------------------------------------------------------------------
188
189
190 def test_plugin_blink1_urls():
191 """NotifyBlink1() Apprise URL matrix."""
192
193 pytest.importorskip("hid")
194
195 with (
196 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
197 mock.patch("apprise.plugins.blink1.time") as mock_time,
198 ):
199 mock_dev = _mk_device()
200 mock_hid.device.return_value = mock_dev
201 mock_time.sleep.return_value = None
202
203 AppriseURLTester(tests=apprise_url_tests).run_all()
204
205
206 def test_plugin_blink1_device_open_failure():
207 """NotifyBlink1() returns False when the USB device cannot be opened."""
208
209 pytest.importorskip("hid")
210
211 with mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid:
212 mock_dev = _mk_device(open_exc=OSError("no device"))
213 mock_hid.device.return_value = mock_dev
214
215 obj = Apprise.instantiate("blink1://ABCD1234/")
216 assert obj is not None
217 assert obj.notify(body="test") is False
218
219
220 def test_plugin_blink1_device_open_failure_no_serial():
221 """NotifyBlink1() failure log omits serial when none is configured."""
222
223 pytest.importorskip("hid")
224
225 with mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid:
226 mock_dev = _mk_device(open_exc=OSError("no device"))
227 mock_hid.device.return_value = mock_dev
228
229 obj = Apprise.instantiate("blink1://")
230 assert obj is not None
231 assert obj.notify(body="test") is False
232
233
234 def test_plugin_blink1_send_feature_report_exception():
235 """NotifyBlink1() returns False and closes device on HID write error."""
236
237 pytest.importorskip("hid")
238
239 with (
240 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
241 mock.patch("apprise.plugins.blink1.time") as mock_time,
242 ):
243 mock_dev = mock.Mock()
244 mock_dev.send_feature_report.side_effect = OSError("USB error")
245 mock_hid.device.return_value = mock_dev
246 mock_time.sleep.return_value = None
247
248 obj = Apprise.instantiate("blink1://")
249 assert obj is not None
250 assert obj.notify(body="test") is False
251
252 # close() must always be called
253 assert mock_dev.close.called
254
255
256 def test_plugin_blink1_short_report_fails():
257 """NotifyBlink1() returns False when the HID report is truncated."""
258
259 pytest.importorskip("hid")
260
261 with (
262 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
263 mock.patch("apprise.plugins.blink1.time") as mock_time,
264 ):
265 mock_dev = _mk_device(report_size=BLINK1_REPORT_SIZE - 1)
266 mock_hid.device.return_value = mock_dev
267 mock_time.sleep.return_value = None
268
269 obj = Apprise.instantiate("blink1://")
270 assert obj is not None
271 assert obj.notify(body="test") is False
272
273
274 def test_plugin_blink1_notify_types():
275 """NotifyBlink1() sends one fade per notification type."""
276
277 pytest.importorskip("hid")
278
279 with (
280 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
281 mock.patch("apprise.plugins.blink1.time") as mock_time,
282 ):
283 mock_dev = _mk_device()
284 mock_hid.device.return_value = mock_dev
285 mock_time.sleep.return_value = None
286
287 obj = Apprise.instantiate("blink1://ABCD1234/?fade=100&duration=50")
288 assert obj is not None
289
290 for notify_type in (
291 NotifyType.INFO,
292 NotifyType.SUCCESS,
293 NotifyType.WARNING,
294 NotifyType.FAILURE,
295 ):
296 assert obj.notify(body="test", notify_type=notify_type) is True
297
298 # Each notification sends two reports (color + off)
299 assert mock_dev.send_feature_report.call_count == 8
300 assert mock_dev.close.call_count == 4
301
302
303 def test_plugin_blink1_fade_buf_layout():
304 """_blink1_fade_buf() constructs the correct 9-byte HID payload."""
305
306 buf = _blink1_fade_buf(255, 128, 0, 1000, ledn=1)
307
308 assert len(buf) == 9
309 assert buf[0] == 0x01 # REPORT_ID
310 assert buf[1] == ord("c") # command byte
311 assert buf[2] == 255 # red
312 assert buf[3] == 128 # green
313 assert buf[4] == 0 # blue
314 # fade_time = 1000 // 10 = 100 = 0x0064
315 assert buf[5] == 0x00 # th
316 assert buf[6] == 0x64 # tl
317 assert buf[7] == 1 # ledn
318 assert buf[8] == 0x00 # padding
319
320
321 def test_plugin_blink1_fade_rgb_args():
322 """NotifyBlink1() passes correct ledn and RGB values to the device."""
323
324 pytest.importorskip("hid")
325
326 with (
327 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
328 mock.patch("apprise.plugins.blink1.time") as mock_time,
329 ):
330 mock_dev = _mk_device()
331 mock_hid.device.return_value = mock_dev
332 mock_time.sleep.return_value = None
333
334 obj = Apprise.instantiate("blink1://?fade=200&duration=100&ledn=2")
335 assert obj is not None
336 assert obj.notify(body="test", notify_type=NotifyType.INFO) is True
337
338 first_call_buf = mock_dev.send_feature_report.call_args_list[0][0][0]
339 assert first_call_buf[1] == ord("c") # command byte
340 assert first_call_buf[7] == 2 # ledn
341 # fade_time = 200 // 10 = 20 = 0x0014
342 assert first_call_buf[5] == 0x00
343 assert first_call_buf[6] == 0x14
344
345
346 def test_plugin_blink1_off_sent_after_duration():
347 """NotifyBlink1() sends an off command after sleeping for fade+duration."""
348
349 pytest.importorskip("hid")
350
351 with (
352 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
353 mock.patch("apprise.plugins.blink1.time") as mock_time,
354 ):
355 mock_dev = _mk_device()
356 mock_hid.device.return_value = mock_dev
357 mock_time.sleep.return_value = None
358
359 obj = Apprise.instantiate("blink1://?fade=500&duration=2000")
360 assert obj is not None
361 assert obj.notify(body="test") is True
362
363 mock_time.sleep.assert_called_once_with(2.5)
364
365 # Second report should be the "off" command (all zeros for RGB)
366 off_buf = mock_dev.send_feature_report.call_args_list[1][0][0]
367 assert off_buf[2] == 0 # red
368 assert off_buf[3] == 0 # green
369 assert off_buf[4] == 0 # blue
370
371
372 def test_plugin_blink1_off_command_failure_returns_false():
373 """NotifyBlink1() returns False when the off HID write fails."""
374
375 pytest.importorskip("hid")
376
377 with (
378 mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid,
379 mock.patch("apprise.plugins.blink1.time") as mock_time,
380 ):
381 mock_dev = mock.Mock()
382 # First call (color fade) succeeds; second call (off) fails.
383 mock_dev.send_feature_report.side_effect = [
384 BLINK1_REPORT_SIZE,
385 BLINK1_REPORT_SIZE - 1,
386 ]
387 mock_hid.device.return_value = mock_dev
388 mock_time.sleep.return_value = None
389
390 obj = Apprise.instantiate("blink1://")
391 assert obj is not None
392 assert obj.notify(body="test") is False
393
394 # close() must still be called
395 assert mock_dev.close.called
396
397
398 def test_plugin_blink1_url_round_trip():
399 """NotifyBlink1() URL reconstructs an equivalent object."""
400
401 obj = NotifyBlink1(serial="ABCD1234", duration=3000, fade=500, ledn=1)
402 rebuilt = NotifyBlink1(**NotifyBlink1.parse_url(obj.url()))
403
404 assert rebuilt.serial == "ABCD1234"
405 assert rebuilt.duration == 3000
406 assert rebuilt.fade == 500
407 assert rebuilt.ledn == 1
408
409
410 def test_plugin_blink1_url_round_trip_no_serial():
411 """NotifyBlink1() URL round-trips correctly without a serial."""
412
413 obj = NotifyBlink1()
414 rebuilt = NotifyBlink1(**NotifyBlink1.parse_url(obj.url()))
415
416 assert rebuilt.serial is None
417 assert rebuilt.duration == BLINK1_DEFAULT_DURATION_MS
418 assert rebuilt.fade == BLINK1_DEFAULT_FADE_MS
419 assert rebuilt.ledn == Blink1LED.ALL
420
421
422 def test_plugin_blink1_url_identifier():
423 """NotifyBlink1() url_identifier distinguishes different connections."""
424
425 obj_a = NotifyBlink1()
426 obj_b = NotifyBlink1(serial="ABCD1234")
427 obj_c = NotifyBlink1(ledn=1)
428
429 assert obj_a.url_identifier != obj_b.url_identifier
430 assert obj_a.url_identifier != obj_c.url_identifier
431 assert obj_b.url_identifier != obj_c.url_identifier
432
433
434 def test_plugin_blink1_invalid_ledn_defaults():
435 """NotifyBlink1() silently falls back to ALL for an unrecognised ledn."""
436
437 obj = NotifyBlink1(ledn="99")
438 assert obj.ledn == Blink1LED.ALL
439
440
441 def test_plugin_blink1_runtime_deps():
442 """NotifyBlink1.runtime_deps() returns the importable hid module name."""
443
444 assert NotifyBlink1.runtime_deps() == ("hid",)
445
446
447 def test_plugin_blink1_parse_url_underscore():
448 """NotifyBlink1() treats underscore host as first-device placeholder."""
449
450 result = NotifyBlink1.parse_url("blink1://_/")
451 assert result is not None
452 assert result.get("serial") is None
453
454
455 def test_plugin_blink1_parse_url_serial():
456 """NotifyBlink1() extracts serial number from URL host field."""
457
458 result = NotifyBlink1.parse_url("blink1://DEADBEEF/")
459 assert result is not None
460 assert result["serial"] == "DEADBEEF"