Files
CoinOX/tests/test_okx_trade_mode.py
dekun 6607d09526 Make this repo hedge-only: skip standalone options UI by default.
Nav/env only expose 永期/期期; set STANDALONE_OPTIONS_UI=true for emergency opt-in.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-14 18:37:46 +08:00

124 lines
4.5 KiB
Python

"""OKX_TRADE_MODE 对冲二选一(本仓默认无单独期权 UI)."""
import os
import unittest
from unittest.mock import patch
from lib.hedge_plan.okx_trade_mode_lib import (
MODE_OO,
MODE_OPTIONS,
MODE_PERP,
block_standalone_open_by_mode_msg,
get_okx_trade_mode,
hedge_module_enabled,
show_options_options,
show_perp_options,
standalone_options_open_allowed,
standalone_options_ui_enabled,
)
class OkxTradeModeTests(unittest.TestCase):
def test_hedge_modes(self):
for mode, hedge, po, oo in (
(MODE_PERP, True, True, False),
(MODE_OO, True, False, True),
):
with patch.dict(
os.environ,
{"OKX_TRADE_MODE": mode, "STANDALONE_OPTIONS_UI": "false"},
clear=False,
):
self.assertEqual(get_okx_trade_mode(), mode)
self.assertEqual(hedge_module_enabled(), hedge)
self.assertEqual(show_perp_options(), po)
self.assertEqual(show_options_options(), oo)
self.assertFalse(standalone_options_open_allowed())
def test_options_mode_coerces_to_perp_without_ui_flag(self):
with patch.dict(
os.environ,
{"OKX_TRADE_MODE": MODE_OPTIONS, "STANDALONE_OPTIONS_UI": "false"},
clear=False,
):
self.assertEqual(get_okx_trade_mode(), MODE_PERP)
self.assertTrue(hedge_module_enabled())
self.assertFalse(standalone_options_open_allowed())
def test_options_mode_with_ui_flag(self):
with patch.dict(
os.environ,
{"OKX_TRADE_MODE": MODE_OPTIONS, "STANDALONE_OPTIONS_UI": "true"},
clear=False,
):
self.assertEqual(get_okx_trade_mode(), MODE_OPTIONS)
self.assertFalse(hedge_module_enabled())
self.assertTrue(standalone_options_ui_enabled())
self.assertTrue(standalone_options_open_allowed())
def test_legacy_infer_defaults_perp(self):
with patch.dict(
os.environ,
{"HEDGE_PLAN_ENABLED": "false", "STANDALONE_OPTIONS_UI": "false"},
clear=False,
):
os.environ.pop("OKX_TRADE_MODE", None)
self.assertEqual(get_okx_trade_mode(), MODE_PERP)
def test_legacy_infer_perp(self):
with patch.dict(
os.environ,
{
"HEDGE_PLAN_ENABLED": "true",
"HEDGE_PLAN_SHOW_PERP_OPTIONS": "true",
"HEDGE_PLAN_SHOW_OPTIONS_OPTIONS": "false",
"STANDALONE_OPTIONS_UI": "false",
},
clear=False,
):
os.environ.pop("OKX_TRADE_MODE", None)
self.assertEqual(get_okx_trade_mode(), MODE_PERP)
def test_block_standalone_hedge_only_msg(self):
with patch.dict(
os.environ,
{"OKX_TRADE_MODE": MODE_OO, "STANDALONE_OPTIONS_UI": "false"},
clear=False,
):
msg = block_standalone_open_by_mode_msg()
self.assertIsNotNone(msg)
self.assertIn("仅对冲", msg or "")
class EnvUiModeSectionsTests(unittest.TestCase):
def test_perp_mode_shows_po_fields(self):
from lib.env.env_ui_manifest import ui_sections_for_exchange
secs = {s["title"]: s["fields"] for s in ui_sections_for_exchange("okx", mode="perp_options")}
self.assertIn("对冲计划·永期", secs)
keys = {f[0] for f in secs["对冲计划·永期"]}
self.assertIn("MAX_ACTIVE_HEDGE_PLANS", keys)
self.assertIn("HEDGE_PLAN_OPEN_ORDER", keys)
self.assertNotIn("HEDGE_PLAN_OO_BIAS_RATIO", keys)
opt_keys = {f[0] for f in secs["期权账户"]}
self.assertNotIn("OKX_OPTIONS_MAX_ACTIVE_POSITIONS", opt_keys)
def test_oo_mode_shows_oo_fields(self):
from lib.env.env_ui_manifest import ui_sections_for_exchange
secs = {s["title"]: s["fields"] for s in ui_sections_for_exchange("okx", mode="options_options")}
self.assertIn("对冲计划·期期", secs)
keys = {f[0] for f in secs["对冲计划·期期"]}
self.assertIn("HEDGE_PLAN_OO_BIAS_RATIO", keys)
self.assertNotIn("HEDGE_PLAN_OPEN_ORDER", keys)
def test_trade_mode_choices_are_hedge_only(self):
from lib.env.env_schema import SELECT_OPTIONS
modes = [k for k, _ in SELECT_OPTIONS["OKX_TRADE_MODE"]]
self.assertEqual(modes, ["perp_options", "options_options"])
if __name__ == "__main__":
unittest.main()