"""Edge-case tests for citation exporters — math mode, list EOF, BibTeX URL, and RIS unicode."""


class TestInlineMathPreservedBetweenDollars:
    """Verify content between $ markers is NOT escaped during LaTeX export."""

    def test_inline_math_preserved_between_dollars(self):
        """$x^2 + y^2$ should keep ^ and + unescaped inside math mode."""
        from local_deep_research.text_optimization.citation_formatter import (
            LaTeXExporter,
        )

        exporter = LaTeXExporter()
        content = "# Title\n\nThe formula $x^2 + y^2 = r^2$ is important."
        result = exporter.export_to_latex(content)

        # Math content between $ should be preserved (not escaped)
        assert "$x^2 + y^2 = r^2$" in result


class TestDisplayMathDoubleDollarPreserved:
    """Verify $$...$$ display math blocks are preserved during LaTeX export."""

    def test_display_math_double_dollar_preserved(self):
        """$$E = mc^2$$ should preserve the math content unescaped."""
        from local_deep_research.text_optimization.citation_formatter import (
            LaTeXExporter,
        )

        exporter = LaTeXExporter()
        content = "# Title\n\n$$E = mc^2$$\n\nSome text."
        result = exporter.export_to_latex(content)

        # The display math content should be preserved
        assert "E = mc^2" in result
        # The ^ should NOT be escaped to \textasciicircum{} inside math
        assert r"\textasciicircum" not in result


class TestListAtEndOfContentGetsClosingTag:
    """Verify list items at EOF trigger \\end{itemize} via _convert_lists lines 940-941."""

    def test_list_at_end_of_content_gets_closing_tag(self):
        """List items at end of content (no trailing text) should get \\end{itemize}."""
        from local_deep_research.text_optimization.citation_formatter import (
            LaTeXExporter,
        )

        exporter = LaTeXExporter()
        # Content ends with list items, no trailing non-list text
        content = "# Title\n\nSome intro text.\n\n- Item one\n- Item two\n- Item three"
        result = exporter.export_to_latex(content)

        begin_count = result.count("\\begin{itemize}")
        end_count = result.count("\\end{itemize}")
        assert begin_count == end_count == 1
        # The last \end{itemize} should be after the last \item
        last_item_pos = result.rfind("\\item")
        last_end_pos = result.rfind("\\end{itemize}")
        assert last_end_pos > last_item_pos


class TestSourceWithoutUrlOmitsBibtexUrlField:
    """Verify BibTeX entry omits url= field when source has no URL."""

    def test_source_without_url_omits_bibtex_url_field(self):
        """[1] Title without URL line → BibTeX entry should not contain url = {...}."""
        from local_deep_research.text_optimization.citation_formatter import (
            QuartoExporter,
        )

        exporter = QuartoExporter()
        # Source without URL line
        content = "# Report\n\nSome text [1].\n\n## Sources\n[1] A Book Title Without URL"
        bib_content = exporter._generate_bibliography(content)

        assert "ref1" in bib_content
        assert "A Book Title Without URL" in bib_content
        # Should NOT have url field since no URL was provided
        assert "url = " not in bib_content
        assert "howpublished" not in bib_content


class TestRisExporterHandlesUnicodeTitle:
    """Verify RIS exporter preserves Unicode characters in TI field."""

    def test_ris_exporter_handles_unicode_title(self):
        """Unicode title '日本語' in RIS TI field should be preserved."""
        from local_deep_research.text_optimization.citation_formatter import (
            RISExporter,
        )

        exporter = RISExporter()
        content = (
            "## Sources\n[1] 日本語の研究論文\n  URL: https://example.jp/paper"
        )
        ris = exporter.export_to_ris(content)

        assert "TI  - " in ris
        assert "日本語" in ris
        assert "UR  - https://example.jp/paper" in ris
