93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
"""Tests for converter UI functionality."""
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
from rich.console import Console
|
|
|
|
from paperlib.ui import ConversionUI
|
|
|
|
|
|
class TestConversionUI:
|
|
"""Test ConversionUI functionality."""
|
|
|
|
@pytest.fixture
|
|
def ui(self):
|
|
"""Create a ConversionUI instance for testing."""
|
|
# Use a console that doesn't output to terminal during tests
|
|
console = Console(file=open("/dev/null", "w"), force_terminal=True)
|
|
return ConversionUI(console=console)
|
|
|
|
@pytest.fixture
|
|
def mock_papers(self):
|
|
"""Create mock paper metadata for testing."""
|
|
papers = []
|
|
for i in range(3):
|
|
paper = Mock()
|
|
paper.paper_id = f"test-paper-{i + 1}"
|
|
paper.title = f"Test Paper Title {i + 1}"
|
|
papers.append(paper)
|
|
return papers
|
|
|
|
def test_format_mineru_output_line(self, ui):
|
|
"""Test formatting of MinerU output lines."""
|
|
# Test INFO line
|
|
info_line = "2026-04-17 17:46:01.450 | INFO | Processing started"
|
|
formatted = ui._format_mineru_output_line(info_line)
|
|
assert "[dim]" in formatted
|
|
|
|
# Test ERROR line
|
|
error_line = "ERROR: Conversion failed"
|
|
formatted = ui._format_mineru_output_line(error_line)
|
|
assert "[red]" in formatted
|
|
|
|
# Test WARNING line
|
|
warning_line = "WARNING: Low memory"
|
|
formatted = ui._format_mineru_output_line(warning_line)
|
|
assert "[yellow]" in formatted
|
|
|
|
# Test progress line
|
|
progress_line = "Layout Predict: 50%|█████ | 22/44 [00:15<00:15, 1.44it/s]"
|
|
formatted = ui._format_mineru_output_line(progress_line)
|
|
assert "[blue]" in formatted
|
|
|
|
# Test fetching line (may be colored blue due to % character)
|
|
fetch_line = "Fetching 7 files: 100%|██████████| 7/7"
|
|
formatted = ui._format_mineru_output_line(fetch_line)
|
|
assert ("[cyan]" in formatted) or (
|
|
"[blue]" in formatted
|
|
) # Either color is fine
|
|
|
|
@patch("threading.Thread")
|
|
@patch("time.sleep")
|
|
def test_run_conversion_with_ui_empty(self, mock_sleep, mock_thread, ui):
|
|
"""Test UI with no papers to convert."""
|
|
result = ui.run_conversion_with_ui([], lambda x: True)
|
|
assert result == (0, 0)
|
|
|
|
def test_create_display_table(self, ui):
|
|
"""Test creating the display table."""
|
|
task_id = ui.progress.add_task("test", total=1)
|
|
|
|
# Test without current paper
|
|
table = ui.create_display_table(task_id)
|
|
assert table is not None
|
|
|
|
# Test with current paper
|
|
table = ui.create_display_table(task_id, "test-paper-1 - Sample Title")
|
|
assert table is not None
|
|
|
|
def test_output_line_management(self, ui):
|
|
"""Test that output lines are properly managed."""
|
|
# Add many lines
|
|
for i in range(60):
|
|
ui.output_lines.append(f"Line {i}")
|
|
|
|
# The list can grow beyond 50, but display is limited to last 15 lines
|
|
assert len(ui.output_lines) == 60
|
|
|
|
# Check that display shows only recent lines
|
|
recent_lines = ui.output_lines[-ui.max_output_lines :]
|
|
assert len(recent_lines) == ui.max_output_lines
|