Files
2026-08-02 08:58:49 +08:00

104 lines
3.1 KiB
Python

from __future__ import annotations
from app.notify.wecom import (
TAG_CLOSE,
TAG_OPEN,
build_markdown,
close_reason_zh,
direction_zh,
notify_close,
notify_open,
venue_label,
)
def test_build_markdown_has_tag_and_title(monkeypatch):
monkeypatch.setattr("app.notify.wecom.venue_label", lambda: "实盘·OKX")
md = build_markdown(tag=TAG_OPEN, title="开仓成功", lines=["组: G-1"])
assert "【实盘·OKX】开仓成功" in md
assert "`OPEN`" in md
assert "组: G-1" in md
def test_venue_label_sim(monkeypatch):
class S:
is_sim = True
exchange = "okx"
monkeypatch.setattr("app.notify.wecom.get_settings", lambda: S())
assert venue_label() is None
def test_direction_and_close_reason_zh() -> None:
assert "Put" in direction_zh({"option_side": "put", "perp_side": "long"})
assert close_reason_zh("liquidity_retry") == "等待流动性后全平"
assert close_reason_zh("fixed_usdt") == "固定净盈利达标·双腿全平"
def test_notify_open_close_markdown(monkeypatch) -> None:
captured: list[str] = []
monkeypatch.setattr("app.notify.wecom.wecom_enabled", lambda: True)
monkeypatch.setattr("app.notify.wecom.wecom_webhook_url", lambda: "http://example.test")
monkeypatch.setattr("app.notify.wecom.venue_label", lambda: None)
monkeypatch.setattr("app.notify.wecom.wecom_machine_name", lambda: "")
def _capture(content: str):
captured.append(content)
return True, "ok"
monkeypatch.setattr("app.notify.wecom._post_markdown_sync", _capture)
monkeypatch.setattr(
"app.notify.wecom.notify_async",
lambda content: captured.append(content),
)
notify_open(
group_id="G-20260802-01",
detail="opened",
extra={
"bias": "put_ask_gt_call",
"option_side": "put",
"perp_side": "long",
"option_inst_id": "ETH-USD-260802-1850-P",
"strike": 1850,
"expiry_ymd": "260802",
"perp_qty_eth": 1.0,
"option_qty_eth": 2.0,
"perp_entry_px": 1860.5,
"option_entry_px": 12.3,
"initial_premium": 24.6,
"perp_margin": 620.0,
"leverage": 3,
},
)
assert captured
open_md = captured[-1]
assert "开仓成功" in open_md
assert "`OPEN`" in open_md or TAG_OPEN in open_md
assert "权利金占用" in open_md
assert "保证金占用" in open_md
assert "开仓数量" in open_md
assert "买Put" in open_md
captured.clear()
notify_close(
reason="liquidity_retry",
detail="closed",
data={
"group_id": "G-20260802-01",
"perp_pnl": -10.5,
"option_pnl": 40.2,
"net": 25.0,
"fees": 4.5,
},
)
close_md = captured[-1]
assert "平仓" in close_md
assert TAG_CLOSE in close_md or "`CLOSE`" in close_md
assert "等待流动性后全平" in close_md
assert "永续盈亏" in close_md
assert "期权盈亏" in close_md
assert "净利润" in close_md
assert "+25.00U" in close_md or "25.00U" in close_md