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.

23.6 KB
Raw Blame
"""The mutating research routes: what they actually DO, not just what they
answer.

Ported from the Flask-era ``tests/web/routes/test_research_routes_deep_coverage.py``
and ``..._extra_coverage.py``, deleted by the FastAPI migration. The
handlers survived; these particular assertions did not, and the branch's
successors stop at the response.

The specific holes each test closes:

* **``clear_history`` never proves it deletes anything.** The two branch
  tests that drive it
  (``tests/web/queue/test_queued_research_lifecycle_races.py``) both check
  that a PROTECTED row survives, and both stub ``get_active_research_ids``
  to ``[]``. Nothing asserts a deletable row is gone, and nothing reaches
  ``cleanup_queued_research_state`` (``deleted_ids`` is empty in both), so
  a handler that computed ``deletable_ids = []`` would pass the suite
  while silently doing nothing.
* **``delete_research``'s early ``IN_PROGRESS -> 400``.** Only the later
  ``~claimed_queue_row`` guard is pinned; the early return can be removed
  on its own and stay green.
* **``terminate_research``'s ``progress_log`` handling and its
  spawn-grace boundary.** The JSON-string parse, healthy-list persistence,
  and "cleanup runs only in the QUEUED arm" negative need real row checks.
* **``get_history``'s item payload.** Not one field of the listing is
  asserted anywhere on the branch.
* **``get_research_report``'s ``None``-vs-``""`` distinction.** Pinned for
  ``routers/history.py`` by ``test_history_report_unit.py`` -- a different
  handler. Getting it backwards here turns every empty-but-finished
  report into a 404, or every missing one into a 200 with no content.
* **``upload_pdf``'s success payload and its file-count guard.** Nothing
  asserts a successful extraction, and
  ``tests/pdf_tests/test_pdf_upload.py::test_upload_too_many_files_rejected``
  passes for the wrong reason -- its 201 fake files all fail extraction,
  so ``processed_files == 0`` produces the 400 whether or not
  ``validate_file_count`` exists. This asserts the count guard's own
  message instead.

Sessions are real in-memory SQLite wherever the assertion is "a row is
gone" -- a MagicMock session cannot show that. FastAPI runs sync handlers
on anyio's threadpool, hence StaticPool + ``check_same_thread=False``.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

_RR = "local_deep_research.web.routers.research"
_LIFECYCLE_CLEANUP = (
    "local_deep_research.web.queue.lifecycle_cleanup."
    "cleanup_queued_research_state"
)
_ASSEMBLY = "local_deep_research.web.services.report_assembly_service"

_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


@pytest.fixture
def db():
    """A real in-memory database with the full schema; yields a session."""
    from local_deep_research.database.models import Base

    engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    try:
        yield session
    finally:
        session.close()
        engine.dispose()


def _patch_session(session):
    @contextmanager
    def _ctx(*_args, **_kwargs):
        yield session

    return patch(f"{_RR}.get_user_db_session", _ctx)


def _add_research(session, rid, status="completed", **kwargs):
    from local_deep_research.database.models import ResearchHistory

    row = ResearchHistory(
        id=rid,
        query=kwargs.pop("query", "q"),
        mode=kwargs.pop("mode", "quick"),
        status=status,
        created_at=kwargs.pop("created_at", "2025-01-01T00:00:00+00:00"),
        **kwargs,
    )
    session.add(row)
    session.commit()
    return row


def _ids(session):
    from local_deep_research.database.models import ResearchHistory

    return {row.id for row in session.query(ResearchHistory).all()}


# ---------------------------------------------------------------------------
# POST /api/clear_history
# ---------------------------------------------------------------------------


def test_clear_history_deletes_the_deletable_rows(authenticated_client, db):
    """The positive half nothing on the branch asserts: a finished
    research is actually removed, and the queue-state cleanup is invoked
    with exactly the ids that were deleted."""
    _add_research(db, "done-1", status="completed")
    _add_research(db, "done-2", status="failed")

    with (
        _patch_session(db),
        patch(f"{_RR}.get_active_research_ids", return_value=[]),
        patch(_LIFECYCLE_CLEANUP) as cleanup,
    ):
        response = authenticated_client.post("/api/clear_history")

    assert response.status_code == 200, response.text[:300]
    assert response.get_json()["status"] == "success"
    assert _ids(db) == set(), "clear_history left rows behind"
    cleanup.assert_called_once()
    assert sorted(cleanup.call_args[0][1]) == ["done-1", "done-2"]


def test_clear_history_protects_a_research_the_registry_says_is_active(
    authenticated_client, db
):
    """The in-memory registry is a second source of protection beside the
    IN_PROGRESS column: a row whose DB status is stale but which a worker
    is still running must survive. Both branch tests stub
    ``get_active_research_ids`` to ``[]``, so this arm is untested there.
    """
    _add_research(db, "running", status="completed")
    _add_research(db, "done", status="completed")

    with (
        _patch_session(db),
        patch(f"{_RR}.get_active_research_ids", return_value=["running"]),
        patch(_LIFECYCLE_CLEANUP) as cleanup,
    ):
        response = authenticated_client.post("/api/clear_history")

    assert response.status_code == 200, response.text[:300]
    assert _ids(db) == {"running"}, (
        "a research the registry reports as active was cleared anyway"
    )
    assert cleanup.call_args[0][1] == ["done"]


def test_clear_history_protects_an_in_progress_row(authenticated_client, db):
    _add_research(db, "live", status="in_progress")
    _add_research(db, "done", status="completed")

    with (
        _patch_session(db),
        patch(f"{_RR}.get_active_research_ids", return_value=[]),
        patch(_LIFECYCLE_CLEANUP),
    ):
        response = authenticated_client.post("/api/clear_history")

    assert response.status_code == 200, response.text[:300]
    assert _ids(db) == {"live"}


def test_clear_history_removes_only_deleted_research_counters(
    authenticated_client, db
):
    """A cleared row must not keep occupying a per-user research slot."""
    from local_deep_research.database.models import UserActiveResearch

    _add_research(db, "done", status="completed")
    _add_research(db, "live", status="in_progress")
    db.add_all(
        [
            UserActiveResearch(username="owner", research_id="done"),
            UserActiveResearch(username="owner", research_id="live"),
        ]
    )
    db.commit()

    with (
        _patch_session(db),
        patch(f"{_RR}.get_active_research_ids", return_value=[]),
        patch(_LIFECYCLE_CLEANUP),
    ):
        response = authenticated_client.post("/api/clear_history")

    assert response.status_code == 200, response.text[:300]
    assert _ids(db) == {"live"}
    assert {row.research_id for row in db.query(UserActiveResearch).all()} == {
        "live"
    }


# ---------------------------------------------------------------------------
# DELETE /api/delete/{research_id}
# ---------------------------------------------------------------------------


def test_delete_refuses_an_in_progress_research(authenticated_client, db):
    """The early ``status == IN_PROGRESS -> 400`` return, isolated from
    the later ``~claimed_queue_row`` filter: no queue row exists here, so
    only the early guard can produce this 400."""
    _add_research(db, "live", status="in_progress")

    with _patch_session(db), patch(_LIFECYCLE_CLEANUP):
        response = authenticated_client.delete("/api/delete/live")

    assert response.status_code == 400, response.text[:300]
    assert "in progress" in response.get_json()["message"].lower()
    assert _ids(db) == {"live"}


def test_delete_removes_the_row_and_cleans_the_queue_state(
    authenticated_client, db
):
    _add_research(db, "done", status="completed")

    with _patch_session(db), patch(_LIFECYCLE_CLEANUP) as cleanup:
        response = authenticated_client.delete("/api/delete/done")

    assert response.status_code == 200, response.text[:300]
    assert response.get_json()["status"] == "success"
    assert _ids(db) == set()
    cleanup.assert_called_once()
    assert cleanup.call_args[0][1] == ["done"]


# ---------------------------------------------------------------------------
# POST /api/terminate/{research_id}
# ---------------------------------------------------------------------------


def _terminate_mocks(active, cleanup):
    """Patch the seams terminate_research reaches once it decides to act."""
    return [
        patch(f"{_RR}.is_research_active", return_value=active),
        patch(f"{_RR}.set_termination_flag"),
        patch(f"{_RR}.get_research_field", return_value=50),
        patch(f"{_RR}.append_research_log"),
        patch(_LIFECYCLE_CLEANUP, cleanup),
        patch(
            "local_deep_research.web.services.socketio_asgi.emit_to_subscribers"
        ),
    ]


@pytest.mark.parametrize(
    "status", ["completed", "failed", "error", "suspended"]
)
def test_terminate_reports_existing_terminal_status_without_cancelling(
    authenticated_client, db, status
):
    row = _add_research(db, "already-terminal", status=status)
    with (
        _patch_session(db),
        patch(f"{_RR}.set_termination_flag") as terminate_flag,
    ):
        response = authenticated_client.post("/api/terminate/already-terminal")

    assert response.status_code == 200
    assert response.json()["status"] == "success"
    assert response.json()["research_status"] == status
    assert row.status == status
    terminate_flag.assert_not_called()


def test_terminate_parses_a_progress_log_stored_as_a_json_string(
    authenticated_client, db
):
    """``progress_log`` is a JSON column that older rows hold as a
    string. The handler must parse it before appending, or the
    termination entry is concatenated onto text and the column becomes
    unreadable. Nothing on the branch touches this branch at all.
    """
    row = _add_research(
        db,
        "res-1",
        status="in_progress",
        progress_log='[{"time": "t", "progress": 0}]',
    )
    cleanup = MagicMock()

    with _patch_session(db):
        for patcher in _terminate_mocks(active=True, cleanup=cleanup):
            patcher.start()
        try:
            response = authenticated_client.post("/api/terminate/res-1")
        finally:
            patch.stopall()

    assert response.status_code == 200, response.text[:300]
    db.expire_all()
    assert isinstance(row.progress_log, list)
    assert len(row.progress_log) == 2
    assert row.progress_log[-1]["message"] == (
        "Research termination requested by user"
    )


def test_terminate_persists_entry_for_a_list_progress_log(
    authenticated_client, db
):
    """A successful Stop must keep its audit entry after the row is reloaded."""
    from local_deep_research.database.models import ResearchHistory

    _add_research(
        db,
        "res-list",
        status="in_progress",
        progress_log=[{"time": "t0", "message": "started", "progress": 0}],
    )
    cleanup = MagicMock()

    with _patch_session(db):
        for patcher in _terminate_mocks(active=True, cleanup=cleanup):
            patcher.start()
        try:
            response = authenticated_client.post("/api/terminate/res-list")
        finally:
            patch.stopall()

    assert response.status_code == 200, response.text[:300]
    db.expire_all()
    stored = db.get(ResearchHistory, "res-list")
    assert stored.status == "suspended"
    assert isinstance(stored.progress_log, list)
    assert len(stored.progress_log) == 2
    assert stored.progress_log[-1]["metadata"]["phase"] == "termination"


@pytest.mark.parametrize("initial_progress_log", ["{not json", '{"time": "t"}'])
def test_terminate_recovers_from_malformed_progress_log_text(
    authenticated_client, db, initial_progress_log
):
    """Malformed text or a JSON object must not prevent a persisted Stop."""
    row = _add_research(
        db, "res-1", status="in_progress", progress_log=initial_progress_log
    )
    cleanup = MagicMock()

    with _patch_session(db):
        for patcher in _terminate_mocks(active=True, cleanup=cleanup):
            patcher.start()
        try:
            response = authenticated_client.post("/api/terminate/res-1")
        finally:
            patch.stopall()

    assert response.status_code == 200, response.text[:300]
    db.expire_all()
    assert isinstance(row.progress_log, list)
    assert len(row.progress_log) == 1


@pytest.mark.parametrize(
    "stored_log", ["true", 5], ids=["text-scalar", "numeric-carrier"]
)
def test_terminate_recovers_from_a_non_list_progress_log(
    authenticated_client, db, stored_log
):
    """A JSON column accepts any shape, and Stop must not care.

    ``progress_log`` is a ``JSON`` column, so nothing constrains it to a
    list: a legacy TEXT ``"true"`` parses to a bool, and a value the driver
    already decoded arrives as an int (SQLite's NUMERIC affinity stores
    ``json.dumps(5)`` as an integer). Either one reached
    ``current_log.append`` in the parse arm, raising ``AttributeError``
    outside the ``try`` -- so a single odd row turned Stop into a 500 and
    left the run alive. The column is a log or it is nothing; a non-list
    starts a fresh log instead of failing the request.

    The unparseable case above stops one branch earlier (the ``except``),
    so neither test covers the other's path.
    """
    row = _add_research(
        db, "res-1", status="in_progress", progress_log=stored_log
    )
    cleanup = MagicMock()

    with _patch_session(db):
        for patcher in _terminate_mocks(active=True, cleanup=cleanup):
            patcher.start()
        try:
            response = authenticated_client.post("/api/terminate/res-1")
        finally:
            patch.stopall()

    assert response.status_code == 200, response.text[:300]
    assert isinstance(row.progress_log, list)
    assert len(row.progress_log) == 1
    assert row.status == "suspended"


def test_terminate_during_the_spawn_grace_window_leaves_queue_state_alone(
    authenticated_client, db
):
    """The negative half of the queued-cleanup rule. An IN_PROGRESS row
    whose worker has not registered yet gets the termination flag and a
    SUSPENDED status, but must NOT have its queued lifecycle state
    reaped -- that state belongs to the handoff still in flight.

    Only the positive arm is observable through the branch's lifecycle
    test, so a handler that called cleanup unconditionally passes there.
    """
    row = _add_research(db, "res-1", status="in_progress")
    cleanup = MagicMock()

    with _patch_session(db):
        for patcher in _terminate_mocks(active=False, cleanup=cleanup):
            patcher.start()
        try:
            response = authenticated_client.post("/api/terminate/res-1")
        finally:
            patch.stopall()

    assert response.status_code == 200, response.text[:300]
    cleanup.assert_not_called()
    assert row.status == "suspended"


# ---------------------------------------------------------------------------
# GET /api/history -- the item payload
# ---------------------------------------------------------------------------


def test_history_items_carry_title_duration_and_document_count(
    authenticated_client, db
):
    """Every field the history page renders. None of them is asserted
    anywhere on the branch, so the listing could return bare ids and the
    suite would not notice."""
    from local_deep_research.database.models import Document

    _add_research(
        db,
        "r-1",
        status="completed",
        title="A Title",
        created_at="2025-01-01T10:00:00+00:00",
        completed_at="2025-01-01T11:00:00+00:00",
    )
    from datetime import UTC, datetime

    for i in range(2):
        db.add(
            Document(
                id=f"doc-{i}",
                source_type_id="src-1",
                research_id="r-1",
                document_hash=f"hash-{i}",
                title=f"doc {i}",
                original_url=f"https://example.test/{i}",
                file_path=f"/tmp/doc-{i}.pdf",
                file_size=1,
                file_type="pdf",
                status="completed",
                processed_at=datetime.now(UTC),
            )
        )
    db.commit()

    with _patch_session(db):
        response = authenticated_client.get("/api/history")

    assert response.status_code == 200, response.text[:300]
    item = response.get_json()["items"][0]
    assert item["id"] == "r-1"
    assert item["query"] == "q"
    assert item["title"] == "A Title"
    assert item["status"] == "completed"
    assert item["duration_seconds"] == 3600
    assert item["document_count"] == 2


def test_history_item_without_a_completed_at_has_no_duration(
    authenticated_client, db
):
    """The control: ``duration_seconds`` must be ``None``, not 0 and not
    an error, for a run that has not finished."""
    _add_research(db, "r-1", status="in_progress", completed_at=None)

    with _patch_session(db):
        response = authenticated_client.get("/api/history")

    assert response.status_code == 200, response.text[:300]
    item = response.get_json()["items"][0]
    assert item["duration_seconds"] is None
    assert item["document_count"] == 0
    assert "title" not in item


# ---------------------------------------------------------------------------
# GET /api/report/{research_id} -- None vs ""
# ---------------------------------------------------------------------------


def test_report_returns_404_only_when_assembly_returns_none(
    authenticated_client, db
):
    """``None`` means "no report"; ``""`` means "a report that is empty",
    which is a real state for a run that produced nothing. Both halves
    are asserted so the two cannot be collapsed in either direction --
    getting it backwards either 404s every finished-but-empty report or
    serves a 200 with no content for one that does not exist.

    ``test_history_report_unit.py`` pins exactly this pair, but on
    ``routers/history.py``'s handler, not this one.
    """
    _add_research(db, "res-1", status="completed")

    with (
        _patch_session(db),
        patch(f"{_ASSEMBLY}.assemble_full_report", return_value=None),
    ):
        missing = authenticated_client.get("/api/report/res-1")
    assert missing.status_code == 404, missing.text[:300]

    with (
        _patch_session(db),
        patch(f"{_ASSEMBLY}.assemble_full_report", return_value=""),
        patch(f"{_ASSEMBLY}.get_research_source_links_batch", return_value={}),
    ):
        empty = authenticated_client.get("/api/report/res-1")
    assert empty.status_code == 200, empty.text[:300]
    assert empty.get_json()["content"] == ""


# ---------------------------------------------------------------------------
# POST /api/upload/pdf
# ---------------------------------------------------------------------------


def test_upload_pdf_returns_the_extracted_text(authenticated_client):
    """The success payload. Nothing on the branch asserts a successful
    extraction -- ``processed_files``, ``extracted_texts`` and
    ``combined_text`` appear in no live assertion, so the whole
    happy-path response shape is unpinned."""
    service = MagicMock()
    service.extract_text_and_metadata.return_value = {
        "success": True,
        "filename": "test.pdf",
        "text": "Hello world",
        "size": 1024,
        "pages": 1,
    }
    validator = MagicMock()
    validator.MAX_FILE_SIZE = 50 * 1024 * 1024
    validator.MAX_FILES_PER_REQUEST = 100
    validator.validate_file_count.return_value = (True, None)
    # The route now consults the per-part size guard (validate_file_size)
    # before reading each spooled part; without this stub the mock returns
    # a non-iterable MagicMock and the guard's tuple unpack raises.
    validator.validate_file_size.return_value = (True, None)
    validator.validate_upload.return_value = (True, None)

    with (
        patch(f"{_RR}.get_pdf_extraction_service", return_value=service),
        patch(f"{_RR}.FileUploadValidator", validator),
    ):
        response = authenticated_client.post(
            "/api/upload/pdf",
            files={"files": ("test.pdf", b"%PDF-1.4 test", "application/pdf")},
        )

    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["status"] == "success"
    assert body["processed_files"] == 1
    assert body["total_files"] == 1
    assert body["extracted_texts"][0]["text"] == "Hello world"
    assert body["extracted_texts"][0]["filename"] == "test.pdf"
    assert body["combined_text"].startswith("--- From test.pdf ---")
    assert body["errors"] == []


def test_upload_pdf_rejects_an_over_count_batch_with_the_count_message(
    authenticated_client,
):
    """``validate_file_count``'s own refusal, asserted by its message.

    ``tests/pdf_tests/test_pdf_upload.py::test_upload_too_many_files_rejected``
    sends 201 fake files that all fail extraction, so the 400 it observes
    is the ``processed_files == 0`` one and the count guard could be
    deleted without it noticing. Here the file would extract fine; only
    the count guard can produce this response.
    """
    service = MagicMock()
    service.extract_text_and_metadata.return_value = {
        "success": True,
        "filename": "test.pdf",
        "text": "Hello world",
        "size": 1,
        "pages": 1,
    }
    validator = MagicMock()
    validator.MAX_FILE_SIZE = 50 * 1024 * 1024
    validator.MAX_FILES_PER_REQUEST = 100
    validator.validate_file_count.return_value = (False, "Too many files")
    validator.validate_upload.return_value = (True, None)

    with (
        patch(f"{_RR}.get_pdf_extraction_service", return_value=service),
        patch(f"{_RR}.FileUploadValidator", validator),
    ):
        response = authenticated_client.post(
            "/api/upload/pdf",
            files={"files": ("test.pdf", b"%PDF-1.4 test", "application/pdf")},
        )

    assert response.status_code == 400, response.text[:300]
    assert response.get_json()["error"] == "Too many files"


def test_upload_pdf_reports_a_failed_extraction_in_errors(
    authenticated_client,
):
    """``result["error"]`` must reach the caller -- "Encrypted PDF" is
    actionable, a bare 400 is not."""
    service = MagicMock()
    service.extract_text_and_metadata.return_value = {
        "success": False,
        "error": "Encrypted PDF",
        "filename": "locked.pdf",
    }
    validator = MagicMock()
    validator.MAX_FILE_SIZE = 50 * 1024 * 1024
    validator.MAX_FILES_PER_REQUEST = 100
    validator.validate_file_count.return_value = (True, None)
    # Premise updated for the pre-read size guard (GHSA-38fh finding 2):
    # the route calls validate_file_size before file.read(); without this
    # stub the mock's return is not unpackable and the part errors out
    # before the extraction service is reached.
    validator.validate_file_size.return_value = (True, None)
    validator.validate_upload.return_value = (True, None)

    with (
        patch(f"{_RR}.get_pdf_extraction_service", return_value=service),
        patch(f"{_RR}.FileUploadValidator", validator),
    ):
        response = authenticated_client.post(
            "/api/upload/pdf",
            files={"files": ("locked.pdf", b"%PDF-1.4", "application/pdf")},
        )

    assert response.status_code == 400, response.text[:300]
    body = response.get_json()
    assert body["status"] == "error"
    assert any("Encrypted PDF" in err for err in body["errors"])