import pytest from unittest.mock import patch, MagicMock from app.services.llm_service import LLMService def test_llm_service_mock_mode(): # If key is not configured, it should return mock recommendation with patch("app.services.llm_service.settings") as mock_settings: mock_settings.NVIDIA_API_KEY = None service = LLMService() assert service.enabled is False # Test old recommend_outfits wrapper res = service.recommend_outfits( user_profile={"gender": "male", "height_cm": 175, "weight_kg": 70}, wardrobe_items=[{"id": "item1", "category": "shirt", "color_hex": "#ffffff"}], weather="28C", event="workplace" ) assert "Mock Outfit" in res["recommendation"] assert "item1" in res["selected_item_ids"] def test_chat_and_recommend_mock_routing(): with patch("app.services.llm_service.settings") as mock_settings: mock_settings.NVIDIA_API_KEY = None service = LLMService() assert service.enabled is False # Test recommendation intent (should be true) res_rec = service.chat_and_recommend( user_profile={"gender": "male", "height_cm": 175, "weight_kg": 70}, wardrobe_items=[{"id": "item1", "category": "shirt", "color_hex": "#ffffff"}], message="Gợi ý phối đồ đi chơi", history=[], weather="28C" ) assert res_rec["is_recommendation"] is True assert "item1" in res_rec["selected_item_ids"] # Test non-recommendation/chat intent (should be false if there is history) res_chat = service.chat_and_recommend( user_profile={"gender": "male", "height_cm": 175, "weight_kg": 70}, wardrobe_items=[{"id": "item1", "category": "shirt", "color_hex": "#ffffff"}], message="Tại sao chiếc áo này lại hợp với tôi?", history=[{"role": "user", "content": "Mặc gì đây"}, {"role": "assistant", "content": "Áo sơ mi"}], weather="28C" ) assert res_chat["is_recommendation"] is False assert "item1" not in res_chat["selected_item_ids"] assert "Tại sao chiếc áo này lại hợp với tôi?" in res_chat["recommendation"] def test_llm_service_nvidia_api_success(): with patch("app.services.llm_service.settings") as mock_settings: mock_settings.NVIDIA_API_KEY = "mock_key" mock_settings.NVIDIA_MODEL = "moonshotai/kimi-k2.6" service = LLMService() assert service.enabled is True # Mock requests.post mock_response = MagicMock() mock_response.raise_for_status.return_value = None mock_response.json.return_value = { "choices": [ { "message": { "content": '{\n "is_recommendation": true,\n "recommendation": "Gợi ý: Áo thun trắng phối quần short năng động.",\n "selected_item_ids": ["item1"]\n}' } } ] } with patch("requests.post", return_value=mock_response) as mock_post: res = service.chat_and_recommend( user_profile={"gender": "male", "height_cm": 175, "weight_kg": 70}, wardrobe_items=[{"id": "item1", "category": "shirt", "color_hex": "#ffffff"}], message="Mặc gì hôm nay?", history=[], weather="28C" ) mock_post.assert_called_once() assert res["is_recommendation"] is True assert res["recommendation"] == "Gợi ý: Áo thun trắng phối quần short năng động." assert res["selected_item_ids"] == ["item1"] def test_llm_service_nvidia_api_error_fallback(): with patch("app.services.llm_service.settings") as mock_settings: mock_settings.NVIDIA_API_KEY = "mock_key" mock_settings.NVIDIA_MODEL = "moonshotai/kimi-k2.6" service = LLMService() assert service.enabled is True with patch("requests.post", side_effect=Exception("API connection timeout")) as mock_post: res = service.chat_and_recommend( user_profile={"gender": "male", "height_cm": 175, "weight_kg": 70}, wardrobe_items=[{"id": "item1", "category": "shirt", "color_hex": "#ffffff"}], message="Mặc gì hôm nay?", history=[] ) mock_post.assert_called_once() assert res["is_recommendation"] is False assert "Hệ thống gặp lỗi" in res["recommendation"] assert res["selected_item_ids"] == []