Newer
Older
cortex-hub / ai-hub / tests / test_config.py
import pytest
import importlib
import yaml


@pytest.fixture
def tmp_config_file(tmp_path):
    """Creates a temporary config.yaml file and returns its path."""
    config_content = {
        "application": {
            "project_name": "Test Project from YAML",
            "log_level": "WARNING"
        },
        "llm_providers": {"deepseek_model_name": "deepseek-from-yaml"}
    }
    config_path = tmp_path / "test_config.yaml"
    with open(config_path, 'w') as f:
        yaml.dump(config_content, f)
    return str(config_path)


def test_env_var_overrides_yaml(monkeypatch, tmp_config_file):
    """Tests that an env var overrides YAML for DEEPSEEK_MODEL_NAME."""
    monkeypatch.setenv("CONFIG_PATH", tmp_config_file)
    monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env")

    from app import config
    importlib.reload(config)

    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-env"


def test_env_var_overrides_default(monkeypatch):
    """Tests that env var overrides hardcoded default when YAML is missing."""
    monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml")
    monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env")

    from app import config
    importlib.reload(config)

    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-env"


def test_hardcoded_default_is_used_last(monkeypatch):
    """Tests fallback to hardcoded default if ENV and YAML missing."""
    monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml")
    monkeypatch.delenv("DEEPSEEK_MODEL_NAME", raising=False)

    from app import config
    importlib.reload(config)

    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-chat"


# --------------------------
# ✅ LOG_LEVEL TESTS
# --------------------------

def test_log_level_env_overrides_yaml(monkeypatch, tmp_config_file):
    """Tests LOG_LEVEL: ENV > YAML > default."""
    monkeypatch.setenv("CONFIG_PATH", tmp_config_file)
    monkeypatch.setenv("LOG_LEVEL", "DEBUG")

    from app import config
    importlib.reload(config)

    assert config.settings.LOG_LEVEL == "DEBUG"


def test_log_level_yaml_over_default(monkeypatch, tmp_config_file):
    """Tests LOG_LEVEL uses YAML when ENV is not set."""
    monkeypatch.setenv("CONFIG_PATH", tmp_config_file)
    monkeypatch.delenv("LOG_LEVEL", raising=False)

    from app import config
    importlib.reload(config)

    assert config.settings.LOG_LEVEL == "WARNING"


def test_log_level_default_used(monkeypatch):
    """Tests LOG_LEVEL falls back to default when neither ENV nor YAML set."""
    monkeypatch.setenv("CONFIG_PATH", "/does/not/exist.yaml")
    monkeypatch.delenv("LOG_LEVEL", raising=False)

    from app import config
    importlib.reload(config)

    assert config.settings.LOG_LEVEL == "INFO"