test_wire.py 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. # tests/test_wire.py
  2. import pytest
  3. from apilot.wire import Frame, pack_frame, unpack_stream
  4. def test_frame_roundtrip():
  5. f = Frame(recog=900000, msg_id=20, p1=123, p2=14481607, p3=8043,
  6. tail=7, body=b"*#acct/role/12345/9/1/0/110585/0/1/1/1/1")
  7. packed = pack_frame(f)
  8. frames, rest = unpack_stream(packed)
  9. assert rest == b""
  10. assert len(frames) == 1
  11. g = frames[0]
  12. assert (g.recog, g.msg_id, g.p1, g.p2, g.p3, g.tail, g.body) == \
  13. (f.recog, f.msg_id, f.p1, f.p2, f.p3, f.tail, f.body)
  14. def test_unpack_partial_packet():
  15. f = Frame(recog=0, msg_id=529, p1=0, p2=0, p3=0, tail=1, body=b"key32abc")
  16. packed = pack_frame(f)
  17. frames, rest = unpack_stream(packed[:-10]) # 掐掉10字节
  18. assert frames == [] and len(rest) == len(packed) - 10
  19. def test_pack_matches_reference_impl():
  20. """与 cq2_pure_enter.send() 的原始拼包逐字节一致(防字段序回归)。"""
  21. import struct, random
  22. sys_path_hack = __import__("sys"); sys_path_hack.path.insert(0, "protocol_bot")
  23. from encode6bitbuf_final import encode6
  24. random.seed(42)
  25. body = b"*#1186115037/role/1/2/3/1/0/110585/0/1/1/1/1"
  26. ref = encode6(struct.pack("<9i", 0, 20, 900000, 111, 14481607, 8043, 0, len(body), 9)) + body
  27. mine = pack_frame(Frame(recog=900000, msg_id=20, p1=111, p2=14481607, p3=8043, tail=9, body=body))
  28. assert mine == ref