~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
"""
Tests for embeddings/providers/implementations/sentence_transformers.py
Tests cover:
- SentenceTransformersProvider.create_embeddings()
- SentenceTransformersProvider.is_available()
- SentenceTransformersProvider.get_available_models()
- Class attributes and metadata
"""
import pathlib
from unittest.mock import patch, MagicMock
import pytest
PUBLIC_SETTINGS = {
"policy.egress_scope": "public_only",
"search.tool": "arxiv",
}
class TestSentenceTransformersProviderMetadata:
"""Tests for SentenceTransformersProvider class metadata."""
def test_provider_name(self):
"""Test provider name is set correctly."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert (
SentenceTransformersProvider.provider_name
== "Sentence Transformers"
)
def test_provider_key(self):
"""Test provider key is set correctly."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert (
SentenceTransformersProvider.provider_key == "SENTENCE_TRANSFORMERS"
)
def test_requires_api_key(self):
"""Test that Sentence Transformers does not require API key."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert SentenceTransformersProvider.requires_api_key is False
def test_supports_local(self):
"""Test that Sentence Transformers supports local."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert SentenceTransformersProvider.supports_local is True
def test_default_model(self):
"""Test default model is set."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert SentenceTransformersProvider.default_model == (
"Alibaba-NLP/gte-modernbert-base"
)
class TestSentenceTransformersProviderAvailableModels:
"""Tests for AVAILABLE_MODELS constant."""
def test_available_models_has_expected_models(self):
"""Test that AVAILABLE_MODELS contains expected models."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
models = SentenceTransformersProvider.AVAILABLE_MODELS
assert "all-MiniLM-L6-v2" in models
assert "all-mpnet-base-v2" in models
assert "multi-qa-MiniLM-L6-cos-v1" in models
assert "paraphrase-multilingual-MiniLM-L12-v2" in models
def test_available_models_have_dimensions(self):
"""Test that all models have dimensions metadata."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
for (
model_name,
model_info,
) in SentenceTransformersProvider.AVAILABLE_MODELS.items():
assert "dimensions" in model_info
assert isinstance(model_info["dimensions"], int)
def test_available_models_have_description(self):
"""Test that all models have description metadata."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
for (
model_name,
model_info,
) in SentenceTransformersProvider.AVAILABLE_MODELS.items():
assert "description" in model_info
assert isinstance(model_info["description"], str)
def test_available_models_have_max_seq_length(self):
"""Test that all models have max_seq_length metadata."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
for (
model_name,
model_info,
) in SentenceTransformersProvider.AVAILABLE_MODELS.items():
assert "max_seq_length" in model_info
assert isinstance(model_info["max_seq_length"], int)
class TestSentenceTransformersProviderCreateEmbeddings:
"""Tests for SentenceTransformersProvider.create_embeddings method."""
def test_create_embeddings_default_model(self):
"""Test creating embeddings with default model."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
mock_embeddings = MagicMock()
def mock_get_setting(key, default=None, settings_snapshot=None):
# Return None to use default model
return default
with patch(
"local_deep_research.embeddings.providers.implementations.sentence_transformers.get_setting_from_snapshot",
side_effect=mock_get_setting,
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=mock_embeddings,
) as mock_class:
result = SentenceTransformersProvider.create_embeddings(
settings_snapshot=PUBLIC_SETTINGS
)
mock_embeddings.embed_query.return_value = [0.25, 0.5]
assert result.embed_query("synthetic query") == [0.25, 0.5]
mock_embeddings.embed_query.assert_called_once_with(
"synthetic query"
)
mock_class.assert_called_once()
call_kwargs = mock_class.call_args[1]
# Default model should be used
assert call_kwargs["model_name"] == (
"Alibaba-NLP/gte-modernbert-base"
)
# CPU is default device
assert call_kwargs["model_kwargs"]["device"] == "cpu"
def test_create_embeddings_with_curated_model(self):
"""Test creating embeddings with another curated model."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
mock_embeddings = MagicMock()
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=mock_embeddings,
) as mock_class:
SentenceTransformersProvider.create_embeddings(
model="all-mpnet-base-v2",
settings_snapshot=PUBLIC_SETTINGS,
)
call_kwargs = mock_class.call_args[1]
assert call_kwargs["model_name"] == "all-mpnet-base-v2"
def test_create_embeddings_with_device(self):
"""Test creating embeddings with specific device."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
mock_embeddings = MagicMock()
with patch(
"local_deep_research.embeddings.providers.implementations.sentence_transformers.get_setting_from_snapshot",
side_effect=lambda key, default=None, settings_snapshot=None: (
default
),
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=mock_embeddings,
) as mock_class:
SentenceTransformersProvider.create_embeddings(
device="cuda", settings_snapshot=PUBLIC_SETTINGS
)
call_kwargs = mock_class.call_args[1]
assert call_kwargs["model_kwargs"]["device"] == "cuda"
def test_create_embeddings_default_device_cpu(self):
"""Test that default device is CPU."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
mock_embeddings = MagicMock()
def mock_get_setting(key, default=None, settings_snapshot=None):
if key == "embeddings.sentence_transformers.device":
return "cpu"
return default
with patch(
"local_deep_research.embeddings.providers.implementations.sentence_transformers.get_setting_from_snapshot",
side_effect=mock_get_setting,
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=mock_embeddings,
) as mock_class:
SentenceTransformersProvider.create_embeddings(
settings_snapshot=PUBLIC_SETTINGS
)
call_kwargs = mock_class.call_args[1]
assert call_kwargs["model_kwargs"]["device"] == "cpu"
def test_create_embeddings_with_settings_snapshot(self):
"""Test creating embeddings with settings snapshot."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
mock_embeddings = MagicMock()
# search.tool present => a public primary resolves PUBLIC_ONLY
# => require_local stays off, so this model-reading test is not
# diverted into the require-local download guard.
settings = {
"embeddings.sentence_transformers.model": "all-mpnet-base-v2",
"search.tool": "searxng",
}
def mock_get_setting(key, default=None, settings_snapshot=None):
if key == "embeddings.sentence_transformers.model":
return "all-mpnet-base-v2"
if key == "embeddings.sentence_transformers.device":
return "cpu"
return default
with patch(
"local_deep_research.embeddings.providers.implementations.sentence_transformers.get_setting_from_snapshot",
side_effect=mock_get_setting,
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=mock_embeddings,
) as mock_class:
SentenceTransformersProvider.create_embeddings(
settings_snapshot=settings
)
call_kwargs = mock_class.call_args[1]
assert call_kwargs["model_name"] == "all-mpnet-base-v2"
class TestSentenceTransformersProviderIsAvailable:
"""Tests for SentenceTransformersProvider.is_available method."""
def test_is_available_always_true(self):
"""Test that Sentence Transformers is always available."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert SentenceTransformersProvider.is_available() is True
def test_is_available_with_settings_snapshot(self):
"""Test that is_available works with settings snapshot."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
assert (
SentenceTransformersProvider.is_available(
settings_snapshot={"some": "settings"}
)
is True
)
class TestSentenceTransformersProviderGetAvailableModels:
"""Tests for SentenceTransformersProvider.get_available_models method."""
def test_get_available_models_returns_list(self):
"""Test that get_available_models returns a list."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
models = SentenceTransformersProvider.get_available_models()
assert isinstance(models, list)
def test_get_available_models_has_correct_structure(self):
"""Test that models have value and label keys."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
models = SentenceTransformersProvider.get_available_models()
for model in models:
assert "value" in model
assert "label" in model
assert isinstance(model["value"], str)
assert isinstance(model["label"], str)
def test_get_available_models_includes_dimensions_in_label(self):
"""Test that labels include dimension info."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
models = SentenceTransformersProvider.get_available_models()
for model in models:
assert "d)" in model["label"] # Dimensions indicator like "384d)"
def test_get_available_models_matches_available_models_constant(self):
"""Test that returned models match AVAILABLE_MODELS."""
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
models = SentenceTransformersProvider.get_available_models()
model_values = [m["value"] for m in models]
for (
expected_model
) in SentenceTransformersProvider.AVAILABLE_MODELS.keys():
assert expected_model in model_values
class TestSentenceTransformersProviderPathConfinement:
"""Security regression tests for embedding-model path confinement.
``local_search_embedding_model`` is user-editable free text. Without
confinement, ``SentenceTransformer`` loads ANY value that resolves to an
existing filesystem path, so an authenticated user could set an arbitrary
absolute server path and (a) probe the filesystem for existence/structure
and (b) load an arbitrary local model directory. The provider now treats a
value as a LOCAL model only when it resolves UNDER the app's models
directory; everything else is a HuggingFace repo id (and an escaping,
path-shaped value is refused outright).
"""
def _provider(self):
from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
SentenceTransformersProvider,
)
return SentenceTransformersProvider
# ---- (a) arbitrary path outside the models dir --------------------------
def test_arbitrary_absolute_path_not_treated_as_local_model(self, tmp_path):
"""An arbitrary absolute path OUTSIDE the models dir (``/etc``) is not
classified as a local model, and is NOT probed with a raw
``Path(...).exists()`` filesystem call. This is the core fix: the value
can no longer be used as a whole-filesystem existence oracle.
"""
provider = self._provider()
# Resolve tmp_path so the test does not silently depend on an
# un-symlinked /tmp: on platforms where the temp root has a symlinked
# ancestor (e.g. macOS /tmp -> /private/tmp) an UN-resolved models_dir
# would not match the resolved models root computed internally.
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir()
arbitrary = "/etc" # exists on the host, but outside the models dir
probed = []
real_exists = pathlib.Path.exists
def spy_exists(self):
probed.append(str(self))
return real_exists(self)
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
with patch(
"huggingface_hub.try_to_load_from_cache", return_value=None
):
with patch.object(pathlib.Path, "exists", spy_exists):
# Not confined -> not a local model...
assert (
provider._confined_local_model_path(arbitrary) is None
)
# ...and not admitted as "cached locally".
assert provider._is_model_cached_locally(arbitrary) is False
# No raw existence probe was performed on the arbitrary path or its
# children. On the pre-fix code the guard did ``Path("/etc").exists()``
# and admitted it as local, so this assertion (and the one above)
# fail there — the differential that proves the fix.
assert not any(
p == arbitrary or p.startswith(arbitrary + "/") for p in probed
)
def test_create_embeddings_refuses_arbitrary_absolute_path(self, tmp_path):
"""``create_embeddings`` refuses a filesystem-path-shaped value that
escapes the models dir and never hands it to the loader — so an
arbitrary server path is neither probed nor loaded.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir()
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings"
) as mock_st:
with pytest.raises(ValueError):
provider.create_embeddings(
model="/etc/passwd", device="cpu"
)
mock_st.assert_not_called()
def test_parent_traversal_value_refused(self, tmp_path):
"""A relative value containing ``..`` (attempting to climb out of the
models dir) is path-shaped, not confined, and refused.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir()
assert provider._looks_like_filesystem_path("../../etc/passwd") is True
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
assert (
provider._confined_local_model_path("../../etc/passwd") is None
)
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings"
) as mock_st:
with pytest.raises(ValueError):
provider.create_embeddings(
model="../../etc/passwd", device="cpu"
)
mock_st.assert_not_called()
# ---- (b) a path INSIDE the models dir still resolves as local -----------
def test_path_inside_models_dir_is_local_model(self, tmp_path):
"""A model directory genuinely UNDER the app's models dir still loads:
it is classified as local (by absolute and by relative reference) and
admitted without an HF cache probe.
``create_embeddings`` resolves the confined path itself and returns
before the Hub-cache helper runs, so the helper classifies a
filesystem-shaped name as a repo-id miss (and, crucially, still never
probes the Hub cache for it).
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
model_dir = models_dir / "custom-st-model"
model_dir.mkdir(parents=True)
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
# Absolute path under the models dir resolves to the model dir.
assert (
provider._confined_local_model_path(str(model_dir))
== model_dir.resolve()
)
# A path relative to the models dir resolves the same way.
assert (
provider._confined_local_model_path("custom-st-model")
== model_dir.resolve()
)
with patch("huggingface_hub.try_to_load_from_cache") as mock_cache:
assert (
provider._is_model_cached_locally(str(model_dir)) is False
)
mock_cache.assert_not_called()
# ...and the end-to-end load takes the confined-path branch.
with (
patch("huggingface_hub.try_to_load_from_cache") as mock_cache,
patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=MagicMock(),
) as mock_st,
):
provider.create_embeddings(
model="custom-st-model", device="cpu"
)
assert mock_st.call_args.kwargs["model_name"] == str(
model_dir.resolve()
)
mock_cache.assert_not_called()
def test_create_embeddings_loads_confined_local_model_by_abs_path(
self, tmp_path
):
"""``create_embeddings`` loads a confined local model from its safe
ABSOLUTE path (so the loader takes its local-path branch even when the
user referenced the model by a name relative to the models dir).
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
model_dir = models_dir / "custom-st-model"
model_dir.mkdir(parents=True)
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=MagicMock(),
) as mock_st:
provider.create_embeddings(
model="custom-st-model", device="cpu"
)
assert mock_st.call_args.kwargs["model_name"] == str(
model_dir.resolve()
)
# Pin the full security-hardening kwargs for this load
# site: a planted local model dir must never opt into
# remote code, authenticated Hub access, or pickle weights.
assert mock_st.call_args.kwargs["model_kwargs"] == {
"device": "cpu",
"trust_remote_code": False,
"token": False,
"model_kwargs": {"use_safetensors": True},
"local_files_only": True,
}
# ---- (b'') a confined dir outranks a curated model of the same name -----
@pytest.mark.parametrize(
"settings_snapshot",
[
{"policy.egress_scope": "public_only", "search.tool": "arxiv"},
{"policy.egress_scope": "strict", "search.tool": "arxiv"},
{"policy.egress_scope": "private_only", "search.tool": "library"},
],
ids=["public_only", "strict", "private_only"],
)
def test_confined_local_dir_named_like_a_catalog_key_wins_under_every_scope(
self, tmp_path, settings_snapshot, loguru_caplog
):
"""An operator-placed model directory whose name matches a catalog key
is still the model that loads, in every egress scope.
The confined models directory is app-owned state: an operator who
copies a model there is naming THAT artifact, and a collection indexed
from it must keep embedding with the same bytes. Resolving the name to
the curated Hub repository instead would silently swap the artifact
under a public scope (different vectors for the same collection) and
refuse the load outright under STRICT / PRIVATE_ONLY — where the local
directory is precisely the offline-usable form. The substitution is
also never silent: it is announced on the policy audit channel, naming
the directory and the curated model it displaces.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
model_dir = models_dir / "all-MiniLM-L6-v2"
model_dir.mkdir(parents=True)
with (
patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
),
patch("huggingface_hub.try_to_load_from_cache") as mock_cache,
patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=MagicMock(),
) as mock_st,
loguru_caplog.at_level("WARNING"),
):
# The audit flag is a bound record extra, not rendered text, so
# it needs a sink that sees the loguru record itself.
from loguru import logger as loguru_logger
records = []
sink_id = loguru_logger.add(
lambda message: records.append(message.record),
level="WARNING",
)
try:
provider.create_embeddings(
model="all-MiniLM-L6-v2",
settings_snapshot=settings_snapshot,
device="cpu",
)
finally:
loguru_logger.remove(sink_id)
# The local directory, loaded offline from its safe absolute path.
assert mock_st.call_args.kwargs["model_name"] == str(
model_dir.resolve()
)
model_kwargs = mock_st.call_args.kwargs["model_kwargs"]
assert model_kwargs["local_files_only"] is True
assert model_kwargs["token"] is False
assert model_kwargs["trust_remote_code"] is False
assert model_kwargs["model_kwargs"]["use_safetensors"] is True
# No Hub artifact is involved: no revision pin, and the Hub cache is
# never consulted for a model that was resolved on disk.
assert "revision" not in model_kwargs
mock_cache.assert_not_called()
# The shadowing is audited, naming both the directory and the model
# it displaces.
assert str(model_dir.resolve()) in loguru_caplog.text
assert "sentence-transformers/all-MiniLM-L6-v2" in loguru_caplog.text
# ...on the policy audit channel, not as an ordinary warning.
shadow_records = [
record
for record in records
if str(model_dir.resolve()) in record["message"]
]
assert len(shadow_records) == 1
assert shadow_records[0]["extra"].get("policy_audit") is True
def test_hub_id_cannot_select_a_matching_working_directory_unpatched(
self, tmp_path, monkeypatch
):
"""Precedence check with nothing patched out: a directory in the
PROCESS WORKING DIRECTORY named like a Hub id is still refused.
The confined-models-dir precedence above is about the application's
OWN models directory. A same-named directory that merely happens to
sit in the working directory is a different concern — upstream's
``os.path.exists`` branch would load it — and stays refused. The
catalog-file version of this test patches ``_confined_local_model_path``
and ``_is_model_cached_locally`` out; here both run for real.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir() # empty: nothing is confined
workdir = tmp_path / "cwd"
(workdir / "sentence-transformers" / "all-MiniLM-L6-v2").mkdir(
parents=True
)
(workdir / "all-MiniLM-L6-v2").mkdir()
monkeypatch.chdir(workdir)
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
for model in (
"all-MiniLM-L6-v2",
"sentence-transformers/all-MiniLM-L6-v2",
):
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings"
) as mock_st:
with pytest.raises(
ValueError, match="conflicts with a local path"
):
provider.create_embeddings(
model=model,
settings_snapshot=PUBLIC_SETTINGS,
device="cpu",
)
mock_st.assert_not_called()
# ---- (b') a symlinked ancestor must not refuse a legit model ------------
def test_symlinked_models_dir_ancestor_still_loads_confined_model(
self, tmp_path
):
"""A legitimate in-tree absolute model still loads when an ANCESTOR of
the models dir is a symlink (NFS-mounted homes, a symlinked
``LDR_DATA_DIR``, macOS ``/tmp`` -> ``/private/tmp``).
Regression test for over-strict confinement: the lexical containment
check must compare the UN-resolved user path against an UN-resolved
models root. If it instead compares the un-resolved user path against a
RESOLVED root, a symlinked ancestor makes every in-tree absolute model
fail the lexical match and be wrongly refused. The secondary
post-resolution re-check must still refuse an in-tree symlink whose
target ESCAPES the models dir.
"""
provider = self._provider()
# Real tree (fully resolved), plus a symlinked view of its parent so
# that referencing the model through the symlink exercises the
# symlinked-ancestor case explicitly rather than relying on the host's
# /tmp being (or not being) a symlink.
real_root = tmp_path.resolve()
models_dir = real_root / "data" / "models"
model_dir = models_dir / "custom-st-model"
model_dir.mkdir(parents=True)
link_root = real_root / "linked" # linked -> data (symlinked ancestor)
try:
link_root.symlink_to(real_root / "data")
except (OSError, NotImplementedError): # pragma: no cover - platform
pytest.skip("symlinks not supported on this platform")
linked_models_dir = link_root / "models"
linked_model_dir = linked_models_dir / "custom-st-model"
# The symlinked reference resolves to the real in-tree model.
assert linked_model_dir.resolve() == model_dir.resolve()
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=linked_models_dir,
):
# Admitted and resolved to its real path. Pre-fix (un-resolved user
# path vs resolved root) this returned None -> wrongly refused.
assert (
provider._confined_local_model_path(str(linked_model_dir))
== model_dir.resolve()
)
# ...and create_embeddings hands the loader that safe absolute path.
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=MagicMock(),
) as mock_st:
provider.create_embeddings(
model=str(linked_model_dir), device="cpu"
)
assert mock_st.call_args.kwargs["model_name"] == str(
model_dir.resolve()
)
# An in-tree symlink whose TARGET escapes the models dir is still
# refused by the secondary post-resolution re-check (the lexical
# relaxation must not open a symlink-escape hole).
escape = models_dir / "escape"
try:
escape.symlink_to(real_root.parent)
except (OSError, NotImplementedError): # pragma: no cover
pytest.skip("symlinks not supported on this platform")
assert (
provider._confined_local_model_path(
str(linked_models_dir / "escape")
)
is None
)
# ---- (b'') a symlink LOOP must fail closed, not raise uncaught ----------
def test_symlink_loop_fails_closed(self, tmp_path):
"""A symlink LOOP under the models dir (``loop_a`` -> ``loop_b`` ->
``loop_a``) must fail CLOSED with a clean refusal, not leak an
uncaught ``RuntimeError``.
``pathlib.Path.resolve()`` raises ``RuntimeError`` (NOT ``OSError``)
when it detects a symlink loop while collapsing a path. Regression
test for the file's fail-closed invariant: a loop must be caught and
translated into the same "not a local model" refusal as any other
unconfined/unresolvable value, never propagate an uncaught
``RuntimeError`` out of ``_confined_local_model_path`` /
``create_embeddings``.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir()
loop_a = models_dir / "loop_a"
loop_b = models_dir / "loop_b"
try:
loop_a.symlink_to(loop_b)
loop_b.symlink_to(loop_a)
except (OSError, NotImplementedError): # pragma: no cover - platform
pytest.skip("symlinks not supported on this platform")
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
# The loop must resolve to "not a local model", not raise.
assert provider._confined_local_model_path(str(loop_a)) is None
# create_embeddings must refuse it with the same clean, generic
# ValueError as any other unconfined/unresolvable value -- never
# an uncaught RuntimeError.
with patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings"
) as mock_st:
with pytest.raises(ValueError):
provider.create_embeddings(model=str(loop_a), device="cpu")
mock_st.assert_not_called()
# ---- (c) a normal HF id still works -------------------------------------
@pytest.mark.parametrize(
"model", ["all-MiniLM-L6-v2", "sentence-transformers/all-MiniLM-L6-v2"]
)
def test_hf_id_treated_as_repo_id(self, tmp_path, model):
"""A normal HuggingFace id — bare, or ``org/name`` containing ``/`` but
not a filesystem path — is NOT classified as a local path and is passed
through to the loader unchanged as a repo id when cached.
"""
provider = self._provider()
tmp_path = tmp_path.resolve()
models_dir = tmp_path / "models"
models_dir.mkdir()
namespaced = "sentence-transformers/all-MiniLM-L6-v2"
assert provider._looks_like_filesystem_path("all-MiniLM-L6-v2") is False
assert provider._looks_like_filesystem_path(namespaced) is False
with patch(
"local_deep_research.config.paths.get_models_directory",
return_value=models_dir,
):
# Confines lexically but does not exist under the models dir, so it
# is handled as a repo id, not a local path.
assert (
provider._confined_local_model_path("all-MiniLM-L6-v2") is None
)
assert provider._confined_local_model_path(namespaced) is None
with (
patch(
"huggingface_hub.try_to_load_from_cache",
return_value="/cache/config.json",
),
patch(
"langchain_community.embeddings.SentenceTransformerEmbeddings",
return_value=MagicMock(),
) as mock_st,
):
provider.create_embeddings(model=model, device="cpu")
assert mock_st.call_args.kwargs["model_name"] == model
assert (
mock_st.call_args.kwargs["model_kwargs"]["local_files_only"]
is True
)