"""instance_settings_lib 单元测试."""
from __future__ import annotations

import os
import unittest

from lib.instance.instance_settings_lib import build_instance_settings_view


class InstanceSettingsLibTest(unittest.TestCase):
    def test_build_settings_view_sections(self):
        view = build_instance_settings_view(
            exchange_key="gate",
            exchange_display="Gate.io",
            risk_status={"status_label": "正常", "can_trade": True},
            data_export_version=3,
        )
        titles = [s["title"] for s in view["sections"]]
        self.assertIn("交易执行", titles)
        self.assertIn("账户冷静期", titles)
        self.assertEqual(view["data_export_version"], 3)
        self.assertTrue(view["show_transfer"])

    def test_force_close_section_when_enabled(self):
        old = os.environ.get("FORCE_CLOSE_ENABLED")
        try:
            os.environ["FORCE_CLOSE_ENABLED"] = "true"
            view = build_instance_settings_view(
                exchange_key="gate",
                exchange_display="Gate.io",
                risk_status={},
            )
            titles = [s["title"] for s in view["sections"]]
            self.assertIn("整点强制清仓", titles)
        finally:
            if old is None:
                os.environ.pop("FORCE_CLOSE_ENABLED", None)
            else:
                os.environ["FORCE_CLOSE_ENABLED"] = old


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