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.8 KB
Raw Blame
"""The app must actually start up and shut down.

``lifespan()`` in ``web/fastapi_app.py`` is a from-scratch rewrite of Flask's
``app_factory`` startup — it registers the socket.io loop and lock, starts the
log-queue drain, the queue processor, the news scheduler and the cleanup
scheduler, and tears them all down in a specific order.

Nothing in the merge gate executed it. ``TestClient(app)`` used bare (the form
every other test uses, 77 instantiations across 53 files) does NOT run
lifespan events — only the context-manager form does. So a change that made
the app fail to start, or hang on shutdown, would pass the entire Python
suite.

These tests are deliberately cheap and few. Their job is to make "the app
boots" a thing CI can fail on, not to re-test the subsystems.

IMPORTANT — one enter/exit per process. ``socketio_asgi.init_lock()`` only
assigns when ``_lock is None``, so a second lifespan cycle in the same process
would leave the ``asyncio.Lock`` bound to a dead loop. Do not turn this into a
reusable fixture, and do not add a second lifespan-entering test to the file.
"""

import weakref

import pytest
from fastapi.testclient import TestClient


@pytest.mark.lifespan
def test_app_boots_serves_and_shuts_down_cleanly(tmp_path):
    """Enter lifespan, prove the app is live, then exit cleanly."""
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.services import socketio_asgi
    from local_deep_research.embeddings.model_cache import (
        shared_sentence_transformer_embeddings,
    )

    model_refs = []

    class CyclicModel:
        def __init__(self, **kwargs):
            self.cycle = self
            model_refs.append(weakref.ref(self))

    # Deliberately NOT asserting that _main_loop is None here. Some fixtures
    # elsewhere in the suite enter lifespan (leaving a closed loop behind), so
    # a precondition assertion would make this test pass or fail on collection
    # order — the exact defect class this file exists to help retire. The
    # post-conditions below hold regardless of what ran first.
    with TestClient(app) as client:
        # Startup ran, and registered a LIVE loop. Checking liveness rather
        # than mere non-None is what makes this order-independent: a stale
        # closed loop left by an earlier cycle would satisfy `is not None`
        # while being useless to the background threads that schedule emits
        # onto it.
        loop = socketio_asgi._main_loop
        assert loop is not None, (
            "lifespan did not register the event loop; background threads "
            "cannot schedule socket.io emits without it"
        )
        assert not loop.is_closed(), (
            "lifespan registered a CLOSED event loop — background threads "
            "would schedule emits onto a dead loop"
        )
        assert socketio_asgi._lock is not None, (
            "lifespan did not initialise the socket.io lock"
        )

        # And the app genuinely serves. /api/v1/health is unauthenticated and
        # is skipped by DatabaseMiddleware, so it exercises the stack without
        # needing a user.
        resp = client.get("/api/v1/health")
        assert resp.status_code == 200, (
            f"app booted but health returned {resp.status_code}: "
            f"{resp.text[:200]}"
        )
        shared_sentence_transformer_embeddings(
            CyclicModel,
            model_name="synthetic/shutdown",
            cache_folder=str(tmp_path),
            model_kwargs={},
        )
        assert model_refs[0]() is not None

    # Exiting the context manager ran shutdown. Reaching here at all means it
    # completed rather than hanging — the failure mode that would otherwise
    # only show up as a killed container.
    assert model_refs[0]() is None, (
        "Shutdown retained the resident embedding model"
    )