46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""ESP-NOW ping session collection."""
|
|
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(PROJECT_ROOT / "src"))
|
|
|
|
from util import espnow_ping # noqa: E402
|
|
from util.espnow_wire import pack_ping_rsp, parse_ping_req # noqa: E402
|
|
|
|
|
|
class _FakeBridge:
|
|
def __init__(self):
|
|
self.packets = []
|
|
|
|
async def send(self, data, addr=None):
|
|
self.packets.append(data)
|
|
return True
|
|
|
|
|
|
def test_run_ping_collects_responses(monkeypatch):
|
|
bridge = _FakeBridge()
|
|
monkeypatch.setattr(espnow_ping, "get_current_bridge", lambda: bridge)
|
|
|
|
async def _run():
|
|
async def _inject():
|
|
await asyncio.sleep(0.05)
|
|
assert len(bridge.packets) == 1
|
|
ping_id = parse_ping_req(bridge.packets[0])
|
|
peer = bytes.fromhex("aabbccddeeff")
|
|
espnow_ping.record_ping_rsp(peer, pack_ping_rsp(ping_id, "led-1"))
|
|
|
|
task = asyncio.create_task(_inject())
|
|
result = await espnow_ping.run_ping(timeout_s=0.2)
|
|
await task
|
|
return result
|
|
|
|
result = asyncio.run(_run())
|
|
|
|
assert result["ok"] is True
|
|
assert "aabbccddeeff" in result["responses"]
|
|
assert result["responses"]["aabbccddeeff"]["name"] == "led-1"
|
|
assert bridge.packets[0][0:2] == bytes([0x4C, 0x05])
|