File size: 2,412 Bytes
790b5af
 
a745005
 
790b5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1b661d
790b5af
 
 
 
 
 
 
 
741570f
 
e143727
 
 
 
 
741570f
 
e143727
741570f
 
 
790b5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Unit tests for configuration management."""

import os
from unittest.mock import patch

from src.utils.config import Settings


def test_settings_with_valid_env(monkeypatch):
    """Test settings load correctly with valid environment variables."""
    monkeypatch.setenv("OPENROUTER_API_KEY", "test-openrouter-key")
    monkeypatch.setenv("TAVILY_API_KEY", "test-tavily-key")
    monkeypatch.setenv("ENVIRONMENT", "production")

    settings = Settings()

    assert settings.openrouter_api_key == "test-openrouter-key"
    assert settings.tavily_api_key == "test-tavily-key"
    assert settings.environment == "production"
    assert settings.is_production is True


def test_settings_with_defaults(monkeypatch):
    """Test settings use defaults for optional fields."""
    monkeypatch.setenv("OPENROUTER_API_KEY", "test-key")
    monkeypatch.setenv("TAVILY_API_KEY", "test-key")
    monkeypatch.delenv("LANGCHAIN_PROJECT", raising=False)

    settings = Settings()

    assert settings.default_model == "x-ai/grok-4.1-fast:free"
    assert settings.max_cost_per_run == 2.0
    assert settings.langchain_project == "market-intelligence-prod"


def test_settings_with_missing_keys():
    """Test settings when some keys are missing (should use defaults)."""
    with patch.dict(
        os.environ,
        {"OPENROUTER_API_KEY": "test", "TAVILY_API_KEY": "test-tavily"},
        clear=True,
    ):
        settings = Settings()
        assert settings.openrouter_api_key == "test"
        assert settings.tavily_api_key == "test-tavily"
        assert (
            settings.default_model == "x-ai/grok-4.1-fast:free"
        )  # Falls back to default


def test_openrouter_base_url():
    """Test OpenRouter base URL property."""
    # Use minimal valid settings
    settings = Settings(openrouter_api_key="test", tavily_api_key="test")
    assert settings.openrouter_base_url == "https://openrouter.ai/api/v1"


def test_is_production_property(monkeypatch):
    """Test is_production property works correctly."""
    monkeypatch.setenv("OPENROUTER_API_KEY", "test")
    monkeypatch.setenv("TAVILY_API_KEY", "test")

    # Test development
    monkeypatch.setenv("ENVIRONMENT", "development")
    settings = Settings()
    assert settings.is_production is False

    # Test production
    monkeypatch.setenv("ENVIRONMENT", "production")
    settings = Settings()
    assert settings.is_production is True