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.

51.9 KB
Raw Blame
"""Tests for ReportGenerator helper methods.

Covers _truncate_at_sentence_boundary and _build_previous_context — two
pure-logic helpers with zero prior test coverage that are critical for
report quality and repetition avoidance.
"""

from unittest.mock import MagicMock

import pytest

from local_deep_research.report_generator import IntegratedReportGenerator


@pytest.fixture
def generator():
    """Create an IntegratedReportGenerator with mocked dependencies."""
    mock_llm = MagicMock()
    mock_search = MagicMock()
    gen = IntegratedReportGenerator.__new__(IntegratedReportGenerator)
    gen.llm = mock_llm
    gen.search_system = mock_search
    gen.max_context_sections = 3
    gen.max_context_chars = 4000
    return gen


# ── _truncate_at_sentence_boundary ──


class TestTruncateAtSentenceBoundary:
    """Tests for _truncate_at_sentence_boundary."""

    def test_text_shorter_than_limit_returned_unchanged(self, generator):
        text = "Short text."
        assert generator._truncate_at_sentence_boundary(text, 100) == text

    def test_text_exactly_at_limit_returned_unchanged(self, generator):
        text = "Exact." + "x" * 94  # 100 chars
        assert generator._truncate_at_sentence_boundary(text, 100) == text

    def test_truncates_at_period_followed_by_space(self, generator):
        text = "First sentence. Second sentence. Third sentence that goes on."
        result = generator._truncate_at_sentence_boundary(text, 35)
        assert result.startswith("First sentence. Second sentence.")
        assert result.endswith("\n[...truncated]")

    def test_truncates_at_exclamation_mark(self, generator):
        text = "Wow! This is amazing! More content follows here."
        result = generator._truncate_at_sentence_boundary(text, 25)
        assert "Wow! This is amazing!" in result
        assert "[...truncated]" in result

    def test_truncates_at_question_mark(self, generator):
        text = "Is this working? Yes it is working perfectly fine here."
        result = generator._truncate_at_sentence_boundary(text, 20)
        assert "Is this working?" in result
        assert "[...truncated]" in result

    def test_boundary_at_end_of_truncated_text(self, generator):
        # Period at exactly the last position of truncated text
        text = "Hello." + "x" * 100
        result = generator._truncate_at_sentence_boundary(text, 6)
        # "Hello." is 6 chars, truncated[:6] = "Hello."
        # last_sentence_end = 6, min_acceptable = int(6*0.8)=4, 6 > 4 → use boundary
        assert "Hello." in result
        assert "[...truncated]" in result

    def test_no_sentence_boundary_falls_back_to_hard_truncation(
        self, generator
    ):
        text = "a" * 200
        result = generator._truncate_at_sentence_boundary(text, 100)
        assert result == "a" * 100 + "\n[...truncated]"

    def test_sentence_boundary_too_early_falls_back(self, generator):
        # Period at position 5 out of 100 → below 80% threshold
        text = "Hi. " + "x" * 200
        result = generator._truncate_at_sentence_boundary(text, 100)
        # min_acceptable = 80, last_sentence_end = 4, 4 < 80 → hard truncation
        assert result == text[:100] + "\n[...truncated]"

    def test_sentence_boundary_at_80_percent_threshold(self, generator):
        # Exactly at 80% boundary
        # max_chars=100, min_acceptable=80
        text = "x" * 80 + ". " + "y" * 50
        result = generator._truncate_at_sentence_boundary(text, 100)
        # last_sentence_end=81, min_acceptable=80, 81 > 80 → use boundary
        assert result.endswith("\n[...truncated]")
        assert result.startswith("x" * 80 + ".")

    def test_period_followed_by_newline(self, generator):
        text = "First sentence.\nSecond sentence continues for a while here."
        result = generator._truncate_at_sentence_boundary(text, 20)
        assert "First sentence." in result
        assert "[...truncated]" in result

    def test_period_not_followed_by_space_or_newline_ignored(self, generator):
        # "3.14" has a period but it's followed by a digit, not space
        text = "The value is 3.14 and more text follows after that point."
        result = generator._truncate_at_sentence_boundary(text, 20)
        # Only sentence boundaries followed by space/newline are valid
        # In "The value is 3.14 a", the period at index 14 is followed by '1', not space
        # So falls back to hard truncation
        assert result == text[:20] + "\n[...truncated]"

    def test_empty_text(self, generator):
        assert generator._truncate_at_sentence_boundary("", 100) == ""

    def test_single_character(self, generator):
        assert generator._truncate_at_sentence_boundary("a", 100) == "a"

    def test_multiple_sentence_endings_uses_last_valid(self, generator):
        text = "One. Two. Three. Four. Five. Six. Seven. Eight."
        result = generator._truncate_at_sentence_boundary(text, 30)
        # Should find the last boundary within the first 30 chars
        # "One. Two. Three. Four. Five. " is 29 chars
        assert "[...truncated]" in result


# ── _build_previous_context ──


class TestBuildPreviousContext:
    """Tests for _build_previous_context."""

    def test_empty_list_returns_empty_string(self, generator):
        assert generator._build_previous_context([]) == ""

    def test_single_finding_included(self, generator):
        result = generator._build_previous_context(["Finding 1"])
        assert "Finding 1" in result
        assert "DO NOT REPEAT" in result
        assert "CONTENT ALREADY WRITTEN" in result

    def test_respects_max_context_sections_limit(self, generator):
        generator.max_context_sections = 2
        findings = ["Finding 1", "Finding 2", "Finding 3", "Finding 4"]
        result = generator._build_previous_context(findings)
        # Should only include last 2 findings
        assert "Finding 3" in result
        assert "Finding 4" in result
        assert "Finding 1" not in result
        assert "Finding 2" not in result

    def test_joins_with_separator(self, generator):
        result = generator._build_previous_context(["A", "B"])
        assert "\n\n---\n\n" in result

    def test_truncates_long_context(self, generator):
        generator.max_context_chars = 50
        long_finding = "x" * 100
        result = generator._build_previous_context([long_finding])
        # The content should be truncated
        assert "[...truncated]" in result

    def test_formatting_markers_present(self, generator):
        result = generator._build_previous_context(["Test content"])
        assert "=== CONTENT ALREADY WRITTEN (DO NOT REPEAT) ===" in result
        assert "=== END OF PREVIOUS CONTENT ===" in result
        assert "CRITICAL:" in result

    def test_context_within_char_limit_not_truncated(self, generator):
        generator.max_context_chars = 10000
        result = generator._build_previous_context(["Short finding."])
        assert "[...truncated]" not in result

    def test_uses_last_n_sections(self, generator):
        generator.max_context_sections = 3
        findings = [f"Finding {i}" for i in range(10)]
        result = generator._build_previous_context(findings)
        assert "Finding 7" in result
        assert "Finding 8" in result
        assert "Finding 9" in result
        assert "Finding 0" not in result


# ── _strip_subsection_boilerplate ──


class TestNormalizeHeadingText:
    """Tests for _normalize_heading_text."""

    def test_strips_bold_and_roman_prefix(self, generator):
        assert (
            generator._normalize_heading_text(
                "**II. Market Structure and Competition: Dual**"
            )
            == "market structure and competition: dual"
        )

    def test_strips_arabic_prefix(self, generator):
        assert generator._normalize_heading_text("1. Foo Bar") == "foo bar"

    def test_empty(self, generator):
        assert generator._normalize_heading_text("") == ""


class TestHeadingRestatesName:
    """Tests for _heading_restates_name against observed LLM patterns."""

    def test_exact_match(self, generator):
        assert generator._heading_restates_name(
            "Climate Drivers and Feedback Loops",
            "Climate Drivers and Feedback Loops",
        )

    def test_extended_with_colon_subtitle(self, generator):
        assert generator._heading_restates_name(
            "Regional Case Studies (North, South, Coastal): "
            "Granular Data on Outcomes",
            "Regional Case Studies (North, South, Coastal)",
        )

    def test_roman_prefix_and_bold(self, generator):
        assert generator._heading_restates_name(
            "**III. Supply Chain Resilience Within the Industry: "
            "Upstream Dependencies**",
            "Supply Chain Resilience Within the Industry",
        )

    def test_en_dash_extension(self, generator):
        assert generator._heading_restates_name(
            "Early Phase (1925–1947): Author's 'Foundational Essay' "
            "– Textual Genesis and Comparative Mechanics",
            "Early Phase (1925–1947): Author's 'Foundational Essay'",
        )

    def test_curly_apostrophe_matches_ascii(self, generator):
        # LLM used U+2019 RIGHT SINGLE QUOTATION MARK in place of ASCII '.
        assert generator._heading_restates_name(
            "Early Phase (1925–1947): Author\u2019s 'Foundational Essay' "
            "– Textual Genesis",
            "Early Phase (1925–1947): Author's 'Foundational Essay'",
        )

    def test_unrelated_heading_does_not_match(self, generator):
        assert not generator._heading_restates_name(
            "I. Strategic Mobilization and Event-Catalyzed Shifts",
            "Mainstream & Dominant Phases (1980–2014+): "
            "The Landmark Events, Coalition Wins",
        )

    def test_does_not_match_substantive_space_prefix(self, generator):
        assert not generator._heading_restates_name(
            "Introduction to Methods",
            "Introduction",
        )

    def test_empty_name_never_matches(self, generator):
        assert not generator._heading_restates_name("Anything", "")


class TestStripSubsectionBoilerplate:
    """Tests for _strip_subsection_boilerplate using generic report patterns."""

    def _strip(
        self,
        generator,
        content,
        name="Name",
        section="Sec",
        siblings=None,
        purpose=None,
    ):
        return generator._strip_subsection_boilerplate(
            content,
            subsection_name=name,
            section_name=section,
            sibling_subsection_names=siblings,
            purpose=purpose,
        )

    def test_empty_content(self, generator):
        assert self._strip(generator, "") == ""

    def test_strips_exact_duplicate_leading_heading(self, generator):
        content = (
            "### Climate Drivers and Feedback Loops\n\n"
            "The source material asserts that under the framework...\n\n"
            "#### 1. The Historical Construction\n"
            "More body.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Climate Drivers and Feedback Loops",
        )
        assert not result.lstrip().startswith("#")
        assert "The source material asserts" in result
        assert "#### 1. The Historical Construction" in result

    def test_strips_roman_prefixed_restatement(self, generator):
        content = (
            "### III. Board-Executive Leadership Overlap: Dual Roles\n\n"
            "Body paragraph about dual roles.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Board-Executive Leadership Overlap",
        )
        assert "### III." not in result
        assert "Body paragraph about dual roles." in result

    def test_strips_bold_wrapped_restatement(self, generator):
        content = (
            "### **Regulatory Status: Open vs. Restricted**\n\n"
            "Metric convergence paragraph.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Regulatory Status: Open vs. Restricted",
        )
        assert "Regulatory Status" not in result.split("\n")[0]
        assert "Metric convergence paragraph." in result

    def test_strips_sibling_bleed_heading(self, generator):
        # LLM copied a sibling subsection's heading into this one's content.
        content = (
            "### The Policy Goal Spectrum\n\n"
            "While the source material posits...\n"
        )
        result = self._strip(
            generator,
            content,
            name="Distinction Between Theory and Practice",
            siblings=["The Policy Goal Spectrum"],
        )
        assert "Policy Goal Spectrum" not in result
        assert "While the source material posits" in result

    def test_preserves_unrelated_organisational_opener(self, generator):
        # Leading ### is a free-form organiser, not a restatement of the
        # subsection name — must be kept.
        content = (
            "### **I. Strategic Mobilization and Event-Catalyzed Shifts "
            "(Regional Focus)**\n\n"
            "The transition of the movement...\n"
        )
        result = self._strip(
            generator,
            content,
            name=(
                "Mainstream & Dominant Phases (1980–2014+): "
                "The Landmark Events, Coalition Wins"
            ),
        )
        assert "Strategic Mobilization" in result
        assert "The transition of the movement" in result

    def test_strips_leading_italic_purpose(self, generator):
        content = (
            "_To summarise the section's scope and limits..._\n\n"
            "Prose begins here.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Anything",
            purpose="To summarise the section's scope and limits...",
        )
        assert "_To summarise" not in result
        assert "Prose begins here." in result

    def test_preserves_legitimate_leading_epigraph_italic(self, generator):
        content = "_In memory of those affected._\n\nProse begins here.\n"
        result = self._strip(
            generator,
            content,
            name="Historical Context",
            purpose="Examine social impacts",
        )
        assert "_In memory of those affected._" in result
        assert "Prose begins here." in result

    def test_strips_purpose_matching_subtitle(self, generator):
        content = (
            "_To examine historical impacts and social consequences._\n\n"
            "Prose begins here.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Historical Context",
            purpose="To examine historical impacts and social consequences.",
        )
        assert "_To examine" not in result
        assert "Prose begins here." in result

    def test_strips_heading_then_italic_purpose(self, generator):
        content = (
            "### Foo Bar\n_To clarify the distinction._\n\nActual prose.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Foo Bar",
            purpose="To clarify the distinction.",
        )
        assert "### Foo Bar" not in result
        assert "_To clarify" not in result
        assert "Actual prose." in result

    def test_strips_trailing_sources_block(self, generator):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n\n"
            "1. Some paper (2020).\n"
            "2. Another paper (2021).\n"
        )
        result = self._strip(generator, content, name="Whatever")
        assert "## Sources" not in result
        assert "Some paper" not in result
        assert "Body of the subsection with findings." in result

    def test_strips_bracket_led_bibliography_note(self, generator):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "[Note: The framework will append the consolidated bibliography.]\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert result == "Body of the subsection with findings.\n"

    def test_strips_bracket_led_placeholder_observed_in_the_wild(
        self, generator
    ):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "[Note: The framework will append the consolidated '## Sources' block after this subsection, so avoid duplicating it here.]\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert result == "Body of the subsection with findings.\n"

    def test_preserves_bracket_led_editorial_note(self, generator):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "[Note: This report includes sources from oral histories and archives.]\n"
            "The evidence requires careful interpretation.\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert (
            "[Note: This report includes sources from oral histories" in result
        )
        assert "The evidence requires careful interpretation." in result

    def test_preserves_bracket_led_framework_methodology_note(self, generator):
        content = (
            "Analysis.\n\n"
            "## Sources\n"
            "[Note: The framework uses primary sources from field interviews.]\n"
            "The evidence requires careful interpretation.\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert (
            "[Note: The framework uses primary sources from field interviews.]"
            in result
        )
        assert "The evidence requires careful interpretation." in result

    def test_preserves_bracket_led_placeholder_inside_code_fence(
        self, generator
    ):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "```text\n"
            "[Note: The framework will append the consolidated bibliography.]\n"
            "```\n"
            "The example is followed by substantive prose.\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert "[Note: The framework will append" in result
        assert "The example is followed by substantive prose." in result

    def test_preserves_fenced_only_sources_block(self, generator):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "```text\n"
            "[Note: The framework will append the consolidated bibliography.]\n"
            "```\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert "## Sources" in result
        assert "[Note: The framework will append" in result

    def test_preserves_fence_when_sources_block_has_citation(self, generator):
        content = (
            "Body of the subsection with findings.\n\n"
            "## Sources\n"
            "```text\n"
            "[Note: The framework will append the consolidated bibliography.]\n"
            "```\n"
            "1. Real citation.\n"
            "Afterword prose.\n"
        )

        result = self._strip(generator, content, name="Whatever")

        assert "## Sources" in result
        assert "[Note: The framework will append" in result
        assert "1. Real citation." in result
        assert "Afterword prose." in result

    def test_strips_selected_bibliography_mid_stream(self, generator):
        # Bibliography appears mid-content, then more prose continues.
        content = (
            "Closing paragraph of the analysis.\n\n"
            "---\n"
            "### Selected Bibliography for Subsection IV: Media "
            "(Supplementary)\n"
            "*(Note: The main report bibliography will be updated.)*\n\n"
            "1. Smith, Jane. Industry Trends.\n"
            "2. Doe, John. Comparative Policy Analysis.\n\n"
            "---\n"
            "### Detailed Content Expansion: New Source Integration\n\n"
            "#### I. Deep Dive\n"
            "Continued analysis after the bib block.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Media, Journalism, and Academic Freedom",
        )
        assert "Selected Bibliography" not in result
        assert "Smith, Jane" not in result
        assert "Doe, John" not in result
        assert "main report bibliography will be updated" not in result
        assert "Detailed Content Expansion" in result
        assert "Continued analysis after the bib block." in result
        assert "Closing paragraph of the analysis." in result

    def test_midstream_bib_with_h4_continuation(self, generator):
        content = (
            "Closing paragraph of the analysis.\n\n"
            "### Selected Bibliography for Subsection IV\n"
            "1. Smith, Jane. Industry Trends.\n\n"
            "#### Deep Dive: Downstream Effects\n"
            "Continued analysis after the bib block.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Media, Journalism, and Academic Freedom",
        )
        assert "Selected Bibliography" not in result
        assert "Smith, Jane" not in result
        assert "#### Deep Dive: Downstream Effects" in result
        assert "Continued analysis after the bib block." in result

    def test_substantive_headings_not_overstripped(self, generator):
        for heading in (
            "### Sources of Bias in the Sampling Frame",
            "## Reference Architecture",
            "### Citation Network Analysis",
        ):
            content = f"Intro paragraph.\n\n{heading}\nBody analysis.\n"
            result = self._strip(generator, content, name="Analysis")
            assert heading in result
            assert "Body analysis." in result

    def test_fenced_code_block_sources_preserved(self, generator):
        content = (
            "Intro text.\n\n"
            "```python\n"
            "## Sources\n"
            "def get_sources():\n"
            "    return []\n"
            "```\n\n"
            "Following prose paragraph.\n"
        )
        result = self._strip(generator, content, name="Python Implementation")
        assert "## Sources" in result
        assert "def get_sources():" in result
        assert "Following prose paragraph." in result

    def test_strips_references_and_citations_variants(self, generator):
        for heading in (
            "## References",
            "### Bibliography",
            "## Key References",
            "### Works Cited",
            "## Further Reading",
            "#### Sources",
            "#### References",
        ):
            content = f"Prose.\n\n{heading}\n\n- item\n"
            result = self._strip(generator, content, name="X")
            assert heading not in result
            assert "Prose." in result
            assert "- item" not in result

    def test_preserves_internal_body_headings(self, generator):
        content = (
            "### Organizational Structure and Financing\n\n"
            "Intro paragraph.\n\n"
            "#### 1. Organizational Governance\n"
            "Detail A.\n\n"
            "#### 2. Financial Architecture\n"
            "Detail B.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Organizational Structure and Financing",
        )
        assert "#### 1. Organizational Governance" in result
        assert "#### 2. Financial Architecture" in result
        # Leading ### stripped; #### body headings kept.
        assert not any(
            line.startswith("### ") and not line.startswith("####")
            for line in result.splitlines()
        )

    def test_no_boilerplate_unchanged_substance(self, generator):
        content = "Just a plain paragraph with no headings at all.\n"
        result = self._strip(generator, content, name="Plain")
        assert "Just a plain paragraph" in result

    def test_preserves_thematic_break_when_no_bib_stripped(self, generator):
        content = "Paragraph 1.\n\n---\n\nParagraph 2.\n"
        result = self._strip(generator, content, name="Plain")
        assert "---" in result
        assert "Paragraph 1." in result
        assert "Paragraph 2." in result

    def test_leading_whitespace_before_heading(self, generator):
        content = "\n\n### Foo Bar\n\nBody.\n"
        result = self._strip(generator, content, name="Foo Bar")
        assert "### Foo Bar" not in result
        assert "Body." in result

    def test_substantive_headings_with_and_or_qualifier_preserved(
        self, generator
    ):
        for heading in (
            "### Sources and Methods",
            "### Sources of Bias in the Sampling Frame",
            "## Reference Architecture",
            "### Citation Network Analysis",
            "### Resources and Logistics",
            "### Further Reading and Next Steps",
        ):
            content = f"Intro paragraph.\n\n{heading}\nBody content.\n"
            result = self._strip(generator, content, name="Analysis")
            assert heading in result
            assert "Body content." in result

    def test_code_fence_variants_and_indented_code_preserved(self, generator):
        # 4-backtick fence wrapping 3-backticks
        content_backtick = (
            "Intro.\n\n````markdown\n```\n## Sources\n```\n````\n\nOutro.\n"
        )
        res_bt = self._strip(generator, content_backtick, name="Code")
        assert "## Sources" in res_bt
        assert "Outro." in res_bt

        # Tilde fence ~~~
        content_tilde = "Intro.\n\n~~~\n## Sources\n~~~\n\nOutro.\n"
        res_tilde = self._strip(generator, content_tilde, name="Code")
        assert "## Sources" in res_tilde
        assert "Outro." in res_tilde

        # Indented code block (4 spaces)
        content_indented = (
            "Intro.\n\n    ## Sources\n    def foo(): pass\n\nOutro.\n"
        )
        res_ind = self._strip(generator, content_indented, name="Code")
        assert "## Sources" in res_ind
        assert "Outro." in res_ind

    def test_mixed_thematic_break_and_bib_removal(self, generator):
        content = (
            "Paragraph 1.\n\n"
            "---\n\n"
            "Paragraph 2.\n\n"
            "---\n\n"
            "## Sources\n"
            "- Source 1\n"
            "- Source 2\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "Paragraph 1." in result
        assert "Paragraph 2." in result
        assert "---" in result  # Legitimate thematic break kept
        assert "## Sources" not in result
        assert "- Source 1" not in result

    def test_asterisk_italic_purpose_and_quote_preservation(self, generator):
        # Epigraph / quote should be preserved
        content_quote = "_To be, or not to be._\n\nProse paragraph.\n"
        res_quote = self._strip(
            generator, content_quote, name="Hamlet", purpose="Analyze play"
        )
        assert "_To be, or not to be._" in res_quote
        assert "Prose paragraph." in res_quote

        # Asterisk italic purpose matching purpose should be stripped
        content_asterisk = "*To analyze play.* \n\nProse paragraph.\n"
        res_ast = self._strip(
            generator,
            content_asterisk,
            name="Hamlet",
            purpose="To analyze play.",
        )
        assert "*To analyze play.*" not in res_ast
        assert "Prose paragraph." in res_ast

        # Asterisk explicit purpose should be stripped
        content_exp = "*Purpose: Overview of system.*\n\nProse paragraph.\n"
        res_exp = self._strip(generator, content_exp, name="Hamlet")
        assert "*Purpose: Overview of system.*" not in res_exp
        assert "Prose paragraph." in res_exp

    def test_bibliography_heading_with_prose_containing_word_bibliography_preserved(
        self, generator
    ):
        # S1 regression test: ### Bibliography followed by prose about bibliographic history must NOT be deleted.
        content = (
            "### Bibliography\n"
            "The bibliography of this era is particularly rich with primary historical records.\n"
            "Scholars have long examined these records to document changes in policy.\n"
        )
        result = self._strip(generator, content, name="Historical Overview")
        assert "### Bibliography" in result
        assert "The bibliography of this era is particularly rich" in result

    def test_sources_heading_with_prose_body_preserved(self, generator):
        # S3 lockdown test: ## Sources followed by substantive prose must be preserved by body-shape safeguard.
        content = (
            "## Sources\n"
            "This subsection analyzes the primary sources of macroeconomic instability in the region.\n"
            "Inflation and currency devaluation remain central concerns.\n"
        )
        result = self._strip(generator, content, name="Macroeconomic Analysis")
        assert "## Sources" in result
        assert "primary sources of macroeconomic instability" in result

    def test_unrelated_epigraph_with_verb_prefix_preserved(self, generator):
        # S2 regression test: _To review the evidence is to understand the past._ preserved when purpose is unrelated.
        content = (
            "_To review the evidence is to understand the past._\n\n"
            "Prose body of the analysis.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Quantum Computing",
            purpose="Analyze quantum computing advancements in hardware",
        )
        assert "_To review the evidence is to understand the past._" in result
        assert "Prose body of the analysis." in result

    def test_matching_verb_prefix_italic_purpose_stripped(self, generator):
        # S2: Italic line starting with verb prefix that shares content words with purpose is stripped.
        content = (
            "_To review quantum computing advancements in hardware._\n\n"
            "Prose body of the analysis.\n"
        )
        result = self._strip(
            generator,
            content,
            name="Quantum Computing",
            purpose="To review quantum computing advancements in hardware",
        )
        assert (
            "_To review quantum computing advancements in hardware._"
            not in result
        )
        assert "Prose body of the analysis." in result

    def test_warning_demoted_to_info_when_truncation_is_bibliography_only(
        self, generator, loguru_caplog
    ):
        # When an unwanted bibliography accounts for >50% reduction,
        # warning is demoted to INFO (expected cleanup, not an over-strip).
        prose = "Substantive analysis prose for this subsection.\n" * 20
        bib = "\n\n## Sources\n" + "\n".join(
            f"[{i}] https://example.com/source/{i}" for i in range(1, 100)
        )
        content = prose + bib
        assert len(content) > 3000

        with loguru_caplog.at_level("INFO"):
            result = self._strip(
                generator,
                content,
                name="Deep Dive Subsection",
                section="Main Section",
            )

        assert "## Sources" not in result
        assert "Substantive analysis prose" in result
        assert len(result) < 0.5 * len(content)
        # Verify warning was demoted to info
        assert (
            "Stripped embedded bibliography from subsection"
            in loguru_caplog.text
        )
        assert (
            "(expected cleanup; warning demoted to INFO)" in loguru_caplog.text
        )
        assert (
            "Stripped subsection boilerplate heavily truncated content"
            not in loguru_caplog.text
        )

    def test_warning_preserved_when_heavy_truncation_is_not_bibliography(
        self, generator, loguru_caplog
    ):
        # Heavy truncation (>50%) driven by non-bibliography removal must still WARN.
        long_heading = "### " + "A" * 200 + "\n\n"
        short_prose = "Short prose."
        content = long_heading + short_prose

        with loguru_caplog.at_level("WARNING"):
            result = self._strip(
                generator,
                content,
                name="A" * 200,
                section="Main Section",
            )

        assert result.strip() == short_prose
        assert len(result) < 0.5 * len(content)
        assert (
            "Stripped subsection boilerplate heavily truncated content"
            in loguru_caplog.text
        )

    def test_warning_preserved_when_content_emptied_by_strip(
        self, generator, loguru_caplog
    ):
        # If content is completely emptied, it must trigger the emptied warning.
        content = "## Sources\n" + "\n".join(
            f"[{i}] https://example.com/ref/{i}" for i in range(1, 5)
        )

        with loguru_caplog.at_level("WARNING"):
            result = self._strip(
                generator,
                content,
                name="Empty Subsection",
                section="Main Section",
            )

        assert result == ""
        assert (
            "Stripped subsection boilerplate emptied non-trivial content"
            in loguru_caplog.text
        )

    def test_production_entry_preserves_reference_style_link_definitions(
        self, generator
    ):
        content = (
            "Analysis prose of subsection [1] and further details [2].\n\n"
            "[1]: https://arxiv.org/abs/1234.5678\n"
            "[2]: https://arxiv.org/abs/8765.4321\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "[1]: https://arxiv.org/abs/1234.5678" in result
        assert "[2]: https://arxiv.org/abs/8765.4321" in result

    def test_production_entry_preserves_trailing_further_reading_urls(
        self, generator
    ):
        content = (
            "Analysis prose of subsection.\n\n"
            "To go deeper, read:\n"
            "https://example.com/deep1\n"
            "https://example.com/deep2\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "https://example.com/deep1" in result
        assert "https://example.com/deep2" in result

    def test_production_entry_preserves_single_url_under_horizontal_rule(
        self, generator
    ):
        content = (
            "Agrarian structure analysis.\n\n"
            "---\n\n"
            "https://example.com/source\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "https://example.com/source" in result
        assert "---" in result

    def test_production_entry_preserves_sources_colon_with_numbered_list(
        self, generator
    ):
        content = (
            "Sources:\n"
            "1. Clone the repository.\n"
            "2. Run make deps.\n"
            "3. Verify the checksums.\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "1. Clone the repository." in result
        assert "2. Run make deps." in result
        assert "3. Verify the checksums." in result

    def test_production_entry_preserves_bold_sources_with_prose_and_list(
        self, generator
    ):
        content = (
            "**Sources**\n\n"
            "This section discusses the sources of variation.\n\n"
            "1. Environmental factors.\n"
            "2. Genetic predispositions.\n"
            "3. Measurement error.\n\n"
            "Conclusion prose follows.\n"
        )
        result = self._strip(generator, content, name="Section")
        assert "**Sources**" in result
        assert "This section discusses the sources of variation." in result
        assert "1. Environmental factors." in result
        assert "Conclusion prose follows." in result

    def test_production_entry_preserves_cjk_footnotes_quiz_and_numbered_links(
        self, generator
    ):
        cjk_content = "Analysis prose.\n\n【1】 第一条注释\n【2】 第二条注释\n"
        assert "【1】 第一条注释" in self._strip(
            generator, cjk_content, name="Section"
        )

        quiz_content = "Analysis prose.\n\n[1] true\n[2] false\n"
        assert "[1] true" in self._strip(
            generator, quiz_content, name="Section"
        )

        links_content = "Analysis prose.\n\n[1](https://example.com/1) [2](https://example.com/2)\n"
        assert "[1](https://example.com/1)" in self._strip(
            generator, links_content, name="Section"
        )


# ── _strip_embedded_bibliographies ──


class TestStripEmbeddedBibliographies:
    """Tests for _strip_embedded_bibliographies."""

    BIB = (
        "## Sources\n"
        "[1] https://example.org/one\n"
        "[2] https://example.org/two\n"
        "[3] https://example.org/three\n"
    )

    def _strip(self, generator, content):
        return generator._strip_embedded_bibliographies(content)

    def test_removes_bibliography_at_end_of_content(self, generator):
        result = self._strip(generator, "Analysis prose.\n\n" + self.BIB)
        assert "## Sources" not in result
        assert "https://example.org/one" not in result
        assert result.strip() == "Analysis prose."

    def test_keeps_next_section_when_heading_follows(self, generator):
        content = "Analysis prose.\n\n" + self.BIB + "\n## Conclusion\nDone.\n"
        result = self._strip(generator, content)
        assert "## Sources" not in result
        assert "## Conclusion\nDone." in result

    def test_preserves_trailing_table(self, generator):
        table = "| Date | Event |\n|------|-------|\n| 200 CE | Compiled |\n"
        result = self._strip(
            generator, "Analysis prose.\n\n" + self.BIB + "\n" + table
        )
        assert "## Sources" not in result
        assert table in result

    def test_preserves_trailing_paragraph(self, generator):
        para = "The text survives in multiple recensions.\n"
        result = self._strip(
            generator, "Analysis prose.\n\n" + self.BIB + "\n" + para
        )
        assert "## Sources" not in result
        assert para in result

    def test_preserves_content_between_bibliography_and_next_heading(
        self, generator
    ):
        para = "The text survives in multiple recensions.\n"
        content = (
            "Analysis prose.\n\n"
            + self.BIB
            + "\n"
            + para
            + "\n## Conclusion\nDone.\n"
        )
        result = self._strip(generator, content)
        assert "## Sources" not in result
        assert para in result
        assert "## Conclusion\nDone." in result

    def test_removes_decorative_rule_trailing_the_citations(self, generator):
        para = "The text survives in multiple recensions.\n"
        result = self._strip(
            generator, "Analysis prose.\n\n" + self.BIB + "\n---\n\n" + para
        )
        assert "## Sources" not in result
        assert "\n---\n" not in result
        assert para in result

    def test_removes_bibliography_with_blank_separated_citations(
        self, generator
    ):
        content = (
            "Analysis prose.\n\n"
            "## Sources\n"
            "[1] https://example.org/one\n\n"
            "[2] https://example.org/two\n"
        )
        result = self._strip(generator, content)
        assert "https://example.org/two" not in result
        assert result.strip() == "Analysis prose."

    def test_removes_bibliography_introduced_by_a_lead_in_line(self, generator):
        content = (
            "Analysis prose.\n\n"
            "## References\n"
            "The following sources were consulted:\n"
            "[1] https://example.org/one\n"
            "[2] https://example.org/two\n"
        )
        result = self._strip(generator, content)
        assert "The following sources were consulted" not in result
        assert "https://example.org/two" not in result

    def test_leaves_substantive_heading_with_prose_body(self, generator):
        content = (
            "Analysis prose.\n\n"
            "## Sources of Bias\n"
            "The survey design introduced selection bias throughout.\n"
        )
        result = self._strip(generator, content)
        assert "## Sources of Bias" in result
        assert "selection bias" in result

    def test_preserves_trailing_prose_when_the_note_line_is_far_down(
        self, generator
    ):
        lead_in = (
            "The passage below records the provenance of every claim. " * 7
        )
        para = "The text survives in multiple recensions.\n"
        content = (
            "Analysis prose.\n\n"
            "## Sources\n" + lead_in.rstrip() + "\n"
            "*(Note: the bibliography above lists every source consulted.)*\n"
            "\n" + para
        )
        assert len(lead_in.rstrip()) >= 300
        result = self._strip(generator, content)
        assert "## Sources" not in result
        assert "every source consulted" not in result
        assert para in result

    def test_strips_bibliography_with_trailing_code_fence(self, generator):
        content = (
            "Analysis prose.\n\n"
            "## Sources\n"
            "[1] https://example.org/one\n"
            "[2] https://example.org/two\n\n"
            "```python\n"
            "print('trailing code')\n"
            "```\n"
        )

        result = self._strip(generator, content)

        assert "## Sources" not in result
        assert "https://example.org/one" not in result
        assert "```python\nprint('trailing code')\n```" in result

    def test_subsection_guidance_omits_literal_source_heading(self):
        from local_deep_research.report_generator import (
            _SUBSECTION_OUTPUT_GUIDANCE,
        )

        assert "## Sources" not in _SUBSECTION_OUTPUT_GUIDANCE
        assert "'## Sources'" not in _SUBSECTION_OUTPUT_GUIDANCE

    def test_strips_empty_heading_with_bold_sources_label(self, generator):
        content = (
            "Analysis prose of subsection.\n\n"
            "###\n"
            "**Sources:**\n"
            "[[34]](/library/document/1/chunks#chunk-52) First source title.\n"
            "[[35]](/library/document/2/chunks#chunk-45) Second source title.\n"
        )
        result = self._strip(generator, content)
        assert "**Sources:**" not in result
        assert "###" not in result
        assert "[[34]]" not in result
        assert "First source title" not in result
        assert result.strip() == "Analysis prose of subsection."

    def test_strips_pseudo_heading_sources_label(self, generator):
        content = (
            "Analysis prose of subsection.\n\n"
            "**Sources:**\n"
            "[1] First source title.\n"
            "[2] Second source title.\n"
        )
        result = self._strip(generator, content)
        assert "**Sources:**" not in result
        assert "[1] First source" not in result
        assert result.strip() == "Analysis prose of subsection."

    def test_strips_unheaded_trailing_citation_list(self, generator):
        content = (
            "Manusmriti analysis concludes here [110].\n\n"
            "[[111]](/library/doc/1) Sastra commemoration volume.\n"
            "[[112]](/library/doc/2) Hopkins and Bhandarkar on Manu origin.\n"
            "[[113]](/library/doc/3) Constitutional development.\n"
        )
        result = self._strip(generator, content)
        assert "Sastra commemoration volume" not in result
        assert "Hopkins and Bhandarkar" not in result
        assert result.strip() == "Manusmriti analysis concludes here [110]."

    def test_strips_unheaded_trailing_concatenated_citations(self, generator):
        content = (
            "Mughal administrative layers concluded here [216].\n\n"
            "[[217]](/library/doc/1)[[218]](/library/doc/2)[[219]](/library/doc/3)"
        )
        result = self._strip(generator, content)
        assert "[[217]]" not in result
        assert "[[218]]" not in result
        assert (
            result.strip()
            == "Mughal administrative layers concluded here [216]."
        )

    def test_concatenated_citations_line_re_redos_resistant(self):
        from local_deep_research.report_generator import (
            _CONCATENATED_CITATIONS_LINE_RE,
        )

        # Pathological string designed to trigger exponential backtracking in vulnerable regexes
        evil_string = "\u30100\u3011" + ("\t\u30100\u3011" * 50) + "X"
        assert not _CONCATENATED_CITATIONS_LINE_RE.match(evil_string)

        valid_line = "[[1]](/a), [2], [[3]], \u30104\u3011"
        assert _CONCATENATED_CITATIONS_LINE_RE.match(valid_line)

    def test_preserves_single_citation_preceded_by_bare_horizontal_rule(
        self, generator
    ):
        content = (
            "Agrarian structure analysis.\n\n"
            "---\n\n"
            "[1] https://example.com/source\n"
        )
        result = self._strip(generator, content)
        assert "https://example.com/source" in result
        assert "---" in result
        assert result.strip() == content.strip()

    def test_preserves_reference_style_link_definitions(self, generator):
        content = (
            "Analysis prose of subsection [1] and further details [2].\n\n"
            "[1]: https://arxiv.org/abs/1234.5678\n"
            "[2]: https://arxiv.org/abs/8765.4321\n"
        )
        result = self._strip(generator, content)
        assert "[1]: https://arxiv.org/abs/1234.5678" in result
        assert "[2]: https://arxiv.org/abs/8765.4321" in result
        assert result.strip() == content.strip()

    def test_preserves_trailing_further_reading_urls(self, generator):
        content = (
            "Analysis prose of subsection.\n\n"
            "To go deeper, read:\n"
            "https://example.com/deep1\n"
            "https://example.com/deep2\n"
        )
        result = self._strip(generator, content)
        assert "https://example.com/deep1" in result
        assert "https://example.com/deep2" in result
        assert result.strip() == content.strip()

    def test_preserves_single_url_under_horizontal_rule(self, generator):
        content = (
            "Agrarian structure analysis.\n\n"
            "---\n\n"
            "https://example.com/source\n"
        )
        result = self._strip(generator, content)
        assert "https://example.com/source" in result
        assert "---" in result
        assert result.strip() == content.strip()

    def test_preserves_sources_colon_followed_by_ordinary_numbered_list(
        self, generator
    ):
        content = (
            "Sources:\n"
            "1. Clone the repository.\n"
            "2. Run make deps.\n"
            "3. Verify the checksums.\n"
        )
        result = self._strip(generator, content)
        assert "1. Clone the repository." in result
        assert "2. Run make deps." in result
        assert "3. Verify the checksums." in result
        assert result.strip() == content.strip()

    def test_preserves_bold_sources_label_with_intervening_prose_and_list(
        self, generator
    ):
        content = (
            "**Sources**\n\n"
            "This section discusses the sources of variation.\n\n"
            "1. Environmental factors.\n"
            "2. Genetic predispositions.\n"
            "3. Measurement error.\n\n"
            "Conclusion prose follows.\n"
        )
        result = self._strip(generator, content)
        assert "**Sources**" in result
        assert "This section discusses the sources of variation." in result
        assert "1. Environmental factors." in result
        assert "Conclusion prose follows." in result

    def test_preserves_cjk_footnote_glosses(self, generator):
        content = "Analysis prose.\n\n【1】 第一条注释\n【2】 第二条注释\n"
        result = self._strip(generator, content)
        assert "【1】 第一条注释" in result
        assert "【2】 第二条注释" in result

    def test_preserves_quiz_answer_keys(self, generator):
        content = "Analysis prose.\n\n[1] true\n[2] false\n"
        result = self._strip(generator, content)
        assert "[1] true" in result
        assert "[2] false" in result

    def test_preserves_numbered_markdown_link_lines(self, generator):
        content = (
            "Analysis prose.\n\n"
            "[1](https://example.com/1) [2](https://example.com/2)\n"
        )
        result = self._strip(generator, content)
        assert "[1](https://example.com/1)" in result
        assert "[2](https://example.com/2)" in result

    def test_preserves_code_fences_with_delimiters_in_embedded_bib_scan(
        self, generator
    ):
        content = (
            "Analysis prose.\n\n"
            "```markdown\n"
            "---\n"
            "### Example Heading\n"
            "```\n\n"
            "## Sources\n"
            "[1] https://example.org/one\n"
            "[2] https://example.org/two\n"
        )
        result = self._strip(generator, content)
        assert "```markdown\n---\n### Example Heading\n```" in result
        assert "## Sources" not in result
        assert "https://example.org/one" not in result

    def test_strips_unheaded_trailing_citations_with_wrapped_last_entry(
        self, generator
    ):
        content = (
            "Manusmriti analysis concludes here [110].\n\n"
            "[[111]](/library/doc/1) Sastra commemoration volume.\n"
            "[[112]](/library/doc/2) Hopkins and Bhandarkar on Manu origin.\n"
            "[[113]](/library/doc/3) Constitutional development of the early\n"
            "    classical period in northern India.\n"
        )
        result = self._strip(generator, content)
        assert "[[111]]" not in result
        assert "[[112]]" not in result
        assert "[[113]]" not in result
        assert "classical period in northern India" not in result
        assert result.strip() == "Manusmriti analysis concludes here [110]."

    def test_preserves_substantive_table_with_citations(self, generator):
        table = (
            "| Aspect | Observations | Supporting Sources |\n"
            "|---|---|---|\n"
            "| Land Revenue | Peasant class formation | [[162]] Primary survey |\n"
        )
        content = "Analysis prose.\n\n" + table
        result = self._strip(generator, content)
        assert "Peasant class formation" in result
        assert "[[162]] Primary survey" in result

    def test_preserves_prose_ending_with_inline_citation(self, generator):
        content = "This historical transformation is well documented [[115]]."
        result = self._strip(generator, content)
        assert (
            result.strip()
            == "This historical transformation is well documented [[115]]."
        )

    def test_preserves_trailing_code_block_with_brackets(self, generator):
        content = (
            "Here is the data structure:\n\n```python\n[100, 200, 300]\n```"
        )
        result = self._strip(generator, content)
        assert "[100, 200, 300]" in result

    def test_strips_empty_heading_separated_by_blank_lines_from_sources_label(
        self, generator
    ):
        content = (
            "Analysis prose of subsection.\n\n"
            "###\n\n"
            "**Sources:**\n"
            "[[34]](/library/document/1) First source title.\n"
            "[[35]](/library/document/2) Second source title.\n"
        )
        result = self._strip(generator, content)
        assert "**Sources:**" not in result
        assert "###" not in result
        assert "[[34]]" not in result
        assert result.strip() == "Analysis prose of subsection."

    def test_strips_trailing_citations_with_sources_colon_label(
        self, generator
    ):
        content = (
            "Analysis prose of subsection.\n\n"
            "Sources:\n"
            "[1] https://example.com/one\n"
        )
        result = self._strip(generator, content)
        assert "Sources:" not in result
        assert "https://example.com/one" not in result
        assert result.strip() == "Analysis prose of subsection."

    def test_preserves_numbered_list_in_conclusion(self, generator):
        content = (
            "The system had three key characteristics:\n"
            "1. Endogamous social units\n"
            "2. Hereditary occupational specialization\n"
            "3. Hierarchical ritual status\n"
        )
        result = self._strip(generator, content)
        assert "1. Endogamous social units" in result
        assert "2. Hereditary occupational specialization" in result
        assert "3. Hierarchical ritual status" in result