"""协议层单元测试:帧/价格差分/浮点/命令解析(黄金字节向量,对照 easy-tdx 已验证实现)""" from __future__ import annotations import struct from collections import Counter from datetime import date, datetime import pytest from pytdxdata.models import Adjust, KlinePeriod, SecurityBar from pytdxdata.protocol.commands.ex_proto import GetExInstrumentBarsCmd from pytdxdata.protocol.commands.kline import GetSecurityBarsCmd from pytdxdata.protocol.commands.mac import MacKlineCmd from pytdxdata.protocol.commands.minute import GetHistoryMinuteTimeDataCmd from pytdxdata.protocol.commands.transaction import MacTransactionCmd from pytdxdata.protocol.frame import FrameHeader, decompress_body, parse_header from pytdxdata.protocol.price import decode_ohlc_diffs, get_price, put_price from pytdxdata.protocol.volume import decode_volume # ---------- 变长有符号整数 ---------- @pytest.mark.parametrize("value", [0, 1, -1, 63, 64, -64, 127, -128, 1000, -100000, 2**20]) def test_price_roundtrip(value: int): enc = put_price(value) dec, pos = get_price(enc, 0) assert dec == value assert pos == len(enc) def test_price_negative_flag(): # 首字节 bit6=符号 enc = put_price(-5) assert enc[0] & 0x40 dec, _ = get_price(enc, 0) assert dec == -5 # ---------- OHLC 差分还原 ---------- def test_decode_ohlc_diffs(): # 手工构造:open_diff=100, close_diff=-20, high_diff=10, low_diff=-30 body = bytearray() for v in (100, -20, 10, -30): body.extend(put_price(v)) od, cd, hd, ld, pos = decode_ohlc_diffs(body, 0) assert (od, cd, hd, ld) == (100, -20, 10, -30) assert pos == len(body) # ---------- 自定义4字节浮点 ---------- def test_decode_volume_zero(): assert decode_volume(0) == 0.0 def test_decode_volume_known(): # 经验值:0x00000001 应为极小正数(非0) assert decode_volume(1) > 0 # ---------- 响应帧头 ---------- def test_frame_header(): # magic=7654321, 未压缩: zipsize==unzipsize buf = struct.pack(" bytes: """构造 0x23FF 响应体:20B头(count@18) + 每条32B(4B时间 + 28B行情)。""" body = bytearray(20 + len(raw_dates) * 32) struct.pack_into(" SecurityBar: return SecurityBar(market=market, code="X", open=1.0, high=1.0, low=1.0, close=1.0, vol=1.0, amount=1.0, year=y, month=mo, day=d, hour=h, minute=mi) def test_us_evening_segment_keeps_date(): # 北京 21:31 ~ 23:59 与美东交易日属同一自然日,日期不动 b = _bar(74, 2026, 10, 12, 21, 31) assert b.datetime == datetime(2026, 10, 12, 21, 31) assert b.trading_date == "2026-10-12" def test_us_midnight_segment_rolls_to_next_day(): # 北京次日 00:00 ~ 04:00 实际是美东 12:00~16:00,日期要 +1 天 b = _bar(74, 2026, 10, 12, 4, 0) assert b.datetime == datetime(2026, 10, 13, 4, 0) assert (b.hour, b.minute) == (4, 0) # 时分不参与归一化 assert b.trading_date == "2026-10-12" # 仍归属美东 10-12 交易日 def test_us_split_hour_boundary(): assert _bar(74, 2026, 10, 12, 11, 59).day == 13 assert _bar(74, 2026, 10, 12, 12, 0).day == 12 def test_us_month_and_year_rollover(): assert _bar(74, 2026, 10, 31, 4, 0).datetime == datetime(2026, 11, 1, 4, 0) assert _bar(74, 2026, 12, 31, 4, 0).datetime == datetime(2027, 1, 1, 4, 0) def test_cn_and_hk_bars_untouched(): cn = _bar(1, 2026, 10, 12, 9, 31) # A股/标准通道 assert (cn.year, cn.month, cn.day) == (2026, 10, 12) assert cn.trading_date == "2026-10-12" hk = _bar(31, 2026, 10, 12, 4, 0) # 港股同为 UTC+8,凌晨段不应 +1 assert (hk.year, hk.month, hk.day) == (2026, 10, 12) assert hk.trading_date == "2026-10-12" def test_us_full_session_is_monotonic_and_single_trading_day(): # 实测口径:北京 21:31~23:59(149 根)+ 次日 00:00~04:00(241 根)= 390 根 mins = list(range(21 * 60 + 31, 24 * 60)) + list(range(4 * 60 + 1)) assert len(mins) == 390 bars = [_bar(74, 2026, 10, 12, m // 60, m % 60) for m in mins] bars.sort(key=lambda b: b.datetime) dts = [b.datetime for b in bars] assert dts == sorted(dts) # 归一化后严格单调 assert dts[0] == datetime(2026, 10, 12, 21, 31) assert dts[-1] == datetime(2026, 10, 13, 4, 0) # 全部归属于同一个美东交易日 assert {b.trading_date for b in bars} == {"2026-10-12"} # 但横跨两个北京自然日(这正是不能用 datetime.date() 聚合的原因) by_day = Counter(b.datetime.date() for b in bars) assert by_day[date(2026, 10, 12)] == 149 assert by_day[date(2026, 10, 13)] == 241 # ---------- MAC 通道 K线(0x122E)同样归一化 ---------- def _mac_bars_body(records: list[tuple[int, int]]) -> bytes: """构造 0x122E 响应体:33B头(count@24) + 每条36B(4B ymd + 4B time秒 + 7float)。""" body = bytearray(33 + len(records) * 36) struct.pack_into("