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.

5.0 KB
Raw Blame
"""URL selection when persisting a research source.

Imports the real ``select_source_url``. An earlier version of this file
reimplemented the rule instead, and the mirror stayed faithful right up
until it faithfully mirrored a bug — the suite was green while a foreign
document's anchor was being persisted unchecked.

It also asserted only that the call did not raise, discarding the return
value, so the two dimensions that mattered (non-str url AND a foreign
recorded anchor) were never crossed.
"""

from __future__ import annotations

import pytest

from local_deep_research.utilities.url_utils import CHUNK_DISPLAY_KEY
from local_deep_research.web.services.research_sources_service import (
    _as_text,
    select_source_url,
)

FOREIGN = "/library/document/EVIL/chunks#chunk-5"


@pytest.mark.parametrize(
    "bad_url", [123, ["/library/document/doc1"], {"href": "x"}, b"x", True]
)
def test_non_str_url_never_yields_a_foreign_anchor(bad_url):
    """Fail CLOSED. The isinstance check was once a conjunct of the
    rejection condition, so a non-str url made the whole chain false, the
    ownership check never ran, and the recorded anchor won outright.
    """
    selected = select_source_url({"url": bad_url, CHUNK_DISPLAY_KEY: FOREIGN})

    assert selected != FOREIGN
    assert "EVIL" not in str(selected)


def test_own_anchor_wins_over_a_recorded_one():
    assert (
        select_source_url(
            {
                "url": "/library/document/doc1/chunks#chunk-2",
                CHUNK_DISPLAY_KEY: "/library/document/doc1/chunks#chunk-777",
            }
        )
        == "/library/document/doc1/chunks#chunk-2"
    )


def test_recorded_anchor_for_another_document_is_ignored():
    assert (
        select_source_url(
            {"url": "/library/document/doc1", CHUNK_DISPLAY_KEY: FOREIGN}
        )
        == "/library/document/doc1"
    )


def test_recorded_anchor_fills_the_gap_when_the_entry_has_none():
    assert (
        select_source_url(
            {
                "url": "/library/document/doc1",
                CHUNK_DISPLAY_KEY: "/library/document/doc1/chunks#chunk-3",
            }
        )
        == "/library/document/doc1/chunks#chunk-3"
    )


@pytest.mark.parametrize("bad_key", [123, ["x"], {"a": 1}, b"x"])
def test_non_str_recorded_key_is_ignored(bad_key):
    assert (
        select_source_url(
            {"url": "https://a.test/p", CHUNK_DISPLAY_KEY: bad_key}
        )
        == "https://a.test/p"
    )


def test_malformed_recorded_anchor_is_ignored():
    assert (
        select_source_url(
            {"url": "https://a.test/p", CHUNK_DISPLAY_KEY: "javascript:x"}
        )
        == "https://a.test/p"
    )


def test_plain_url_passes_through():
    assert select_source_url({"url": "https://a.test/p"}) == "https://a.test/p"
    assert select_source_url({"link": "https://b.test/p"}) == "https://b.test/p"


@pytest.mark.parametrize(
    "bad", [["/library/document/doc1"], {"href": "x"}, 123, True, b"x"]
)
def test_persisted_url_is_always_text(bad):
    """``select_source_url`` returns its input unchanged for a non-str, and
    that value goes to a ``Text`` column — where a list or dict raises at
    ``flush()``, inside the same broad ``except`` that drops the citation.
    Guarding only the read moved the exception; it did not prevent the
    drop.
    """
    value = _as_text(select_source_url({"url": bad}))

    assert isinstance(value, str)


def test_alias_spellings_persist_as_the_canonical_route():
    """``research_resources.url`` should hold a route that resolves.

    The absolute alias was persisted exactly as typed. The plain form
    happens to resolve in fetch, but the ``:443`` and userinfo spellings —
    which the alias parser accepts as naming the same document while
    ``library_resolver`` refuses them (it compares netloc exactly) — became
    dead links in the DB.
    """
    from local_deep_research.web.services.research_sources_service import (
        select_source_url,
    )

    for spelling in (
        "https://library.document/abc",
        "https://library.document:443/abc",
        "https://[email protected]/abc",
        "/lib/document/abc",
        "/library/document/abc",
    ):
        assert select_source_url({"url": spelling}) == (
            "/library/document/abc"
        ), spelling


def test_normalisation_does_not_touch_external_urls_or_valid_anchors():
    """It must normalise only what it recognises."""
    from local_deep_research.web.services.research_sources_service import (
        select_source_url,
    )

    # A valid chunk anchor still wins over the bare route.
    assert (
        select_source_url({"url": "/library/document/abc/chunks#chunk-3"})
        == "/library/document/abc/chunks#chunk-3"
    )

    # External URLs keep their own spelling, query and fragment included.
    for external in (
        "https://example.com/p?a=1#frag",
        "https://example.com/docs/page#chunk-2",
        "https://library.document.evil.test/abc",
    ):
        assert select_source_url({"url": external}) == external, external