~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Exhaustive tests for on-the-spot PDF persistence and auto-persist gating.
Covers helpers, versioning, gating matrix, executor bounds, sweep paths,
and regression pins that the feature test file does not exercise.
"""
import hashlib
from datetime import UTC, datetime
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from local_deep_research.constants import FILE_PATH_TEXT_ONLY, ResearchStatus
from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import DownloadTracker
from local_deep_research.database.models.library import (
Collection,
Document,
DocumentCollection,
DocumentStatus,
SourceType,
)
from local_deep_research.database.models.research import (
ResearchHistory,
ResearchResource,
)
from local_deep_research.research_library.services import (
download_service as ds,
)
from local_deep_research.research_library.services.download_service import (
DownloadService,
auto_persist_research_sources,
)
@pytest.fixture
def db():
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
session.add(Collection(id="default-library", name="Library"))
session.add(
SourceType(
id="st-research",
name="research_download",
display_name="Research Download",
)
)
session.add(
ResearchHistory(
id="res-123",
query="Quantum Computing",
mode="quick",
status=ResearchStatus.COMPLETED,
created_at=datetime.now(UTC).isoformat(),
)
)
session.commit()
yield session
session.close()
def _mock_db_session(monkeypatch, session):
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_user_db_session",
lambda *a, **k: MagicMock(
__enter__=lambda s: session, __exit__=lambda *a: None
),
)
def _settings_mock(monkeypatch, values):
mock_settings = Mock()
mock_settings.get_setting.side_effect = lambda k, d=None: values.get(k, d)
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_settings_manager",
lambda *a, **k: mock_settings,
)
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_default_library_id",
lambda *a, **k: "default-library",
)
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_source_type_id",
lambda *a, **k: "st-research",
)
return mock_settings
def _live_settings(monkeypatch, auto=True, mode="database"):
return _settings_mock(
monkeypatch,
{
"research_library.auto_persist_research_sources": auto,
"research_library.pdf_storage_mode": mode,
"research_library.max_pdf_size_mb": 50,
},
)
def _make_resource(db, rid, url, research_id="res-123", document_id=None):
r = ResearchResource(
id=rid,
research_id=research_id,
url=url,
title=f"Resource {rid}",
document_id=document_id,
created_at=datetime.now(UTC).isoformat(),
)
db.add(r)
db.commit()
return r
def _stub_service(settings_values):
mock_settings = Mock()
mock_settings.get_setting.side_effect = lambda k, d=None: (
settings_values.get(k, d)
)
service = DownloadService.__new__(DownloadService)
service.username = "testuser"
service.password = None
service.settings = mock_settings
service.library_root = "/fake/root"
return service
# ---------------------------------------------------------------------------
# _is_pdf_bytes
# ---------------------------------------------------------------------------
def test_is_pdf_bytes_direct_magic():
assert ds._is_pdf_bytes(b"%PDF-1.4 hello") is True
def test_is_pdf_bytes_leading_whitespace():
assert ds._is_pdf_bytes(b"\n\n%PDF-1.4 hello") is True
def test_is_pdf_bytes_leading_bytes_within_1024():
assert ds._is_pdf_bytes(b" " * 1000 + b"%PDF-1.4") is True
def test_is_pdf_bytes_header_beyond_1024_rejected():
assert ds._is_pdf_bytes(b" " * 1025 + b"%PDF-1.4") is False
def test_is_pdf_bytes_empty_and_none():
assert ds._is_pdf_bytes(b"") is False
assert ds._is_pdf_bytes(None) is False
assert ds._is_pdf_bytes(b"%P") is False
def test_is_pdf_bytes_non_pdf():
assert ds._is_pdf_bytes(b"<html>not a pdf</html>") is False
def test_is_pdf_bytes_html_mentioning_pdf_magic():
html_doc = b"<!DOCTYPE html><html><body>Document describing %PDF-1.7 format</body></html>"
assert ds._is_pdf_bytes(html_doc) is False
assert (
ds._is_pdf_bytes(b"<html><head><title>%PDF spec</title></head></html>")
is False
)
assert ds._is_pdf_bytes(b'{"magic": "%PDF-1.4", "type": "doc"}') is False
assert ds._is_pdf_bytes(b"<body><h1>%PDF Reference</h1></body>") is False
assert ds._is_pdf_bytes(b"<head><title>%PDF</title></head>") is False
def test_is_pdf_bytes_binary_stream_with_leading_junk():
binary_pdf = b"\x00\xff\xfe\x80junk%PDF-1.4 binary data\x00\x01"
assert ds._is_pdf_bytes(binary_pdf) is True
def test_bom_prefixed_pdf_stays_a_pdf():
assert ds._is_pdf_bytes(b"\xef\xbb\xbf%PDF-1.4 hello") is True
assert ds._is_pdf_bytes(b"\x00\x00\x00\x00%PDF-1.4 nul padded") is True
assert ds._is_pdf_bytes(b"Copyright 17 byte\n%PDF-1.4 ascii prefix") is True
# ---------------------------------------------------------------------------
# _is_auto_persist_enabled
# ---------------------------------------------------------------------------
def test_auto_enabled_snapshot_true():
assert (
ds._is_auto_persist_enabled(
{"research_library.auto_persist_research_sources": True},
"u",
)
is True
)
def test_auto_enabled_snapshot_false():
assert (
ds._is_auto_persist_enabled(
{"research_library.auto_persist_research_sources": False},
"u",
)
is False
)
def test_auto_enabled_non_boolean_snapshot_values_disabled():
for non_bool in (
"false",
"off",
"true",
1,
0,
{"value": False},
{"value": "true"},
{"value": 1},
[True],
):
assert (
ds._is_auto_persist_enabled(
{"research_library.auto_persist_research_sources": non_bool},
"u",
)
is False
)
def test_auto_enabled_wrapped_dict_snapshot():
assert (
ds._is_auto_persist_enabled(
{"research_library.auto_persist_research_sources": {"value": True}},
"u",
)
is True
)
assert (
ds._is_auto_persist_enabled(
{
"research_library.auto_persist_research_sources": {
"value": True,
"ui_element": "checkbox",
}
},
"u",
)
is True
)
assert (
ds._is_auto_persist_enabled(
{
"research_library.auto_persist_research_sources": {
"value": False
}
},
"u",
)
is False
)
def test_auto_enabled_missing_key_falls_back_to_live(monkeypatch):
_live_settings(monkeypatch, auto=True)
assert ds._is_auto_persist_enabled({"_username": "u"}, "u") is True
def test_auto_enabled_missing_key_live_false(monkeypatch):
_live_settings(monkeypatch, auto=False)
assert ds._is_auto_persist_enabled({"_username": "u"}, "u") is False
def test_auto_enabled_no_snapshot_live_true(monkeypatch):
_live_settings(monkeypatch, auto=True)
assert ds._is_auto_persist_enabled(None, "u") is True
def test_auto_enabled_no_username_no_snapshot():
assert ds._is_auto_persist_enabled(None, None) is False
assert ds._is_auto_persist_enabled({}, None) is False
def test_auto_enabled_live_lookup_failure_returns_false(monkeypatch):
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_settings_manager",
Mock(side_effect=RuntimeError("db down")),
)
assert ds._is_auto_persist_enabled({"other": 1}, "u") is False
# ---------------------------------------------------------------------------
# _resolve_pdf_storage_mode
# ---------------------------------------------------------------------------
def test_storage_mode_filesystem_gated_to_database_by_default():
# Operator gate closed in test env: filesystem coerces to database.
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "filesystem",
"document_scheduler.download_pdfs": True,
},
"u",
)
== "database"
)
def test_storage_mode_filesystem_allowed_with_gate_open(monkeypatch):
monkeypatch.setattr(
"local_deep_research.research_library.services.pdf_storage_manager.filesystem_pdf_storage_allowed",
lambda: True,
)
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "filesystem",
"document_scheduler.download_pdfs": True,
},
"u",
)
== "filesystem"
)
def test_storage_mode_scheduler_false_forces_none():
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": False,
},
"u",
)
== "none"
)
def test_storage_mode_wrapped_dict_handling(monkeypatch):
from local_deep_research.research_library.services.pdf_storage_manager import (
resolve_pdf_storage_mode,
)
# download_pdfs wrapped as {"value": False} forces "none"
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": {"value": "database"},
"document_scheduler.download_pdfs": {"value": False},
},
"u",
)
== "none"
)
# filesystem wrapped in dict coerces to database when operator gate closed
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": {"value": "filesystem"},
"document_scheduler.download_pdfs": {"value": True},
},
"u",
)
== "database"
)
# download_pdfs present-but-None forces "none"
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": None,
},
"u",
)
== "none"
)
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": {"value": None},
},
"u",
)
== "none"
)
# direct resolve_pdf_storage_mode with wrapped dict coerces to database
assert resolve_pdf_storage_mode({"value": "filesystem"}) == "database"
def test_storage_mode_keyless_snapshot_uses_live_none(monkeypatch):
_settings_mock(
monkeypatch,
{
"research_library.pdf_storage_mode": "none",
"document_scheduler.download_pdfs": True,
},
)
assert ds._resolve_pdf_storage_mode({"_username": "u"}, "u") == "none"
def test_storage_mode_snapshot_key_beats_live(monkeypatch):
_settings_mock(
monkeypatch,
{
"research_library.pdf_storage_mode": "none",
"document_scheduler.download_pdfs": True,
},
)
assert (
ds._resolve_pdf_storage_mode(
{"research_library.pdf_storage_mode": "database"}, "u"
)
== "database"
)
def test_storage_mode_no_snapshot_uses_live(monkeypatch):
_settings_mock(
monkeypatch,
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
},
)
assert ds._resolve_pdf_storage_mode(None, "u") == "database"
def test_storage_mode_invalid_value_passes_through_unvalidated():
# No validation at this layer: unknown values pass through unchanged.
assert (
ds._resolve_pdf_storage_mode(
{
"research_library.pdf_storage_mode": "bogus",
"document_scheduler.download_pdfs": True,
},
"u",
)
== "bogus"
)
# ---------------------------------------------------------------------------
# resolve_fetch_identity
# ---------------------------------------------------------------------------
def test_identity_explicit_wins():
from local_deep_research.utilities.thread_context import (
resolve_fetch_identity,
)
u, p, s = resolve_fetch_identity(
username="exp", password="pw", settings_snapshot={"a": 1}
)
assert (u, p, s) == ("exp", "pw", {"a": 1})
def test_identity_egress_used_when_no_explicit():
from local_deep_research.utilities.thread_context import (
resolve_fetch_identity,
)
egress = SimpleNamespace(username="eg", password="egpw")
u, p, _ = resolve_fetch_identity(egress_context=egress)
assert (u, p) == ("eg", "egpw")
def test_identity_egress_without_username_falls_through():
from local_deep_research.utilities.thread_context import (
resolve_fetch_identity,
)
egress = SimpleNamespace(username=None, password=None)
u, p, _ = resolve_fetch_identity(
egress_context=egress,
settings_snapshot={"_username": "snap"},
)
assert u == "snap"
def test_identity_thread_context_used():
from local_deep_research.utilities.thread_context import (
clear_search_context,
resolve_fetch_identity,
set_search_context,
)
set_search_context(
{
"username": "thr",
"user_password": "thrpw",
"settings_snapshot": {"k": "v"},
}
)
try:
u, p, s = resolve_fetch_identity()
assert (u, p, s) == ("thr", "thrpw", {"k": "v"})
finally:
clear_search_context()
def test_identity_explicit_beats_thread_context():
from local_deep_research.utilities.thread_context import (
clear_search_context,
resolve_fetch_identity,
set_search_context,
)
set_search_context({"username": "thr", "user_password": "thrpw"})
try:
u, p, _ = resolve_fetch_identity(username="exp", password="exppw")
assert (u, p) == ("exp", "exppw")
finally:
clear_search_context()
def test_identity_non_dict_snapshot():
from local_deep_research.utilities.thread_context import (
resolve_fetch_identity,
)
u, p, s = resolve_fetch_identity(settings_snapshot="nope")
assert u is None and s == "nope"
# ---------------------------------------------------------------------------
# Executor bounds and lifecycle
# ---------------------------------------------------------------------------
def test_pdf_executor_is_singleton():
assert (
ds._get_pdf_persistence_executor() is ds._get_pdf_persistence_executor()
)
def test_pdf_executor_shutdown_is_idempotent_and_renewable():
first = ds._get_pdf_persistence_executor()
ds._shutdown_pdf_persistence_executor()
ds._shutdown_pdf_persistence_executor()
second = ds._get_pdf_persistence_executor()
assert second is not first
def test_auto_persist_executor_singleton_and_shutdown():
from local_deep_research.web.queue.processor_v2 import (
_get_auto_persist_executor,
_shutdown_auto_persist_executor,
)
assert _get_auto_persist_executor() is _get_auto_persist_executor()
_shutdown_auto_persist_executor()
_shutdown_auto_persist_executor()
assert _get_auto_persist_executor() is not None
def test_background_submit_skipped_when_slots_exhausted(monkeypatch):
import threading
mock_executor = Mock()
monkeypatch.setattr(
ds, "_get_pdf_persistence_executor", lambda: mock_executor
)
# Isolate from slots held by background submits in earlier tests of
# a combined run (mock executors there never run the closure).
monkeypatch.setattr(ds, "_PDF_PERSIST_SEMAPHORE", threading.Semaphore(4))
with ds._PDF_PERSIST_BYTES_LOCK:
ds._PDF_PERSIST_BYTES = 0
acquired = []
try:
for _ in range(4):
assert ds._PDF_PERSIST_SEMAPHORE.acquire(blocking=False)
acquired.append(True)
res = ds.persist_downloaded_pdf_on_the_spot(
url="https://example.com/full.pdf",
pdf_content=b"%PDF-1.4 x",
text="t",
username="u",
run_in_background=True,
)
assert res is None
mock_executor.submit.assert_not_called()
finally:
for _ in acquired:
ds._PDF_PERSIST_SEMAPHORE.release()
def test_background_submit_skipped_when_byte_cap_hit(monkeypatch):
mock_executor = Mock()
monkeypatch.setattr(
ds, "_get_pdf_persistence_executor", lambda: mock_executor
)
with ds._PDF_PERSIST_BYTES_LOCK:
ds._PDF_PERSIST_BYTES = ds._PDF_PERSIST_MAX_PENDING_BYTES
try:
res = ds.persist_downloaded_pdf_on_the_spot(
url="https://example.com/big.pdf",
pdf_content=b"%PDF-1.4 x" * 100,
text="t",
username="u",
run_in_background=True,
)
assert res is None
mock_executor.submit.assert_not_called()
finally:
with ds._PDF_PERSIST_BYTES_LOCK:
ds._PDF_PERSIST_BYTES = 0
def test_background_closure_releases_slot_and_bytes(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
captured = []
mock_executor = Mock()
mock_executor.submit.side_effect = lambda fn: captured.append(fn)
monkeypatch.setattr(
ds, "_get_pdf_persistence_executor", lambda: mock_executor
)
res = ds.persist_downloaded_pdf_on_the_spot(
url="https://example.com/acct.pdf",
pdf_content=b"%PDF-1.4 accounting",
text="accounting text",
username="testuser",
settings_snapshot={"research_library.pdf_storage_mode": "database"},
run_in_background=True,
)
assert res is None
assert len(captured) == 1
captured[0]()
with ds._PDF_PERSIST_BYTES_LOCK:
assert ds._PDF_PERSIST_BYTES == 0
assert ds._PDF_PERSIST_SEMAPHORE.acquire(blocking=False)
ds._PDF_PERSIST_SEMAPHORE.release()
assert (
db.query(Document)
.filter_by(original_url="https://example.com/acct.pdf")
.count()
== 1
)
# ---------------------------------------------------------------------------
# _save_pdf_document_record: versioning and field hygiene
# ---------------------------------------------------------------------------
def _storage_manager(tmp_path):
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
return PDFStorageManager(
library_root=tmp_path,
storage_mode="database",
max_pdf_size_mb=50,
username="testuser",
)
def test_save_changed_bytes_mint_new_doc_and_advance_tracker(
db, monkeypatch, tmp_path
):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 11, "https://example.com/evolving.pdf")
old_bytes = b"%PDF-1.4 version one"
new_bytes = b"%PDF-1.4 version two updated"
old_hash = hashlib.sha256(old_bytes).hexdigest()
new_hash = hashlib.sha256(new_bytes).hexdigest()
doc_old, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=old_bytes,
file_hash=old_hash,
url="https://example.com/evolving.pdf",
text="OLD TEXT",
username="testuser",
resource_id=11,
pdf_storage_mode="database",
)
db.commit()
doc_new, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=new_bytes,
file_hash=new_hash,
url="https://example.com/evolving.pdf",
text="NEW TEXT",
username="testuser",
resource_id=11,
pdf_storage_mode="database",
)
db.commit()
assert doc_new.id != doc_old.id
assert db.query(Document).count() == 2
assert (
db.query(Document).filter_by(id=doc_old.id).first().text_content
== "OLD TEXT"
)
tracker = ds.find_tracker_by_url(db, "https://example.com/evolving.pdf")
assert tracker is not None
assert tracker.file_hash == new_hash
def test_save_identical_bytes_across_urls_share_one_doc(
db, monkeypatch, tmp_path
):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 21, "https://mirror1.example.com/paper.pdf")
_make_resource(db, 22, "https://mirror2.example.com/paper.pdf")
payload = b"%PDF-1.4 identical bytes"
file_hash = hashlib.sha256(payload).hexdigest()
doc_a, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://mirror1.example.com/paper.pdf",
text="SAME TEXT",
username="testuser",
resource_id=21,
pdf_storage_mode="database",
)
db.commit()
doc_b, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://mirror2.example.com/paper.pdf",
text="SAME TEXT",
username="testuser",
resource_id=22,
pdf_storage_mode="database",
)
db.commit()
assert doc_a.id == doc_b.id
assert db.query(Document).count() == 1
def test_save_require_text_rejects_garbage(db, monkeypatch, tmp_path):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
"local_deep_research.research_library.downloaders.base.BaseDownloader.extract_text_from_pdf",
lambda b: None,
)
doc, path = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=b"%PDF-garbage-no-text",
file_hash=hashlib.sha256(b"%PDF-garbage-no-text").hexdigest(),
url="https://example.com/garbage.pdf",
text=None,
username="testuser",
pdf_storage_mode="database",
require_text=True,
)
assert doc is None and path is None
assert db.query(Document).count() == 0
def test_save_library_path_tolerates_empty_text(db, monkeypatch, tmp_path):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
monkeypatch.setattr(
"local_deep_research.research_library.downloaders.base.BaseDownloader.extract_text_from_pdf",
lambda b: "SHOULD NOT BE EXTRACTED ON LIBRARY PATH",
)
payload = b"%PDF-1.4 synthetic no text"
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=hashlib.sha256(payload).hexdigest(),
url="https://example.com/synth.pdf",
text=None,
username="testuser",
pdf_storage_mode="database",
require_text=False,
)
db.commit()
assert doc is not None
assert doc.file_type == "pdf"
# Library path leaves text to its canonical step and stores None,
# never "", so the RAG sweep's IS NOT NULL check skips it.
assert doc.text_content is None
@pytest.mark.parametrize("stored_mode", ["database", "filesystem"])
def test_save_text_only_reuse_preserves_stored_pdf_metadata(
db, monkeypatch, tmp_path, stored_mode
):
"""Text-only reuse must not relabel a document that stores its PDF."""
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import (
document_awaits_pdf,
)
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 31, "https://example.com/stored.pdf")
payload = b"%PDF-1.4 stored bytes"
file_hash = hashlib.sha256(payload).hexdigest()
url = "https://example.com/stored.pdf"
stored_manager = PDFStorageManager(
library_root=tmp_path,
storage_mode=stored_mode,
max_pdf_size_mb=50,
username="testuser",
)
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=stored_manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="STORED TEXT",
username="testuser",
resource_id=31,
pdf_storage_mode=stored_mode,
require_text=True,
)
db.commit()
assert doc is not None
assert stored_manager.has_pdf(doc, db) is True
stored_shape = (doc.storage_mode, doc.file_path)
none_manager = PDFStorageManager(
library_root=tmp_path,
storage_mode="none",
max_pdf_size_mb=50,
username="testuser",
)
reused, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=none_manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="STORED TEXT",
username="testuser",
resource_id=31,
pdf_storage_mode="none",
require_text=True,
)
db.commit()
assert reused is not None and reused.id == doc.id
assert (reused.storage_mode, reused.file_path) == stored_shape
assert reused.storage_mode == stored_mode
assert document_awaits_pdf(reused) is False
def test_save_text_only_reuse_empty_blob_relabels_and_awaits_pdf(
db, monkeypatch, tmp_path
):
"""Empty or unretrievable blob row must be treated as not stored and await PDF."""
from local_deep_research.database.models.library import DocumentBlob
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import (
document_awaits_pdf,
)
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 32, "https://example.com/empty_blob.pdf")
payload = b"%PDF-1.4 empty blob test"
file_hash = hashlib.sha256(payload).hexdigest()
url = "https://example.com/empty_blob.pdf"
doc = Document(
id="doc-empty-blob",
source_type_id="st-research",
resource_id=32,
research_id="res-123",
document_hash=file_hash,
original_url=url,
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Empty Blob PDF",
status=DocumentStatus.COMPLETED,
text_content="EXTRACTED TEXT",
storage_mode="database",
file_path=None,
)
db.add(doc)
empty_blob = DocumentBlob(
document_id=doc.id,
pdf_binary=b"",
blob_hash=hashlib.sha256(b"").hexdigest(),
stored_at=datetime.now(UTC),
)
db.add(empty_blob)
db.commit()
none_manager = PDFStorageManager(
library_root=tmp_path,
storage_mode="none",
max_pdf_size_mb=50,
username="testuser",
)
reused, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=none_manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="EXTRACTED TEXT",
username="testuser",
resource_id=32,
pdf_storage_mode="none",
require_text=True,
)
db.commit()
assert reused is not None and reused.id == doc.id
assert reused.storage_mode == "none"
assert reused.file_path == FILE_PATH_TEXT_ONLY
assert document_awaits_pdf(reused) is True
def test_save_dedup_check_probes_metadata_without_calling_load_pdf_or_dirtying_session(
db, monkeypatch, tmp_path
):
"""Dedup check must probe metadata via func.length without materializing PDF bytes via load_pdf
and without marking the DocumentBlob dirty in the session.
"""
from local_deep_research.database.models.library import DocumentBlob
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import document_awaits_pdf
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 77, "https://example.com/no_load_pdf.pdf")
payload = b"%PDF-1.4 metadata probe only"
file_hash = hashlib.sha256(payload).hexdigest()
url = "https://example.com/no_load_pdf.pdf"
doc = Document(
id="doc-no-load-pdf",
source_type_id="st-research",
resource_id=77,
research_id="res-123",
document_hash=file_hash,
original_url=url,
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Probe PDF",
status=DocumentStatus.COMPLETED,
text_content="EXTRACTED TEXT",
storage_mode="database",
file_path="database",
)
db.add(doc)
init_time = datetime.now(UTC)
blob = DocumentBlob(
document_id=doc.id,
pdf_binary=payload,
blob_hash=file_hash,
stored_at=init_time,
last_accessed=init_time,
)
db.add(blob)
db.commit()
manager = PDFStorageManager(
library_root=tmp_path,
storage_mode="database",
max_pdf_size_mb=50,
username="testuser",
)
load_pdf_called = False
original_load = manager.load_pdf
def spy_load_pdf(*args, **kwargs):
nonlocal load_pdf_called
load_pdf_called = True
return original_load(*args, **kwargs)
manager.load_pdf = spy_load_pdf
reused, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="EXTRACTED TEXT",
username="testuser",
resource_id=77,
pdf_storage_mode="database",
require_text=True,
)
assert load_pdf_called is False
assert blob not in db.dirty
assert reused.id == doc.id
assert reused.storage_mode == "database"
assert document_awaits_pdf(reused) is False
db.commit()
def test_save_pdf_document_record_over_cap_reuse_legacy_row_resets_file_path(
db, monkeypatch, tmp_path
):
"""When a legacy document with a truthy file_path (e.g. 'database') is reused
and PDF storage cannot save due to size cap, file_path must be reset to FILE_PATH_TEXT_ONLY
under require_text=True so document_awaits_pdf reports True.
"""
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import document_awaits_pdf
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 88, "https://example.com/overcap_legacy.pdf")
payload = b"%PDF-1.4 legacy overcap"
file_hash = hashlib.sha256(payload).hexdigest()
url = "https://example.com/overcap_legacy.pdf"
doc = Document(
id="doc-legacy-overcap",
source_type_id="st-research",
resource_id=88,
research_id="res-123",
document_hash=file_hash,
original_url=url,
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Legacy Overcap PDF",
status=DocumentStatus.COMPLETED,
text_content="EXTRACTED TEXT",
storage_mode="none",
file_path="database",
)
db.add(doc)
db.commit()
manager = PDFStorageManager(
library_root=tmp_path,
storage_mode="database",
max_pdf_size_mb=0,
username="testuser",
)
manager.max_pdf_size_bytes = 1
reused, file_path_result = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="EXTRACTED TEXT",
username="testuser",
resource_id=88,
pdf_storage_mode="database",
require_text=True,
)
db.commit()
assert reused.id == doc.id
assert reused.storage_mode == "none"
assert reused.file_path == FILE_PATH_TEXT_ONLY
assert document_awaits_pdf(reused) is True
@pytest.mark.parametrize("stored_mode", ["database", "filesystem"])
@pytest.mark.parametrize("require_text", [True, False])
def test_save_pdf_document_record_over_cap_storage_enabled(
db, monkeypatch, tmp_path, stored_mode, require_text
):
"""When PDF exceeds max_pdf_size_mb with storage enabled, no UnboundLocalError occurs
and document is handled consistently as text-only/not-stored.
"""
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import (
document_awaits_pdf,
)
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
res_id = 45 if require_text else 46
_make_resource(
db,
res_id,
f"https://example.com/overcap_{stored_mode}_{require_text}.pdf",
)
payload = b"%PDF-1.4 " + b"X" * 2000
file_hash = hashlib.sha256(payload).hexdigest()
url = f"https://example.com/overcap_{stored_mode}_{require_text}.pdf"
manager = PDFStorageManager(
library_root=tmp_path,
storage_mode=stored_mode,
max_pdf_size_mb=0,
username="testuser",
)
manager.max_pdf_size_bytes = 100
doc, file_path_result = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=manager,
pdf_content=payload,
file_hash=file_hash,
url=url,
text="OVERCAP TEXT",
username="testuser",
resource_id=res_id,
pdf_storage_mode=stored_mode,
require_text=require_text,
)
db.commit()
assert doc is not None
assert file_path_result is None
assert doc.file_type == "pdf"
assert doc.storage_mode == "none"
tracker = ds.find_tracker_by_url(db, url)
assert tracker is not None
assert tracker.file_path is None
if require_text:
assert doc.file_path == FILE_PATH_TEXT_ONLY
assert document_awaits_pdf(doc) is True
else:
assert doc.file_path is None
def test_save_backfills_missing_text_title_status_and_ids(
db, monkeypatch, tmp_path
):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
payload = b"%PDF-1.4 backfill"
file_hash = hashlib.sha256(payload).hexdigest()
thin = Document(
id="doc-thin",
source_type_id="st-research",
document_hash=file_hash,
original_url="https://example.com/thin.pdf",
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Untitled",
status=DocumentStatus.FAILED,
error_message="boom",
text_content="",
storage_mode="database",
file_path="database",
)
db.add(thin)
db.commit()
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://example.com/thin.pdf",
text="BACKFILLED TEXT",
title="Real Title",
username="testuser",
resource_id=31,
research_id="res-123",
pdf_storage_mode="database",
)
db.commit()
assert doc.id == "doc-thin"
assert doc.text_content == "BACKFILLED TEXT"
assert doc.title == "Real Title"
assert doc.status == DocumentStatus.COMPLETED
assert doc.error_message is None
assert doc.resource_id == 31
assert doc.research_id == "res-123"
def test_save_does_not_clobber_existing_text_title_or_ids(
db, monkeypatch, tmp_path
):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
payload = b"%PDF-1.4 keep me"
file_hash = hashlib.sha256(payload).hexdigest()
_make_resource(db, 32, "https://example.com/keep.pdf")
_make_resource(db, 33, "https://example.com/keep.pdf")
first, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://example.com/keep.pdf",
text="ORIGINAL",
title="Original Title",
username="testuser",
resource_id=32,
research_id="res-123",
pdf_storage_mode="database",
)
db.commit()
second, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://example.com/keep.pdf",
text="NEWTEXT",
title="New Title",
username="testuser",
resource_id=33,
research_id="res-999",
pdf_storage_mode="database",
)
db.commit()
assert second.id == first.id
assert second.text_content == "ORIGINAL"
assert second.title == "Original Title"
assert second.resource_id == 32
assert second.research_id == "res-123"
def test_save_collection_failure_still_returns_doc(db, monkeypatch, tmp_path):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds,
"get_default_library_id",
Mock(side_effect=RuntimeError("no library")),
)
db.query(Collection).delete()
db.commit()
payload = b"%PDF-1.4 lonely doc"
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=hashlib.sha256(payload).hexdigest(),
url="https://example.com/lonely.pdf",
text="lonely text",
username="testuser",
pdf_storage_mode="database",
)
db.commit()
assert doc is not None
assert db.query(Document).filter_by(id=doc.id).count() == 1
def test_save_uses_canonical_url_hash_for_trackers(db, monkeypatch, tmp_path):
from local_deep_research.utilities.download_url import get_url_hash
from local_deep_research.utilities.url_utils import canonical_url_key
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 41, "https://example.com/paper?utm_source=x")
payload = b"%PDF-1.4 canonical hash"
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=hashlib.sha256(payload).hexdigest(),
url="https://example.com/paper?utm_source=x",
text="canonical text",
username="testuser",
resource_id=41,
extra_urls=["https://cdn.example.com/final.pdf"],
pdf_storage_mode="database",
)
db.commit()
assert doc is not None
for u in (
"https://example.com/paper?utm_source=x",
"https://cdn.example.com/final.pdf",
):
tracker = ds.find_tracker_by_url(db, u)
assert tracker is not None
assert tracker.url_hash == get_url_hash(canonical_url_key(u))
assert tracker.file_size == len(payload)
def test_save_race_winner_still_writes_trackers_and_collection(
db, monkeypatch, tmp_path
):
# True concurrency shape: the hash lookup misses (winner not yet
# committed) but the flush collides. Hide the winner from the first
# Document-hash lookup only; the loser's real flush then raises a real
# IntegrityError against the committed winner.
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
_make_resource(db, 51, "https://mirror-b.example.com/p.pdf")
payload = b"%PDF-1.4 racy bytes"
file_hash = hashlib.sha256(payload).hexdigest()
winner = Document(
id="doc-winner",
source_type_id="st-research",
document_hash=file_hash,
original_url="https://mirror-a.example.com/p.pdf",
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Winner",
status=DocumentStatus.COMPLETED,
text_content="winner text",
storage_mode="database",
file_path="database",
)
db.add(winner)
db.commit()
real_query = db.query
skipped = {"once": False}
def hiding_query(*entities, **kwargs):
if not skipped["once"] and entities and entities[0] is Document:
skipped["once"] = True
class _Miss:
def filter_by(self, **kw):
return self
def first(self):
return None
return _Miss()
return real_query(*entities, **kwargs)
monkeypatch.setattr(db, "query", hiding_query)
doc, _ = ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=payload,
file_hash=file_hash,
url="https://mirror-b.example.com/p.pdf",
text="loser text",
username="testuser",
resource_id=51,
extra_urls=["https://mirror-b.example.com/p.pdf"],
collection_id="default-library",
pdf_storage_mode="database",
require_text=True,
)
db.commit()
assert doc.id == "doc-winner"
assert db.query(Document).count() == 1
tracker = ds.find_tracker_by_url(db, "https://mirror-b.example.com/p.pdf")
assert tracker is not None
assert tracker.file_hash == file_hash
assert (
db.query(DocumentCollection)
.filter_by(document_id="doc-winner", collection_id="default-library")
.count()
== 1
)
def test_save_non_integrity_flush_error_propagates(db, monkeypatch, tmp_path):
monkeypatch.setattr(ds, "get_source_type_id", lambda *a, **k: "st-research")
monkeypatch.setattr(
ds, "get_default_library_id", lambda *a, **k: "default-library"
)
def bad_flush():
raise ValueError("disk on fire")
monkeypatch.setattr(db, "flush", bad_flush)
with pytest.raises(ValueError, match="disk on fire"):
ds._save_pdf_document_record(
session=db,
pdf_storage_manager=_storage_manager(tmp_path),
pdf_content=b"%PDF-1.4 x",
file_hash=hashlib.sha256(b"%PDF-1.4 x").hexdigest(),
url="https://example.com/err.pdf",
text="t",
username="testuser",
pdf_storage_mode="database",
require_text=True,
)
# ---------------------------------------------------------------------------
# _do_persist gating matrix
# ---------------------------------------------------------------------------
def _do_kwargs(**overrides):
base = {
"url": "https://example.com/gate.pdf",
"pdf_content": b"%PDF-1.4 gated",
"text": "gated text",
"title": "Gated",
"username": "testuser",
}
base.update(overrides)
return base
def test_do_persist_sync_baseline(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(**_do_kwargs())
assert doc_id is not None
doc = db.query(Document).filter_by(id=doc_id).first()
assert doc.text_content == "gated text"
assert doc.file_type == "pdf"
def test_do_persist_explicit_opt_out_honored_without_flag(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(
settings_snapshot={
"research_library.auto_persist_research_sources": False
}
)
)
assert doc_id is None
assert db.query(Document).count() == 0
def test_do_persist_gated_on_with_snapshot_key(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=False)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(
settings_snapshot={
"research_library.auto_persist_research_sources": True
}
),
check_setting=True,
)
assert doc_id is not None
def test_do_persist_wrapped_dict_snapshot_enabled(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=False)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(
settings_snapshot={
"research_library.auto_persist_research_sources": {
"value": True,
"ui_element": "checkbox",
},
"research_library.max_pdf_size_mb": {"value": 50},
}
),
check_setting=True,
)
assert doc_id is not None
def test_do_persist_gated_off_with_snapshot_key(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(
settings_snapshot={
"research_library.auto_persist_research_sources": False
}
),
check_setting=True,
)
assert doc_id is None
assert db.query(Document).count() == 0
def test_do_persist_non_boolean_snapshot_values_disabled(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
for non_bool in ("false", "off", "true", 1, 0, {"value": False}):
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(
settings_snapshot={
"research_library.auto_persist_research_sources": non_bool
}
),
check_setting=True,
)
assert doc_id is None
assert db.query(Document).count() == 0
def test_do_persist_gated_missing_key_uses_live_true(db, monkeypatch):
_mock_db_session(monkeypatch, db)
mock_settings = _live_settings(monkeypatch, auto=True)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(settings_snapshot={"_username": "testuser"}),
check_setting=True,
)
assert doc_id is not None
assert mock_settings.get_setting.called
def test_do_persist_gated_missing_key_uses_live_false(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=False)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(settings_snapshot={"_username": "testuser"}),
check_setting=True,
)
assert doc_id is None
assert db.query(Document).count() == 0
def test_do_persist_gated_no_snapshot_live_true(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
doc_id = ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(), check_setting=True
)
assert doc_id is not None
def test_do_persist_rejects_non_pdf(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
assert (
ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(pdf_content=b"<html>nope</html>")
)
is None
)
def test_do_persist_no_identity_returns_none(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
assert (
ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(username=None, settings_snapshot=None)
)
is None
)
def test_do_persist_empty_text_with_flag_rejected(db, monkeypatch):
_mock_db_session(monkeypatch, db)
_live_settings(monkeypatch, auto=True)
monkeypatch.setattr(
"local_deep_research.research_library.downloaders.base.BaseDownloader.extract_text_from_pdf",
lambda b: " ",
)
assert (
ds._do_persist_downloaded_pdf_on_the_spot(
**_do_kwargs(text=" "), check_setting=True
)
is None
)
assert db.query(Document).count() == 0
# ---------------------------------------------------------------------------
# persist_research_sources: scheduler, tracker link, failures
# ---------------------------------------------------------------------------
def test_sweep_scheduler_off_forces_text_path(db, monkeypatch):
_make_resource(db, 61, "https://arxiv.org/pdf/2301.00001.pdf")
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": False,
}
)
service.download_resource = Mock()
service.download_as_text = Mock(return_value=(True, None))
summary = service.persist_research_sources("res-123")
assert summary["persisted"] == 1
service.download_resource.assert_not_called()
service.download_as_text.assert_called_once_with(61)
def test_sweep_tracker_redirect_links_without_network(db, monkeypatch):
payload = b"%PDF-1.4 redirect target"
file_hash = hashlib.sha256(payload).hexdigest()
doc = Document(
id="doc-redirect",
source_type_id="st-research",
document_hash=file_hash,
original_url="https://doi.org/10.9/orig",
file_size=len(payload),
file_type="pdf",
mime_type="application/pdf",
title="Redirect Target",
status=DocumentStatus.COMPLETED,
text_content="redirect text",
storage_mode="database",
file_path="database",
)
first = ResearchResource(
id=70,
research_id="res-123",
url="https://doi.org/10.9/orig",
title="Orig",
created_at=datetime.now(UTC).isoformat(),
)
r = ResearchResource(
id=71,
research_id="res-123",
url="https://publisher.example.com/final.pdf",
title="Final",
created_at=datetime.now(UTC).isoformat(),
)
from local_deep_research.utilities.download_url import get_url_hash
from local_deep_research.utilities.url_utils import canonical_url_key
tracker = DownloadTracker(
url="https://publisher.example.com/final.pdf",
url_hash=get_url_hash(
canonical_url_key("https://publisher.example.com/final.pdf")
),
first_resource_id=70,
file_hash=file_hash,
file_size=len(payload),
is_downloaded=True,
)
db.add_all([doc, first, r, tracker])
db.commit()
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
}
)
service.download_resource = Mock(return_value=(True, None))
service.download_as_text = Mock(return_value=(True, None))
summary = service.persist_research_sources("res-123")
assert summary == {
"total": 2,
"persisted": 2,
"failed": 0,
"skipped": 0,
}
called_ids = [
c.args[0] for c in service.download_resource.call_args_list
] + [c.args[0] for c in service.download_as_text.call_args_list]
assert 71 not in called_ids
assert (
db.query(ResearchResource).filter_by(id=71).first().document_id
== "doc-redirect"
)
def test_sweep_both_pdf_and_text_fail_counts_failed(db, monkeypatch):
_make_resource(db, 72, "https://example.com/broken-page")
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
}
)
service.download_resource = Mock(return_value=(False, "conn reset"))
service.download_as_text = Mock(return_value=(False, "404"))
summary = service.persist_research_sources("res-123")
assert summary == {"total": 1, "persisted": 0, "failed": 1, "skipped": 0}
def test_sweep_awaiting_pdf_refills_via_download_resource(db, monkeypatch):
doc = Document(
id="doc-await",
source_type_id="st-research",
resource_id=73,
document_hash="hash-await",
original_url="https://arxiv.org/pdf/2301.00002.pdf",
file_size=10,
file_type="pdf",
mime_type="application/pdf",
title="Awaiting",
status=DocumentStatus.COMPLETED,
text_content="some text",
storage_mode="none",
file_path=FILE_PATH_TEXT_ONLY,
)
r = ResearchResource(
id=73,
research_id="res-123",
url="https://arxiv.org/pdf/2301.00002.pdf",
title="Awaiting",
document_id="doc-await",
created_at=datetime.now(UTC).isoformat(),
)
db.add_all([doc, r])
db.commit()
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
}
)
service.download_resource = Mock(return_value=(True, None))
service.download_as_text = Mock()
summary = service.persist_research_sources("res-123")
assert summary["persisted"] == 1
service.download_resource.assert_called_once_with(73, collection_id=None)
service.download_as_text.assert_not_called()
def test_sweep_completed_non_awaiting_is_skipped(db, monkeypatch):
doc = Document(
id="doc-done",
source_type_id="st-research",
resource_id=74,
document_hash="hash-done",
original_url="https://example.com/done",
file_size=10,
file_type="text",
status=DocumentStatus.COMPLETED,
text_content="done",
)
r = ResearchResource(
id=74,
research_id="res-123",
url="https://example.com/done",
title="Done",
document_id="doc-done",
created_at=datetime.now(UTC).isoformat(),
)
db.add_all([doc, r])
db.commit()
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
}
)
service.download_resource = Mock()
service.download_as_text = Mock()
summary = service.persist_research_sources("res-123")
assert summary == {"total": 1, "persisted": 0, "failed": 0, "skipped": 1}
service.download_resource.assert_not_called()
service.download_as_text.assert_not_called()
def test_sweep_unexpected_exception_in_one_resource_continues(db, monkeypatch):
_make_resource(db, 75, "https://arxiv.org/pdf/2301.00003.pdf")
_make_resource(db, 76, "https://example.com/ok-page")
_mock_db_session(monkeypatch, db)
service = _stub_service(
{
"research_library.pdf_storage_mode": "database",
"document_scheduler.download_pdfs": True,
}
)
service.download_resource = Mock(side_effect=RuntimeError("disk gone"))
service.download_as_text = Mock(return_value=(True, None))
summary = service.persist_research_sources("res-123")
assert summary == {"total": 2, "persisted": 1, "failed": 1, "skipped": 0}
# ---------------------------------------------------------------------------
# auto_persist wrapper
# ---------------------------------------------------------------------------
def test_auto_persist_enabled_path_delegates(monkeypatch):
mock_settings = Mock()
mock_settings.get_setting.side_effect = lambda k, d=None: (
True if k == "research_library.auto_persist_research_sources" else d
)
mock_settings.get_settings_snapshot.return_value = {"snapshot": True}
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_settings_manager",
lambda *a, **k: mock_settings,
)
summary = {"total": 2, "persisted": 2, "failed": 0, "skipped": 0}
svc_cls = MagicMock()
svc_cls.return_value.__enter__.return_value.persist_research_sources.return_value = summary
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.DownloadService",
svc_cls,
)
assert (
auto_persist_research_sources("alice", "r1", db_password="pw")
== summary
)
svc_cls.assert_called_once_with(
"alice", password="pw", settings_snapshot={"snapshot": True}
)
def test_auto_persist_exception_redacts_password(monkeypatch):
secret = "SuperSecretPassword99"
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_settings_manager",
Mock(side_effect=RuntimeError(f"boom {secret}")),
)
res = auto_persist_research_sources("alice", "r1", db_password=secret)
assert res["persisted"] == 0
assert secret not in res.get("error", "")
assert "***REDACTED***" in res.get("error", "")
def test_auto_persist_stringly_false_is_disabled(monkeypatch):
mock_settings = Mock()
mock_settings.get_setting.side_effect = lambda k, d=None: (
"false" if k == "research_library.auto_persist_research_sources" else d
)
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.get_settings_manager",
lambda *a, **k: mock_settings,
)
svc_cls = MagicMock()
monkeypatch.setattr(
"local_deep_research.research_library.services.download_service.DownloadService",
svc_cls,
)
res = auto_persist_research_sources("alice", "r1", db_password="pw")
assert res["persisted"] == 0
assert res["skipped_reason"] == "disabled"
svc_cls.assert_not_called()
# ---------------------------------------------------------------------------
# Repo-level pins: setting default and changelog
# ---------------------------------------------------------------------------
def test_auto_persist_setting_defaults_off():
import json
import local_deep_research
root = list(local_deep_research.__path__)[0]
from pathlib import Path
defaults = json.loads(
Path(
root, "defaults", "research_library", "library_settings.json"
).read_text()
)
entry = defaults["research_library.auto_persist_research_sources"]
assert entry["value"] is False
assert entry["editable"] is True
def test_changelog_fragment_exists():
import local_deep_research
from pathlib import Path
root = Path(list(local_deep_research.__path__)[0]).parents[1]
fragment = root / "changelog.d" / "7416.feature.md"
assert fragment.exists()
text = fragment.read_text()
assert "auto" in text.lower() and "persist" in text.lower()