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.

2.0 KB
Raw Blame
"""Shared helpers for Notes tests."""

import hashlib


def _generate_hash(content: str) -> str:
    """Generate a simple hash for test documents."""
    return hashlib.sha256(content.encode()).hexdigest()


def seed_note_documents(session, source_type_id, doc_ids, titles=None):
    """Seed minimal note Documents (source_type='note') for ``doc_ids``.

    The orphan filter added to ``semantic_search`` / ``find_similar_passages``
    drops any FAISS hit whose ``doc_id`` is absent from the Document lookup
    (built from ``session.query(Document).filter(Document.id.in_(doc_ids))``).
    Tests that mock the search engine must therefore seed real Document rows
    for the doc_ids their engine returns, or every hit is filtered out.

    Args:
        session: a real SQLAlchemy session (the ``db_session`` fixture).
        source_type_id: the ``note`` SourceType id (``note_source_type.id``).
        doc_ids: iterable of document ids to create.
        titles: optional mapping ``{doc_id: title}`` (defaults to ``Note <id>``).

    Returns the list of created Document instances.
    """
    from local_deep_research.database.models import Document

    titles = titles or {}
    created = []
    for doc_id in doc_ids:
        content = f"content for {doc_id}"
        doc = Document(
            id=doc_id,
            title=titles.get(doc_id, f"Note {doc_id}"),
            text_content=content,
            file_type="note",
            file_size=0,
            source_type_id=source_type_id,
            document_hash=_generate_hash(f"{doc_id}:{content}"),
            tags=[],
        )
        session.add(doc)
        created.append(doc)
    session.commit()
    return created


def _unwrap(fn):
    """Return a route view's undecorated function.

    Flask route handlers are wrapped by auth/error decorators; tests that
    call them directly want the inner function. A single peel is enough for
    these handlers (they expose ``__wrapped__`` once); falls back to ``fn``
    when it isn't wrapped.
    """
    return fn.__wrapped__ if hasattr(fn, "__wrapped__") else fn