"""FORCE_CLOSE 部署策略:只补缺失,不覆盖手调.""" import os import tempfile import unittest from pathlib import Path from unittest import mock from scripts import sync_common_trading_env as sync class TestForceCloseFillMissing(unittest.TestCase): def test_does_not_overwrite_existing(self): with tempfile.TemporaryDirectory() as td: gate = Path(td) / "crypto_monitor_gate" gate.mkdir() (gate / ".env").write_text( "FORCE_CLOSE_ENABLED=false\nFORCE_CLOSE_BJ_HOUR=1\n", encoding="utf-8", ) with mock.patch.object(sync, "REPO", td): changed = sync.apply_force_close_policy(dry_run=False) self.assertFalse(changed) text = (gate / ".env").read_text(encoding="utf-8") self.assertIn("FORCE_CLOSE_ENABLED=false", text) self.assertIn("FORCE_CLOSE_BJ_HOUR=1", text) def test_fills_missing_keys(self): with tempfile.TemporaryDirectory() as td: gate = Path(td) / "crypto_monitor_gate" gate.mkdir() (gate / ".env").write_text("APP_USERNAME=x\n", encoding="utf-8") with mock.patch.object(sync, "REPO", td): changed = sync.apply_force_close_policy(dry_run=False) self.assertTrue(changed) text = (gate / ".env").read_text(encoding="utf-8") self.assertIn("FORCE_CLOSE_ENABLED=true", text) self.assertIn("FORCE_CLOSE_BJ_HOUR=0", text) if __name__ == "__main__": unittest.main()