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.

40.4 KB
Raw Blame
"""Unit tests for ``utilities.openalex_enrichment``.

Covers:
  - ``_normalize_doi``: every branch of the anchored ``startswith`` ladder,
    including the CodeQL-reviewed https/http/bare-10.* paths.
  - ``enrich_results_with_source_ids``: happy path, skip conditions,
    already-enriched input, OpenAlex HTTP errors, malformed responses,
    and batching past the 50-per-request cap.

All network access goes through a mocked ``safe_get`` so the tests are
deterministic and offline.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.utilities.openalex_enrichment import (
    ERROR_BODY_SCRUB_LIMIT,
    _normalize_doi,
    bounded_error_body,
    enrich_results_with_source_ids,
    normalize_openalex_api_key,
    send_with_key_fallback,
)


# ---------------------------------------------------------------------------
# _normalize_doi
# ---------------------------------------------------------------------------


class TestNormalizeDoi:
    """Pure function, no I/O — one branch per input shape."""

    def test_https_doi_returned_unchanged(self):
        """CodeQL-anchored path: already the canonical form."""
        doi = "https://doi.org/10.1038/nature12373"
        assert _normalize_doi(doi) == doi

    def test_http_doi_upgraded_to_https(self):
        """http://doi.org/... gets scheme-upgraded; prefix preserved."""
        assert (
            _normalize_doi("http://doi.org/10.1038/nature12373")
            == "https://doi.org/10.1038/nature12373"
        )

    def test_bare_10_doi_wrapped(self):
        """Most APIs return DOIs as bare ``10.xxxx/...``."""
        assert (
            _normalize_doi("10.1038/nature12373")
            == "https://doi.org/10.1038/nature12373"
        )

    def test_whitespace_stripped(self):
        """Incoming values from various citation parsers often have
        trailing whitespace."""
        assert (
            _normalize_doi("  10.1038/nature12373  ")
            == "https://doi.org/10.1038/nature12373"
        )

    def test_unrecognized_form_passed_through(self):
        """A non-DOI string (or a DOI with an unexpected prefix like
        ``dx.doi.org``) passes through unchanged — we don't guess."""
        assert _normalize_doi("dx.doi.org/10.1038/nature12373") == (
            "dx.doi.org/10.1038/nature12373"
        )
        assert _normalize_doi("not-a-doi") == "not-a-doi"
        assert _normalize_doi("") == ""

    def test_codeql_anchoring_is_real(self):
        """Regression guard for CodeQL alert 7635. A substring match
        on ``doi.org/`` anywhere in the URL would be unsafe, so this
        asserts that the function does NOT normalize a URL that merely
        *contains* ``doi.org/`` in a non-anchored position.
        """
        # Malicious-looking input: the prefix-check is anchored via
        # startswith, so this hostile URL is passed through unchanged
        # rather than being mangled into an ambiguous canonical form.
        malicious = "https://attacker.example/?ref=doi.org/10.1038/x"
        assert _normalize_doi(malicious) == malicious


# ---------------------------------------------------------------------------
# enrich_results_with_source_ids
# ---------------------------------------------------------------------------


def _make_openalex_response(works):
    """Build a MagicMock response mimicking the OpenAlex /works payload."""
    resp = MagicMock()
    resp.status_code = 200
    resp.json.return_value = {"results": works}
    return resp


def _work(doi, source_id, source_type="journal"):
    """Build a single OpenAlex work record with a resolved primary_location."""
    return {
        "doi": doi,
        "primary_location": {
            "source": {
                "id": f"https://openalex.org/{source_id}",
                "type": source_type,
            }
        },
    }


class TestEnrichResultsWithSourceIds:
    """HTTP layer mocked at ``safe_get``."""

    def test_empty_list_returns_empty(self):
        """Short-circuit: no work to do, no request made."""
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get"
        ) as mock_get:
            result = enrich_results_with_source_ids([])
            assert result == []
            assert mock_get.call_count == 0

    def test_results_without_dois_skip_network(self):
        """No DOI in any result → no request made, inputs untouched."""
        results = [
            {"title": "Paper A", "url": "https://example.com/a"},
            {"title": "Paper B"},
        ]
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get"
        ) as mock_get:
            out = enrich_results_with_source_ids(results)
        assert out is results  # in-place semantics
        assert "openalex_source_id" not in results[0]
        assert mock_get.call_count == 0

    def test_already_enriched_result_skipped(self):
        """Results with an ``openalex_source_id`` already populated must
        not be re-requested (network savings + stability)."""
        results = [
            {
                "doi": "10.1038/nature12373",
                "openalex_source_id": "S4306417988",
            }
        ]
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get"
        ) as mock_get:
            enrich_results_with_source_ids(results)
            assert mock_get.call_count == 0

    def test_happy_path_populates_source_id_and_type(self):
        """One DOI → one resolved source; the result dict gets both
        ``openalex_source_id`` and ``source_type`` populated."""
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response(
            [_work("https://doi.org/10.1038/nature12373", "S137773608")]
        )
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ):
            enrich_results_with_source_ids(results)
        assert results[0]["openalex_source_id"] == "S137773608"
        assert results[0]["source_type"] == "journal"

    def test_multiple_results_same_doi_all_enriched(self):
        """Duplicate DOIs across results share one HTTP request and all
        get the resolved source_id applied."""
        results = [
            {"doi": "10.1038/nature12373"},
            {"doi": "10.1038/nature12373"},
        ]
        mock_resp = _make_openalex_response(
            [_work("https://doi.org/10.1038/nature12373", "S137773608")]
        )
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(results)
        assert mock_get.call_count == 1
        for r in results:
            assert r["openalex_source_id"] == "S137773608"

    def test_unresolved_doi_leaves_result_unchanged(self):
        """OpenAlex can't resolve every DOI; unmatched results are left
        untouched — no silent mis-attribution."""
        results = [{"doi": "10.0000/never-existed"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ):
            enrich_results_with_source_ids(results)
        assert "openalex_source_id" not in results[0]

    def test_non_200_response_logs_and_continues(self):
        """HTTP 429/500 etc. must not raise — the batch aborts silently
        and results pass through unenriched. Caller shouldn't fail just
        because OpenAlex is rate-limiting.
        """
        results = [{"doi": "10.1038/nature12373"}]
        bad_resp = MagicMock()
        bad_resp.status_code = 503
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=bad_resp,
        ) as mock_get:
            out = enrich_results_with_source_ids(results)
        assert "openalex_source_id" not in results[0]
        assert out is results
        assert mock_get.call_args.kwargs["require_https"] is True

    def test_network_exception_swallowed_graceful(self):
        """``safe_get`` itself can raise on network errors. The function
        catches and logs — caller still gets back its list."""
        results = [{"doi": "10.1038/nature12373"}]
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            side_effect=ConnectionError("boom"),
        ):
            out = enrich_results_with_source_ids(results)
        assert "openalex_source_id" not in results[0]
        assert out is results

    def test_work_without_primary_location_skipped(self):
        """OpenAlex returns a work with no primary_location (e.g.
        preprints, withdrawn papers). Must not KeyError or write a
        bogus source_id."""
        results = [{"doi": "10.0000/preprint"}]
        bad_work = {"doi": "https://doi.org/10.0000/preprint"}  # no location
        mock_resp = _make_openalex_response([bad_work])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ):
            enrich_results_with_source_ids(results)
        assert "openalex_source_id" not in results[0]

    def test_work_with_null_source_skipped(self):
        """``primary_location.source`` can be literally ``null`` in the
        OpenAlex payload (book chapters, datasets). Must handle without
        crashing.
        """
        results = [{"doi": "10.0000/chapter"}]
        mock_resp = _make_openalex_response(
            [
                {
                    "doi": "https://doi.org/10.0000/chapter",
                    "primary_location": {"source": None},
                }
            ]
        )
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ):
            enrich_results_with_source_ids(results)
        assert "openalex_source_id" not in results[0]

    def test_source_type_optional(self):
        """``source.type`` may be missing — source_id still gets written
        but source_type does not."""
        results = [{"doi": "10.1038/nature12373"}]
        work = {
            "doi": "https://doi.org/10.1038/nature12373",
            "primary_location": {
                "source": {"id": "https://openalex.org/S137773608"}
            },
        }
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=_make_openalex_response([work]),
        ):
            enrich_results_with_source_ids(results)
        assert results[0]["openalex_source_id"] == "S137773608"
        assert "source_type" not in results[0]

    def test_batching_respects_50_per_request_cap(self):
        """75 distinct DOIs → 2 HTTP requests (50 + 25). Verified by
        call_count and by the ``per_page`` param on each call.
        """
        # 75 results, each with a unique DOI
        results = [{"doi": f"10.1234/paper{i:03d}"} for i in range(75)]
        # Return a minimal valid response for each call
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(results)
        assert mock_get.call_count == 2
        per_pages = [
            call.kwargs["params"]["per_page"]
            for call in mock_get.call_args_list
        ]
        assert per_pages == ["50", "25"]

    def test_api_key_sent_as_authorization_header(self):
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(results, api_key="openalex-test-key")
        call = mock_get.call_args
        assert call.kwargs["headers"]["Authorization"] == (
            "Bearer openalex-test-key"
        )
        assert "mailto" not in call.kwargs["params"]
        assert "api_key" not in call.kwargs["params"]
        assert "openalex-test-key" not in call.kwargs["params"].values()

    def test_email_only_overrides_user_agent(self):
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(
                results, email="[email protected]"
            )
        call = mock_get.call_args
        assert "mailto" not in call.kwargs["params"]
        assert "[email protected]" in call.kwargs["headers"]["User-Agent"]
        assert "Authorization" not in call.kwargs["headers"]

    def test_email_omitted_leaves_mailto_absent(self):
        """No email → no mailto param, no User-Agent override."""
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(results)
        call = mock_get.call_args
        assert "mailto" not in call.kwargs["params"]
        assert "User-Agent" not in call.kwargs["headers"]


if __name__ == "__main__":
    pytest.main([__file__, "-v"])


# ---------------------------------------------------------------------------
# normalize_openalex_api_key
# ---------------------------------------------------------------------------


class TestNormalizeOpenAlexApiKey:
    """A value that cannot be a real key must mean "go keyless"."""

    @pytest.mark.parametrize(
        "configured_key",
        [
            None,
            "",
            "   ",
            "\t\n",
            "False",
            " False ",
            "false",
            "FALSE",
            "none",
            "null",
            "placeholder",
            "api_key",
            "your_api_key",
            "your-api-key-here",
            "YOUR_API_KEY_HERE",
            "<your_api_key>",
            "${OPENALEX_API_KEY}",
            "${openalex_api_key}",
            True,
            1,
            3.5,
            ["k"],
            {"k": 1},
        ],
    )
    def test_unusable_values_become_none(self, configured_key):
        assert normalize_openalex_api_key(configured_key) is None

    @pytest.mark.parametrize(
        ("configured_key", "expected"),
        [
            ("oa-live-abc123", "oa-live-abc123"),
            ("  oa-live-abc123  ", "oa-live-abc123"),
            ("falsework-key", "falsework-key"),
            ("nonesuch-key", "nonesuch-key"),
        ],
    )
    def test_real_keys_survive(self, configured_key, expected):
        assert normalize_openalex_api_key(configured_key) == expected

    @pytest.mark.parametrize(
        "configured_key",
        [
            "abc\ndef",
            "abc\rdef",
            "abc\tdef",
            "abc def",
            "abc\x00def",
            "abc\x7fdef",
            "abc\u00a0def",
            "abc\u200bdef",
        ],
    )
    def test_internal_whitespace_or_control_chars_become_none(
        self, configured_key
    ):
        """Such a value cannot be a header, and its repr defeats scrubbing.

        ``requests`` raises ``InvalidHeader`` embedding the value's
        ``repr``; the literal-secret pass cannot match it (the literal
        holds a real newline, the message holds a backslash and an "n")
        and the anchored Bearer regex stops at the backslash. Rejecting
        the value up front is what keeps it out of the logs entirely.
        """
        assert normalize_openalex_api_key(configured_key) is None

    def test_whitespace_rejection_warns_without_echoing_the_value(self):
        logged = []
        with patch(
            "local_deep_research.utilities.openalex_enrichment.logger.warning",
            side_effect=lambda msg, *a, **kw: logged.append(msg),
        ):
            assert normalize_openalex_api_key("oa-live\nabc123") is None

        assert logged, "an unusable-looking real key must be reported"
        assert not any("abc123" in message for message in logged)
        assert not any("oa-live" in message for message in logged)

    def test_placeholder_with_whitespace_is_dropped_silently(self):
        """The placeholder list runs first, so no misleading warning."""
        logged = []
        with patch(
            "local_deep_research.utilities.openalex_enrichment.logger.warning",
            side_effect=lambda msg, *a, **kw: logged.append(msg),
        ):
            assert normalize_openalex_api_key("<your api key>") is None

        assert logged == []


class TestEnrichmentKeyNormalization:
    """The enrichment request applies the same filter as the engine."""

    @pytest.mark.parametrize(
        "configured_key",
        ["${OPENALEX_API_KEY}", "your-api-key-here", "False", "   ", True],
    )
    def test_placeholder_key_sends_no_authorization_header(
        self, configured_key
    ):
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(results, api_key=configured_key)

        headers = mock_get.call_args.kwargs["headers"]
        assert "Authorization" not in headers

    def test_real_key_is_trimmed_before_use(self):
        results = [{"doi": "10.1038/nature12373"}]
        mock_resp = _make_openalex_response([])
        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=mock_resp,
        ) as mock_get:
            enrich_results_with_source_ids(
                results, api_key="  oa-live-abc123  "
            )

        headers = mock_get.call_args.kwargs["headers"]
        assert headers["Authorization"] == "Bearer oa-live-abc123"

    @pytest.mark.parametrize("auth_status", [401, 403])
    def test_rejected_key_logs_a_distinct_warning(self, auth_status):
        results = [{"doi": "10.1038/nature12373"}]
        rejected = MagicMock()
        rejected.status_code = auth_status
        logged = []

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                return_value=rejected,
            ),
            patch(
                "local_deep_research.utilities.openalex_enrichment.logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            enrich_results_with_source_ids(results, api_key="oa-live-abc123")

        assert any(
            "rejected the configured API key" in message for message in logged
        )
        assert not any("oa-live-abc123" in message for message in logged)


class TestRejectedKeyKeylessFallback:
    """A rejected key must never cost the enrichment pass its results.

    Same three-case shape as the search engine and the library
    downloader, because all three go through
    ``send_with_key_fallback``.
    """

    @pytest.mark.parametrize("auth_status", [401, 403])
    def test_rejected_key_retries_keyless_and_enriches(self, auth_status):
        """401/403 with a key → one keyless retry that actually works."""
        results = [{"doi": "10.1038/nature12373"}]
        rejected = MagicMock()
        rejected.status_code = auth_status
        ok = _make_openalex_response(
            [_work("https://doi.org/10.1038/nature12373", "S137773608")]
        )

        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            side_effect=[rejected, ok],
        ) as mock_get:
            enrich_results_with_source_ids(results, api_key="oa-live-abc123")

        assert mock_get.call_count == 2
        first, second = mock_get.call_args_list
        assert first.kwargs["headers"]["Authorization"] == (
            "Bearer oa-live-abc123"
        )
        # The retry is the same request minus the credential.
        assert "Authorization" not in second.kwargs["headers"]
        assert second.kwargs["params"] == first.kwargs["params"]
        # And the batch was enriched, not dropped.
        assert results[0]["openalex_source_id"] == "S137773608"

    def test_rejected_key_is_not_sent_again_by_a_later_batch(self):
        """The key is dropped for the rest of the pass, not per batch.

        Two chunks (>50 DOIs). Without the drop, chunk 2 would send the
        same rejected credential and pay a second wasted round-trip, and
        the "rejected" warning would be logged twice.
        """
        results = [{"doi": f"10.1234/paper{index}"} for index in range(60)]
        rejected = MagicMock()
        rejected.status_code = 401
        empty = _make_openalex_response([])
        logged = []

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=[rejected, empty, empty],
            ) as mock_get,
            patch(
                "local_deep_research.utilities.openalex_enrichment.logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            enrich_results_with_source_ids(results, api_key="oa-live-abc123")

        # chunk 1 keyed + chunk 1 keyless retry + chunk 2 keyless = 3
        assert mock_get.call_count == 3
        sent_with_key = [
            call
            for call in mock_get.call_args_list
            if "Authorization" in call.kwargs["headers"]
        ]
        assert len(sent_with_key) == 1
        assert (
            sum(
                "rejected the configured API key" in message
                for message in logged
            )
            == 1
        )

    def test_keyless_retry_failing_too_skips_the_batch(self):
        """Pre-key behaviour when even the keyless request is refused."""
        results = [{"doi": "10.1038/nature12373"}]
        rejected = MagicMock()
        rejected.status_code = 401
        logged = []

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=[rejected, rejected],
            ) as mock_get,
            patch(
                "local_deep_research.utilities.openalex_enrichment.logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            out = enrich_results_with_source_ids(
                results, api_key="oa-live-abc123"
            )

        assert mock_get.call_count == 2
        assert out is results
        assert "openalex_source_id" not in results[0]
        assert any(
            "DOI enrichment: OpenAlex returned 401" in message
            for message in logged
        )
        assert not any("oa-live-abc123" in message for message in logged)

    @pytest.mark.parametrize("auth_status", [401, 403])
    def test_keyless_401_is_unchanged_and_blames_no_key(self, auth_status):
        """No key configured → one request, and no "your key" advice."""
        results = [{"doi": "10.1038/nature12373"}]
        rejected = MagicMock()
        rejected.status_code = auth_status
        logged = []

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                return_value=rejected,
            ) as mock_get,
            patch(
                "local_deep_research.utilities.openalex_enrichment.logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            enrich_results_with_source_ids(results)

        assert mock_get.call_count == 1
        assert not any(
            "API key" in message or "openalex.org/settings/api" in message
            for message in logged
        )
        assert any(
            f"DOI enrichment: OpenAlex returned {auth_status}" in message
            for message in logged
        )

    def test_lookup_failure_scrubs_the_key_from_the_log(self):
        results = [{"doi": "10.1038/nature12373"}]
        logged = []

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=RuntimeError(
                    "connect failed with Authorization: Bearer oa-live-abc123"
                ),
            ),
            patch(
                "local_deep_research.utilities.openalex_enrichment.logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            out = enrich_results_with_source_ids(
                results, api_key="oa-live-abc123"
            )

        assert out is results  # graceful: results pass through unenriched
        assert logged
        assert not any("oa-live-abc123" in message for message in logged)


class TestNormalizedKeysAreAlwaysHeaderSafe:
    """The general property, not the example codepoints.

    ``normalize_openalex_api_key`` exists so that no value it returns can
    blow up header construction. Whitespace and control characters are one
    axis; latin-1 encodability is the other, and it is the one that
    actually decides whether the request leaves the process:
    ``http.client`` does ``one_value.encode('latin-1')`` on every header
    value, and ``requests``' own header validation does not gate the
    encoding. A ``UnicodeEncodeError`` there is raised before any socket
    work, carries no status code, and so cannot be turned into a keyless
    retry by anything that branches on ``response.status_code``.
    """

    @pytest.mark.parametrize(
        ("configured_key", "why"),
        [
            ("oa-key‐abc", "U+2010 HYPHEN — copied out of a PDF"),
            ("oa-key–abc", "U+2013 EN DASH — editor autocorrect"),
            ("oa’key", "U+2019 RIGHT SINGLE QUOTATION MARK"),
            ("oa-key—abc", "U+2014 EM DASH"),
            ("oa-key€abc", "U+20AC EURO SIGN (not in latin-1)"),
            ("oa-keyŁabc", "U+0141 LATIN CAPITAL LETTER L WITH STROKE"),
            ("oa-key中abc", "CJK ideograph"),
            ("oa-key\U0001f600abc", "astral-plane emoji"),
        ],
    )
    def test_non_latin1_keys_become_none(self, configured_key, why):
        """None of these is whitespace or category C — only the encoding."""
        import unicodedata

        assert not any(
            character.isspace()
            or unicodedata.category(character).startswith("C")
            for character in configured_key
        ), f"{why}: this case must exercise the encoding check, not the old one"
        assert normalize_openalex_api_key(configured_key) is None

    @pytest.mark.parametrize(
        "configured_key",
        [
            "ké",  # é — in latin-1, must survive
            "oa-key-ü",  # ü
            "éüñ-key",
            "oa-live-abc123",
        ],
    )
    def test_latin1_keys_survive(self, configured_key):
        """The encoding check must not swallow a usable key."""
        assert normalize_openalex_api_key(configured_key) == configured_key

    def test_no_returned_key_can_fail_header_encoding(self):
        """Sweep the codepoint space, not a handful of examples.

        For every codepoint below U+3000 plus a spread of higher ones,
        embed it in the middle of an otherwise-fine key and check the
        invariant that matters: whatever comes back either is ``None`` or
        can actually be sent. "Can be sent" is modelled on the step the
        stack performs — ``http.client.putheader`` does
        ``value.encode("latin-1")`` on each header value, and this
        re-implements that call on the full ``Bearer <key>`` string
        rather than driving ``http.client`` itself.
        """
        codepoints = list(range(0x3000)) + [
            0x303F,
            0x3400,
            0x4E2D,
            0xFEFF,
            0xFF21,
            0x1F600,
            0x10FFFF,
        ]
        survivors = 0
        for codepoint in codepoints:
            candidate = f"oa-key{chr(codepoint)}abc"
            normalized = normalize_openalex_api_key(candidate)
            if normalized is None:
                continue
            survivors += 1
            # Models the encode step http/client.py performs per header
            # value; it is not http.client itself.
            f"Bearer {normalized}".encode("latin-1")
        # Sanity: the sweep is not vacuous — plenty of codepoints are fine.
        assert survivors > 100

    def test_the_encoding_check_is_what_rejects_them(self):
        """Guard against "the old predicates already covered this".

        If the whitespace/control predicates alone were enough, this
        assertion would hold for the non-latin-1 codepoints too, and the
        encode check would be dead code.
        """
        import unicodedata

        leaked = [
            codepoint
            for codepoint in range(0x3000)
            if not (
                chr(codepoint).isspace()
                or unicodedata.category(chr(codepoint)).startswith("C")
            )
            and codepoint > 0xFF
        ]
        assert leaked, "expected non-latin-1, non-space, non-control codepoints"
        for codepoint in leaked[:200]:
            assert (
                normalize_openalex_api_key(f"oa-key{chr(codepoint)}abc") is None
            )

    def test_encoding_rejection_warns_without_echoing_the_value(self):
        logged = []
        with patch(
            "local_deep_research.utilities.openalex_enrichment.logger.warning",
            side_effect=lambda msg, *a, **kw: logged.append(msg),
        ):
            assert normalize_openalex_api_key("oa-live–abc123") is None

        assert logged, "an unusable-looking real key must be reported"
        assert not any("abc123" in message for message in logged)
        assert not any("oa-live" in message for message in logged)


class TestHeaderEncodingFallback:
    """Defence in depth for a caller that skipped the normalizer.

    ``UnicodeEncodeError`` is the one exception that can only come from
    encoding a header value, i.e. before any request leaves the process.
    It gets the rejection treatment; nothing else does.
    """

    @staticmethod
    def _ok():
        response = MagicMock()
        response.status_code = 200
        return response

    def test_unencodable_key_is_dropped_and_the_send_is_retried_keyless(self):
        calls = []
        events = []
        ok = self._ok()
        logged = []

        def _send(with_api_key):
            calls.append(with_api_key)
            if with_api_key:
                # Exactly what http.client does to a header value.
                "Bearer oa-live–abc123".encode("latin-1")
            return ok

        with patch(
            "local_deep_research.utilities.openalex_enrichment.logger.warning",
            side_effect=lambda msg, *a, **kw: logged.append(msg),
        ):
            response, key = send_with_key_fallback(
                _send,
                api_key="oa-live–abc123",
                context="Unit test",
                on_key_rejected=lambda: events.append("rejected"),
                before_retry=lambda: events.append("retry"),
            )

        assert response is ok
        assert key is None
        assert calls == [True, False]
        assert events == ["rejected", "retry"]
        assert len(logged) == 1
        assert not any("oa-live" in message for message in logged)
        assert not any("abc123" in message for message in logged)

    @pytest.mark.parametrize(
        "error",
        [
            RuntimeError("connection reset"),
            # UnicodeEncodeError's base classes: widening the except to
            # either of these would swallow a real failure and throw the
            # key away for a fault OpenAlex never reported.
            ValueError("some other value error"),
            UnicodeError("some other unicode error"),
            OSError("network down"),
        ],
    )
    def test_other_exceptions_are_not_retried_keyless(self, error):
        calls = []
        events = []

        def _send(with_api_key):
            calls.append(with_api_key)
            raise error

        with pytest.raises(type(error)):
            send_with_key_fallback(
                _send,
                api_key="oa-live-abc123",
                context="Unit test",
                on_key_rejected=lambda: events.append("rejected"),
                before_retry=lambda: events.append("retry"),
            )

        assert calls == [True], "no keyless resend for a non-encoding failure"
        assert events == [], "the key must not be dropped for a network fault"

    def test_keyless_encoding_failure_propagates(self):
        """No key means the encoding fault is not the credential's."""
        calls = []

        def _send(with_api_key):
            calls.append(with_api_key)
            "–".encode("latin-1")

        with pytest.raises(UnicodeEncodeError):
            send_with_key_fallback(_send, api_key=None, context="Unit test")

        assert calls == [False]

    def test_on_key_rejected_runs_before_the_keyless_resend(self):
        """Ordering, pinned by making the resend observe the drop.

        Moving ``on_key_rejected()`` after ``send_request(False)`` is
        otherwise invisible: every response-list-driven test sees the same
        call sequence either way.
        """
        holder = {"api_key": "oa-live-abc123"}
        rejected = MagicMock()
        rejected.status_code = 401
        ok = self._ok()

        def _send(with_api_key):
            if with_api_key:
                return rejected
            assert holder["api_key"] is None, (
                "the key must already be dropped when the keyless resend "
                "is issued: otherwise a raise here leaves it live"
            )
            return ok

        def _drop():
            holder["api_key"] = None

        response, key = send_with_key_fallback(
            _send,
            api_key=holder["api_key"],
            context="Unit test",
            on_key_rejected=_drop,
        )

        assert response is ok
        assert key is None


class TestEnrichmentRejectionCallback:
    """``on_key_rejected`` lets the caller latch what this call learned."""

    def test_callback_receives_the_refused_key_once(self):
        results = [{"doi": f"10.1234/paper{index}"} for index in range(60)]
        rejected = MagicMock()
        rejected.status_code = 401
        empty = _make_openalex_response([])
        seen = []

        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            side_effect=[rejected, empty, empty],
        ):
            enrich_results_with_source_ids(
                results,
                api_key="oa-live-abc123",
                on_key_rejected=seen.append,
            )

        assert seen == ["oa-live-abc123"]

    def test_callback_is_absent_when_the_key_is_accepted(self):
        results = [{"doi": "10.1038/nature12373"}]
        seen = []

        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            return_value=_make_openalex_response(
                [_work("https://doi.org/10.1038/nature12373", "S137773608")]
            ),
        ):
            enrich_results_with_source_ids(
                results,
                api_key="oa-live-abc123",
                on_key_rejected=seen.append,
            )

        assert seen == []

    def test_a_raising_callback_does_not_resurrect_the_key(self):
        """The drop happens first, so a bad callback cannot undo it."""
        results = [{"doi": f"10.1234/paper{index}"} for index in range(60)]
        rejected = MagicMock()
        rejected.status_code = 401
        empty = _make_openalex_response([])

        def _boom(_key):
            raise RuntimeError("callback exploded")

        with patch(
            "local_deep_research.utilities.openalex_enrichment.safe_get",
            side_effect=[rejected, empty, empty],
        ) as mock_get:
            enrich_results_with_source_ids(
                results, api_key="oa-live-abc123", on_key_rejected=_boom
            )

        # chunk 1 keyed (401) then the callback raises, which the module's
        # blanket handler turns into a skipped chunk; chunk 2 must still go
        # out keyless rather than re-sending the refused credential.
        keyed = [
            call
            for call in mock_get.call_args_list
            if "Authorization" in call.kwargs["headers"]
        ]
        assert len(keyed) == 1

    def test_a_second_drop_keeps_the_key_redactable(self):
        """Idempotency: the second drop must not store None over the key.

        Unreachable single-threaded — after the first drop the helper
        short-circuits on the falsy key — but the module's own scrub
        depends on the retained literal, so the guard is pinned here by
        stubbing the helper to invoke the callback twice.
        """
        results = [{"doi": "10.1038/nature12373"}]
        logged = []

        def _fake_helper(
            send_request,
            *,
            api_key,
            context,
            on_key_rejected=None,
            before_retry=None,
        ):
            on_key_rejected()
            on_key_rejected()  # the racing second rejection
            raise RuntimeError("upstream said oa-live-abc123")

        with (
            patch(
                "local_deep_research.utilities.openalex_enrichment."
                "send_with_key_fallback",
                _fake_helper,
            ),
            patch(
                "local_deep_research.utilities.openalex_enrichment."
                "logger.warning",
                side_effect=lambda msg, *a, **kw: logged.append(msg),
            ),
        ):
            enrich_results_with_source_ids(results, api_key="oa-live-abc123")

        assert logged
        assert not any("oa-live-abc123" in message for message in logged)


class TestBoundedErrorBody:
    """``bounded_error_body`` — bound the scrub without splitting a token.

    The whole point of the helper is that a caller may hand its output to
    a shape-anchored scrubber and then truncate the result. That is only
    safe if the bound cannot cut inside a whitespace-free run, because a
    key never contains whitespace (``normalize_openalex_api_key`` refuses
    one that does).
    """

    def test_a_short_body_is_returned_unchanged(self):
        body = "upstream said no\nwith a trailing token abcdefgh"
        assert len(body) <= ERROR_BODY_SCRUB_LIMIT
        assert bounded_error_body(body) is body

    def test_a_body_exactly_at_the_limit_is_returned_unchanged(self):
        body = "x" * ERROR_BODY_SCRUB_LIMIT
        assert bounded_error_body(body) == body

    def test_the_cut_lands_on_a_whitespace_character(self):
        body = "token " * 4000
        assert len(body) > ERROR_BODY_SCRUB_LIMIT
        bounded = bounded_error_body(body)

        assert len(bounded) <= ERROR_BODY_SCRUB_LIMIT
        assert bounded.endswith(" ")
        assert body.startswith(bounded)
        # Nothing between the cut and the limit is a whitespace character,
        # i.e. the cut really is the *last* boundary available.
        assert not any(
            character.isspace()
            for character in body[len(bounded) : ERROR_BODY_SCRUB_LIMIT]
        )

    def test_a_run_straddling_the_limit_is_dropped_in_full(self):
        run = "K" * 60
        head = "a b " * 2100
        body = head[: ERROR_BODY_SCRUB_LIMIT - 30] + run + " tail"
        assert len(body) > ERROR_BODY_SCRUB_LIMIT

        bounded = bounded_error_body(body)
        # Not one character of the straddling run survives.
        assert "K" not in bounded
        assert bounded[-1].isspace()
        assert body.startswith(bounded)

    def test_no_whitespace_anywhere_yields_the_empty_string(self):
        body = "n" * (ERROR_BODY_SCRUB_LIMIT + 500)
        assert bounded_error_body(body) == ""

    def test_whitespace_only_after_the_limit_yields_the_empty_string(self):
        body = "n" * (ERROR_BODY_SCRUB_LIMIT + 10) + " tail"
        assert bounded_error_body(body) == ""

    def test_non_ascii_whitespace_counts_as_a_boundary(self):
        """``str.isspace()``, not a latin-1 or ASCII whitelist.

        An upstream body is arbitrary decoded text; a non-breaking or
        ideographic space is as good a token boundary as ``" "``.
        """
        body = "head " + "T" * (ERROR_BODY_SCRUB_LIMIT + 100)
        bounded = bounded_error_body(body)

        assert bounded == "head "
        assert "T" not in bounded

    def test_the_limit_is_a_parameter(self):
        body = "abcd efgh ijkl"
        assert bounded_error_body(body, limit=6) == "abcd "
        assert bounded_error_body(body, limit=4) == ""