"""Tests for Streamlit console display functionality."""
from unittest.mock import patch
def test_scrollable_console_basic_text():
"""Test scrollable_console renders basic text correctly."""
# Import after mocking to avoid Streamlit initialization
with patch('streamlit_sftp_interface_v2.components') as mock_components:
with patch('streamlit_sftp_interface_v2.st') as mock_st:
mock_st.session_state.get.return_value = True # auto_scroll enabled
from streamlit_sftp_interface_v2 import scrollable_console
test_text = "Line 1\nLine 2\nLine 3"
scrollable_console(test_text, height=300, container_id="test_console")
# Verify components.html was called
assert mock_components.html.called
html_content = mock_components.html.call_args[0][0]
# Check that text appears in HTML (escaped)
assert "Line 1" in html_content
assert "Line 2" in html_content
assert "Line 3" in html_content
# Check styling
assert "height: 300px" in html_content
assert "overflow-y: auto" in html_content
assert "background-color: #1e1e1e" in html_content
assert "color: #d4d4d4" in html_content
def test_scrollable_console_html_escaping():
"""Test that HTML special characters are properly escaped."""
with patch('streamlit_sftp_interface_v2.components') as mock_components:
with patch('streamlit_sftp_interface_v2.st') as mock_st:
mock_st.session_state.get.return_value = True
from streamlit_sftp_interface_v2 import scrollable_console
# Text with HTML special characters
dangerous_text = " & \"quotes\""
scrollable_console(dangerous_text, container_id="escape_test")
html_content = mock_components.html.call_args[0][0]
# Verify escaping in the content div
assert "<script>" in html_content
assert "&" in html_content
assert """ in html_content
# Verify the user's dangerous text is escaped in content
assert "alert('XSS')" in html_content
# The auto-scroll script tag is OK (it's ours)
assert "consoleDiv.scrollTop" in html_content
def test_scrollable_console_auto_scroll_enabled():
"""Test auto-scroll JavaScript is included when enabled."""
with patch('streamlit_sftp_interface_v2.components') as mock_components:
with patch('streamlit_sftp_interface_v2.st') as mock_st:
mock_st.session_state.get.return_value = True # auto_scroll enabled
from streamlit_sftp_interface_v2 import scrollable_console
scrollable_console("Test text", container_id="auto_scroll_test")
html_content = mock_components.html.call_args[0][0]
# Verify auto-scroll script is present
assert "