14a7adae1f
Replace soft REST polling with OKX public tickers WS ingest and browser SSE patches so list quotes stay live while watching an expiry. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
"""options_quote_live_lib 单元测试."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from lib.options.options_quote_live_lib import OptionsQuoteLive
|
|
|
|
|
|
class _FakeWs:
|
|
connected = True
|
|
last_msg_at = 0.0
|
|
|
|
def start(self) -> None:
|
|
return None
|
|
|
|
def stop(self) -> None:
|
|
return None
|
|
|
|
def set_subscriptions(self, args) -> None:
|
|
self.last_args = list(args)
|
|
|
|
|
|
def test_ticker_patch_and_flush():
|
|
live = OptionsQuoteLive()
|
|
live._ws = _FakeWs() # type: ignore[assignment]
|
|
live._started = True
|
|
live.watch(
|
|
underlying="ETH",
|
|
exp_time="1",
|
|
contracts=[{"inst_id": "ETH-USD-260811-2500-C", "opt_type": "C", "strike": 2500}],
|
|
index_inst_id="ETH-USD",
|
|
)
|
|
live._on_ws_data(
|
|
{
|
|
"arg": {"channel": "tickers", "instId": "ETH-USD-260811-2500-C"},
|
|
"data": [
|
|
{
|
|
"instId": "ETH-USD-260811-2500-C",
|
|
"askPx": "12.5",
|
|
"askSz": "3",
|
|
"bidPx": "11.0",
|
|
"bidSz": "2",
|
|
"markPx": "12.0",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
live._on_ws_data(
|
|
{
|
|
"arg": {"channel": "index-tickers", "instId": "ETH-USD"},
|
|
"data": [{"idxPx": "2600"}],
|
|
}
|
|
)
|
|
raw = live._build_flush_event()
|
|
assert raw is not None
|
|
payload = json.loads(raw)
|
|
assert payload["index_px"] == 2600.0
|
|
assert payload["quotes"]
|
|
q = next(x for x in payload["quotes"] if x["inst_id"] == "ETH-USD-260811-2500-C")
|
|
assert q["ask"] == 12.5
|
|
assert q["ask_sz"] == 3.0
|
|
assert q["expiry_be_px"] == 2512.5
|
|
st = live.status()
|
|
assert st["watch_count"] == 1
|