~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
"""Exercise library setup and fixture privacy without app imports."""
from contextlib import redirect_stdout
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import textwrap
from types import ModuleType
import unittest
from unittest.mock import Mock, patch
ROOT = Path(__file__).resolve().parents[3]
class ModelPreparationTests(unittest.TestCase):
def setUp(self):
spec = importlib.util.spec_from_file_location(
"prepare_library_embeddings",
ROOT / "scripts/ci/prepare_library_embeddings.py",
)
self.script = importlib.util.module_from_spec(spec)
spec.loader.exec_module(self.script)
self.embeddings = Mock()
self.embeddings.embed_query.return_value = [0.25] * 384
self.get_embeddings = Mock(return_value=self.embeddings)
module = ModuleType("local_deep_research.embeddings.embeddings_config")
module.get_embeddings = self.get_embeddings
# Exercise the real cleanup helper without importing the application.
self.logger = Mock()
loguru = ModuleType("loguru")
loguru.logger = self.logger
cleanup_spec = importlib.util.spec_from_file_location(
"local_deep_research.utilities.resource_utils",
ROOT / "src/local_deep_research/utilities/resource_utils.py",
)
cleanup = importlib.util.module_from_spec(cleanup_spec)
with patch.dict(sys.modules, {"loguru": loguru}):
cleanup_spec.loader.exec_module(cleanup)
self.modules = patch.dict(
sys.modules,
{module.__name__: module, cleanup.__name__: cleanup},
)
self.modules.start()
self.addCleanup(self.modules.stop)
self.environment = patch.dict(os.environ, {"LDR_DATA_DIR": "/data"})
self.environment.start()
self.addCleanup(self.environment.stop)
def test_preparation_explicitly_authorizes_only_public_local_model_setup(
self,
):
self.script.prepare_embeddings()
self.get_embeddings.assert_called_once_with(
provider="sentence_transformers",
model="all-MiniLM-L6-v2",
device="cpu",
settings_snapshot={
"policy.egress_scope": "public_only",
"search.tool": "arxiv",
"embeddings.require_local": False,
},
)
self.embeddings.embed_query.assert_called_once()
self.embeddings.close.assert_called_once()
def test_incomplete_model_failure_propagates_and_closes_the_handle(self):
failure = OSError("synthetic incomplete model")
self.embeddings.embed_query.side_effect = failure
with self.assertRaises(OSError) as raised:
self.script.prepare_embeddings()
self.assertIs(raised.exception, failure)
self.embeddings.close.assert_called_once()
def test_model_failure_is_preserved_when_cleanup_also_fails(self):
failure = OSError("synthetic incomplete model")
self.embeddings.embed_query.side_effect = failure
self.embeddings.close.side_effect = RuntimeError(
"synthetic cleanup failure"
)
with self.assertRaises(OSError) as raised:
self.script.prepare_embeddings()
self.assertIs(raised.exception, failure)
self.embeddings.close.assert_called_once()
self.logger.warning.assert_called_once_with(
"Failed to close library fixture embeddings"
)
def test_cleanup_failure_is_logged_without_private_exception_details(self):
private_value = "synthetic-private-cleanup-detail"
self.embeddings.close.side_effect = RuntimeError(private_value)
self.script.prepare_embeddings()
self.embeddings.close.assert_called_once()
self.logger.warning.assert_called_once_with(
"Failed to close library fixture embeddings"
)
self.assertNotIn(private_value, repr(self.logger.mock_calls))
def test_wrong_dimension_or_nonfinite_vectors_fail_setup(self):
for vector in ([0.25] * 768, [float("nan")] * 384):
with self.subTest(dimension=len(vector)):
self.embeddings.reset_mock()
self.embeddings.embed_query.return_value = vector
with self.assertRaises(ValueError):
self.script.prepare_embeddings()
self.embeddings.close.assert_called_once()
def test_missing_data_directory_fails_before_model_acquisition(self):
with patch.dict(os.environ, {"LDR_DATA_DIR": ""}):
with self.assertRaises(ValueError):
self.script.prepare_embeddings()
self.get_embeddings.assert_not_called()
class ManifestPrivacyTests(unittest.TestCase):
def setUp(self):
spec = importlib.util.spec_from_file_location(
"seed_legacy_pkl_migration",
ROOT / "tests/ui_tests/seed_legacy_pkl_migration.py",
)
self.seed = importlib.util.module_from_spec(spec)
spec.loader.exec_module(self.seed)
self.temporary = tempfile.TemporaryDirectory()
self.addCleanup(self.temporary.cleanup)
self.directory = Path(self.temporary.name)
self.manifest = {
"username": "synthetic-private-fixture-user",
"password": "synthetic-private-fixture-password",
"collections": {},
}
def test_manifest_remains_usable_without_printing_credentials(self):
output = io.StringIO()
with redirect_stdout(output):
path = self.seed._write_manifest(self.directory, self.manifest)
self.assertEqual(json.loads(path.read_text()), self.manifest)
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
self.assertNotIn(self.manifest["password"], output.getvalue())
self.assertNotIn(self.manifest["username"], output.getvalue())
def test_replacing_an_old_manifest_repairs_permissive_permissions(self):
path = self.directory / "migtest_manifest.json"
path.write_text("old fixture")
path.chmod(0o644)
self.seed._write_manifest(self.directory, self.manifest)
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
self.assertEqual(json.loads(path.read_text()), self.manifest)
def test_failed_serialization_preserves_old_manifest_and_removes_tempfile(
self,
):
path = self.directory / "migtest_manifest.json"
path.write_text("old fixture")
with self.assertRaises(TypeError):
self.seed._write_manifest(self.directory, {"invalid": object()})
self.assertEqual(path.read_text(), "old fixture")
self.assertEqual(list(self.directory.iterdir()), [path])
def test_destination_symlink_does_not_overwrite_another_file(self):
target = self.directory / "unrelated"
target.write_text("synthetic unrelated content")
path = self.directory / "migtest_manifest.json"
path.symlink_to(target)
self.seed._write_manifest(self.directory, self.manifest)
self.assertFalse(path.is_symlink())
self.assertEqual(target.read_text(), "synthetic unrelated content")
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
class LibraryWorkflowTests(unittest.TestCase):
def invoke(self, shard, fail_preparation=False):
source = (ROOT / ".github/workflows/docker-tests.yml").read_text()
step = source.split(
" - name: Run UI tests with retry (fresh server per attempt)\n",
1,
)[1]
# Execute the real setup through the server launch, stopping before
# the health-check loop. Only Docker itself is replaced.
command = step.split(" command: |\n", 1)[1].split(
" # --max-time bounds", 1
)[0]
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
calls = root / "docker-calls.jsonl"
docker = root / "docker"
docker.write_text(
f"#!{sys.executable}\n"
"import json, os, sys\n"
"with open(os.environ['DOCKER_CALLS'], 'a') as output:\n"
" output.write(json.dumps(sys.argv[1:]) + '\\n')\n"
"if os.environ['FAIL_PREPARATION'] == '1' and any(\n"
" arg.endswith('/prepare_library_embeddings.py')\n"
" for arg in sys.argv\n"
"):\n"
" sys.exit(17)\n"
)
docker.chmod(0o700)
result = subprocess.run(
["bash", "-c", textwrap.dedent(command)],
env={
**os.environ,
"PATH": f"{root}:/usr/bin:/bin",
"SHARD": shard,
"TEST_IMAGE": "fixture-image",
"GITHUB_WORKSPACE": str(root / "workspace with spaces"),
"DOCKER_CALLS": str(calls),
"FAIL_PREPARATION": str(int(fail_preparation)),
},
capture_output=True,
text=True,
timeout=10,
)
invocations = [
json.loads(line) for line in calls.read_text().splitlines()
]
return result, invocations
def test_library_prepares_and_seeds_shared_cache_before_offline_server(
self,
):
result, calls = self.invoke("library")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(
[call[-1] for call in calls],
[
"../scripts/ci/init_test_database.py",
"../scripts/ci/prepare_library_embeddings.py",
"../tests/ui_tests/seed_legacy_pkl_migration.py",
"ldr-web",
],
)
for call in calls:
self.assertIn("LDR_DATA_DIR=/data", call)
self.assertTrue(any(arg.endswith("/ci-data:/data") for arg in call))
# ldr-test does not inherit the production image's HF_HUB_CACHE env.
# Each disposable process must explicitly select the shared mount.
for call in calls[1:]:
self.assertIn("HF_HUB_CACHE=/data/cache/huggingface/hub", call)
server = calls[-1]
for setting in (
"LDR_LOCAL_SEARCH_EMBEDDING_PROVIDER=sentence_transformers",
"LDR_LOCAL_SEARCH_EMBEDDING_MODEL=all-MiniLM-L6-v2",
"LDR_EMBEDDINGS_REQUIRE_LOCAL=true",
"HF_HUB_OFFLINE=1",
"TRANSFORMERS_OFFLINE=1",
):
self.assertIn(setting, server)
def test_failed_acquisition_prevents_seed_and_server_startup(self):
result, calls = self.invoke("library", fail_preparation=True)
self.assertEqual(result.returncode, 17)
self.assertEqual(len(calls), 2)
def test_other_shards_do_not_acquire_or_override_embedding_models(self):
result, calls = self.invoke("auth-login")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(len(calls), 2)
self.assertFalse(any("EMBEDDING" in arg for arg in calls[-1]))
self.assertNotIn("HF_HUB_OFFLINE=1", calls[-1])
if __name__ == "__main__":
unittest.main()