"""deploy/sanitize_hub_settings.py 单元测试."""
from __future__ import annotations

import json
import sys
from pathlib import Path

REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / "deploy"))

from sanitize_hub_settings import sanitize_settings  # noqa: E402


def test_drops_gate_bot_and_keeps_gate():
    raw = {
        "exchanges": [
            {"id": "0", "key": "binance", "name": "币安", "agent_url": "http://127.0.0.1:15200"},
            {"id": "3", "key": "gate_bot", "name": "Gate bot", "agent_url": "http://127.0.0.1:15203"},
            {"id": "2", "key": "gate", "name": "Gate", "flask_url": "http://127.0.0.1:5000"},
        ]
    }
    cleaned, removed = sanitize_settings(raw)
    keys = [x["key"] for x in cleaned["exchanges"]]
    assert keys == ["binance", "gate"]
    assert len(removed) == 1


def test_drops_port_5002_legacy():
    raw = {
        "exchanges": [
            {
                "id": "3",
                "key": "legacy",
                "name": "crypto_monitor_gate_bot",
                "flask_url": "http://127.0.0.1:5002",
            },
        ]
    }
    cleaned, removed = sanitize_settings(raw)
    assert cleaned["exchanges"] == []
    assert removed
