"""Edge-case tests for BaseExporter._generate_safe_filename() and _prepend_title_if_needed().

Extends test_base_exporter.py with boundary conditions not covered:
all-special-char titles, exact 50-char boundary, all-whitespace titles,
and #NoSpace heading detection.
"""


class TestGenerateSafeFilenameEdgeCases:
    """Edge cases for _generate_safe_filename()."""

    def _get_exporter(self):
        from local_deep_research.exporters.latex_exporter import LaTeXExporter

        return LaTeXExporter()

    def test_all_special_chars_produces_extension_only(self):
        """Title with only special chars: regex removes all → empty safe_title."""
        exporter = self._get_exporter()
        result = exporter._generate_safe_filename("@#$%^&*()")
        # safe_title is "" (truthy title enters if-branch but regex strips all)
        assert result == ".tex"

    def test_exactly_50_chars_preserved(self):
        """Title that is exactly 50 chars after cleaning should not be truncated."""
        exporter = self._get_exporter()
        title = "A" * 50
        result = exporter._generate_safe_filename(title)
        assert result == "A" * 50 + ".tex"

    def test_51_chars_truncated_to_50(self):
        """Title 51 chars after cleaning should truncate to 50."""
        exporter = self._get_exporter()
        title = "A" * 51
        result = exporter._generate_safe_filename(title)
        name_part = result.replace(".tex", "")
        assert len(name_part) == 50

    def test_all_whitespace_title_produces_extension_only(self):
        """Whitespace-only title is truthy but strip() makes it empty."""
        exporter = self._get_exporter()
        result = exporter._generate_safe_filename("   ")
        # "   " is truthy → enters if-branch → regex keeps spaces → strip() → ""
        assert result == ".tex"


class TestPrependTitleEdgeCases:
    """Edge cases for _prepend_title_if_needed()."""

    def _get_exporter(self):
        from local_deep_research.exporters.latex_exporter import LaTeXExporter

        return LaTeXExporter()

    def test_hash_without_space_still_detected_as_heading(self):
        """Content starting with '#' (no space) is still treated as a heading."""
        exporter = self._get_exporter()
        result = exporter._prepend_title_if_needed("#NoSpace content", "Title")
        # lstrip().startswith("#") → True → no prepend
        assert result == "#NoSpace content"

    def test_title_partial_match_no_prepend(self):
        """Content starting with '# Title' matches startswith check — no prepend."""
        exporter = self._get_exporter()
        # content.startswith("# Title") is True for "# Title Extra"
        result = exporter._prepend_title_if_needed(
            "# Title Extra\ncontent", "Title"
        )
        # "# Title Extra".startswith("# Title") → True → no prepend
        assert result == "# Title Extra\ncontent"

    def test_title_substring_mismatch(self):
        """Content heading that differs from title at start."""
        exporter = self._get_exporter()
        result = exporter._prepend_title_if_needed("# Other\ncontent", "Title")
        # Doesn't start with "# Title", but starts with "#" → no prepend
        assert result == "# Other\ncontent"

    def test_content_with_only_newlines_gets_title(self):
        """Content of just newlines has no heading → title prepended."""
        exporter = self._get_exporter()
        result = exporter._prepend_title_if_needed("\n\n\n", "Title")
        assert result == "# Title\n\n\n\n\n"
