Spaces:
Running

File size: 2,035 Bytes
6b46e13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import os
from unittest.mock import patch, MagicMock
import streamlit as st

# Test the API key handling in the UI app
def test_api_key_handling_with_key():
    """Test UI app behavior when API key is configured"""
    with patch.dict(os.environ, {"AGENT_API_KEY": "test-key-123"}, clear=True):
        # Import the app after setting environment
        from app import AGENT_URL
        
        # The app should have access to the API key
        api_key = os.getenv("AGENT_API_KEY")
        assert api_key == "test-key-123"

def test_api_key_handling_without_key():
    """Test UI app behavior when no API key is configured"""
    with patch.dict(os.environ, {}, clear=True):
        # Import the app after clearing environment
        from app import AGENT_URL
        
        # The app should handle missing API key gracefully
        api_key = os.getenv("AGENT_API_KEY")
        assert api_key is None

def test_environment_variable_loading():
    """Test that environment variables are properly loaded"""
    # Mock the dotenv loading BEFORE importing the app
    with patch('dotenv.load_dotenv'):
        # Clear any existing environment and set our test values
        with patch.dict(os.environ, {
            "ENVIRONMENT": "dev",
            "AGENT_URL": "http://test-agent:8001",
            "MCP_SERVER_URL": "http://test-mcp:8000"
        }, clear=True):
            # Force reload the app module to get fresh environment
            import sys
            if 'app' in sys.modules:
                del sys.modules['app']
            
            from app import ENVIRONMENT, AGENT_URL, MCP_SERVER_URL
            
            # App should use the environment variable we set
            assert ENVIRONMENT == "dev"
            # App should use the mapped URLs from ENV_URLS
            assert AGENT_URL == "http://35.91.47.182:8001"  # dev environment URL
            assert MCP_SERVER_URL == "http://34.220.95.49:8000"  # dev environment URL

if __name__ == "__main__":
    pytest.main([__file__, "-v"])