Unify K1 discovery ownership and verification across enrollment paths

This commit is contained in:
DCCONSTRUCTIONS
2026-09-07 20:04:46 +03:00
parent 9c4d70b0e9
commit 762d77ef95
27 changed files with 615 additions and 191 deletions
+16 -11
View File
@@ -49,10 +49,8 @@ def test_selected_transport_survives_native_cache_loss(monkeypatch, operation, n
)
return device if present else None
async def find(selected_address, *, timeout, bluez):
assert selected_address == address
assert 0 < timeout <= 8
assert bluez == {"adapter": "hci7"}
async def advertise(callback):
await asyncio.sleep(0)
events.append("scan")
assert events.count("scan") == 1
if native == "absent":
@@ -61,11 +59,11 @@ def test_selected_transport_survives_native_cache_loss(monkeypatch, operation, n
monkeypatch.setattr(scanner, "ble_runtime_owner_epoch_for_current_loop", lambda: -1)
if native == "invalidated":
scanner.demote_connected_device_handle_after_gatt_failure(capture)
return BLEDevice(
callback(BLEDevice(
"AA:BB:CC:DD:EE:00" if native == "wrong-address" else address,
"synthetic",
{"path": path.replace("hci7", "hci8") if native == "wrong-adapter" else path},
)
), None)
service = SimpleNamespace(uuid=wifi.SERVICE_UUID)
write = SimpleNamespace(
@@ -129,17 +127,21 @@ def test_selected_transport_survives_native_cache_loss(monkeypatch, operation, n
))
monkeypatch.setattr(scanner, "_retrieve_bluez_device", retrieve)
class Scanner:
find_device_by_address = staticmethod(find)
def __init__(self, *, bluez):
def __init__(self, *, detection_callback, bluez):
assert bluez == {"adapter": "hci7"}
self.callback = detection_callback
self.task = None
async def __aenter__(self):
events.append("hold-discovery")
if native not in {"present", "cache-cleanup-race"}:
self.task = asyncio.create_task(advertise(self.callback))
return self
async def __aexit__(self, *_args):
events.append("release-discovery")
if self.task:
await self.task
monkeypatch.setattr(scanner, "BleakScanner", Scanner)
monkeypatch.setattr(wifi, "BleakClient", Client)
@@ -172,14 +174,17 @@ def test_selected_transport_survives_native_cache_loss(monkeypatch, operation, n
elif fails_before_connect or native == "connect-failed" or (
native == "write-failed" and operation == "provision"
):
with pytest.raises(BleakError) as raised:
with pytest.raises((BleakError, TimeoutError)) as raised:
await action
if fails_before_connect:
assert isinstance(raised.value, BleakDeviceNotFoundError)
assert isinstance(raised.value, (BleakDeviceNotFoundError, TimeoutError))
assert "connect" not in events
if operation == "provision":
assert raised.value.device_write_attempted is (native == "write-failed")
assert events.count("write") == (native == "write-failed")
if fails_before_connect and native not in {"owner-changed", "invalidated"}:
# A failed discovery has not tested/revoked the original capture.
assert scanner.captured_device_handle(capture) is device
else:
result = await action
assert result.get("outcome", "lan_address_observed") == "lan_address_observed"