from __future__ import annotations from uuid import uuid4 import os import unittest from unittest.mock import patch from prod.clean_arch.dita_v2 import ( DITAv2LauncherBundle, LauncherVenueMode, LauncherZincMode, KernelControlSnapshot, MockVenueAdapter, build_launcher_bundle, ) from prod.bingx.enums import BingxEnvironment from prod.clean_arch.dita_v2.launcher import _maybe_close from prod.clean_arch.dita_v2.launcher import build_bingx_exec_client_config class DummyCloseable: def __init__(self) -> None: self.closed = False def close(self) -> None: self.closed = True class DummyControlPlane: def __init__(self) -> None: self.snapshot = KernelControlSnapshot() def read(self) -> KernelControlSnapshot: return self.snapshot def close(self) -> None: pass class DummyZincPlane: def __init__(self) -> None: self.control_updates: list[KernelControlSnapshot] = [] self.slot_writes: list[object] = [] def update_control(self, snapshot: KernelControlSnapshot) -> None: self.control_updates.append(snapshot) def write_slot(self, slot: object) -> None: self.slot_writes.append(slot) class TestDITAv2Launcher(unittest.TestCase): def test_build_launcher_bundle_defaults_to_mock_and_in_memory(self) -> None: bundle = build_launcher_bundle(prefix=f"dita_v2_{uuid4().hex}") try: self.assertIsInstance(bundle, DITAv2LauncherBundle) self.assertIsInstance(bundle.venue, MockVenueAdapter) self.assertEqual(bundle.kernel.max_slots, 10) self.assertEqual(bundle.kernel.control.mode.value, "NORMAL") finally: bundle.close() def test_build_launcher_bundle_can_select_real_components_via_env(self) -> None: prefix = f"dita_v2_{uuid4().hex}" dummy_control = DummyControlPlane() dummy_zinc = DummyZincPlane() with patch("prod.clean_arch.dita_v2.launcher.build_control_plane", return_value=dummy_control), patch( "prod.clean_arch.dita_v2.launcher._build_zinc_plane", return_value=dummy_zinc ): bundle = build_launcher_bundle( prefix=prefix, venue_mode=LauncherVenueMode.BINGX, zinc_mode=LauncherZincMode.REAL, bingx_backend=object(), ) try: self.assertIs(bundle.control_plane, dummy_control) self.assertIs(bundle.zinc_plane, dummy_zinc) self.assertEqual(bundle.venue.__class__.__name__, "BingxVenueAdapter") finally: bundle.close() def test_build_launcher_bundle_respects_explicit_modes(self) -> None: prefix = f"dita_v2_{uuid4().hex}" bundle = build_launcher_bundle( prefix=prefix, venue_mode=LauncherVenueMode.MOCK, zinc_mode=LauncherZincMode.IN_MEMORY, ) try: self.assertIsInstance(bundle.venue, MockVenueAdapter) self.assertEqual(bundle.kernel.max_slots, 10) finally: bundle.close() def test_bingx_exec_client_config_uses_standard_testnet_credentials(self) -> None: with patch.dict( os.environ, { "BINGX_API_KEY": "test-api-key", "BINGX_SECRET_KEY": "test-secret-key", "DOLPHIN_BINGX_ENV": "VST", "DOLPHIN_BINGX_ALLOW_MAINNET": "0", "DOLPHIN_BINGX_RECV_WINDOW_MS": "60000", "DOLPHIN_BINGX_DEFAULT_LEVERAGE": "1", "DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP": "3", }, clear=False, ): cfg = build_bingx_exec_client_config() self.assertEqual(cfg.api_key, "test-api-key") self.assertEqual(cfg.secret_key, "test-secret-key") self.assertIs(cfg.environment, BingxEnvironment.VST) self.assertFalse(cfg.allow_mainnet) self.assertEqual(cfg.recv_window_ms, 60000) self.assertEqual(cfg.default_leverage, 1) self.assertEqual(cfg.exchange_leverage_cap, 3) def test_maybe_close_handles_closeable_objects(self) -> None: dummy = DummyCloseable() _maybe_close(dummy) self.assertTrue(dummy.closed) if __name__ == "__main__": unittest.main()