File size: 1,968 Bytes
81e3673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import unittest
from unittest.mock import MagicMock, patch

# Add backend to path
sys.path.append(os.getcwd())

from ai.nlp_engine import NaturalLanguageEngine


class TestLLMPriority(unittest.TestCase):
    
    @patch('ai.nlp_engine.OpenAI')
    @patch.dict(os.environ, {}, clear=True) # Start with empty env
    def test_priority_openai(self, mock_openai):
        """Test that OpenAI is picked first if available"""
        with patch.dict(os.environ, {
            "OPENAI_API_KEY": "sk-openai", 
            "ANTHROPIC_API_KEY": "sk-anthropic"
        }):
            engine = NaturalLanguageEngine()
            # Should initialize with OpenAI key
            mock_openai.assert_called_with(api_key="sk-openai")
            print("✅ OpenAI Priority Verification Passed")

    @patch('ai.nlp_engine.OpenAI')
    @patch.dict(os.environ, {}, clear=True)
    def test_priority_anthropic(self, mock_openai):
        """Test that Anthropic is picked if OpenAI is missing"""
        with patch.dict(os.environ, {
            "ANTHROPIC_API_KEY": "sk-anthropic",
            "DEEPSEEK_API_KEY": "sk-deepseek"
        }):
            engine = NaturalLanguageEngine()
            # Should initialize with Anthropic key (passed to OpenAI client wrapper)
            mock_openai.assert_called_with(api_key="sk-anthropic")
            print("✅ Anthropic Priority Verification Passed")

    @patch('ai.nlp_engine.OpenAI')
    @patch.dict(os.environ, {}, clear=True)
    def test_priority_deepseek(self, mock_openai):
        """Test that DeepSeek is picked if others are missing"""
        with patch.dict(os.environ, {
            "DEEPSEEK_API_KEY": "sk-deepseek"
        }):
            engine = NaturalLanguageEngine()
            # Should initialize with DeepSeek key
            mock_openai.assert_called_with(api_key="sk-deepseek")
            print("✅ DeepSeek Priority Verification Passed")

if __name__ == '__main__':
    unittest.main()