"""key_auto_order_lib 单元测试."""
import unittest

from lib.key_monitor.key_auto_order_lib import (
    check_monitor_type_add_allowed,
    effective_entry_reason_options,
    effective_stats_segment_defs,
    load_key_auto_order_enabled,
)
from lib.trade.position_sizing_lib import MODE_FULL_MARGIN, MODE_RISK

FULL_OPTS = (
    "趋势A",
    "趋势B",
    "趋势C",
    "趋势D",
    "趋势E",
    "关键位箱体突破",
    "关键位收敛突破",
    "关键位斐波0.618",
    "关键位斐波0.786",
    "关键位假突破",
    "关键位回调触价开仓",
    "关键位突破触价开仓",
    "趋势回调",
    "顺势加仓",
)

STATS_DEFS = (
    ("all", "全部", {}),
    ("key_box", "箱体", {}),
    ("key_trigger", "触价", {}),
)


class KeyAutoOrderLibTest(unittest.TestCase):
    def test_load_default_false(self):
        self.assertFalse(load_key_auto_order_enabled({"KEY_AUTO_ORDER_ENABLED": "false"}))
        self.assertFalse(load_key_auto_order_enabled({}))
        self.assertTrue(load_key_auto_order_enabled({"KEY_AUTO_ORDER_ENABLED": "true"}))

    def test_entry_reason_off(self):
        out = effective_entry_reason_options(FULL_OPTS, MODE_RISK, False)
        self.assertNotIn("关键位箱体突破", out)
        self.assertNotIn("关键位回调触价开仓", out)
        self.assertIn("顺势加仓", out)

    def test_entry_reason_risk_on(self):
        out = effective_entry_reason_options(FULL_OPTS, MODE_RISK, True)
        self.assertIn("关键位箱体突破", out)
        self.assertIn("关键位回调触价开仓", out)

    def test_entry_reason_full_margin_on(self):
        out = effective_entry_reason_options(FULL_OPTS, MODE_FULL_MARGIN, True)
        self.assertNotIn("关键位箱体突破", out)
        self.assertIn("关键位回调触价开仓", out)

    def test_stats_segments_off(self):
        segs = effective_stats_segment_defs(STATS_DEFS, MODE_RISK, False)
        keys = {x[0] for x in segs}
        self.assertIn("all", keys)
        self.assertNotIn("key_box", keys)

    def test_add_key_rs_always(self):
        ok, _ = check_monitor_type_add_allowed("关键支撑阻力", MODE_RISK, False)
        self.assertTrue(ok)

    def test_add_key_trigger_off(self):
        ok, msg = check_monitor_type_add_allowed("回调触价开仓", MODE_RISK, False)
        self.assertFalse(ok)
        self.assertIn("KEY_AUTO_ORDER_ENABLED", msg)


if __name__ == "__main__":
    unittest.main()
