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.

8.2 KB
Raw Blame
"""Tests for project-wide constants in local_deep_research.constants."""

import json

from local_deep_research.constants import (
    DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
    DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
    DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC,
    DEFAULT_LOCAL_SEARCH_INDEX_TYPE,
    DEFAULT_LOCAL_SEARCH_MODEL,
    DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS,
    DEFAULT_LOCAL_SEARCH_PROVIDER,
    DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
    DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS,
    DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS_JSON,
    DEFAULT_SEARCH_TOOL,
)
from local_deep_research.defaults import DEFAULTS_DIR


def test_default_search_tool_matches_registry():
    """DEFAULT_SEARCH_TOOL (the code-side fallback every reader imports for a
    missing ``search.tool`` setting) must equal the registered default in
    defaults/default_settings.json.

    This single test replaces scattered ``"searxng"`` fallback literals and
    is what prevents the code/registry drift that let the old ``"auto"``
    default linger across ~30 sites. Mirrors
    test_egress_policy::test_default_scope_constant_matches_registry.
    """
    path = DEFAULTS_DIR / "default_settings.json"
    assert path.exists()
    with open(path, encoding="utf-8-sig") as f:
        registry = json.load(f)
    assert registry["search.tool"]["value"] == DEFAULT_SEARCH_TOOL


def test_default_search_tool_is_a_registered_option():
    """The default must be one of the engines offered in the settings UI."""
    path = DEFAULTS_DIR / "default_settings.json"
    with open(path, encoding="utf-8-sig") as f:
        registry = json.load(f)
    option_values = {opt["value"] for opt in registry["search.tool"]["options"]}
    assert DEFAULT_SEARCH_TOOL in option_values


def test_default_local_search_settings_match_registry():
    """Local search default constants must equal the registered defaults in
    defaults/settings_local_search.json to prevent code/registry drift."""
    path = DEFAULTS_DIR / "settings_local_search.json"
    assert path.exists()
    with open(path, encoding="utf-8-sig") as f:
        registry = json.load(f)

    assert (
        registry["local_search_embedding_provider"]["value"]
        == DEFAULT_LOCAL_SEARCH_PROVIDER
    )
    assert (
        registry["local_search_embedding_model"]["value"]
        == DEFAULT_LOCAL_SEARCH_MODEL
    )
    assert (
        registry["local_search_chunk_size"]["value"]
        == DEFAULT_LOCAL_SEARCH_CHUNK_SIZE
    )
    assert (
        registry["local_search_chunk_overlap"]["value"]
        == DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP
    )
    assert (
        registry["local_search_splitter_type"]["value"]
        == DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE
    )
    assert (
        registry["local_search_text_separators"]["value"]
        == DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
    )
    assert (
        json.loads(DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS_JSON)
        == DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
    )
    assert (
        registry["local_search_distance_metric"]["value"]
        == DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC
    )
    assert (
        registry["local_search_normalize_vectors"]["value"]
        == DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS
    )
    assert (
        registry["local_search_index_type"]["value"]
        == DEFAULT_LOCAL_SEARCH_INDEX_TYPE
    )


def test_default_local_search_options_are_registered():
    """Selected defaults for select settings must exist in their option lists."""
    path = DEFAULTS_DIR / "settings_local_search.json"
    with open(path, encoding="utf-8-sig") as f:
        registry = json.load(f)

    provider_options = {
        opt["value"]
        for opt in registry["local_search_embedding_provider"]["options"]
    }
    assert DEFAULT_LOCAL_SEARCH_PROVIDER in provider_options

    splitter_options = {
        opt["value"]
        for opt in registry["local_search_splitter_type"]["options"]
    }
    assert DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE in splitter_options

    distance_options = {
        opt["value"]
        for opt in registry["local_search_distance_metric"]["options"]
    }
    assert DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC in distance_options

    index_options = {
        opt["value"] for opt in registry["local_search_index_type"]["options"]
    }
    assert DEFAULT_LOCAL_SEARCH_INDEX_TYPE in index_options


def test_injected_frontend_local_search_constants_match_defaults():
    """Template rendering (via TemplateResponse in template_config.py) must
    inject local_search_defaults that match the backend constants and the
    registry."""
    from starlette.requests import Request
    from local_deep_research.web.fastapi_app import _setup_template_globals
    from local_deep_research.web.template_config import templates

    _setup_template_globals()
    req = Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/",
            "headers": [],
            "session": {},
        }
    )
    ctx = {"request": req}
    resp = templates.TemplateResponse(
        request=req,
        name="base.html",
        context=ctx,
    )

    assert ctx == {"request": req}
    assert "local_search_defaults" in resp.context
    html = resp.body.decode()
    assert "window.LDR_LOCAL_SEARCH_DEFAULTS" in html
    ls = resp.context["local_search_defaults"]
    assert ls["provider"] == DEFAULT_LOCAL_SEARCH_PROVIDER
    assert ls["model"] == DEFAULT_LOCAL_SEARCH_MODEL
    assert ls["chunk_size"] == DEFAULT_LOCAL_SEARCH_CHUNK_SIZE
    assert ls["chunk_overlap"] == DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP
    assert ls["splitter_type"] == DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE
    assert ls["text_separators"] == DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
    assert ls["distance_metric"] == DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC
    assert ls["normalize_vectors"] == DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS
    assert ls["index_type"] == DEFAULT_LOCAL_SEARCH_INDEX_TYPE


def test_embedding_settings_template_renders_without_syntax_error():
    """The page route renders defaults and the shared model catalog."""
    from starlette.requests import Request
    from local_deep_research.embeddings.sentence_transformer_models import (
        SENTENCE_TRANSFORMER_MODELS,
    )
    from local_deep_research.web.fastapi_app import _setup_template_globals
    from local_deep_research.web.routers.rag import embedding_settings_page

    _setup_template_globals()
    req = Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/library/embedding-settings",
            "headers": [],
            "session": {},
        }
    )
    resp = embedding_settings_page(req, username="testuser")

    html = resp.body.decode()
    assert resp.status_code == 200
    assert html.index("/static/js/security/safe-fetch.js") < html.index(
        "/static/js/embedding_performance_hint.js"
    )
    assert (
        resp.context["sentence_transformer_models"]
        is SENTENCE_TRANSFORMER_MODELS
    )
    assert html.count('href="https://huggingface.co/') == len(
        SENTENCE_TRANSFORMER_MODELS
    )
    for model_name, model in SENTENCE_TRANSFORMER_MODELS.items():
        assert f'href="https://huggingface.co/{model.repository}"' in html
        assert f">{model_name}</a>" in html
        assert model.description in html
        assert f"Input limit: {model.max_seq_length} tokens." in html
    assert f">{DEFAULT_LOCAL_SEARCH_MODEL}</a> (default)" in html
    assert "Default Embedding Settings" in html
    assert 'id="text-separators"' in html
    assert (
        'placeholder="[&quot;\\n\\n&quot;, &quot;\\n&quot;, &quot;. &quot;, &quot; &quot;, &quot;&quot;]"'
        in html
        or '["\\n\\n"' in html
    )
    assert (
        f'id="chunk-size" class="ldr-form-control" value="{DEFAULT_LOCAL_SEARCH_CHUNK_SIZE}"'
        in html
    )
    assert (
        f'id="chunk-overlap" class="ldr-form-control" value="{DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP}"'
        in html
    )
    assert (
        f'<option value="{DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE}" selected>'
        in html
    )
    assert (
        f'<option value="{DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC}" selected>'
        in html
    )
    assert (
        f'<option value="{DEFAULT_LOCAL_SEARCH_INDEX_TYPE}" selected>' in html
    )
    assert 'id="normalize-vectors" checked' in html