|
|
@@ -1,5 +1,13 @@
|
|
|
import pytest
|
|
|
-from cq2_sdk.transport.framing import encode_frame, decode_frame, FrameHeader, HEADER_SIZE
|
|
|
+from cq2_sdk.transport.framing import (
|
|
|
+ HEADER_SIZE,
|
|
|
+ PLAIN_HEADER_SIZE,
|
|
|
+ WIRE_HEADER_SIZE,
|
|
|
+ FrameHeader,
|
|
|
+ decode_frame,
|
|
|
+ decode_header,
|
|
|
+ encode_frame,
|
|
|
+)
|
|
|
from cq2_sdk.transport.encode6 import decode6, encode6
|
|
|
from cq2_sdk.messages import constants as C
|
|
|
|
|
|
@@ -26,23 +34,37 @@ def test_decode6_rejects_non_wire_character():
|
|
|
decode6(b"<~")
|
|
|
|
|
|
|
|
|
-def test_header_is_24_bytes():
|
|
|
+def test_header_is_48_bytes_on_wire():
|
|
|
raw = encode_frame(1, 0, 0, 0, 0, b"")
|
|
|
- assert len(raw) == 24
|
|
|
- assert HEADER_SIZE == 24
|
|
|
+ assert len(raw) == WIRE_HEADER_SIZE
|
|
|
+ assert PLAIN_HEADER_SIZE == 36
|
|
|
+ assert WIRE_HEADER_SIZE == 48
|
|
|
+ assert HEADER_SIZE == WIRE_HEADER_SIZE
|
|
|
|
|
|
|
|
|
def test_encode_decode_roundtrip():
|
|
|
body = b"hello"
|
|
|
- raw = encode_frame(C.CS_2001, recog=900000, param1=12345, param2=0, param3=0, body=body)
|
|
|
- assert len(raw) == 24 + len(body)
|
|
|
+ raw = encode_frame(
|
|
|
+ C.CS_2001,
|
|
|
+ recog=900000,
|
|
|
+ param1=12345,
|
|
|
+ param2=0,
|
|
|
+ param3=0,
|
|
|
+ body=body,
|
|
|
+ pre=7,
|
|
|
+ sequence=42,
|
|
|
+ )
|
|
|
+ assert len(raw) == WIRE_HEADER_SIZE + len(body)
|
|
|
hdr, decoded_body = decode_frame(raw)
|
|
|
+ assert hdr.pre == 7
|
|
|
assert hdr.msg_id == C.CS_2001
|
|
|
assert hdr.recog == 900000
|
|
|
assert hdr.param1 == 12345
|
|
|
assert hdr.param2 == 0
|
|
|
assert hdr.param3 == 0
|
|
|
assert hdr.body_len == 5
|
|
|
+ assert hdr.codec == 0
|
|
|
+ assert hdr.sequence == 42
|
|
|
assert decoded_body == body
|
|
|
|
|
|
|
|
|
@@ -61,13 +83,12 @@ def test_empty_body():
|
|
|
|
|
|
def test_truncated_body_raises():
|
|
|
raw = encode_frame(1, 0, 0, 0, 0, b"hello world")
|
|
|
- truncated = raw[:30] # cut off in the middle of the body
|
|
|
+ truncated = raw[:WIRE_HEADER_SIZE + 4] # cut off in the middle of the body
|
|
|
with pytest.raises(ValueError, match="truncated"):
|
|
|
decode_frame(truncated)
|
|
|
|
|
|
|
|
|
def test_too_short_raises():
|
|
|
- from cq2_sdk.transport.framing import decode_header
|
|
|
with pytest.raises(ValueError, match="too short"):
|
|
|
decode_header(b"\x00" * 10)
|
|
|
|
|
|
@@ -79,6 +100,19 @@ def test_negative_recog_allowed():
|
|
|
assert hdr.recog == -1
|
|
|
|
|
|
|
|
|
+def test_unsupported_body_codec_raises():
|
|
|
+ raw = encode_frame(1, 0, 0, 0, 0, b"compressed", codec=1)
|
|
|
+ with pytest.raises(ValueError, match="unsupported body codec: 1"):
|
|
|
+ decode_frame(raw)
|
|
|
+
|
|
|
+
|
|
|
+def test_decode_header_reads_the_first_wire_header_from_a_complete_frame():
|
|
|
+ raw = encode_frame(529, 1, 0, 0, 0, b"response", sequence=8)
|
|
|
+ header = decode_header(raw)
|
|
|
+ assert header.msg_id == 529
|
|
|
+ assert header.sequence == 8
|
|
|
+
|
|
|
+
|
|
|
def test_constants_loaded():
|
|
|
# Smoke check: a few known constants are importable and distinct
|
|
|
assert C.CS_2001 == 2001
|