"""Tests for QuickReplyContentService.""" import json from pathlib import Path from unittest.mock import Mock, patch import pytest from capa_de_integracion.config import Settings from capa_de_integracion.models.quick_replies import QuickReplyScreen from capa_de_integracion.services.quick_reply_content import QuickReplyContentService @pytest.fixture def mock_settings(): """Create mock settings for testing.""" settings = Mock(spec=Settings) settings.base_path = Path("/tmp/test_resources") return settings @pytest.fixture def service(mock_settings): """Create QuickReplyContentService instance.""" return QuickReplyContentService(mock_settings) @pytest.mark.asyncio async def test_get_quick_replies_empty_screen_id(service): """Test get_quick_replies with empty screen_id.""" result = await service.get_quick_replies("") assert isinstance(result, QuickReplyScreen) assert result.header == "empty" assert result.body is None assert result.button is None assert result.header_section is None assert result.preguntas == [] @pytest.mark.asyncio async def test_get_quick_replies_none_screen_id(service): """Test get_quick_replies with None screen_id.""" result = await service.get_quick_replies(None) assert isinstance(result, QuickReplyScreen) assert result.header == "empty" assert result.preguntas == [] @pytest.mark.asyncio async def test_get_quick_replies_whitespace_screen_id(service): """Test get_quick_replies with whitespace screen_id.""" result = await service.get_quick_replies(" ") assert isinstance(result, QuickReplyScreen) assert result.header == "empty" assert result.preguntas == [] @pytest.mark.asyncio async def test_get_quick_replies_file_not_found(service, tmp_path): """Test get_quick_replies raises error when file not found.""" # Set service to use a temp directory where the file won't exist service.quick_replies_path = tmp_path / "nonexistent_dir" with pytest.raises(ValueError, match="Error loading quick replies"): await service.get_quick_replies("nonexistent") @pytest.mark.asyncio async def test_get_quick_replies_success(service, tmp_path): """Test get_quick_replies successfully loads file.""" # Create test JSON file quick_replies_dir = tmp_path / "quick_replies" quick_replies_dir.mkdir() service.quick_replies_path = quick_replies_dir test_data = { "header": "Test Header", "body": "Test Body", "button": "Test Button", "header_section": "Test Section", "preguntas": [ { "titulo": "Question 1", "descripcion": "Description 1", "respuesta": "Answer 1", }, { "titulo": "Question 2", "respuesta": "Answer 2", }, ], } test_file = quick_replies_dir / "test_screen.json" test_file.write_text(json.dumps(test_data), encoding="utf-8") result = await service.get_quick_replies("test_screen") assert isinstance(result, QuickReplyScreen) assert result.header == "Test Header" assert result.body == "Test Body" assert result.button == "Test Button" assert result.header_section == "Test Section" assert len(result.preguntas) == 2 assert result.preguntas[0].titulo == "Question 1" assert result.preguntas[0].descripcion == "Description 1" assert result.preguntas[0].respuesta == "Answer 1" assert result.preguntas[1].titulo == "Question 2" assert result.preguntas[1].descripcion is None assert result.preguntas[1].respuesta == "Answer 2" @pytest.mark.asyncio async def test_get_quick_replies_invalid_json(service, tmp_path): """Test get_quick_replies raises error for invalid JSON.""" quick_replies_dir = tmp_path / "quick_replies" quick_replies_dir.mkdir() service.quick_replies_path = quick_replies_dir test_file = quick_replies_dir / "invalid.json" test_file.write_text("{ invalid json }", encoding="utf-8") with pytest.raises(ValueError, match="Invalid JSON format"): await service.get_quick_replies("invalid") @pytest.mark.asyncio async def test_get_quick_replies_minimal_data(service, tmp_path): """Test get_quick_replies with minimal data.""" quick_replies_dir = tmp_path / "quick_replies" quick_replies_dir.mkdir() service.quick_replies_path = quick_replies_dir test_data = { "preguntas": [], } test_file = quick_replies_dir / "minimal.json" test_file.write_text(json.dumps(test_data), encoding="utf-8") result = await service.get_quick_replies("minimal") assert isinstance(result, QuickReplyScreen) assert result.header is None assert result.body is None assert result.button is None assert result.header_section is None assert result.preguntas == [] @pytest.mark.asyncio async def test_validate_file_exists(service, tmp_path): """Test _validate_file with existing file.""" test_file = tmp_path / "test.json" test_file.write_text("{}", encoding="utf-8") # Should not raise service._validate_file(test_file, "test") @pytest.mark.asyncio async def test_validate_file_not_exists(service, tmp_path): """Test _validate_file with non-existing file.""" test_file = tmp_path / "nonexistent.json" with pytest.raises(ValueError, match="Quick reply file not found"): service._validate_file(test_file, "test")