""" Tests unitaires pour le module utils.widgets. Ces tests vérifient le fonctionnement des widgets HTML personnalisés. """ import pytest from unittest.mock import patch, MagicMock from utils.widgets import html_expander class TestHtmlExpander: """Tests pour la fonction html_expander.""" @patch('utils.widgets.st') @patch('utils.widgets.markdown') def test_expander_basic(self, mock_markdown, mock_st): """Test la création basique d'un expander.""" mock_markdown.markdown.return_value = "
Test content
" html_expander("Test Title", "Test content") # Vérifier que markdown.markdown a été appelé mock_markdown.markdown.assert_called_once_with("Test content") # Vérifier que st.markdown a été appelé assert mock_st.markdown.called call_args = mock_st.markdown.call_args html_output = call_args[0][0] assert "Test Title" in html_output assert "Test content
" in html_output assert "Content
" # Créer deux expanders html_expander("Title 1", "Content 1") call_1 = mock_st.markdown.call_args[0][0] html_expander("Title 2", "Content 2") call_2 = mock_st.markdown.call_args[0][0] # Extraire les IDs import re id_pattern = r'id="(expander_[a-f0-9]+)"' id_1 = re.search(id_pattern, call_1).group(1) id_2 = re.search(id_pattern, call_2).group(1) # Les IDs doivent être différents assert id_1 != id_2 @patch('utils.widgets.st') @patch('utils.widgets.markdown') def test_expander_unsafe_html_enabled(self, mock_markdown, mock_st): """Test que unsafe_allow_html est activé.""" mock_markdown.markdown.return_value = "Content
" html_expander("Title", "Content") # Vérifier que unsafe_allow_html=True call_kwargs = mock_st.markdown.call_args[1] assert call_kwargs.get("unsafe_allow_html") is True @patch('utils.widgets.st') @patch('utils.widgets.markdown') def test_expander_with_special_characters(self, mock_markdown, mock_st): """Test avec des caractères spéciaux dans le titre et le contenu.""" mock_markdown.markdown.return_value = "Content <>
" html_expander("Title <>&", "Content <>&") call_args = mock_st.markdown.call_args html_output = call_args[0][0] # Le titre doit être présent assert "Title <>&" in html_output @patch('utils.widgets.st') @patch('utils.widgets.markdown') def test_expander_empty_content(self, mock_markdown, mock_st): """Test avec un contenu vide.""" mock_markdown.markdown.return_value = "" html_expander("Title", "") # Ne doit pas crasher assert mock_st.markdown.called @patch('utils.widgets.st') @patch('utils.widgets.markdown') def test_expander_multiline_content(self, mock_markdown, mock_st): """Test avec du contenu multiligne.""" content = """ # Titre Paragraphe 1 Paragraphe 2 """ mock_markdown.markdown.return_value = "Paragraphe 1
Paragraphe 2
" html_expander("Title", content) call_args = mock_st.markdown.call_args html_output = call_args[0][0] assert "