import os import sys import asyncio from unittest.mock import MagicMock, patch # Add project root to path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from backend.gemini_analyzer import GeminiPrescriptionAnalyzer, GeminiPrescription import pytest @pytest.mark.asyncio async def test_invalid_image_detection(): print("๐Ÿงช Testing Invalid Image Detection...") # Mock the Gemini client mock_response = MagicMock() mock_response.text = """ { "medical_context": { "is_prescription": false, "diagnosis": "", "symptoms": "", "allergies": "" }, "patient_info": {}, "healthcare_provider": {}, "prescription_details": {}, "medicines": [], "special_instructions": "This image does not appear to be a prescription." } """ with patch('google.generativeai.GenerativeModel') as MockModel: mock_client = MockModel.return_value mock_client.generate_content.return_value = mock_response analyzer = GeminiPrescriptionAnalyzer(api_key="dummy_key") # Test with dummy text result = analyzer.analyze_prescription(raw_text="dummy", image_path="dummy.jpg") print(f"Is Prescription: {result.is_prescription}") if result.is_prescription is False: print("โœ… Successfully detected invalid image") else: print("โŒ Failed to detect invalid image") if __name__ == "__main__": asyncio.run(test_invalid_image_detection())