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.

5.5 KB
Raw Blame
from __future__ import annotations

from pathlib import Path
from typing import Final, Protocol
from unittest.mock import patch
from uuid import uuid4

import pytest

from local_deep_research.constants import ResearchStatus
from local_deep_research.database.library_init import (
    initialize_library_for_user,
)
from local_deep_research.database.models import (
    Collection,
    Document,
    DocumentCollection,
    ResearchHistory,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.web.queue.processor_v2 import QueueProcessorV2

from tests.connected.conftest import register_connected_user


COMPLETION_NOTIFICATION_PATH: Final = "local_deep_research.web.queue.processor_v2.send_research_completed_notification_from_session"


class ConnectedUserFixture(Protocol):
    # Flask app/client type hints dropped: the branch's ``client`` fixture
    # is a Flask-compat-shimmed Starlette TestClient.
    app: object
    client: object
    username: str
    password: str
    data_root: Path


@pytest.fixture
def initialized_connected_user(
    connected_user: ConnectedUserFixture,
) -> ConnectedUserFixture:
    register_connected_user(connected_user)

    _ = initialize_library_for_user(
        connected_user.username,
        connected_user.password,
    )
    return connected_user


def test_completion_conversion_persists_one_document_when_reported_twice(
    initialized_connected_user: ConnectedUserFixture,
):
    case = initialized_connected_user
    research_id = str(uuid4())
    report_content = "# Connected conversion report\n\nPersisted through the queue completion path."

    # Given: a completed report in the real encrypted per-user database.
    with get_user_db_session(case.username, case.password) as session:
        session.add(
            ResearchHistory(
                id=research_id,
                query="Connected document conversion",
                mode="detailed_report",
                status=ResearchStatus.COMPLETED,
                created_at="2026-07-31T00:00:00",
                report_content=report_content,
                title="Connected conversion report",
            )
        )
        session.commit()

    processor = QueueProcessorV2()

    # When: the production completion path reports the same research twice.
    with patch(COMPLETION_NOTIFICATION_PATH):
        processor.notify_research_completed(
            case.username,
            research_id,
            user_password=case.password,
        )
        processor.notify_research_completed(
            case.username,
            research_id,
            user_password=case.password,
        )

    # Then: conversion is persisted exactly once in the initialized History collection.
    with get_user_db_session(case.username, case.password) as session:
        assert session.query(Document).count() == 1
        assert session.query(DocumentCollection).count() == 1

        document_id = (
            session.query(Document.id)
            .filter(Document.research_id == research_id)
            .scalar()
        )
        assert document_id is not None
        document_text = (
            session.query(Document.text_content)
            .filter(Document.research_id == research_id)
            .scalar()
        )
        association_collection_id = (
            session.query(DocumentCollection.collection_id)
            .filter(DocumentCollection.document_id == document_id)
            .scalar()
        )
        assert association_collection_id is not None
        collection_type = (
            session.query(Collection.collection_type)
            .filter(Collection.id == association_collection_id)
            .scalar()
        )

        assert document_text == report_content
        assert collection_type == "research_history"


@pytest.mark.parametrize(
    ("status", "report_content"),
    (
        (ResearchStatus.IN_PROGRESS, "# Incomplete report"),
        (ResearchStatus.COMPLETED, ""),
    ),
    ids=("incomplete", "empty-content"),
)
def test_completion_conversion_creates_no_rows_when_research_is_not_convertible(
    initialized_connected_user: ConnectedUserFixture,
    status: ResearchStatus,
    report_content: str,
):
    case = initialized_connected_user
    research_id = str(uuid4())

    # Given: an incomplete or contentless report in the real encrypted database.
    with get_user_db_session(case.username, case.password) as session:
        session.add(
            ResearchHistory(
                id=research_id,
                query="Rejected connected conversion",
                mode="detailed_report",
                status=status,
                created_at="2026-07-31T00:00:00",
                report_content=report_content,
                title="Rejected connected conversion",
            )
        )
        session.commit()
        document_count_before = session.query(Document).count()
        association_count_before = session.query(DocumentCollection).count()

    processor = QueueProcessorV2()

    # When: the production completion path is invoked.
    with patch(COMPLETION_NOTIFICATION_PATH):
        processor.notify_research_completed(
            case.username,
            research_id,
            user_password=case.password,
        )

    # Then: no document or collection association was added.
    with get_user_db_session(case.username, case.password) as session:
        assert session.query(Document).count() == document_count_before
        assert (
            session.query(DocumentCollection).count()
            == association_count_before
        )