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.

56.5 KB
Raw Blame
"""Tests for the extraction pipeline orchestration logic.

Covers: extract_content, extract_content_with_metadata, _quality_score,
_count_boilerplate, _try_specialized_downloader, fetch_and_extract,
batch_fetch_and_extract.

All extractors are monkeypatched — no network, no DB, no optional deps.
"""

from unittest.mock import Mock
from typing import NamedTuple

import pytest

from local_deep_research.research_library.downloaders.extraction import pipeline
from local_deep_research.research_library.downloaders.base import (
    BaseDownloader,
    ContentType,
    DownloadResult,
)


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _html(body: str, head: str = "") -> str:
    return f"<html><head>{head}</head><body>{body}</body></html>"


class _SpecializedState(NamedTuple):
    content: str | None
    fallback_allowed: bool
    skip_reason: str | None = None


def _specialized(
    content: str | None = None,
    fallback_allowed: bool = True,
    skip_reason: str | None = None,
) -> _SpecializedState:
    return _SpecializedState(content, fallback_allowed, skip_reason)


LONG_TEXT = "A " * 600  # 1200 chars — above METADATA_ENRICHMENT_THRESHOLD
SHORT_TEXT = "Short content that passes min length but is under enrichment threshold okay."
TINY_TEXT = "tiny"


# ---------------------------------------------------------------------------
# _count_boilerplate / _quality_score
# ---------------------------------------------------------------------------


class TestQualityScoring:
    def test_count_boilerplate_empty(self):
        assert pipeline._count_boilerplate("") == 0
        assert pipeline._count_boilerplate(None) == 0

    def test_count_boilerplate_no_keywords(self):
        assert pipeline._count_boilerplate("This is clean content.") == 0

    def test_count_boilerplate_with_keywords(self):
        text = "Accept all cookies. Read our privacy policy and newsletter."
        count = pipeline._count_boilerplate(text)
        assert (
            count >= 3
        )  # "cookie", "accept all", "privacy policy", "newsletter"

    def test_quality_score_empty(self):
        assert pipeline._quality_score("") == 0
        assert pipeline._quality_score(None) == 0

    def test_quality_score_clean_text(self):
        text = "A" * 500
        assert pipeline._quality_score(text) == 500

    def test_quality_score_penalizes_boilerplate(self):
        text = "cookie " * 100  # 700 chars, 1 keyword
        score = pipeline._quality_score(text)
        assert score == len(text) - pipeline.BOILERPLATE_PENALTY


# ---------------------------------------------------------------------------
# _run_extractors_parallel
# ---------------------------------------------------------------------------


class TestRunExtractorsParallel:
    def test_both_succeed(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: "traf result"
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": "np result"
        )
        t, n = pipeline._run_extractors_parallel("<html></html>", "")
        assert t == "traf result"
        assert n == "np result"

    def test_trafilatura_exception(self, monkeypatch):
        def boom(html):
            raise RuntimeError("boom")

        monkeypatch.setattr(pipeline._trafilatura, "extract", boom)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": "np ok"
        )
        t, n = pipeline._run_extractors_parallel("<html></html>", "")
        assert t is None
        assert n == "np ok"

    def test_newspaper_exception(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: "traf ok"
        )

        def boom(html, url=""):
            raise RuntimeError("boom")

        monkeypatch.setattr(pipeline._newspaper, "extract", boom)
        t, n = pipeline._run_extractors_parallel("<html></html>", "")
        assert t == "traf ok"
        assert n is None

    def test_both_fail(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura,
            "extract",
            lambda html: (_ for _ in ()).throw(RuntimeError()),
        )
        monkeypatch.setattr(
            pipeline._newspaper,
            "extract",
            lambda html, url="": (_ for _ in ()).throw(RuntimeError()),
        )

        # Use proper callables that raise
        def traf_boom(html):
            raise RuntimeError

        def np_boom(html, url=""):
            raise RuntimeError

        monkeypatch.setattr(pipeline._trafilatura, "extract", traf_boom)
        monkeypatch.setattr(pipeline._newspaper, "extract", np_boom)
        t, n = pipeline._run_extractors_parallel("<html></html>", "")
        assert t is None
        assert n is None


# ---------------------------------------------------------------------------
# extract_content — primary path (trafilatura vs newspaper4k selection)
# ---------------------------------------------------------------------------


class TestExtractContentPrimaryPath:
    def test_empty_html_returns_none(self):
        assert pipeline.extract_content("") is None
        assert pipeline.extract_content("   ") is None
        assert pipeline.extract_content(None) is None

    def test_trafilatura_wins_when_higher_quality(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: LONG_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": SHORT_TEXT
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result is not None
        assert result.startswith("A ")

    def test_newspaper_wins_when_higher_quality(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: SHORT_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": LONG_TEXT
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result == LONG_TEXT

    def test_newspaper_wins_when_trafilatura_none(self, monkeypatch):
        monkeypatch.setattr(pipeline._trafilatura, "extract", lambda html: None)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": LONG_TEXT
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result == LONG_TEXT

    def test_trafilatura_fallback_when_newspaper_none(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: LONG_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result.startswith("A ")


# ---------------------------------------------------------------------------
# extract_content — fallback cascade (readability → justext → get_text)
# ---------------------------------------------------------------------------


class TestExtractContentFallbackCascade:
    def _stub_primary_extractors_fail(self, monkeypatch):
        monkeypatch.setattr(pipeline._trafilatura, "extract", lambda html: None)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )

    def test_readability_justext_cascade(self, monkeypatch):
        self._stub_primary_extractors_fail(monkeypatch)
        readability_html = f"<div>{LONG_TEXT}</div>"
        monkeypatch.setattr(
            pipeline._readability, "extract", lambda html: readability_html
        )
        monkeypatch.setattr(
            pipeline._justext_en, "extract", lambda html: LONG_TEXT
        )
        result = pipeline.extract_content(_html(f"<p>{LONG_TEXT}</p>"))
        assert result is not None
        assert len(result) >= pipeline.MIN_CONTENT_LENGTH

    def test_readability_only(self, monkeypatch):
        self._stub_primary_extractors_fail(monkeypatch)
        monkeypatch.setattr(
            pipeline._readability, "extract", lambda html: f"<p>{LONG_TEXT}</p>"
        )
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        result = pipeline.extract_content(_html(f"<p>{LONG_TEXT}</p>"))
        assert result is not None

    def test_justext_skipped_when_discards_too_much(self, monkeypatch):
        """Safety discard ratio: if justext output is <20% of readability, skip it."""
        self._stub_primary_extractors_fail(monkeypatch)
        big_content = "X " * 500  # 1000 chars
        tiny_content = "Y"  # 1 char — way below 20%
        monkeypatch.setattr(
            pipeline._readability, "extract", lambda html: big_content
        )
        monkeypatch.setattr(
            pipeline._justext_en, "extract", lambda html: tiny_content
        )
        result = pipeline.extract_content(_html(f"<p>{big_content}</p>"))
        # Should keep readability result, not the tiny justext one
        assert result is not None
        assert "X" in result

    def test_last_resort_get_text(self, monkeypatch):
        self._stub_primary_extractors_fail(monkeypatch)
        monkeypatch.setattr(pipeline._readability, "extract", lambda html: None)
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        body_text = "A " * 100  # 200 chars, above min_length
        result = pipeline.extract_content(_html(f"<p>{body_text}</p>"))
        assert result is not None
        assert "A" in result

    def test_all_fail_below_min_length(self, monkeypatch):
        self._stub_primary_extractors_fail(monkeypatch)
        monkeypatch.setattr(pipeline._readability, "extract", lambda html: None)
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        result = pipeline.extract_content(_html("<p>hi</p>"))
        assert result is None

    def test_non_english_creates_new_justext(self, monkeypatch):
        """Non-English language should create a new JustextExtractor."""
        self._stub_primary_extractors_fail(monkeypatch)
        monkeypatch.setattr(pipeline._readability, "extract", lambda html: None)

        from local_deep_research.research_library.downloaders.extraction.justext_extractor import (
            JustextExtractor,
        )

        created = []
        original_init = JustextExtractor.__init__

        def tracking_init(self, language="English"):
            created.append(language)
            original_init(self, language)
            self.extract = lambda html: LONG_TEXT

        monkeypatch.setattr(JustextExtractor, "__init__", tracking_init)
        pipeline.extract_content(
            _html(f"<p>{LONG_TEXT}</p>"), language="German"
        )
        assert "German" in created

    def test_strips_html_from_readability_only_result(self, monkeypatch):
        """When only readability succeeds, HTML tags should be stripped."""
        self._stub_primary_extractors_fail(monkeypatch)
        html_content = f"<p>{LONG_TEXT}</p><div>more content here</div>"
        monkeypatch.setattr(
            pipeline._readability, "extract", lambda html: html_content
        )
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        result = pipeline.extract_content(_html(f"<p>{LONG_TEXT}</p>"))
        assert result is not None
        assert "<p>" not in result
        assert "<div>" not in result

    def test_script_tags_removed_in_fallback(self, monkeypatch):
        self._stub_primary_extractors_fail(monkeypatch)
        monkeypatch.setattr(pipeline._readability, "extract", lambda html: None)
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        body = f"<p>{'A ' * 100}</p><script>alert('xss')</script>"
        result = pipeline.extract_content(_html(body))
        assert result is not None
        assert "alert" not in result


# ---------------------------------------------------------------------------
# extract_content — metadata enrichment
# ---------------------------------------------------------------------------


class TestMetadataEnrichment:
    def test_enriches_thin_content(self, monkeypatch):
        thin = "Short text. " * 5  # ~60 chars, below 1000 threshold
        monkeypatch.setattr(pipeline._trafilatura, "extract", lambda html: thin)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        monkeypatch.setattr(
            pipeline,
            "extract_metadata",
            lambda html: {
                "json_ld": [{"@type": "Product", "name": "Widget"}],
                "opengraph": [],
                "microdata": [],
            },
        )
        monkeypatch.setattr(
            pipeline, "metadata_to_text", lambda meta: "Product: Widget"
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert "Widget" in result

    def test_no_enrichment_for_long_content(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: LONG_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        called = []
        monkeypatch.setattr(
            pipeline, "extract_metadata", lambda html: called.append(1) or {}
        )
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result is not None
        assert len(called) == 0

    def test_no_enrichment_when_metadata_empty(self, monkeypatch):
        thin = "Short text. " * 5
        monkeypatch.setattr(pipeline._trafilatura, "extract", lambda html: thin)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        monkeypatch.setattr(
            pipeline,
            "extract_metadata",
            lambda html: {"json_ld": [], "opengraph": [], "microdata": []},
        )
        monkeypatch.setattr(pipeline, "metadata_to_text", lambda meta: None)
        result = pipeline.extract_content(_html("<p>test</p>"))
        assert result is not None
        # Should still return the thin content without enrichment
        assert "Short text" in result


# ---------------------------------------------------------------------------
# extract_content_with_metadata
# ---------------------------------------------------------------------------


class TestExtractContentWithMetadata:
    def test_empty_html(self):
        assert pipeline.extract_content_with_metadata("") is None
        assert pipeline.extract_content_with_metadata("  ") is None

    def test_extracts_title_and_description(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: LONG_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        html = _html(
            f"<p>{LONG_TEXT}</p>",
            head='<title>My Title</title><meta name="description" content="My Desc">',
        )
        result = pipeline.extract_content_with_metadata(html)
        assert result is not None
        assert result["title"] == "My Title"
        assert result["description"] == "My Desc"
        assert result["content"] is not None

    def test_og_tags_override_standard(self, monkeypatch):
        monkeypatch.setattr(
            pipeline._trafilatura, "extract", lambda html: LONG_TEXT
        )
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        html = _html(
            f"<p>{LONG_TEXT}</p>",
            head=(
                "<title>Standard Title</title>"
                '<meta property="og:title" content="OG Title">'
                '<meta name="description" content="Std Desc">'
                '<meta property="og:description" content="OG Desc">'
            ),
        )
        result = pipeline.extract_content_with_metadata(html)
        assert result["title"] == "OG Title"
        assert result["description"] == "OG Desc"

    def test_returns_none_when_content_extraction_fails(self, monkeypatch):
        monkeypatch.setattr(pipeline._trafilatura, "extract", lambda html: None)
        monkeypatch.setattr(
            pipeline._newspaper, "extract", lambda html, url="": None
        )
        monkeypatch.setattr(pipeline._readability, "extract", lambda html: None)
        monkeypatch.setattr(pipeline._justext_en, "extract", lambda html: None)
        result = pipeline.extract_content_with_metadata(
            "<html><body>x</body></html>"
        )
        assert result is None


# ---------------------------------------------------------------------------
# _try_specialized_downloader
# ---------------------------------------------------------------------------


class TestTrySpecializedDownloader:
    def test_non_academic_url_allows_fallback(self):
        result = pipeline._try_specialized_downloader(
            "https://example.com/page"
        )
        assert result.content is None
        assert result.fallback_allowed is True

    def test_trailing_dot_ar5iv_lookalike_allows_fallback(self):
        result = pipeline._try_specialized_downloader(
            "https://ar5iv.org.example.com./2301.12345"
        )
        assert result.content is None
        assert result.fallback_allowed is True

    @pytest.mark.parametrize(
        "url",
        (
            "https://arxiv.org/about",
            "https://arxiv.org/help",
            "https://arxiv.org/list/cs.AI",
            "https://arxiv.org/blog",
        ),
    )
    def test_arxiv_non_paper_pages_allow_generic_fallback(self, url):
        result = pipeline._try_specialized_downloader(url)

        assert result.content is None
        assert result.fallback_allowed is True

    @pytest.mark.parametrize(
        "url",
        (
            "https://ARXIV.ORG./not-an-arxiv-id",
            "https://EXPORT.ARXIV.ORG./not-an-arxiv-id",
            "https://arxiv.org/abs/not-an-id",
            "https://arxiv.org/list/cs.AI/recent",
            "https://info.arxiv.org/help/api/tou.html",
            "https://arxiv.org/ftp/arxiv/papers/2301/2301.12345.pdf",
        ),
    )
    def test_arxiv_host_without_an_identifier_allows_fallback(self, url):
        # Given an owned arXiv host whose path names no paper
        # When specialization is attempted
        result = pipeline._try_specialized_downloader(url)

        # Then ownership is not claimed, because the arXiv downloader has no
        # canonical source for these and terminal ownership would return
        # nothing where generic extraction returns the page
        assert result.content is None
        assert result.fallback_allowed is True

    def test_arxiv_url_routes_to_downloader(self, monkeypatch):
        # Given an exact dynamically imported BaseDownloader implementation
        instances = []

        class StubArxivDownloader(BaseDownloader):
            def __init__(self, timeout=30):
                self.timeout = timeout
                self.closed = False
                instances.append(self)

            def can_handle(self, url):
                return True

            def download(self, url, content_type=ContentType.TEXT):
                return b"Extracted arXiv paper text " * 10

            def download_with_result(self, url, content_type=ContentType.TEXT):
                return DownloadResult(
                    content=b"Extracted arXiv paper text " * 10,
                    is_success=True,
                )

            def close(self):
                self.closed = True

        import local_deep_research.research_library.downloaders.arxiv as arxiv_mod

        monkeypatch.setattr(arxiv_mod, "ArxivDownloader", StubArxivDownloader)

        # When the arXiv specialization is requested
        result = pipeline._try_specialized_downloader(
            "https://AR5IV.ORG./2301.00001v2"
        )

        # Then exact content is returned terminally and the downloader is closed
        assert result.content == "Extracted arXiv paper text " * 10
        assert result.fallback_allowed is False
        assert len(instances) == 1
        assert instances[0].closed is True

    def test_specialized_downloader_failure_returns_none(self, monkeypatch):
        class FailingArxivDownloader(BaseDownloader):
            def __init__(self, timeout=30):
                self.timeout = timeout

            def can_handle(self, url):
                return True

            def download(self, url, content_type=ContentType.TEXT):
                return None

            def download_with_result(self, url, content_type=ContentType.TEXT):
                return DownloadResult(skip_reason="unavailable")

            def close(self):
                return None

        import local_deep_research.research_library.downloaders.arxiv as arxiv_mod

        monkeypatch.setattr(
            arxiv_mod, "ArxivDownloader", FailingArxivDownloader
        )
        result = pipeline._try_specialized_downloader(
            "https://ar5iv.org/2301.00001v2"
        )
        assert result.content is None
        assert result.fallback_allowed is False
        assert result.skip_reason == "unavailable"

    def test_terminal_failure_logs_specialist_reason(self, monkeypatch):
        reason = "Invalid arXiv URL - could not extract article ID"
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(
                fallback_allowed=False, skip_reason=reason
            ),
        )

        warning = Mock()
        monkeypatch.setattr(pipeline.logger, "warning", warning)

        assert (
            pipeline.fetch_and_extract(
                "https://arxiv.org/abs/not-an-identifier"
            )
            is None
        )

        assert warning.call_args.args[-1] == reason

    def test_import_error_returns_none(self, monkeypatch):
        """If url_classifier can't be imported, returns None."""
        import sys

        monkeypatch.setitem(
            sys.modules,
            "local_deep_research.content_fetcher.url_classifier",
            None,
        )
        # This should trigger ImportError inside the function
        # But since the module is already imported, we need a different approach
        result = pipeline._try_specialized_downloader("https://example.com")
        assert result.content is None
        assert result.fallback_allowed is True

    @pytest.mark.parametrize(
        ("url", "fallback_allowed"),
        (
            ("https://AR5IV.ORG./2301.12345v2", False),
            ("https://ARXIV.ORG./abs/2301.12345", False),
            ("https://AR5IV.ORG./not-an-arxiv-id", True),
            ("https://ARXIV.ORG./not-an-arxiv-id", True),
        ),
    )
    def test_import_error_keeps_arxiv_paper_terminal(
        self, monkeypatch, url, fallback_allowed
    ):
        # Given the classifier import is unavailable
        import sys

        monkeypatch.setitem(
            sys.modules,
            "local_deep_research.content_fetcher.url_classifier",
            None,
        )

        # When the owned host reaches specialization
        result = pipeline._try_specialized_downloader(url)

        # Then the paper identifier, not the host, decides the fallback
        assert result.content is None
        assert result.fallback_allowed is fallback_allowed


# ---------------------------------------------------------------------------
# fetch_and_extract
# ---------------------------------------------------------------------------


class TestFetchAndExtract:
    def test_returns_specialized_when_available(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(
                "Specialized content from academic source", False
            ),
        )
        result = pipeline.fetch_and_extract("https://arxiv.org/abs/1234")
        assert result == "Specialized content from academic source"

    def test_falls_back_to_html_downloader(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )

        mock_downloader = Mock()
        mock_downloader.download.return_value = b"Downloaded HTML content text"
        mock_downloader.close = Mock()

        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        result = pipeline.fetch_and_extract("https://example.com")
        assert result == "Downloaded HTML content text"
        mock_downloader.close.assert_called_once()

    def test_returns_none_on_download_failure(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )

        mock_downloader = Mock()
        mock_downloader.download.return_value = None
        mock_downloader.close = Mock()

        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        result = pipeline.fetch_and_extract("https://example.com")
        assert result is None

    def test_returns_none_on_exception(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )

        mock_downloader = Mock()
        mock_downloader.download.side_effect = RuntimeError("network error")
        mock_downloader.close = Mock()

        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        result = pipeline.fetch_and_extract("https://example.com")
        assert result is None

    def test_arxiv_terminal_failure_does_not_use_html(self, monkeypatch):
        # Given terminal specialized failure for an ar5iv input
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(
                fallback_allowed=False
            ),
        )
        mock_class = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When the single-URL pipeline runs
        result = pipeline.fetch_and_extract("https://ar5iv.org/2301.12345v2")

        # Then failure is terminal without targeting the original host
        assert result is None
        mock_class.assert_not_called()

    @pytest.mark.parametrize(
        "url",
        (
            "https://ARXIV.ORG./not-an-arxiv-id",
            "https://arxiv.org/list/cs.AI/recent",
        ),
    )
    def test_arxiv_page_without_an_identifier_uses_html(self, monkeypatch, url):
        # Given a generic HTML downloader that returns page text
        instance = Mock()
        instance.download.return_value = b"generic page text " * 10
        mock_class = Mock(return_value=instance)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When an owned arXiv URL names no paper
        result = pipeline.fetch_and_extract(url)

        # Then the generic pipeline runs: these pages had text on main, and
        # the arXiv downloader has no source that could replace it
        assert result == "generic page text " * 10
        mock_class.assert_called_once()

    def test_arxiv_paper_failure_does_not_use_html(self, monkeypatch):
        # Given a real arXiv downloader that produces nothing, and a generic
        # HTML constructor tripwire
        class FailingArxivDownloader(BaseDownloader):
            def __init__(self, timeout=30):
                self.timeout = timeout

            def can_handle(self, url):
                return True

            def download(self, url, content_type=ContentType.TEXT):
                return None

            def download_with_result(self, url, content_type=ContentType.TEXT):
                return DownloadResult(
                    skip_reason="Could not retrieve full text"
                )

            def close(self):
                return None

        import local_deep_research.research_library.downloaders.arxiv as arxiv_mod

        monkeypatch.setattr(
            arxiv_mod, "ArxivDownloader", FailingArxivDownloader
        )
        mock_class = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When an owned arXiv paper URL fails specialization
        result = pipeline.fetch_and_extract("https://ar5iv.org/2301.12345v2")

        # Then specialization fails terminally before generic HTML construction
        assert result is None
        mock_class.assert_not_called()

    @pytest.mark.parametrize(
        "url",
        (
            "https://ar5iv.org/2301.12345v2",
            "https://ARXIV.ORG./abs/2301.12345",
        ),
    )
    def test_arxiv_paper_exception_does_not_use_html(self, monkeypatch, url):
        # Given the specialist raises before producing its terminal result
        def raise_specialized_error(url, timeout=30):
            raise RuntimeError("specialized failure")

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", raise_specialized_error
        )
        mock_class = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When the owned paper URL is processed
        result = pipeline.fetch_and_extract(url)

        # Then paper ownership keeps the exception path terminal
        assert result is None
        mock_class.assert_not_called()


# ---------------------------------------------------------------------------
# batch_fetch_and_extract
# ---------------------------------------------------------------------------


class TestBatchFetchAndExtract:
    def test_routes_specialized_and_generic(self, monkeypatch):
        def mock_specialized(url, timeout=30, **kwargs):
            if "arxiv" in url:
                return _specialized("arXiv paper content", False)
            return _specialized()

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", mock_specialized
        )

        mock_downloader = Mock()
        mock_downloader.download.return_value = b"Generic HTML content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        urls = ["https://arxiv.org/abs/1234", "https://example.com/page"]
        results = pipeline.batch_fetch_and_extract(urls)
        assert results["https://arxiv.org/abs/1234"] == "arXiv paper content"
        assert results["https://example.com/page"] == "Generic HTML content"

    def test_handles_download_failure_per_url(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )

        call_count = [0]

        def download_side_effect(url):
            call_count[0] += 1
            if call_count[0] == 1:
                raise RuntimeError("fail")
            return b"success"

        mock_downloader = Mock()
        mock_downloader.download = download_side_effect
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        urls = ["https://fail.com", "https://ok.com"]
        results = pipeline.batch_fetch_and_extract(urls)
        assert results["https://fail.com"] is None
        assert results["https://ok.com"] == "success"

    def test_empty_url_list(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )
        results = pipeline.batch_fetch_and_extract([])
        assert results == {}

    def test_specialized_exception_falls_through(self, monkeypatch):
        def boom(url, timeout=30):
            raise RuntimeError("specialized crash")

        monkeypatch.setattr(pipeline, "_try_specialized_downloader", boom)

        mock_downloader = Mock()
        mock_downloader.download.return_value = b"fallback content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        results = pipeline.batch_fetch_and_extract(["https://example.com"])
        assert results["https://example.com"] == "fallback content"

    def test_terminal_arxiv_failure_is_excluded_from_html_batch(
        self, monkeypatch
    ):
        # Given one terminal ar5iv failure and one fallback-eligible web URL
        def specialized(url, timeout=30):
            if "ar5iv.org" in url:
                return _specialized(fallback_allowed=False)
            return _specialized()

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", specialized
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"generic content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        ar5iv_url = "https://ar5iv.org/2301.12345v2"
        generic_url = "https://example.com/page"

        # When the batch pipeline runs
        results = pipeline.batch_fetch_and_extract([ar5iv_url, generic_url])

        # Then only the fallback-eligible URL reaches generic HTML
        assert results == {ar5iv_url: None, generic_url: "generic content"}
        mock_downloader.download.assert_called_once_with(generic_url)

    def test_malformed_arxiv_paper_does_not_construct_html_batch(
        self, monkeypatch
    ):
        # Given an arXiv downloader that produces nothing and a generic HTML
        # constructor tripwire. Only the downloader is stubbed: the ownership
        # gate runs for real, and the real downloader would canonicalise the
        # identifier and fetch the paper, so nothing may leave the test.
        class FailingArxivDownloader(BaseDownloader):
            def __init__(self, timeout=30):
                self.timeout = timeout

            def can_handle(self, url):
                return True

            def download(self, url, content_type=ContentType.TEXT):
                return None

            def download_with_result(self, url, content_type=ContentType.TEXT):
                return DownloadResult(skip_reason="rendition unavailable")

            def close(self):
                return None

        import local_deep_research.research_library.downloaders.arxiv as arxiv_mod

        monkeypatch.setattr(
            arxiv_mod, "ArxivDownloader", FailingArxivDownloader
        )
        mock_class = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        url = "https://ARXIV.ORG./abs/2301.12345"

        # When a batch contains only the owned paper URL
        results = pipeline.batch_fetch_and_extract([url])

        # Then the URL fails terminally before generic HTML construction
        assert results == {url: None}
        mock_class.assert_not_called()

    def test_arxiv_page_without_an_identifier_joins_the_html_batch(
        self, monkeypatch
    ):
        # Given a generic HTML downloader
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"generic content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        url = "https://arxiv.org/list/cs.AI/recent"

        # When a batch contains an arXiv page that names no paper
        results = pipeline.batch_fetch_and_extract([url])

        # Then it is crawled rather than dropped: batch_fetch_and_extract is
        # fed arbitrary search-result links, and these returned text on main
        assert results == {url: "generic content"}
        mock_downloader.download.assert_called_once_with(url)

    @pytest.mark.parametrize(
        "owned_url",
        (
            "https://ar5iv.org/2301.12345v2",
            "https://ARXIV.ORG./abs/2301.12345",
        ),
    )
    def test_arxiv_paper_exception_is_excluded_from_html_batch(
        self, monkeypatch, owned_url
    ):
        # Given specialization raises only for the malformed owned URL
        generic_url = "https://example.com/page"

        def specialized(url, timeout=30):
            if url == owned_url:
                raise RuntimeError("specialized failure")
            return _specialized()

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", specialized
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"generic content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When both URLs are processed
        results = pipeline.batch_fetch_and_extract([owned_url, generic_url])

        # Then only the unrelated URL reaches generic HTML
        assert results == {owned_url: None, generic_url: "generic content"}
        mock_downloader.download.assert_called_once_with(generic_url)

    @pytest.mark.parametrize(
        "unowned_url",
        (
            "https://arxiv.org/list/cs.AI/recent",
            "https://ARXIV.ORG./not-an-arxiv-id",
            "https://arxiv.org/ftp/arxiv/papers/2301/2301.12345.pdf",
        ),
    )
    def test_arxiv_host_without_a_paper_survives_a_specialized_crash(
        self, monkeypatch, unowned_url
    ):
        # Given specialization raises for an arXiv-family URL that names no
        # paper. The exception handler asks the same question the rest of the
        # routing does -- does this URL name a paper? -- so a host-only test
        # here would drop pages the generic pipeline can still read, on a
        # path fed arbitrary search-result links.
        def specialized(url, timeout=30):
            raise RuntimeError("specialized failure")

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", specialized
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"generic content"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )

        # When the batch runs
        results = pipeline.batch_fetch_and_extract([unowned_url])

        # Then the crash is not terminal: the page still reaches generic HTML
        assert results == {unowned_url: "generic content"}
        mock_downloader.download.assert_called_once_with(unowned_url)


class TestBatchFetchProgress:
    """The ``progress_callback`` liveness hook."""

    def _html_only(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30: _specialized(),
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"content"
        mock_downloader.close = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            Mock(return_value=mock_downloader),
        )

    def test_reports_start_and_each_url(self, monkeypatch):
        self._html_only(monkeypatch)
        calls = []
        urls = ["https://a.example/1", "https://a.example/2"]

        results = pipeline.batch_fetch_and_extract(
            urls,
            progress_callback=lambda done, total: calls.append((done, total)),
        )

        assert len(results) == 2
        assert calls == [(0, 2), (1, 2), (2, 2)]

    @pytest.mark.parametrize("all_cached", [False, True])
    @pytest.mark.parametrize("flaky_callback", [False, True])
    def test_cached_results_still_report_progress(
        self, monkeypatch, all_cached, flaky_callback
    ):
        from local_deep_research.utilities.research_content_cache import (
            ResearchContentCache,
        )

        self._html_only(monkeypatch)
        monkeypatch.setattr(
            pipeline, "_is_cache_hit_allowed", lambda *args: True
        )
        cache = ResearchContentCache()
        urls = ["https://a.example/1", "https://a.example/2"]
        cache.put(urls[0], "cached first")
        if all_cached:
            cache.put(urls[1], "cached second")
        calls = []

        def progress(done, total):
            calls.append((done, total))
            if flaky_callback and done == 1:
                raise RuntimeError("flaky callback")

        results = pipeline.batch_fetch_and_extract(
            urls, content_cache=cache, progress_callback=progress
        )
        assert results == {
            urls[0]: "cached first",
            urls[1]: "cached second" if all_cached else "content",
        }
        assert calls == [(0, 2), (1, 2), (2, 2)]

    def test_cached_result_can_cancel_before_remaining_downloads(
        self, monkeypatch
    ):
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.utilities.research_content_cache import (
            ResearchContentCache,
        )

        download = Mock(
            side_effect=AssertionError("cancelled batch performed a download")
        )
        monkeypatch.setattr(pipeline, "_try_specialized_downloader", download)
        monkeypatch.setattr(
            pipeline, "_is_cache_hit_allowed", lambda *args: True
        )
        cache = ResearchContentCache()
        urls = ["https://a.example/1", "https://a.example/2"]
        cache.put(urls[0], "cached first")
        calls = []

        def progress(done, total):
            calls.append((done, total))
            if done == 1:
                raise ResearchTerminatedException()

        with pytest.raises(ResearchTerminatedException):
            pipeline.batch_fetch_and_extract(
                urls, content_cache=cache, progress_callback=progress
            )
        assert calls == [(0, 2), (1, 2)]
        download.assert_not_called()

    def test_specialized_hits_count_as_done(self, monkeypatch):
        def mock_specialized(url, timeout=30):
            if "arxiv" in url:
                return _specialized("arXiv paper content", False)
            return _specialized()

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", mock_specialized
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"generic"
        mock_downloader.close = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            Mock(return_value=mock_downloader),
        )
        calls = []
        urls = ["https://arxiv.org/abs/1234", "https://example.com/page"]

        pipeline.batch_fetch_and_extract(
            urls,
            progress_callback=lambda done, total: calls.append((done, total)),
        )

        assert calls == [(0, 2), (1, 2), (2, 2)]

    def test_empty_list_reports_nothing(self, monkeypatch):
        self._html_only(monkeypatch)
        calls = []
        assert (
            pipeline.batch_fetch_and_extract([], progress_callback=calls.append)
            == {}
        )
        assert calls == []

    def test_termination_propagates_through_per_url_handler(self, monkeypatch):
        """A termination signal must not be swallowed as a URL failure."""
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        self._html_only(monkeypatch)
        calls = []

        def stop_on_second(done, total):
            calls.append((done, total))
            if done >= 1:
                raise ResearchTerminatedException()

        with pytest.raises(ResearchTerminatedException):
            pipeline.batch_fetch_and_extract(
                ["https://a.example/1", "https://a.example/2"],
                progress_callback=stop_on_second,
            )
        assert calls[0] == (0, 2)

    def test_flaky_callback_does_not_corrupt_specialized_hit(self, monkeypatch):
        """A one-shot callback failure must not overwrite good content.

        Reproduces review item 1: URL 1 resolves to good specialized
        content, the callback raises once at ``done == 1``, and the
        unguarded ``except Exception`` used to mistake it for a
        downloader failure — falling through to the HTML path and
        overwriting the result with the fallback.
        """

        def mock_specialized(url, timeout=30, **kwargs):
            if "special" in url:
                return _specialized("good specialized content", False)
            return _specialized()

        monkeypatch.setattr(
            pipeline, "_try_specialized_downloader", mock_specialized
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"html fallback"
        mock_downloader.close = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            Mock(return_value=mock_downloader),
        )
        calls = []
        raised = {"done": False}

        def flaky(done, total):
            calls.append((done, total))
            if done == 1 and not raised["done"]:
                raised["done"] = True
                raise RuntimeError("flaky UI callback")

        urls = ["https://special.example/1", "https://example.com/2"]
        results = pipeline.batch_fetch_and_extract(
            urls, progress_callback=flaky
        )

        assert results["https://special.example/1"] == (
            "good specialized content"
        )
        assert results["https://example.com/2"] == "html fallback"
        assert calls[0] == (0, 2)
        assert calls[-1] == (2, 2)

    def test_flaky_callback_does_not_abort_html_batch(self, monkeypatch):
        """A flaky callback in the HTML loop must not fail the search."""
        self._html_only(monkeypatch)
        calls = []
        raised = {"done": False}

        def flaky(done, total):
            calls.append((done, total))
            if done == 1 and not raised["done"]:
                raised["done"] = True
                raise RuntimeError("flaky UI callback")

        results = pipeline.batch_fetch_and_extract(
            ["https://a.example/1", "https://a.example/2"],
            progress_callback=flaky,
        )

        assert results == {
            "https://a.example/1": "content",
            "https://a.example/2": "content",
        }
        assert calls[0] == (0, 2)
        assert calls[-1] == (2, 2)

    def test_initial_callback_failure_does_not_abort_batch(self, monkeypatch):
        """An exception on the upfront ``(0, total)`` report is swallowed."""
        self._html_only(monkeypatch)
        calls = []

        def flaky(done, total):
            calls.append((done, total))
            if done == 0:
                raise RuntimeError("flaky upfront callback")

        results = pipeline.batch_fetch_and_extract(
            ["https://a.example/1"],
            progress_callback=flaky,
        )

        assert results == {"https://a.example/1": "content"}
        assert calls[0] == (0, 1)


# ---------------------------------------------------------------------------
# enable_js_rendering plumbing through fetch_and_extract /
# batch_fetch_and_extract — issue #3826
# ---------------------------------------------------------------------------


class TestFetchAndExtractJSRenderingPlumbing:
    """The ``enable_js_rendering`` flag must be forwarded into the
    ``AutoHTMLDownloader`` constructor when the HTML pipeline is used."""

    def _patched_downloader(self, monkeypatch):
        """Stub specialized + AutoHTMLDownloader; return the captured Mock class."""
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"x"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        return mock_class

    def test_fetch_and_extract_default_disables_js(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract("https://example.com")
        kwargs = mock_class.call_args.kwargs
        assert kwargs.get("enable_js_rendering") is False

    def test_fetch_and_extract_forwards_explicit_true(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract(
            "https://example.com", enable_js_rendering=True
        )
        kwargs = mock_class.call_args.kwargs
        assert kwargs.get("enable_js_rendering") is True

    def test_fetch_and_extract_forwards_explicit_false(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract(
            "https://example.com", enable_js_rendering=False
        )
        kwargs = mock_class.call_args.kwargs
        assert kwargs.get("enable_js_rendering") is False

    def test_batch_default_disables_js(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.batch_fetch_and_extract(["https://example.com"])
        kwargs = mock_class.call_args.kwargs
        assert kwargs.get("enable_js_rendering") is False

    def test_batch_forwards_explicit_true(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.batch_fetch_and_extract(
            ["https://example.com"], enable_js_rendering=True
        )
        kwargs = mock_class.call_args.kwargs
        assert kwargs.get("enable_js_rendering") is True


# ---------------------------------------------------------------------------
# _flatten_html
# ---------------------------------------------------------------------------


class TestFlattenHtml:
    """The fallback flattener must keep a sentence together."""

    def _flatten(self, html: str) -> str:
        from bs4 import BeautifulSoup

        return pipeline._flatten_html(BeautifulSoup(html, "html.parser"))

    def test_a_sentence_survives_its_inline_markup(self):
        """`get_text(separator="\\n")` would break this into six lines."""
        html = '<p>Hello <b>world</b>! See <a href="#">this</a>.</p>'

        assert self._flatten(html) == "Hello world! See this."

    def test_blocks_are_kept_apart(self):
        html = "<h1>Quarterly Report</h1><p>Revenue rose.</p><ul><li>one</li><li>two</li></ul>"

        assert (
            self._flatten(html) == "Quarterly Report\nRevenue rose.\none\ntwo"
        )

    def test_the_title_and_options_keep_their_own_lines(self):
        """Neither is a block, but both read as a line in a flattened page."""
        html = (
            "<html><head><title>Test Page</title></head><body><p>Body text</p>"
            "<select><option>alpha</option><option>beta</option></select>"
            "</body></html>"
        )

        assert self._flatten(html) == "Test Page\nBody text\nalpha\nbeta"

    def test_a_line_break_element_becomes_a_line_break(self):
        assert (
            self._flatten("<p>line one<br>line two</p>") == "line one\nline two"
        )

    def test_blank_lines_are_dropped(self):
        assert (
            self._flatten("<div></div><p>only this</p><div>  </div>")
            == "only this"
        )

    def test_text_before_a_block_keeps_its_own_line(self):
        """Only block ends were marked, so ``x<p>y</p>z`` read as ``xy``."""
        assert self._flatten("x<p>y</p>z") == "x\ny\nz"

    def test_script_style_and_template_text_stays_out(self):
        html = (
            "<p>kept</p><script>var a = 1;</script>"
            "<style>.a{color:red}</style><template>hidden</template>"
        )

        assert self._flatten(html) == "kept"

    def test_the_parsed_tree_is_left_unchanged(self):
        """The last-resort call site flattens a soup it may read again."""
        from bs4 import BeautifulSoup

        soup = BeautifulSoup("<p>one<br>two</p><div>three</div>", "html.parser")
        before = str(soup)

        pipeline._flatten_html(soup)

        assert str(soup) == before

    @pytest.mark.timeout(10)
    def test_many_line_breaks_flatten_in_linear_time(self):
        """Replacing each <br> in place took 6.2 s for 20,000 of them and
        quadruples with every doubling, so 50,000 would take about 40 s."""
        html = "<div>" + "x<br>" * 50_000 + "</div>"

        lines = self._flatten(html).split("\n")

        assert len(lines) == 50_000
        assert set(lines) == {"x"}


class TestFetchAndExtractPrivateIpPlumbing:
    """The ``allow_private_ips`` flag must be forwarded into the
    ``AutoHTMLDownloader`` constructor and default to blocked."""

    def _patched_downloader(self, monkeypatch):
        monkeypatch.setattr(
            pipeline,
            "_try_specialized_downloader",
            lambda url, timeout=30, **kwargs: _specialized(),
        )
        mock_downloader = Mock()
        mock_downloader.download.return_value = b"x"
        mock_downloader.close = Mock()
        mock_class = Mock(return_value=mock_downloader)
        monkeypatch.setattr(
            "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
            mock_class,
        )
        return mock_class

    def test_fetch_and_extract_forwards_explicit_private_ip_opt_in(
        self, monkeypatch
    ):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract(
            "https://example.com", allow_private_ips=True
        )
        assert mock_class.call_args.kwargs.get("allow_private_ips") is True

    def test_fetch_and_extract_defaults_private_ips_to_blocked(
        self, monkeypatch
    ):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract("https://example.com")
        assert mock_class.call_args.kwargs.get("allow_private_ips") is False
        assert mock_class.call_args.kwargs.get("block_link_local") is False

    def test_fetch_and_extract_forwards_block_link_local(self, monkeypatch):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.fetch_and_extract(
            "https://example.com",
            allow_private_ips=True,
            block_link_local=True,
        )
        assert mock_class.call_args.kwargs.get("block_link_local") is True

    def test_batch_fetch_and_extract_forwards_explicit_private_ip_opt_in(
        self, monkeypatch
    ):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.batch_fetch_and_extract(
            ["https://example.com"], allow_private_ips=True
        )
        assert mock_class.call_args.kwargs.get("allow_private_ips") is True

    def test_batch_fetch_and_extract_defaults_private_ips_to_blocked(
        self, monkeypatch
    ):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.batch_fetch_and_extract(["https://example.com"])
        assert mock_class.call_args.kwargs.get("allow_private_ips") is False
        assert mock_class.call_args.kwargs.get("block_link_local") is False

    def test_batch_fetch_and_extract_forwards_block_link_local(
        self, monkeypatch
    ):
        mock_class = self._patched_downloader(monkeypatch)
        pipeline.batch_fetch_and_extract(
            ["https://example.com"],
            allow_private_ips=True,
            block_link_local=True,
        )
        assert mock_class.call_args.kwargs.get("block_link_local") is True