from __future__ import annotations

from web_core.settings.settings_schema import normalize_web_settings


def test_normalize_watchlist_from_string_and_deduplicates() -> None:
    raw = {"watchlist": "aapl, msft\nAAPL\n btc-usd "}
    out = normalize_web_settings(raw)
    assert out["watchlist"] == ["AAPL", "MSFT", "BTC-USD"]


def test_normalize_watchlist_from_list_caps_length() -> None:
    raw_list = [f"sym{i}" for i in range(60)]
    out = normalize_web_settings({"watchlist": raw_list})
    assert len(out["watchlist"]) == 40
    assert out["watchlist"][0] == "SYM0"


def test_market_preset_defaults_to_custom_on_invalid_value() -> None:
    out = normalize_web_settings({"market_preset": "not-real"})
    assert out["market_preset"] == "Custom"


def test_normalize_watchlist_splits_legacy_escaped_newline_values() -> None:
    out = normalize_web_settings({"watchlist": "AAPL\\nMSFT\\NTSLA"})
    assert out["watchlist"] == ["AAPL", "MSFT", "TSLA"]


def test_normalize_watchlist_splits_escaped_newlines_inside_list_items() -> None:
    out = normalize_web_settings({"watchlist": ["AMD\\NTSLA"]})
    assert out["watchlist"] == ["AMD", "TSLA"]
