"""Tests for Closed-Loop Autonomous Mapping Coordinator with Currency Exchange.
=============================================================================
Kiểm thử tích hợp Stage 0D: Currency Exchange Routine (Phím '/') trong Coordinator.
"""

from unittest.mock import MagicMock
from tools.autonomous_mapping_coordinator import AutonomousMappingCoordinator


def test_coordinator_stage0_currency_exchange_routine():
    coordinator = AutonomousMappingCoordinator(is_simulated=True)

    # Giả lập ví có nhiều Exalted Orb thặng dư
    wallet = {
        "Exalted Orb": 25,  # Thặng dư 20 Exalted
        "Chaos Orb": 0,
    }
    market_prices = {
        "Exalted Orb": 10.0,
        "Divine Orb": 150.0,
    }

    res = coordinator.execute_stage0_currency_exchange_routine(
        wallet=wallet,
        market_prices=market_prices,
        current_gold=12000,
    )

    assert res["status"] == "SUCCESS"
    assert res["orders_executed"] >= 1
    assert res["chaos_converted"] > 0
    assert res["error"] == ""


def test_coordinator_stage0_currency_exchange_rejected_low_gold():
    coordinator = AutonomousMappingCoordinator(is_simulated=True)

    res = coordinator.execute_stage0_currency_exchange_routine(
        wallet={"Exalted Orb": 20},
        market_prices={"Exalted Orb": 10.0},
        current_gold=3000,  # Dưới ngưỡng 5000
    )

    assert res["status"] == "REJECTED"
    assert "ngưỡng an toàn" in res["error"]
