#!/usr/bin/env python3 """Basic tests for the MCP Sentiment Analysis Tool.""" import os import sys from unittest.mock import MagicMock, patch # Add the current directory to sys.path to import app sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from app import analyze_sentiment def test_analyze_sentiment_no_token(): """Test sentiment analyzer behavior when no HF token is provided.""" with patch.dict(os.environ, {}, clear=True): with patch("app.HF_API_TOKEN", None): result = analyze_sentiment("Test text") assert "āŒ Error: Hugging Face API Token" in result def test_analyze_sentiment_empty_input(): """Test sentiment analyzer behavior with empty input.""" with patch("app.HF_API_TOKEN", "fake_token"): result = analyze_sentiment("") assert "āŒ Error: Please provide text to analyze" in result def test_analyze_sentiment_short_input(): """Test sentiment analyzer behavior with very short input.""" with patch("app.HF_API_TOKEN", "fake_token"): result = analyze_sentiment("Hi") assert "āŒ Error: Text too short" in result def test_analyze_sentiment_success(): """Test successful sentiment analysis.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.json.return_value = [[ {"label": "POSITIVE", "score": 0.85}, {"label": "NEGATIVE", "score": 0.10}, {"label": "NEUTRAL", "score": 0.05} ]] mock_response.raise_for_status.return_value = None with patch("app.HF_API_TOKEN", "fake_token"): with patch("app.requests.post", return_value=mock_response): result = analyze_sentiment("I love this product!") assert "😊 Sentiment Analysis Result" in result assert "POSITIVE" in result assert "85.0%" in result def test_analyze_sentiment_api_error(): """Test API error handling.""" import requests mock_response = MagicMock() mock_response.status_code = 503 mock_response.json.return_value = {"error": "Service unavailable"} # Create a proper HTTPError http_error = requests.exceptions.HTTPError("503 Service Unavailable") http_error.response = mock_response mock_response.raise_for_status.side_effect = http_error with patch("app.HF_API_TOKEN", "fake_token"): with patch("app.requests.post", return_value=mock_response): result = analyze_sentiment("Test text for API error") assert "šŸ”„ Service temporarily unavailable" in result def test_analyze_sentiment_loading_model(): """Test model loading scenario.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.json.return_value = {"error": "Model is currently loading"} mock_response.raise_for_status.return_value = None with patch("app.HF_API_TOKEN", "fake_token"): with patch("app.requests.post", return_value=mock_response): result = analyze_sentiment("Test text for loading model") assert "šŸ”„ Model is currently loading" in result if __name__ == "__main__": # Run tests print("Running MCP Sentiment Analysis Tool tests...") test_analyze_sentiment_no_token() print("āœ“ No token test passed") test_analyze_sentiment_empty_input() print("āœ“ Empty input test passed") test_analyze_sentiment_short_input() print("āœ“ Short input test passed") test_analyze_sentiment_success() print("āœ“ Success test passed") test_analyze_sentiment_api_error() print("āœ“ API error test passed") test_analyze_sentiment_loading_model() print("āœ“ Loading model test passed") print("\nšŸŽ‰ All tests passed!")