"""Exercise the actual observer against synthetic D-Bus objects, without BLE.""" import importlib import importlib.util import sys import types import uuid from pathlib import Path import pytest @pytest.fixture def observer(monkeypatch, tmp_path): package = "jk_observer_test_" + uuid.uuid4().hex root = Path(__file__).resolve().parents[1] / "runtime" spec = importlib.util.spec_from_file_location( package, root / "__init__.py", submodule_search_locations=[str(root)] ) module = importlib.util.module_from_spec(spec) monkeypatch.setitem(sys.modules, spec.name, module) spec.loader.exec_module(module) calls, objects = [], {} class Proxy: def __init__(self, path): self.path = path def __getattr__(self, name): def call(*args, **kwargs): calls.append((self.path, name, args)) if name == "GetManagedObjects": return objects if name == "GetAll": return objects[self.path][args[0]] return call class Bus: def get_object(self, destination, path, **kwargs): assert destination == "org.bluez" and kwargs.get("introspect") is False return Proxy(path) def add_signal_receiver(self, *args, **kwargs): pass dbus = types.ModuleType("dbus") dbus.DBusException = type("DBusException", (Exception,), {}) dbus.SystemBus = Bus dbus.Interface = lambda obj, interface: obj dbus.Array = lambda value, **kwargs: value dbus.Dictionary = lambda value, **kwargs: value dbus.Boolean, dbus.String = bool, str monkeypatch.setitem(sys.modules, "dbus", dbus) monkeypatch.setitem(sys.modules, "dbus.mainloop", types.ModuleType("dbus.mainloop")) glib = types.ModuleType("dbus.mainloop.glib") glib.DBusGMainLoop = lambda **kwargs: None monkeypatch.setitem(sys.modules, "dbus.mainloop.glib", glib) runtime = importlib.import_module(package + ".bluez") service = importlib.import_module(package + ".service").Service(tmp_path) now = [100.0] monkeypatch.setattr(runtime.time, "monotonic", lambda: now[0]) obj = runtime.Observer(service) adapter, peer = "/org/bluez/hci5", "/org/bluez/hci5/dev_AA_BB_CC_DD_EE_01" props = { "Name": "JK_synthetic", "Address": "AA:BB:CC:DD:EE:01", "AddressType": "public", "ManufacturerData": {0x0B65: bytes.fromhex("88a0aabbccddee01")}, "Adapter": adapter, "RSSI": -65, } objects[adapter] = {runtime.ADAPTER: {"Powered": True}} objects[peer] = {runtime.DEVICE: props} objects[peer + "/service"] = {"org.bluez.GattService1": {"UUID": runtime.SERVICE}} objects[peer + "/char"] = { runtime.GATT: { "Service": peer + "/service", "UUID": runtime.CHARACTERISTIC, "Flags": ["notify", "write-without-response"], } } return obj, runtime, objects, props, calls, now, peer def start(observer): obj, runtime, objects, props, calls, now, peer = observer obj.tick() obj.added(peer, {runtime.DEVICE: props}) now[0] = 113 obj.tick() props.update(Connected=True, ServicesResolved=True) obj.tick() def info(version): data = bytearray(300) data[:5] = bytes.fromhex("55aaeb9003") data[6:18] = b"JK_BD6A17S6P" data[30:35] = version data[-1] = sum(data[:-1]) & 255 return bytes(data) def test_verification_precedes_telemetry_request(observer): start(observer) obj, runtime, objects, props, calls, now, peer = observer def writes(): return [args[0][4] for _, name, args in calls if name == "WriteValue"] assert writes() == [0x97] obj.changed(runtime.GATT, {"Value": info(b"11.47")}, [], path=peer + "/char") obj.tick() assert writes() == [0x97, 0x96] props["Connected"] = False obj.tick() assert obj.active is None assert any(name == "StopNotify" for _, name, _ in calls) assert writes() == [0x97, 0x96] def test_unsupported_firmware_never_requests_telemetry(observer): start(observer) obj, runtime, objects, props, calls, now, peer = observer obj.changed(runtime.GATT, {"Value": info(b"14.00")}, [], path=peer + "/char") assert obj.active is None assert [args[0][4] for _, name, args in calls if name == "WriteValue"] == [0x97] assert ( obj.service.inventory("synthetic")["items"][0]["bms_status"]["state"] == "unsupported_firmware" ) def test_foreign_connection_is_never_taken_over(observer): obj, runtime, objects, props, calls, now, peer = observer props["Connected"] = True obj.tick() obj.added(peer, {runtime.DEVICE: props}) now[0] = 113 obj.tick() assert obj.active is None assert not any( name in {"Connect", "Disconnect", "WriteValue", "StartNotify"} for _, name, _ in calls )