from pathlib import Path
import re

from config.settings import _load_ruoyi_mysql_defaults, settings


def test_ruoyi_mysql_defaults_should_follow_application_dev_config():
    project_root = Path(__file__).resolve().parents[2]
    config_path = project_root / "ruoyi-admin" / "src" / "main" / "resources" / "application-dev.yml"
    text = config_path.read_text(encoding="utf-8")

    match = re.search(r"jdbc:mysql://[^:/?#]+:(?P<port>\d+)/", text)

    assert match is not None
    assert _load_ruoyi_mysql_defaults()["port"] == match.group("port")


def test_settings_should_default_to_lsfx_target_mysql_host_and_port():
    assert settings.CCDI_DB_HOST == "116.62.17.81"
    assert settings.CCDI_DB_PORT == 3307


def test_settings_should_still_use_ruoyi_mysql_defaults_for_db_name_and_credentials():
    defaults = _load_ruoyi_mysql_defaults()

    assert settings.CCDI_DB_NAME == defaults["database"]
    assert settings.CCDI_DB_USERNAME == defaults["username"]
    assert settings.CCDI_DB_PASSWORD == defaults["password"]
