|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from intelliperf.core.llm import validate_llm_credentials |
| 5 | + |
| 6 | + |
| 7 | +def test_validate_credentials_amd_success(): |
| 8 | + """Test successful credential validation for AMD provider.""" |
| 9 | + with patch("requests.post") as mock_post, patch("dspy.LM"), patch("dspy.configure"): |
| 10 | + mock_response = Mock() |
| 11 | + mock_response.status_code = 200 |
| 12 | + mock_response.json.return_value = {"choices": [{"message": {"content": "test"}}]} |
| 13 | + mock_post.return_value = mock_response |
| 14 | + |
| 15 | + # Should not raise an exception |
| 16 | + result = validate_llm_credentials( |
| 17 | + api_key="test-key", model="test-model", provider="https://llm-api.amd.com/azure" |
| 18 | + ) |
| 19 | + assert result is True |
| 20 | + |
| 21 | + |
| 22 | +def test_validate_credentials_amd_auth_failure(): |
| 23 | + """Test credential validation fails with 401 for AMD provider.""" |
| 24 | + with patch("requests.post") as mock_post, patch("dspy.LM"), patch("dspy.configure"): |
| 25 | + mock_response = Mock() |
| 26 | + mock_response.status_code = 401 |
| 27 | + mock_http_error = Exception("401 Unauthorized") |
| 28 | + mock_http_error.response = mock_response |
| 29 | + mock_response.raise_for_status.side_effect = mock_http_error |
| 30 | + mock_post.return_value = mock_response |
| 31 | + |
| 32 | + with pytest.raises(SystemExit): |
| 33 | + validate_llm_credentials(api_key="bad-key", model="test-model", provider="https://llm-api.amd.com/azure") |
| 34 | + |
| 35 | + |
| 36 | +def test_validate_credentials_amd_model_not_found(): |
| 37 | + """Test credential validation fails with 404 for AMD provider.""" |
| 38 | + with patch("requests.post") as mock_post, patch("dspy.LM"), patch("dspy.configure"): |
| 39 | + mock_response = Mock() |
| 40 | + mock_response.status_code = 404 |
| 41 | + mock_http_error = Exception("404 Not Found") |
| 42 | + mock_http_error.response = mock_response |
| 43 | + mock_response.raise_for_status.side_effect = mock_http_error |
| 44 | + mock_post.return_value = mock_response |
| 45 | + |
| 46 | + with pytest.raises(SystemExit): |
| 47 | + validate_llm_credentials( |
| 48 | + api_key="test-key", |
| 49 | + model="nonexistent-model", |
| 50 | + provider="https://llm-api.amd.com/azure", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_validate_credentials_openai_success(): |
| 55 | + """Test successful credential validation for OpenAI provider.""" |
| 56 | + with patch("dspy.LM"), patch("dspy.configure"), patch("dspy.ChainOfThought") as mock_chain: |
| 57 | + # Mock the chain of thought call |
| 58 | + mock_chain_instance = Mock() |
| 59 | + mock_chain_instance.return_value = Mock(output="test") |
| 60 | + mock_chain.return_value = mock_chain_instance |
| 61 | + |
| 62 | + # Should not raise an exception |
| 63 | + result = validate_llm_credentials(api_key="test-key", model="gpt-4", provider="openai") |
| 64 | + assert result is True |
| 65 | + |
| 66 | + |
| 67 | +def test_validate_credentials_openai_failure(): |
| 68 | + """Test credential validation fails for OpenAI provider.""" |
| 69 | + with patch("dspy.LM"), patch("dspy.configure"), patch("dspy.ChainOfThought") as mock_chain: |
| 70 | + # Mock the chain of thought to raise an exception |
| 71 | + mock_chain_instance = Mock() |
| 72 | + mock_chain_instance.side_effect = Exception("Authentication failed") |
| 73 | + mock_chain.return_value = mock_chain_instance |
| 74 | + |
| 75 | + with pytest.raises(SystemExit): |
| 76 | + validate_llm_credentials(api_key="bad-key", model="gpt-4", provider="openai") |
0 commit comments