| 1 |
"""Conformance canary for the private ``bleak`` internals this package couples to. |
| 2 |
|
| 3 |
The rest of the suite mocks the BlueZ manager, so a green run proves the package's |
| 4 |
*logic* but not that it still matches the *real* ``bleak`` — in particular the private |
| 5 |
attributes (``BlueZManager._bus`` / ``._properties`` / ``._services_cache`` / |
| 6 |
``._wait_condition``) and ``bluezdbus.defs`` constants it reaches into. Those are exactly |
| 7 |
what a ``bleak`` major bump silently removes or renames, and mocks always satisfy them. |
| 8 |
|
| 9 |
These tests exercise the *installed* ``bleak`` with no mocking. If a future bump breaks |
| 10 |
one of these coupling points, CI fails here with a precise message instead of shipping a |
| 11 |
package that only its own mocks can pass. Update ``src/`` and these assertions together. |
| 12 |
""" |
| 13 |
|
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
import inspect |
| 17 |
|
| 18 |
import pytest |
| 19 |
from bleak.exc import BleakError |
| 20 |
|
| 21 |
# The bluezdbus backend is Linux-only; degrade gracefully elsewhere. |
| 22 |
manager = pytest.importorskip("bleak.backends.bluezdbus.manager") |
| 23 |
defs = pytest.importorskip("bleak.backends.bluezdbus.defs") |
| 24 |
|
| 25 |
|
| 26 |
# Private BlueZManager instance attributes read by dbus.py / bluez.py. |
| 27 |
_MANAGER_INSTANCE_ATTRS = ("_bus", "_properties", "_services_cache") |
| 28 |
|
| 29 |
# defs constants referenced in dbus.py / bluez.py. |
| 30 |
_DEFS_CONSTANTS = ( |
| 31 |
"ADAPTER_INTERFACE", |
| 32 |
"BLUEZ_SERVICE", |
| 33 |
"DEVICE_INTERFACE", |
| 34 |
"GATT_SERVICE_INTERFACE", |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
def test_get_global_bluez_manager_is_importable() -> None: |
| 39 |
"""bleak_manager.py imports this factory from the bluezdbus manager module.""" |
| 40 |
assert callable(manager.get_global_bluez_manager) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize("attr", _MANAGER_INSTANCE_ATTRS) |
| 44 |
def test_bluez_manager_exposes_private_attr(attr: str) -> None: |
| 45 |
"""dbus.py / bluez.py read these private attributes off the manager instance.""" |
| 46 |
instance = manager.BlueZManager() |
| 47 |
assert hasattr(instance, attr) |
| 48 |
|
| 49 |
|
| 50 |
def test_bluez_manager_wait_condition_signature() -> None: |
| 51 |
"""bluez.py calls manager._wait_condition(device_path, "Connected", False).""" |
| 52 |
method = getattr(manager.BlueZManager, "_wait_condition", None) |
| 53 |
assert callable(method) |
| 54 |
params = list(inspect.signature(method).parameters) |
| 55 |
# self + the three positional arguments the call site passes. |
| 56 |
assert params[:4] == ["self", "device_path", "property_name", "property_value"] |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize("const", _DEFS_CONSTANTS) |
| 60 |
def test_defs_constant_present(const: str) -> None: |
| 61 |
"""dbus.py / bluez.py reference these interface/service string constants.""" |
| 62 |
assert isinstance(getattr(defs, const), str) |
| 63 |
|
| 64 |
|
| 65 |
def test_all_bleak_exceptions_derive_from_bleak_error() -> None: |
| 66 |
"""The retry loop catches ``BLEAK_EXCEPTIONS = (AttributeError, BleakError)``. |
| 67 |
|
| 68 |
Every concrete exception ``bleak`` raises must subclass ``BleakError`` or it would |
| 69 |
escape ``establish_connection``'s ``except BLEAK_EXCEPTIONS`` and abort the retry |
| 70 |
loop instead of being backed off. A major bump can add a new exception that breaks |
| 71 |
this — mocks never would. Assert the whole ``bleak.exc`` surface stays covered. |
| 72 |
""" |
| 73 |
exc = pytest.importorskip("bleak.exc") |
| 74 |
concrete = [ |
| 75 |
obj |
| 76 |
for obj in vars(exc).values() |
| 77 |
if inspect.isclass(obj) and issubclass(obj, Exception) |
| 78 |
] |
| 79 |
assert concrete # guard against introspecting an empty module |
| 80 |
escapes = [c.__name__ for c in concrete if not issubclass(c, BleakError)] |
| 81 |
assert not escapes, f"bleak exceptions escaping BLEAK_EXCEPTIONS: {escapes}" |