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.

31.8 KB
Raw Blame
"""Tests for ODTExporter using pypandoc."""

import re

import pytest
from unittest.mock import patch

# Check if pandoc is available (required by pypandoc)
try:
    import pypandoc

    pypandoc.get_pandoc_version()
    PANDOC_AVAILABLE = True
except (ImportError, OSError):
    PANDOC_AVAILABLE = False

pytestmark = pytest.mark.skipif(
    not PANDOC_AVAILABLE,
    reason="Pandoc is not installed (required for ODT export)",
)


class TestODTExporterProperties:
    """Tests for ODTExporter properties."""

    def test_format_name_is_odt(self):
        """Test that format_name is 'odt'."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        exporter = ODTExporter()

        assert exporter.format_name == "odt"

    def test_file_extension_is_odt(self):
        """Test that file_extension is '.odt'."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        exporter = ODTExporter()

        assert exporter.file_extension == ".odt"

    def test_mimetype_is_correct(self):
        """Test that mimetype is correct for ODT."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        exporter = ODTExporter()

        assert exporter.mimetype == "application/vnd.oasis.opendocument.text"


class TestODTExporterExport:
    """Tests for ODTExporter.export method."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_returns_export_result(self, exporter, simple_markdown):
        """Test that export returns ExportResult."""
        from local_deep_research.exporters import ExportResult

        result = exporter.export(simple_markdown)

        assert isinstance(result, ExportResult)

    def test_result_content_is_bytes(self, exporter, simple_markdown):
        """Test that result content is bytes."""
        result = exporter.export(simple_markdown)

        assert isinstance(result.content, bytes)

    def test_result_is_valid_odt(self, exporter, simple_markdown):
        """Test that result is a valid ODT file (ZIP archive)."""
        result = exporter.export(simple_markdown)

        # ODT files are ZIP archives, they start with PK
        assert result.content[:2] == b"PK"

    def test_result_has_reasonable_size(self, exporter, simple_markdown):
        """Test that generated ODT has a reasonable size."""
        result = exporter.export(simple_markdown)

        # A simple ODT should be at least a few KB
        assert len(result.content) > 1000

    def test_result_filename_uses_title(self, exporter, simple_markdown):
        """Test that filename uses provided title."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(title="My Research Report")
        result = exporter.export(simple_markdown, options)

        assert "My_Research_Report" in result.filename
        assert result.filename.endswith(".odt")

    def test_result_filename_default_when_no_title(
        self, exporter, simple_markdown
    ):
        """Test that filename uses default when no title."""
        result = exporter.export(simple_markdown)

        assert "research_report" in result.filename
        assert result.filename.endswith(".odt")

    def test_result_mimetype_is_correct(self, exporter, simple_markdown):
        """Test that result mimetype is correct."""
        result = exporter.export(simple_markdown)

        assert result.mimetype == "application/vnd.oasis.opendocument.text"

    def test_handles_empty_markdown(self, exporter):
        """Test handling of empty markdown."""
        result = exporter.export("")

        assert result.content[:2] == b"PK"

    def test_handles_markdown_with_headings(self, exporter, sample_markdown):
        """Test handling of markdown with multiple heading levels."""
        result = exporter.export(sample_markdown)

        assert result.content[:2] == b"PK"
        assert len(result.content) > 1000

    def test_handles_markdown_with_tables(self, exporter, sample_markdown):
        """Test handling of markdown with tables."""
        result = exporter.export(sample_markdown)

        # Should create valid ODT
        assert result.content[:2] == b"PK"

    def test_handles_markdown_with_code_blocks(self, exporter, sample_markdown):
        """Test handling of markdown with code blocks."""
        result = exporter.export(sample_markdown)

        assert result.content[:2] == b"PK"

    def test_handles_markdown_with_lists(self, exporter, sample_markdown):
        """Test handling of markdown with lists."""
        result = exporter.export(sample_markdown)

        assert result.content[:2] == b"PK"

    def test_handles_special_characters(
        self, exporter, markdown_with_special_chars
    ):
        """Test handling of special characters."""
        result = exporter.export(markdown_with_special_chars)

        assert result.content[:2] == b"PK"

    def test_handles_nested_lists(self, exporter, markdown_with_nested_lists):
        """Test handling of nested lists."""
        result = exporter.export(markdown_with_nested_lists)

        assert result.content[:2] == b"PK"

    def test_handles_links(self, exporter, markdown_with_links):
        """Test handling of markdown with links."""
        result = exporter.export(markdown_with_links)

        assert result.content[:2] == b"PK"

    def test_handles_large_markdown(self, exporter):
        """Test handling of large markdown content."""
        # Create a large markdown document
        large_content = "# Large Document\n\n"
        large_content += ("This is a paragraph. " * 100 + "\n\n") * 50

        result = exporter.export(large_content)

        assert result.content[:2] == b"PK"
        # Large content should produce larger ODT (Pandoc compresses efficiently)
        assert len(result.content) > 5000

    def test_includes_ldr_attribution(self, exporter, simple_markdown):
        """Test that LDR attribution is included."""
        # We can't easily check the content inside ODT without parsing it,
        # but we verify the export succeeds
        result = exporter.export(simple_markdown)

        assert result.content[:2] == b"PK"

    def test_logs_odt_size(self, exporter, simple_markdown):
        """Test that ODT size is logged."""
        with patch(
            "local_deep_research.exporters.odt_exporter.logger"
        ) as mock_logger:
            exporter.export(simple_markdown)

            mock_logger.info.assert_called_once()
            call_args = mock_logger.info.call_args[0][0]
            assert "Generated ODT" in call_args
            assert "bytes" in call_args


class TestODTExporterErrorHandling:
    """Tests for error handling in ODTExporter."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_logs_exception_on_error(self, exporter):
        """Test that exceptions are logged when pandoc fails."""
        import subprocess

        with patch(
            "local_deep_research.exporters.odt_exporter.subprocess.run",
            side_effect=subprocess.CalledProcessError(
                1, "pandoc", stderr=b"Test error"
            ),
        ):
            with patch(
                "local_deep_research.exporters.odt_exporter.logger"
            ) as mock_logger:
                with pytest.raises(RuntimeError):
                    exporter.export("test")

                mock_logger.exception.assert_called_once()


class TestODTExporterIntegration:
    """Integration tests for ODTExporter with ExporterRegistry."""

    def test_registered_in_registry(self):
        """Test that ODTExporter is registered in the registry."""
        from local_deep_research.exporters import ExporterRegistry

        assert ExporterRegistry.is_format_supported("odt")

    def test_can_get_from_registry(self):
        """Test that ODTExporter can be retrieved from registry."""
        from local_deep_research.exporters import ExporterRegistry
        from local_deep_research.exporters.odt_exporter import ODTExporter

        exporter = ExporterRegistry.get_exporter("odt")

        assert isinstance(exporter, ODTExporter)

    def test_export_via_registry(self, simple_markdown):
        """Test export via registry lookup."""
        from local_deep_research.exporters import ExporterRegistry

        exporter = ExporterRegistry.get_exporter("odt")
        result = exporter.export(simple_markdown)

        assert result.content[:2] == b"PK"
        assert result.filename.endswith(".odt")


class TestODTExporterFilenameTruncation:
    """Tests for filename truncation with long titles."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_filename_truncated_to_50_chars(self, exporter, simple_markdown):
        """Test that filename is truncated when title exceeds 50 chars."""
        from local_deep_research.exporters import ExportOptions

        # Title with more than 50 characters
        long_title = "A" * 60
        options = ExportOptions(title=long_title)
        result = exporter.export(simple_markdown, options)

        # Filename should be truncated to 50 chars + extension
        filename_without_ext = result.filename.rsplit(".", 1)[0]
        assert len(filename_without_ext) == 50
        assert result.filename.endswith(".odt")

    def test_filename_not_truncated_under_50_chars(
        self, exporter, simple_markdown
    ):
        """Test that filename is not truncated when under 50 chars."""
        from local_deep_research.exporters import ExportOptions

        title = "Short Title"
        options = ExportOptions(title=title)
        result = exporter.export(simple_markdown, options)

        assert "Short_Title" in result.filename
        assert result.filename.endswith(".odt")


class TestODTExporterMetadataVariations:
    """Tests for various metadata combinations."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_title_only(self, exporter, simple_markdown):
        """Test export with only title metadata."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(title="Title Only Test")
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"
        assert "Title_Only_Test" in result.filename

    def test_author_only(self, exporter, simple_markdown):
        """Test export with only author metadata."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(metadata={"author": "Test Author"})
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"

    def test_date_only(self, exporter, simple_markdown):
        """Test export with only date metadata."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(metadata={"date": "2024-01-15"})
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"

    def test_all_metadata(self, exporter, simple_markdown):
        """Test export with all metadata fields."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(
            title="Full Test Document",
            metadata={"author": "Test Author", "date": "2024-01-15"},
        )
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"
        assert "Full_Test_Document" in result.filename

    def test_unicode_in_title(self, exporter, simple_markdown):
        """Test export with unicode characters in title."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(title="Café naïve 日本語")
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"

    def test_unicode_in_author(self, exporter, simple_markdown):
        """Test export with unicode characters in author."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(metadata={"author": "José García 山田太郎"})
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"


class TestODTExporterContentSizeLimit:
    """Tests for content size limit enforcement."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_raises_error_for_oversized_content(self, exporter):
        """Test that ValueError is raised for content exceeding size limit."""
        from local_deep_research.exporters.base import BaseExporter

        MAX_CONTENT_SIZE = BaseExporter.MAX_CONTENT_SIZE

        # Create content that exceeds the limit
        oversized_content = "x" * (MAX_CONTENT_SIZE + 1)

        with pytest.raises(ValueError) as exc_info:
            exporter.export(oversized_content)

        assert "exceeds maximum size" in str(exc_info.value)

    def test_accepts_content_at_limit(self, exporter):
        """Test that content at exactly the limit is accepted (doesn't raise)."""
        from local_deep_research.exporters.base import BaseExporter

        MAX_CONTENT_SIZE = BaseExporter.MAX_CONTENT_SIZE
        import zipfile
        import io

        # Create content at exactly the limit
        content_at_limit = "x" * MAX_CONTENT_SIZE

        # Create a minimal valid ODT file for mocking
        odt_buffer = io.BytesIO()
        with zipfile.ZipFile(odt_buffer, "w") as zf:
            zf.writestr("mimetype", "application/vnd.oasis.opendocument.text")
            zf.writestr("content.xml", "<office:document-content/>")
        mock_odt_bytes = odt_buffer.getvalue()

        # Mock subprocess.run to avoid actually processing 50MB
        mock_result = type(
            "Result", (), {"stdout": mock_odt_bytes, "stderr": b""}
        )()

        with patch(
            "local_deep_research.exporters.odt_exporter.subprocess.run",
            return_value=mock_result,
        ):
            # This should not raise ValueError for size limit
            result = exporter.export(content_at_limit)
            assert result.content[:2] == b"PK"

    def test_accepts_content_under_limit(self, exporter, simple_markdown):
        """Test that content under the limit is accepted."""
        result = exporter.export(simple_markdown)

        assert result.content[:2] == b"PK"


class TestODTExporterMetadataSanitization:
    """Tests for metadata sanitization to prevent injection."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_sanitizes_title_with_dashes(self, exporter, simple_markdown):
        """Test that double dashes are removed from title."""
        from local_deep_research.exporters import ExportOptions

        # Title with potential argument injection pattern
        options = ExportOptions(title="Title --extract-media=/tmp")
        result = exporter.export(simple_markdown, options)

        # Should succeed and produce valid ODT
        assert result.content[:2] == b"PK"

    def test_sanitizes_author_with_newlines(self, exporter, simple_markdown):
        """Test that newlines are removed from author metadata."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(metadata={"author": "Author\nWith\nNewlines"})
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"

    def test_sanitizes_date_with_special_chars(self, exporter, simple_markdown):
        """Test that date with injection patterns is sanitized."""
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(metadata={"date": "2024--01--15 --verbose"})
        result = exporter.export(simple_markdown, options)

        assert result.content[:2] == b"PK"


class TestODTExporterInMemoryConversion:
    """Tests verifying ODT conversion happens entirely in memory."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_no_temp_files_used(self, exporter, simple_markdown):
        """Test that conversion uses subprocess stdin/stdout, not temp files."""
        import subprocess

        with patch(
            "local_deep_research.exporters.odt_exporter.subprocess.run",
            wraps=subprocess.run,
        ) as mock_run:
            exporter.export(simple_markdown)

            # Verify subprocess was called with stdin input and stdout capture
            call_kwargs = mock_run.call_args
            assert call_kwargs.kwargs.get("input") is not None
            assert call_kwargs.kwargs.get("capture_output") is True

            # Verify -o - (stdout) is in the command
            cmd = call_kwargs.args[0]
            assert "-o" in cmd
            assert "-" in cmd[cmd.index("-o") + 1]

    def test_pandoc_error_propagates_without_cleanup_needed(self, exporter):
        """Test that pandoc errors propagate cleanly (no temp cleanup needed)."""
        import subprocess

        with patch(
            "local_deep_research.exporters.odt_exporter.subprocess.run",
            side_effect=subprocess.CalledProcessError(
                1, "pandoc", stderr=b"Pandoc failed"
            ),
        ):
            with pytest.raises(RuntimeError, match="Pandoc conversion failed"):
                exporter.export("# Test content")


class TestODTExporterFooter:
    """Tests for LDR attribution footer in output."""

    @pytest.fixture
    def exporter(self):
        """Create ODTExporter instance."""
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_footer_content_present_in_output(self, exporter, simple_markdown):
        """Test that LDR attribution footer appears in the ODT output."""
        import io
        import zipfile

        result = exporter.export(simple_markdown)

        # ODT is a ZIP archive - extract content.xml
        with zipfile.ZipFile(io.BytesIO(result.content), "r") as zf:
            content_xml = zf.read("content.xml").decode("utf-8")

        # Verify footer text is present in the content
        assert "Local Deep Research" in content_xml
        assert "Open Source AI Research Assistant" in content_xml


def _odt_members(odt_bytes: bytes) -> dict:
    """The ODT's archive members, by name."""
    import io
    import zipfile

    with zipfile.ZipFile(io.BytesIO(odt_bytes), "r") as zf:
        return {name: zf.read(name) for name in zf.namelist()}


def _undated(meta_xml: bytes) -> bytes:
    """meta.xml without its creation and modification dates."""
    return re.sub(rb"<(meta:creation-date|dc:date)>[^<]*</\1>", b"", meta_xml)


class TestODTExporterRawPassthroughIsOff:
    """Raw-content syntax in report text reaches the ODT as text.

    By default pandoc's markdown reader copies a ``{=opendocument}`` raw
    block or inline into content.xml as-is, drops raw HTML and TeX, and
    reads ``<div>`` and ``<span>`` tags as document structure. The
    exporter reads report text with all of these switched off
    (``ODTExporter.READER_FORMAT``), so it arrives as ordinary, escaped
    text, consistent with the PDF export. The probe is a benign style
    name: as markup it would appear as a ``text:style-name`` attribute,
    as text only in escaped form.
    """

    MARKER = "RawProbeMarker"

    ORDINARY = (
        "# Research Report\n\n"
        "## Summary\n\n"
        "Paragraph with **bold**, *italic*, `inline code` and a\n"
        "[link](https://example.org/page) plus <https://example.org>.\n\n"
        "- item one\n"
        "- item two\n"
        "  1. nested\n"
        "  2. nested two\n\n"
        "> A quoted line.\n\n"
        "| Column A | Column B |\n"
        "|----------|----------|\n"
        "| 1        | two      |\n\n"
        "```python\n"
        "def f(x):\n"
        "    return 1 < x < 5\n"
        "```\n\n"
        "Inline math $E = mc^2$ and display math:\n\n"
        "$$\n\\int_0^1 x^2 \\, dx = \\frac{1}{3}\n$$\n\n"
        "A claim with a footnote.[^1] Also 3 < 5 and a & b.\n\n"
        "![Figure caption](https://example.org/figure.png)\n\n"
        "[^1]: The footnote text.\n\n"
        "## Sources\n\n"
        "[1] Example Source Title (source nr: 1)\n"
        "   URL: https://example.org/source\n"
    )

    @pytest.fixture
    def exporter(self):
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    @pytest.mark.parametrize(
        "markdown",
        [
            "# Report\n\nBefore.\n\n"
            "```{=opendocument}\n"
            '<text:p text:style-name="RawProbeMarker">probe</text:p>\n'
            "```\n\nAfter.\n",
            "# Report\n\nText "
            '`<text:span text:style-name="RawProbeMarker">probe</text:span>`'
            "{=opendocument} end.\n",
        ],
        ids=["block", "inline"],
    )
    def test_raw_opendocument_is_text_not_markup(self, exporter, markdown):
        content = _odt_members(exporter.export(markdown).content)[
            "content.xml"
        ].decode("utf-8")

        # Never markup ...
        assert f'text:style-name="{self.MARKER}"' not in content
        # ... but present, as escaped text.
        assert f"text:style-name=&quot;{self.MARKER}&quot;" in content

    def test_raw_html_and_tex_are_text_not_dropped(self, exporter):
        content = _odt_members(
            exporter.export(
                "# Report\n\nText <b>html-probe</b> and \\textbf{tex-probe}.\n"
            ).content
        )["content.xml"].decode("utf-8")

        # Shown as written, rather than dropped as raw HTML and raw TeX.
        assert "&lt;b&gt;html-probe&lt;/b&gt;" in content
        assert "\\textbf{tex-probe}" in content

    @pytest.mark.parametrize(
        ("tag", "markdown"),
        [
            (
                "span",
                "# Report\n\nText "
                '<span custom-style="RawProbeMarker">probe</span> end.\n',
            ),
            (
                "div",
                '# Report\n\n<div custom-style="RawProbeMarker">\n\n'
                "Inside.\n\n</div>\n\nAfter.\n",
            ),
        ],
        ids=["span", "div"],
    )
    def test_div_and_span_tags_are_text_not_markup(
        self, exporter, tag, markdown
    ):
        """With pandoc's ``native_spans`` and ``native_divs`` on, these tags
        are read as a Span or Div even with raw HTML off, and their
        ``custom-style`` attribute becomes the ODT style name."""
        content = _odt_members(exporter.export(markdown).content)[
            "content.xml"
        ].decode("utf-8")

        # Never markup ...
        assert f'text:style-name="{self.MARKER}"' not in content
        # ... but present, as escaped text. The quotes around the attribute
        # value are left out: pandoc's smart typography curls them.
        assert f"&lt;{tag} custom-style=" in content
        assert self.MARKER in content
        assert f"&lt;/{tag}&gt;" in content

    @pytest.mark.parametrize(
        "div",
        [
            '<div class="probe">One line</div>',
            '<div id="probe">One line</div>',
        ],
        ids=["class", "id"],
    )
    def test_one_line_div_leaves_the_rest_of_the_report_alone(
        self, exporter, div
    ):
        """With ``native_divs`` on, a ``<div>`` whose closing tag is on the
        same line stays open to the end of the document: the closing tag
        shows as text, and with an ``id`` the rest of the report, footer
        included, is wrapped in a ``text:section``. Read as text, the line
        is one paragraph and the paragraphs after it are exactly those of
        the same report with a plain line in its place."""

        def report(line):
            return (
                "# Report\n\nIntro.\n\n"
                f"{line}\n\n"
                "Later paragraph.\n\n## Later heading\n\nMore.\n"
            )

        def after_the_line(content):
            return content[
                content.index("</text:p>", content.index("One line")) :
            ]

        content = _odt_members(exporter.export(report(div)).content)[
            "content.xml"
        ].decode("utf-8")
        plain = _odt_members(exporter.export(report("One line")).content)[
            "content.xml"
        ].decode("utf-8")

        assert "<text:section" not in content
        # The whole tag line is one paragraph of text.
        assert re.search(
            r"<text:p [^>]*>&lt;div [^<]*&gt;One line&lt;/div&gt;</text:p>",
            content,
        ), content
        # Everything after it, up to the end of the document, is unchanged.
        assert after_the_line(content) == after_the_line(plain)
        assert "Later paragraph." in after_the_line(content)

    def test_ordinary_markdown_converts_as_with_the_default_reader(
        self, exporter
    ):
        """Switching these extensions off changes nothing else: for
        ordinary markdown the ODT matches, member for member, the one
        pandoc's default markdown reader produces through the same
        exporter."""
        from local_deep_research.exporters.base import ExportOptions
        from local_deep_research.exporters.odt_exporter import ODTExporter

        options = ExportOptions(title="Report")
        hardened = _odt_members(exporter.export(self.ORDINARY, options).content)
        with patch.object(ODTExporter, "READER_FORMAT", "markdown"):
            default = _odt_members(
                exporter.export(self.ORDINARY, options).content
            )

        assert hardened.keys() == default.keys()
        for name in hardened:
            # meta.xml holds the title from the --metadata argument, the
            # generator and the creation dates. The dates are removed
            # before comparing, so a pandoc that stamps the current time
            # cannot make this flaky.
            if name == "meta.xml":
                assert _undated(hardened[name]) == _undated(default[name])
            else:
                assert hardened[name] == default[name], name
        assert b"<dc:title>Report</dc:title>" in hardened["meta.xml"]
        # Arrival: the markdown features are really in the document.
        content = hardened["content.xml"].decode("utf-8")
        assert "<table:table " in content
        assert "<text:list" in content
        assert "<text:note " in content
        assert 'xlink:href="https://example.org/page"' in content
        assert "Example Source Title" in content
        assert any(name.startswith("Formula-") for name in hardened)


class TestODTExporterBodyMetadataIsText:
    """A ``---`` block in report text is text, not document metadata.

    By default pandoc's markdown reader treats a ``---`` line directly
    followed by text as the start of a YAML metadata block, closed by the
    next ``---`` line, which can be the footer's rule. A block that parses
    as YAML sets document properties and is removed from the body; one
    that does not fails the export (#7821). The exporter reads report text
    with ``yaml_metadata_block`` off, so neither happens, while the title,
    author and date still come from the ``--metadata`` arguments.
    ``multiline_tables`` is off as well: a ``---`` line directly followed
    by text, with the next rule only after a blank line, stays a
    horizontal rule instead of a one-column table. A ``---`` line, lines
    of text and another ``---`` line with no blank line between them are
    still a simple table, and pipe tables are unaffected.
    """

    @pytest.fixture
    def exporter(self):
        from local_deep_research.exporters.odt_exporter import ODTExporter

        return ODTExporter()

    def test_body_yaml_block_is_text_not_document_metadata(self, exporter):
        from local_deep_research.exporters.base import ExportOptions

        members = _odt_members(
            exporter.export(
                "# Report\n\nIntro.\n\n---\nauthor: MarkerAuthor\n---\n\n"
                "After.\n",
                ExportOptions(title="Report", metadata={"date": "2026-01-02"}),
            ).content
        )
        meta = members["meta.xml"].decode("utf-8")
        content = members["content.xml"].decode("utf-8")

        # Not document metadata ...
        assert "MarkerAuthor" not in meta
        # ... but text, with the text around it kept. (As metadata the
        # author also appears in content.xml, in the title block, but
        # without the "author:" key.)
        assert "author: MarkerAuthor" in content
        assert "Intro." in content
        assert "After." in content
        # The arguments still set the document metadata.
        assert "<dc:title>Report</dc:title>" in meta
        assert "2026-01-02" in meta

    @pytest.mark.parametrize(
        "markdown",
        [
            "Intro text.\n\n---\n## Sources\n\n[1] Example source\n",
            # The shape topic_organization_strategy.py emits itself.
            "Intro text.\n\n---\n**Total Sources Organized:** 5\n\n"
            "## Sources\n\n[1] Example source\n",
        ],
        ids=["7821", "topic-organization"],
    )
    def test_rule_directly_followed_by_text_is_a_rule(self, exporter, markdown):
        """The #7821 shape: a rule directly followed by text. Read as the
        start of a YAML block that the footer's rule closes, it made pandoc
        exit with "Error parsing YAML metadata". With YAML blocks off,
        pandoc's ``multiline_tables`` read the same lines as a one-column
        table: the rest of the report, up to the footer, in one table cell,
        with its headings as literal ``##`` text. Both rules stay rules and
        the heading stays a heading."""
        content = _odt_members(exporter.export(markdown).content)[
            "content.xml"
        ].decode("utf-8")

        assert "<table:table" not in content
        assert "##" not in content
        assert re.search(
            r"<text:h [^>]*>(?:<text:bookmark-start [^>]*/>)?Sources"
            r"(?:<text:bookmark-end [^>]*/>)?</text:h>",
            content,
        ), content
        # This rule and the footer's.
        assert content.count('text:style-name="Horizontal_20_Line"') >= 2
        assert "Intro text." in content
        assert "[1] Example source" in content
        assert "Open Source AI Research Assistant" in content

    def test_indented_rule_keeps_the_following_lines_whole(self, exporter):
        """Read as the top of a multiline table, an indented ``---`` line
        followed by text cut the first characters from every line after
        it, up to the footer's rule."""
        content = _odt_members(
            exporter.export(
                "Intro.\n\n  ---\nABCDEF line\nGHIJKL line\n\nAfter.\n"
            ).content
        )["content.xml"].decode("utf-8")

        assert "<table:table" not in content
        assert "ABCDEF line GHIJKL line" in content
        assert ">After.</text:p>" in content
        assert "Open Source AI Research Assistant" in content

    @pytest.mark.parametrize(
        "markdown",
        [
            "| Column A | Column B |\n|----------|----------|\n| 1 | two |\n",
            "---\n  1     two\n  3     four\n---\n",
        ],
        ids=["pipe-table", "simple-table"],
    )
    def test_ordinary_tables_still_render_as_tables(self, exporter, markdown):
        """Pipe tables, and a headerless simple table whose rules have no
        blank line between them, stay tables with ``multiline_tables`` off."""
        content = _odt_members(exporter.export(markdown).content)[
            "content.xml"
        ].decode("utf-8")

        assert "<table:table" in content
        assert "two" in content