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.

10.1 KB
Raw Blame
"""Drive the production news payload through parsing and provider construction."""

import json
from pathlib import Path
import re
import shutil
import subprocess
from unittest.mock import Mock, patch

import pytest
from langchain_core.language_models.fake_chat_models import FakeListChatModel

from local_deep_research.config.llm_config import get_llm
from local_deep_research.llm.providers import discover_providers
from local_deep_research.llm.providers.base import normalize_provider
from local_deep_research.news.subscription_runner import (
    resolve_subscription_model,
)
from local_deep_research.web.routers.research import _extract_research_params

NEWS_JS = (
    Path(__file__).resolve().parents[2]
    / "src/local_deep_research/web/static/js/pages/news.js"
)
SUBSCRIPTION_FORM = (
    Path(__file__).resolve().parents[2]
    / "src/local_deep_research/web/templates/pages/news-subscription-form.html"
)
PAYLOAD_PROBE = r"""
const fs = require('node:fs');
const vm = require('node:vm');
const selected = process.argv[2];
let payload;
const context = vm.createContext({
    document: {
        addEventListener() {},
        getElementById() { return { value: selected }; },
    },
    window: {
        api: { getCsrfToken() { return 'test-csrf'; } },
        RESEARCH_STATUS: { QUEUED: 'queued' },
    },
    SafeLogger: { log() {}, error() {} },
    URLS: { API: { START_RESEARCH: '/api/start_research' } },
    fetch: async (url, options) => {
        if (url !== '/api/start_research') throw new Error(url);
        payload = JSON.parse(options.body);
        return { ok: true, json: async () => ({}) };
    },
});
vm.runInContext(fs.readFileSync(process.argv[1], 'utf8'), context);
vm.runInContext('showAlert = () => {}; showNewsAlert = () => {};', context);
vm.runInContext(
    "performAdvancedNewsSearch('news', 'source-based', getNewsSubscriptionModelConfig())",
    context,
).then(() => process.stdout.write(JSON.stringify(payload)));
"""


@pytest.mark.parametrize(
    "selected,provider,model",
    [
        ("OPENAI:gpt-4.1-mini", "openai", "gpt-4.1-mini"),
        ("OLLAMA:llama3.1:8b", "ollama", "llama3.1:8b"),
        ("llama3.1:8b", "ollama", "llama3.1:8b"),
        ("custom:model:tag", "ollama", "custom:model:tag"),
        # A typed model tag stays whole on the default provider, even when
        # its first segment is a provider key in lowercase.
        ("ollama:llama3.1:8b", "ollama", "ollama:llama3.1:8b"),
        ("qwen:7b", "ollama", "qwen:7b"),
        ("OLLAMA:OPENAI:model:tag", "ollama", "OPENAI:model:tag"),
        ("gpt-4.1-mini", "ollama", "gpt-4.1-mini"),
        ("", "ollama", "saved-model:tag"),
    ],
)
def test_news_run_once_model_reaches_provider_constructor(
    selected, provider, model
):
    node = shutil.which("node")
    if node is None:
        pytest.skip(
            "Node.js is required to execute the production news payload"
        )
    result = subprocess.run(
        [node, "-e", PAYLOAD_PROBE, str(NEWS_JS), selected],
        capture_output=True,
        text=True,
        check=True,
        timeout=10,
    )
    payload = json.loads(result.stdout)
    snapshot = {
        "llm.provider": "ollama",
        "llm.model": "saved-model:tag",
        "llm.openai.api_key": "sk-test-key",
        "llm.ollama.url": "http://localhost:11434",
        "llm.supports_max_tokens": True,
        "llm.max_tokens": 4096,
        "rate_limiting.llm_enabled": False,
        "search.tool": "searxng",
    }
    settings = Mock()
    settings.get_setting.side_effect = snapshot.get
    params = _extract_research_params(payload, settings)
    assert params["model_provider"] == provider
    discover_providers(force_refresh=True)
    fake = FakeListChatModel(responses=["unused"])
    target = (
        "local_deep_research.llm.providers.implementations.openai.ChatOpenAI"
        if provider == "openai"
        else "local_deep_research.llm.providers.implementations.ollama.ChatOllama"
    )
    with patch(target, return_value=fake) as constructor:
        llm = get_llm(
            provider=params["model_provider"],
            model_name=params["model"],
            settings_snapshot=snapshot,
        )
    constructor.assert_called_once()
    assert constructor.call_args.kwargs["model"] == model
    assert llm.base_llm is fake


@pytest.mark.parametrize(
    "source", [NEWS_JS, SUBSCRIPTION_FORM], ids=lambda path: path.name
)
def test_news_run_once_provider_set_matches_the_provider_registry(source):
    """Run Once splits a ``PROVIDER:model`` selection only when the prefix
    is in news.js's ``knownProviders`` set; any other prefix stays inside
    the model id and the run silently uses the default provider. The
    subscription form's Test Run resolves saved rows with the same set.
    Both sets are hand-maintained, so pin them to the auto-discovered
    provider registry: a provider added to the registry (or removed from
    it) must be added to (or removed from) both sets too."""
    match = re.search(
        r"const knownProviders = new Set\(\[(.*?)\]\);",
        source.read_text(encoding="utf-8"),
        re.DOTALL,
    )
    assert match is not None, f"knownProviders set not found in {source.name}"
    listed = re.findall(r"'([^']*)'", match.group(1))
    assert len(listed) == len(set(listed)), f"duplicate keys: {listed}"
    registry = set(discover_providers(force_refresh=True))
    assert registry, "provider auto-discovery returned nothing"
    assert set(listed) == registry, (
        f"{source.name} knownProviders is missing "
        f"{sorted(registry - set(listed))} "
        f"and lists unregistered {sorted(set(listed) - registry)}"
    )


def _extract_form_resolver(source: str) -> str:
    """The ``resolveSavedSubscriptionModel`` body out of the template."""
    start = source.index("function resolveSavedSubscriptionModel")
    open_brace = source.index("{", start)
    depth = 0
    for index in range(open_brace, len(source)):
        if source[index] == "{":
            depth += 1
        elif source[index] == "}":
            depth -= 1
            if depth == 0:
                return source[start : index + 1]
    raise AssertionError(
        "resolveSavedSubscriptionModel has an unterminated body"
    )


def test_form_test_run_resolver_drops_non_registry_providers():
    """Test Run must resolve a saved row the way scheduled runs do.

    ``resolve_subscription_model`` keeps a provider only when it names a
    built-in provider key (compared after stripping); a non-registry
    provider is dropped — the run then uses the user's default provider —
    and the model is kept whole. Pinned from the template source so the
    mirror holds without a JS runtime; the behavioural parity matrix runs
    under node in the test below.
    """
    body = _extract_form_resolver(SUBSCRIPTION_FORM.read_text(encoding="utf-8"))
    assert re.search(
        r"if\s*\(\s*provider\s*&&\s*!\s*knownProviders\.has\("
        r"\s*provider\.trim\(\)\.toUpperCase\(\)\s*\)\s*\)"
        r"\s*\{\s*provider\s*=\s*''\s*;",
        body,
    ), body


FORM_RESOLVER_PROBE = r"""
const fs = require('node:fs');
const source = fs.readFileSync(process.argv[1], 'utf8');
const start = source.indexOf('function resolveSavedSubscriptionModel');
const openBrace = source.indexOf('{', start);
let depth = 0;
let end = -1;
for (let i = openBrace; i < source.length; i += 1) {
    if (source[i] === '{') depth += 1;
    else if (source[i] === '}') {
        depth -= 1;
        if (depth === 0) { end = i + 1; break; }
    }
}
const resolve = eval('(' + source.slice(start, end) + ')');
const rows = JSON.parse(process.argv[2]);
const out = rows.map(([provider, model]) => {
    const resolved = resolve(provider, model);
    return [resolved.provider || null, resolved.model || null];
});
process.stdout.write(JSON.stringify(out));
"""


#: ``(provider, model)`` rows covering the resolver matrix's shapes:
#: the old-form uppercase prefix, typed model tags, plain rows, unset
#: values, and non-registry providers (where the Test Run resolver used to
#: keep the provider the scheduled run drops).
FORM_RESOLVER_MATRIX = [
    ("ollama", "OLLAMA:llama3"),
    ("OLLAMA", "OLLAMA:llama3.1:8b"),
    ("ollama", "OLLAMA:llama3.1:8b"),
    ("openai", "OPENAI:gpt-4.1-mini"),
    ("openai_endpoint", "OPENAI_ENDPOINT:org/model"),
    ("ollama", "OLLAMA:OLLAMA:model"),
    ("ollama", "OLLAMA:"),
    ("qwen", "qwen:7b"),
    ("llama3.1", "llama3.1:8b"),
    ("ollama", "ollama:llama3.1:8b"),
    ("ollama", "llama3.1:8b"),
    ("openai", "OPENAI_ENDPOINT:org/model"),
    ("ollama", "OPENAI:model:tag"),
    ("openrouter", "vendor/model:free"),
    ("custom", "my-model"),
    ("custom", "custom:tag"),
    ("custom", "CUSTOM:tag"),
    ("custom", "OLLAMA:llama3"),
    ("", "OLLAMA:llama3"),
    ("ollama", ""),
    ("", ""),
]


def test_form_test_run_resolver_parity_with_the_python_resolver():
    """The template's Test Run resolver must resolve every row to the run
    ``resolve_subscription_model`` describes. An empty resolved field is
    what the request omits (``if (provider) requestData.model_provider =
    provider``), the API spelling of the Python resolver's ``None`` ("use
    the user's setting"); the provider is compared in canonical form,
    because the request carries the raw saved casing and the backend
    normalizes both sides with ``normalize_provider``."""
    node = shutil.which("node")
    if node is None:
        pytest.skip("Node.js is required to execute the template's resolver")
    result = subprocess.run(
        [
            node,
            "-e",
            FORM_RESOLVER_PROBE,
            str(SUBSCRIPTION_FORM),
            json.dumps(FORM_RESOLVER_MATRIX),
        ],
        capture_output=True,
        text=True,
        check=True,
        timeout=10,
    )
    resolved = json.loads(result.stdout)
    assert len(resolved) == len(FORM_RESOLVER_MATRIX)
    for (provider, model), (js_provider, js_model) in zip(
        FORM_RESOLVER_MATRIX, resolved
    ):
        py_provider, py_model = resolve_subscription_model(provider, model)
        assert js_model == py_model, (provider, model)
        assert normalize_provider(js_provider) == normalize_provider(
            py_provider
        ), (provider, model)