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"},
        "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 a value from an environment variable has priority over the YAML file.
    """
    # Arrange: Set BOTH a YAML value and an environment variable
    monkeypatch.setenv("CONFIG_PATH", tmp_config_file)
    # The value from the environment variable
    monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env")
    
    # Act
    # We need to import the module after setting the env variable for the test to work
    from app import config
    importlib.reload(config)
    
    # Assert: The value from the environment variable should be used
    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-env"

def test_env_var_overrides_default(monkeypatch):
    """
    Tests that an environment variable overrides the hardcoded default when
    the YAML file is missing or doesn't contain the key.
    """
    # Arrange: Point to a non-existent file and set an environment variable
    monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml")
    monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env")

    # Act
    from app import config
    importlib.reload(config)

    # Assert: The value from the environment variable should be used
    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-env"
    
def test_hardcoded_default_is_used_last(monkeypatch):
    """
    Tests that the hardcoded default is used when the YAML is missing and
    the environment variable is not set.
    """
    # Arrange
    monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml")
    monkeypatch.delenv("DEEPSEEK_MODEL_NAME", raising=False)

    # Act
    from app import config
    importlib.reload(config)

    # Assert: The hardcoded default value should be used
    assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-chat"