aioesphomeapi/tests/test__frame_helper.py

66 lines
1.6 KiB
Python
Raw Normal View History

2023-01-03 23:39:50 +01:00
import asyncio
from unittest.mock import MagicMock
import pytest
2023-04-20 08:47:38 +02:00
from aioesphomeapi._frame_helper import APIPlaintextFrameHelper
2023-01-03 23:39:50 +01:00
from aioesphomeapi.util import varuint_to_bytes
PREAMBLE = b"\x00"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"in_bytes, pkt_data, pkt_type",
[
(PREAMBLE + varuint_to_bytes(0) + varuint_to_bytes(1), b"", 1),
(
PREAMBLE + varuint_to_bytes(192) + varuint_to_bytes(1) + (b"\x42" * 192),
(b"\x42" * 192),
1,
),
(
PREAMBLE + varuint_to_bytes(192) + varuint_to_bytes(100) + (b"\x42" * 192),
(b"\x42" * 192),
100,
),
(
PREAMBLE + varuint_to_bytes(4) + varuint_to_bytes(100) + (b"\x42" * 4),
(b"\x42" * 4),
100,
),
(
PREAMBLE
+ varuint_to_bytes(8192)
+ varuint_to_bytes(8192)
+ (b"\x42" * 8192),
(b"\x42" * 8192),
8192,
),
(
PREAMBLE + varuint_to_bytes(256) + varuint_to_bytes(256) + (b"\x42" * 256),
(b"\x42" * 256),
256,
),
],
)
async def test_plaintext_frame_helper(in_bytes, pkt_data, pkt_type):
for _ in range(5):
packets = []
2023-04-20 08:47:38 +02:00
def _packet(type_: int, data: bytes):
packets.append((type_, data))
def _on_error(exc: Exception):
raise exc
2023-01-03 23:39:50 +01:00
helper = APIPlaintextFrameHelper(on_pkt=_packet, on_error=_on_error)
2023-01-03 23:39:50 +01:00
helper.data_received(in_bytes)
2023-01-03 23:39:50 +01:00
pkt = packets.pop()
2023-04-20 08:47:38 +02:00
type_, data = pkt
2023-01-03 23:39:50 +01:00
2023-04-20 08:47:38 +02:00
assert type_ == pkt_type
assert data == pkt_data