Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.1 KB
Raw Blame
"""Tests for BaseExporter helper methods."""


class TestPrependTitleIfNeeded:
    """Tests for BaseExporter._prepend_title_if_needed() method."""

    def _get_exporter(self):
        """Get a concrete exporter instance to test base class methods."""
        from local_deep_research.exporters.latex_exporter import LaTeXExporter

        return LaTeXExporter()

    def test_prepends_title_when_no_heading(self):
        """Title should be prepended when content has no heading."""
        exporter = self._get_exporter()
        content = "This is content without a heading."
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == "# My Report\n\nThis is content without a heading."

    def test_does_not_prepend_when_same_title_exists(self):
        """Title should NOT be prepended when content starts with same title."""
        exporter = self._get_exporter()
        content = "# My Report\n\nThis is the content."
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == content  # Unchanged

    def test_does_not_prepend_when_different_heading_exists(self):
        """Title should NOT be prepended when content starts with any heading."""
        exporter = self._get_exporter()
        content = "# Different Heading\n\nThis is the content."
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == content  # Unchanged - already has a heading

    def test_does_not_prepend_when_h2_heading_exists(self):
        """Title should NOT be prepended when content starts with H2."""
        exporter = self._get_exporter()
        content = "## Section Heading\n\nThis is the content."
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == content  # Unchanged

    def test_returns_unchanged_when_title_is_none(self):
        """Content should be returned unchanged when title is None."""
        exporter = self._get_exporter()
        content = "This is content without a heading."

        result = exporter._prepend_title_if_needed(content, None)

        assert result == content

    def test_returns_unchanged_when_title_is_empty(self):
        """Content should be returned unchanged when title is empty string."""
        exporter = self._get_exporter()
        content = "This is content without a heading."

        result = exporter._prepend_title_if_needed(content, "")

        assert result == content

    def test_handles_content_with_leading_whitespace(self):
        """Should check for heading after stripping leading whitespace."""
        exporter = self._get_exporter()
        content = "   # Existing Heading\n\nContent here."
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        # Should NOT prepend because content has a heading (after lstrip)
        assert result == content

    def test_handles_empty_content(self):
        """Should handle empty content gracefully."""
        exporter = self._get_exporter()
        content = ""
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == "# My Report\n\n"

    def test_handles_whitespace_only_content(self):
        """Should handle whitespace-only content."""
        exporter = self._get_exporter()
        content = "   \n\n   "
        title = "My Report"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == "# My Report\n\n   \n\n   "

    def test_preserves_special_characters_in_title(self):
        """Title with special characters should be preserved."""
        exporter = self._get_exporter()
        content = "Some content here."
        title = "Analysis: AI & ML (2024)"

        result = exporter._prepend_title_if_needed(content, title)

        assert result == "# Analysis: AI & ML (2024)\n\nSome content here."

    def test_preserves_unicode_in_title(self):
        """Unicode characters in title should be preserved."""
        exporter = self._get_exporter()
        content = "Some content here."
        title = "Исследование машинного обучения"

        result = exporter._prepend_title_if_needed(content, title)

        assert result.startswith("# Исследование машинного обучения\n\n")


class TestGenerateSafeFilename:
    """Tests for BaseExporter._generate_safe_filename() method."""

    def _get_exporter(self):
        """Get a concrete exporter instance to test base class methods."""
        from local_deep_research.exporters.latex_exporter import LaTeXExporter

        return LaTeXExporter()

    def test_generates_filename_from_title(self):
        """Should generate filename from title."""
        exporter = self._get_exporter()

        result = exporter._generate_safe_filename("My Research Report")

        assert result == "My_Research_Report.tex"

    def test_removes_special_characters(self):
        """Should remove special characters from filename."""
        exporter = self._get_exporter()

        result = exporter._generate_safe_filename("Report: AI & ML (2024)")

        assert result == "Report_AI__ML_2024.tex"

    def test_truncates_long_titles(self):
        """Should truncate titles longer than 50 characters."""
        exporter = self._get_exporter()
        long_title = "A" * 100

        result = exporter._generate_safe_filename(long_title)

        # 50 chars + .tex extension
        assert len(result) == 54
        assert result.endswith(".tex")

    def test_uses_default_when_title_is_none(self):
        """Should use default filename when title is None."""
        exporter = self._get_exporter()

        result = exporter._generate_safe_filename(None)

        assert result == "research_report.tex"

    def test_uses_default_when_title_is_empty(self):
        """Should use default filename when title is empty."""
        exporter = self._get_exporter()

        result = exporter._generate_safe_filename("")

        assert result == "research_report.tex"