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.

3.1 KB
Raw Blame
"""Server startup applies process policy before dispatching work."""

import asyncio
from unittest.mock import Mock

import pytest

from local_deep_research.security import process_dump_policy as policy
from local_deep_research.web import app as web_app
from local_deep_research.web import fastapi_app
from local_deep_research.web.services import socketio_asgi


@pytest.fixture
def web_startup(monkeypatch):
    monkeypatch.setattr(web_app, "_install_thread_excepthook", Mock())
    monkeypatch.setattr(web_app, "config_logger", Mock())
    monkeypatch.setattr(
        web_app,
        "load_server_config",
        Mock(
            return_value={
                "host": "127.0.0.1",
                "port": 5000,
                "debug": False,
                "use_https": False,
            }
        ),
    )
    from local_deep_research.vector_stores import legacy_cleanup

    migrate = Mock()
    serve = Mock()
    monkeypatch.setattr(legacy_cleanup, "migrate_legacy_docstores", migrate)
    monkeypatch.setattr(web_app, "_run_with_uvicorn", serve)
    return migrate, serve


def test_web_command_enforces_policy_before_uvicorn(monkeypatch, web_startup):
    migrate, serve = web_startup
    order = []
    monkeypatch.setattr(
        policy, "enforce_process_dump_policy", lambda: order.append("policy")
    )
    migrate.side_effect = lambda: order.append("migration")
    serve.side_effect = lambda *_args: order.append("serve")

    web_app.main()

    assert order == ["policy", "migration", "serve"]


def test_web_command_stops_when_policy_fails(monkeypatch, web_startup):
    migrate, serve = web_startup
    monkeypatch.setattr(
        policy,
        "enforce_process_dump_policy",
        Mock(side_effect=policy.ProcessDumpPolicyError("synthetic refusal")),
    )

    with pytest.raises(policy.ProcessDumpPolicyError):
        web_app.main()

    migrate.assert_not_called()
    serve.assert_not_called()


def test_asgi_lifespan_enforces_policy_before_background_setup(monkeypatch):
    order = []

    class StopStartup(RuntimeError):
        pass

    def stop_before_setup(_loop):
        order.append("background")
        raise StopStartup

    monkeypatch.setattr(
        policy, "enforce_process_dump_policy", lambda: order.append("policy")
    )
    monkeypatch.setattr(socketio_asgi, "set_main_loop", stop_before_setup)

    async def start():
        async with fastapi_app.lifespan(fastapi_app.app):
            pytest.fail("Startup should stop before acquiring resources")

    with pytest.raises(StopStartup):
        asyncio.run(start())

    assert order == ["policy", "background"]


def test_asgi_policy_failure_stops_before_background_setup(monkeypatch):
    background = Mock()
    monkeypatch.setattr(socketio_asgi, "set_main_loop", background)
    monkeypatch.setattr(
        policy,
        "enforce_process_dump_policy",
        Mock(side_effect=policy.ProcessDumpPolicyError("synthetic refusal")),
    )

    async def start():
        async with fastapi_app.lifespan(fastapi_app.app):
            pytest.fail("Startup should be refused")

    with pytest.raises(policy.ProcessDumpPolicyError):
        asyncio.run(start())

    background.assert_not_called()