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.

4.8 KB
Raw Blame
"""Pin the error-redaction contract of ``scripts/ldr-research.py``.

The script prints its error JSON to stdout, where the CI workflow captures it
into a ``::error::`` annotation and uploads ``response.json`` as a workflow
artifact. Provider and HTTP errors can embed the submitted API keys in their
message text, so the script must render the exception through the security
module's scrub pipeline (``scrub_error`` with the in-scope key literals, then
``sanitize_error_for_client``) and fall back to the bare exception class name
whenever the sanitizer cannot be imported or fails. These tests run the real
script as a subprocess with the live sanitizer package and its dependencies,
so a revert to raw ``str(e)`` fails here instead of leaking keys into CI logs
and artifacts.
"""

import json
import subprocess
import sys
from pathlib import Path

import pytest

import local_deep_research

SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "ldr-research.py"
PACKAGE = Path(local_deep_research.__file__).resolve().parent

OR_KEY = "sk-or-v1-test0123456789abcdef"
# Prefixless provider-key shape: not matched by any credential regex, so it
# only gets redacted because the script passes the in-scope literal to
# scrub_error().
SERPER_KEY = "f4a8b2c9d7e61530aaaabbbb"

API_INIT = """\
import os


def quick_summary(**kwargs):
    mode = os.environ.get("STUB_MODE", "raise")
    if mode == "ok":
        return {
            "formatted_findings": "findings text",
            "sources": [],
            "findings": [],
            "iterations": 2,
        }
    raise RuntimeError(os.environ.get("STUB_ERROR", "boom"))
"""

SETTINGS_UTILS = "def create_settings_snapshot(overrides=None):\n    return {'overrides': True}\n"


def build_stub(root, security_init=""):
    """Materialize a stub local_deep_research package shadowing the real one.

    Stub only the research API. Extend package search paths to the live
    package so sanitizer imports include their transitive dependencies;
    copying individual files can silently exercise the fallback instead.
    """
    pkg = root / "local_deep_research"
    (pkg / "api").mkdir(parents=True)
    (pkg / "security").mkdir(parents=True)
    (pkg / "__init__.py").write_text(
        f"__path__.append({str(PACKAGE)!r})\n", encoding="utf-8"
    )
    (pkg / "api" / "__init__.py").write_text(API_INIT, encoding="utf-8")
    (pkg / "api" / "settings_utils.py").write_text(
        SETTINGS_UTILS, encoding="utf-8"
    )
    (pkg / "security" / "__init__.py").write_text(
        f"__path__.append({str(PACKAGE / 'security')!r})\n{security_init}",
        encoding="utf-8",
    )
    return root


def run_script(stub_root, mode="raise", error_msg="boom"):
    env = {
        "PATH": "/usr/bin:/bin",
        "PYTHONPATH": str(stub_root),
        "OPENROUTER_API_KEY": OR_KEY,
        "SERPER_API_KEY": SERPER_KEY,
        "LDR_PROVIDER": "openrouter",
        "LDR_SEARCH_TOOL": "serper",
        "STUB_MODE": mode,
        "STUB_ERROR": error_msg,
    }
    return subprocess.run(
        [sys.executable, str(SCRIPT)],
        input="what is rag?\n",
        capture_output=True,
        text=True,
        env=env,
        timeout=60,
    )


def test_credential_shaped_keys_are_redacted(tmp_path):
    stub = build_stub(tmp_path)
    proc = run_script(
        stub, error_msg=f"401 Unauthorized: Invalid API key {OR_KEY}"
    )
    assert proc.returncode == 1
    payload = json.loads(proc.stdout)
    assert OR_KEY not in payload["error"]
    assert "REDACTED" in payload["error"]


def test_bare_provider_key_literal_is_redacted(tmp_path):
    stub = build_stub(tmp_path)
    proc = run_script(
        stub, error_msg=f"serper error: key {SERPER_KEY} rejected"
    )
    assert proc.returncode == 1
    payload = json.loads(proc.stdout)
    assert SERPER_KEY not in payload["error"]
    assert "REDACTED" in payload["error"]


@pytest.mark.parametrize(
    "security_init",
    [
        'raise ImportError("stub: security package absent")\n',
        "from . import log_sanitizer\n"
        "def fail(*args, **kwargs):\n"
        "    raise RuntimeError('stub: scrubber failure')\n"
        "log_sanitizer.scrub_error = fail\n",
    ],
    ids=["import-unavailable", "scrubber-failure"],
)
def test_fails_closed_to_exception_class_when_sanitizer_unreachable(
    tmp_path, security_init
):
    stub = build_stub(tmp_path, security_init=security_init)
    proc = run_script(stub, error_msg=f"401 Unauthorized: {OR_KEY}")
    assert proc.returncode == 1
    payload = json.loads(proc.stdout)
    assert payload["error"] == "RuntimeError"
    assert OR_KEY not in proc.stdout


def test_success_path_still_emits_research_json(tmp_path):
    stub = build_stub(tmp_path)
    proc = run_script(stub, mode="ok")
    assert proc.returncode == 0
    payload = json.loads(proc.stdout)
    assert payload["research"] == "findings text"