~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
"""Contracts for the settings defaults LDR actually ships.
Scope: the JSON under ``src/local_deep_research/defaults/`` (the values
every fresh install runs with), the environment-only registry under
``src/local_deep_research/settings/env_definitions/``, and the handful of
places in ``src/`` that keep a *second* copy of a shipped default.
Four contracts, in the order they matter:
1. **Secure by default.** Every security-relevant shipped value is
enumerated explicitly -- not sampled -- and asserted to be the safe
value. A completeness guard re-derives the security-shaped key set from
the defaults themselves and fails if a key lands outside every
classification, so a *new* security setting cannot be added without a
deliberate decision recorded here.
2. **Every default is loadable.** Each key's ``value`` must survive the
app's own ``get_typed_setting_value`` and sit inside its own declared
``min_value``/``max_value``/``options``. A default that fails its own
validation is a live bug.
3. **No credential or personal value ships.** Nothing that looks like a
key, token, home directory, or e-mail address may appear in a default
value.
4. **Registry consistency.** Every environment-registry key is read by
something, and every shipped default key is referenced by something.
Relationship to the neighbouring files. ``test_settings_defaults_
integrity.py`` checks structural schema and the consumed-vs-defined key
sets; ``test_settings_defaults_runtime_validation.py`` replays the
JS-disabled Save path over every default. Neither of them looks at *what
the values mean*: whether a gate is open, whether a rate limit is small
enough to matter, whether a second hardcoded copy of a default has
drifted. That is this file.
Known defects are recorded as ``xfail(strict=True)`` rather than as
entries in an exemption list. The difference is deliberate: an exemption
list reads as "this is acceptable", while a strict xfail reads as "this is
broken, and the day it is fixed this test turns red until the marker is
deleted". Each carries the defect in its ``reason``.
"""
import ast
import functools
import json
import os
import re
from pathlib import Path
from typing import Any, Dict, List, Tuple
import pytest
from limits import parse_many
from local_deep_research.security.security_settings import (
get_security_default,
)
from local_deep_research.settings.env_definitions import ALL_SETTINGS
from local_deep_research.settings.env_settings import (
BooleanSetting,
EnumSetting,
IntegerSetting,
StringSetting,
)
from local_deep_research.settings.manager import (
DYNAMIC_SETTINGS,
UI_ELEMENT_TO_SETTING_TYPE,
SettingsManager,
get_typed_setting_value,
)
from local_deep_research.web import server_config
# ---------------------------------------------------------------------------
# Paths and corpus floors
# ---------------------------------------------------------------------------
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
SRC_DIR = REPO_ROOT / "src" / "local_deep_research"
DEFAULTS_DIR = SRC_DIR / "defaults"
# Floors. A sweep that loads nothing and passes is the failure mode these
# guard against: every test below asserts against one of them so an empty
# or half-loaded corpus can never look like a clean run. Current counts are
# 31 files / 586 keys; the floors sit below that so ordinary additions do
# not churn them, but far above zero.
MIN_DEFAULT_FILES = 25
MIN_DEFAULT_KEYS = 550
MIN_SRC_FILES_SWEPT = 400
# Floor on the env-only registry: guards against mass removal or a registry
# that failed to load, not a count. 30 are registered, so 27 absorbs a few
# deliberate removals but trips when a whole category (security 11,
# db_config 10, server 4) is lost.
MIN_ENV_SETTINGS = 27
# Keys that must exist for the corpus to be considered loaded at all. One
# per shipped file family, chosen because each is load-bearing somewhere
# below.
ANCHOR_KEYS = frozenset(
{
"app.allow_registrations",
"app.debug",
"llm.openai.api_key",
"policy.egress_scope",
"research_library.pdf_storage_mode",
"search.engine.web.paperless.default_params.verify_ssl",
"search.engine.web.searxng.default_params.instance_url",
"security.account_lockout_threshold",
"security.rate_limit_login",
"security.rate_limit_registration",
"security.session_remember_me_days",
"web.host",
"web.use_https",
"zotero.api_key",
}
)
# ---------------------------------------------------------------------------
# Loaders
# ---------------------------------------------------------------------------
@functools.lru_cache(maxsize=1)
def _raw_defaults() -> Tuple[Dict[str, Any], Tuple[str, ...]]:
"""Parse the shipped JSON directly, without going through the app.
Deliberately independent of ``SettingsManager`` so a fault in the
loader cannot hide a fault in the data. Merge order mirrors
``SettingsManager.default_settings`` (``sorted(rglob("*.json"))``).
"""
merged: Dict[str, Any] = {}
files: List[str] = []
for path in sorted(DEFAULTS_DIR.rglob("*.json")):
files.append(str(path.relative_to(DEFAULTS_DIR)))
with open(path, "r", encoding="utf-8-sig") as handle:
merged.update(json.load(handle))
return merged, tuple(files)
def raw_defaults() -> Dict[str, Any]:
merged, _ = _raw_defaults()
return merged
def default_files() -> Tuple[str, ...]:
_, files = _raw_defaults()
return files
@functools.lru_cache(maxsize=1)
def resolved_defaults() -> Dict[str, Any]:
"""Defaults as the app computes them, including the runtime option
injection ``SettingsManager.default_settings`` performs (theme
registry, search-strategy list). Options validation must run against
these, not the raw JSON.
"""
return SettingsManager(db_session=None).default_settings
@functools.lru_cache(maxsize=1)
def _src_texts() -> Tuple[Tuple[str, str], ...]:
"""Every Python/JS/HTML source file under ``src/``, excluding the
defaults tree itself (a key naming itself in its own JSON is not a
reference).
"""
out: List[Tuple[str, str]] = []
for pattern in ("*.py", "*.js", "*.html", "*.ts"):
for path in SRC_DIR.rglob(pattern):
if DEFAULTS_DIR in path.parents:
continue
try:
out.append(
(
str(path.relative_to(SRC_DIR)),
path.read_text(encoding="utf-8"),
)
)
except (OSError, UnicodeDecodeError):
continue
return tuple(out)
@functools.lru_cache(maxsize=1)
def _src_blob() -> str:
return "\n".join(text for _, text in _src_texts())
def env_var_for(key: str) -> str:
"""The LDR_ environment variable name derived from a setting key.
Same derivation as ``EnvSetting.__init__`` and
``SettingsManager._key_to_env_var``.
"""
return "LDR_" + key.upper().replace(".", "_")
@pytest.fixture(autouse=True)
def _clean_ldr_env():
"""Clear ``LDR_*`` for the duration of each test.
Without this, an exported override in the developer's shell could make
an insecure shipped default look safe (or vice versa). Mirrors the
fixture in the two neighbouring defaults test modules.
"""
saved = {k: v for k, v in os.environ.items() if k.startswith("LDR_")}
for key in list(os.environ):
if key.startswith("LDR_"):
os.environ.pop(key, None)
yield
for key in list(os.environ):
if key.startswith("LDR_"):
os.environ.pop(key, None)
os.environ.update(saved)
# ---------------------------------------------------------------------------
# 0. The corpus really loaded
# ---------------------------------------------------------------------------
class TestCorpusFloor:
"""Nothing below is meaningful if the defaults did not load."""
def test_defaults_corpus_is_actually_loaded(self):
files = default_files()
defaults = raw_defaults()
assert len(files) >= MIN_DEFAULT_FILES, (
f"only {len(files)} JSON files found under {DEFAULTS_DIR}; "
f"expected at least {MIN_DEFAULT_FILES}. Either the defaults "
f"tree moved or this sweep is examining nothing."
)
assert len(defaults) >= MIN_DEFAULT_KEYS, (
f"only {len(defaults)} setting keys parsed; expected at least "
f"{MIN_DEFAULT_KEYS}"
)
missing = sorted(ANCHOR_KEYS - set(defaults))
assert not missing, (
f"anchor keys absent from the shipped defaults: {missing}. "
f"These are the keys the security assertions below depend on; "
f"if one was renamed, update this test rather than dropping it."
)
# Every entry must be a metadata dict with the fields the loader
# and the validators read. A bare scalar here would silently break
# `_filter_setting_columns` on seeding.
required = ("value", "name", "description", "ui_element", "type")
malformed = [
key
for key, meta in defaults.items()
if not isinstance(meta, dict)
or any(field not in meta for field in required)
]
assert not malformed, (
f"{len(malformed)} shipped defaults are not well-formed "
f"metadata dicts: {malformed[:10]}"
)
def test_settings_manager_resolves_every_shipped_key(self):
"""The app's own loader must surface every key on disk.
A file that fails to parse is swallowed by a broad ``except`` in
``SettingsManager.default_settings`` and merely logged, so a
corrupt shipped file would otherwise ship silently.
"""
raw = raw_defaults()
resolved = resolved_defaults()
assert len(resolved) >= MIN_DEFAULT_KEYS
dropped = sorted(set(raw) - set(resolved))
assert not dropped, (
f"SettingsManager did not load {len(dropped)} shipped keys "
f"(a JSON parse failure is swallowed and only logged): "
f"{dropped[:10]}"
)
def test_src_sweep_reads_a_real_tree(self):
files = _src_texts()
assert len(files) >= MIN_SRC_FILES_SWEPT, (
f"only {len(files)} source files swept under {SRC_DIR}; the "
f"reference checks below would pass vacuously"
)
assert len(_src_blob()) > 1_000_000
# ---------------------------------------------------------------------------
# 1. Secure by default
# ---------------------------------------------------------------------------
# Every security-relevant shipped default and the value it must hold.
# Enumerated, not sampled. "Safe" here means: the value that does not
# widen exposure -- gate closed, protection on, warning shown, credential
# absent.
SAFE_SHIPPED_DEFAULTS: Dict[str, Any] = {
# --- logging / diagnostics -------------------------------------------
# Debug raises log verbosity and is also handed to uvicorn by
# web/app.py via load_server_config()["debug"].
"app.debug": False,
"app.enable_file_logging": False,
# --- listen address ---------------------------------------------------
# web.host is the value web/app.py actually binds. See
# test_app_host_is_not_the_listen_address for the app.host trap.
"web.host": "127.0.0.1",
"web.use_https": True,
# --- egress / SSRF ----------------------------------------------------
# "unprotected" is an option on this select, but selecting it is gated
# by the env-only policy.allow_unprotected_egress (asserted False
# below) and coerced back to adaptive when that gate is closed.
"policy.egress_scope": "adaptive",
"policy.trusted_inference_providers": [],
"policy.trusted_search_engines": [],
"llm.allowed_local_hostnames": [],
# --- outbound notifications ------------------------------------------
"notifications.enabled": False,
# --- multi-tenant isolation ------------------------------------------
# "filesystem" writes third-party PDFs as plaintext (CWE-312) and is
# itself gated by research_library.allow_filesystem_pdf_storage.
"research_library.pdf_storage_mode": "database",
"research_library.shared_library": False,
"zotero.pdf_storage_mode": "none",
# --- API control surface ---------------------------------------------
"news.scheduler.allow_api_control": False,
# --- transport --------------------------------------------------------
"search.engine.web.paperless.default_params.verify_ssl": True,
# --- content safety ---------------------------------------------------
"search.safe_search": True,
# --- warnings must be visible until dismissed ------------------------
# Every one of these suppresses an operator-facing warning about a
# risky configuration (cloud LLM/embeddings egress, a publicly
# reachable Elasticsearch or Paperless URL, a private SearXNG URL,
# the adaptive egress scope). Shipping any of them pre-dismissed
# would hide the warning on a fresh install.
"app.warnings.dismiss_adaptive_scope_info": False,
"app.warnings.dismiss_cloud_embeddings": False,
"app.warnings.dismiss_cloud_llm": False,
"app.warnings.dismiss_context_below_history": False,
"app.warnings.dismiss_context_truncation_history": False,
"app.warnings.dismiss_egress_policy": False,
"app.warnings.dismiss_elasticsearch_public_url": False,
"app.warnings.dismiss_high_context": False,
"app.warnings.dismiss_legacy_config": False,
"app.warnings.dismiss_model_mismatch": False,
"app.warnings.dismiss_paperless_public_url": False,
"app.warnings.dismiss_searxng_private_url": False,
"app.warnings.dismiss_searxng_recommendation": False,
# --- per-engine content filtering ------------------------------------
# mojeek and searxng are the two exceptions; see
# DOCUMENTED_PERMISSIVE_DEFAULTS.
"search.engine.web.brave.default_params.safe_search": True,
"search.engine.web.google_pse.default_params.safe_search": True,
"search.engine.web.scaleserp.default_params.safe_search": True,
"search.engine.web.serpapi.default_params.safe_search": True,
"search.engine.web.serper.default_params.safe_search": True,
# --- abuse controls on -----------------------------------------------
"rate_limiting.enabled": True,
# --- session / lockout numerics --------------------------------------
"security.account_lockout_threshold": 10,
"security.account_lockout_duration_minutes": 15,
"security.session_timeout_hours": 2,
# NOTE: 30 is also this setting's own max_value -- the shipped default
# is the *longest* remember-me window the schema permits, and it is
# what SessionMiddleware uses as the session cookie max_age. Pinned
# here so any change is deliberate; see the report for the rationale.
"security.session_remember_me_days": 30,
}
# Security-shaped defaults that are deliberately *permissive*, each with
# the reason and the control that compensates. Separated from the table
# above so "safe value" never has to mean "whatever we ship".
DOCUMENTED_PERMISSIVE_DEFAULTS: Dict[str, Tuple[Any, str]] = {
"app.allow_registrations": (
True,
"Initial account creation must work out of the box. Compensating "
"controls: the default listen address is loopback (web.host) and "
"registration is rate limited to 10/minute and 300/hour per IP.",
),
"app.lock_settings": (
False,
"Locking settings is a production hardening opt-in, not a gate; "
"on by default would make first-run configuration impossible.",
),
"app.enable_web": (
True,
"The web UI is the product. Exposure is governed by web.host.",
),
"llm.require_local_endpoint": (
False,
"Forcing a local-only LLM endpoint would disable every hosted "
"provider. Opt-in policy control, audited on change via "
"settings.manager._POLICY_AUDIT_KEYS.",
),
"embeddings.require_local": (
False,
"Same rationale as llm.require_local_endpoint.",
),
"notifications.on_auth_issue": (
False,
"A notification preference, not an access control.",
),
"search.engine.web.mojeek.default_params.safe_search": (
False,
"Ships OFF while the global search.safe_search is True and every "
"other engine defaults ON. Recorded here rather than asserted "
"safe because it is a real inconsistency, not a decision: the "
"engine class signature (search_engine_mojeek.py) also defaults "
"safe_search=False.",
),
"search.engine.web.searxng.default_params.safe_search": (
"OFF",
"Ships OFF, and searxng is DEFAULT_SEARCH_TOOL -- so the engine a "
"fresh install actually searches with has content filtering "
"disabled while search.safe_search reads True in the UI. This is "
"a three-level select (OFF/MODERATE/STRICT), so the safe value "
"exists and is simply not the default.",
),
}
# Keys the security-shape regex matches that are not access controls at
# all. Naming them is what keeps the completeness guard honest: an
# unclassified match fails rather than being quietly skipped.
NOT_A_SECURITY_CONTROL = frozenset(
{
# Informational performance tip only; dismissal changes no policy.
"app.warnings.dismiss_embedding_performance",
"app.host", # notification URL building only -- see its own test
"app.port",
"web.port",
"backup.enabled",
"document_scheduler.enabled",
"news.scheduler.enabled",
"notifications.rate_limit_per_day", # covered by rate-limit tests
"notifications.rate_limit_per_hour",
"search.engine.web.arxiv.journal_reputation.enabled",
"search.engine.web.nasa_ads.journal_reputation.enabled",
"search.engine.web.openalex.journal_reputation.enabled",
"search.engine.web.paperless.enabled",
"search.engine.web.semantic_scholar.journal_reputation.enabled",
"security.rate_limit_default", # covered by rate-limit tests
"security.rate_limit_login",
"security.rate_limit_registration",
"security.rate_limit_settings",
"security.rate_limit_upload_ip",
"security.rate_limit_upload_user",
"zotero.enabled",
}
)
# Matches the *last* dot-segment of a key, so `foo.allow_bar` matches but
# `foo.requires_api_key` (a boolean capability flag, covered by the
# credential tests) does not.
SECURITY_SHAPED_KEY = re.compile(
r"(?:^|\.)(?:"
r"allow_\w+|require_\w+|requires_local|trusted_\w+|allowed_\w+|"
r"verify_\w+|rate_limit\w*|account_lockout\w*|session_\w+|cors\w*|"
r"csrf\w*|egress_\w+|shared_library|pdf_storage_mode|lock_settings|"
r"debug|enable_file_logging|use_https|host|port|safe_search|"
r"dismiss_\w+|enabled"
r")$"
)
class TestSecureByDefault:
"""Enumerated, not sampled: every security-relevant shipped value."""
def test_safe_defaults_hold_their_safe_value(self):
defaults = raw_defaults()
assert len(SAFE_SHIPPED_DEFAULTS) >= 38
missing = sorted(set(SAFE_SHIPPED_DEFAULTS) - set(defaults))
assert not missing, (
f"security-relevant keys vanished from the shipped defaults: "
f"{missing}. A control that no longer ships is not a control."
)
wrong = {
key: (defaults[key]["value"], expected)
for key, expected in SAFE_SHIPPED_DEFAULTS.items()
if defaults[key]["value"] != expected
}
assert not wrong, (
"shipped defaults are not at their safe value "
"(key: (shipped, expected)): " + repr(wrong)
)
def test_documented_permissive_defaults_still_hold(self):
"""Permissive by design -- pinned so a change is never silent."""
defaults = raw_defaults()
assert len(DOCUMENTED_PERMISSIVE_DEFAULTS) >= 8
for key, (expected, reason) in DOCUMENTED_PERMISSIVE_DEFAULTS.items():
assert key in defaults, f"{key} no longer ships"
assert defaults[key]["value"] == expected, (
f"{key} changed from its documented permissive value "
f"{expected!r} to {defaults[key]['value']!r}. Reason on "
f"record for the old value: {reason}"
)
assert len(reason) > 40, (
f"{key} needs a real justification, not a placeholder"
)
def test_every_security_shaped_default_is_classified(self):
"""Completeness guard.
Re-derives the security-shaped key set from the shipped defaults
instead of trusting the tables above to be exhaustive, so a newly
added ``allow_*`` / ``*_rate_limit`` / session key cannot ship
without someone deciding which table it belongs in.
"""
defaults = raw_defaults()
shaped = {k for k in defaults if SECURITY_SHAPED_KEY.search(k)}
assert len(shaped) >= 55, (
f"only {len(shaped)} security-shaped keys matched; the "
f"classifier is not seeing the corpus"
)
classified = (
set(SAFE_SHIPPED_DEFAULTS)
| set(DOCUMENTED_PERMISSIVE_DEFAULTS)
| NOT_A_SECURITY_CONTROL
)
unclassified = sorted(shaped - classified)
assert not unclassified, (
f"unclassified security-shaped defaults: {unclassified}. Add "
f"each to SAFE_SHIPPED_DEFAULTS (with its safe value), to "
f"DOCUMENTED_PERMISSIVE_DEFAULTS (with a reason and a "
f"compensating control), or to NOT_A_SECURITY_CONTROL."
)
stale = sorted(
(set(SAFE_SHIPPED_DEFAULTS) | set(NOT_A_SECURITY_CONTROL))
- set(defaults)
)
assert not stale, (
f"classified keys that no longer ship: {stale}; drop them so "
f"the tables keep describing reality"
)
def test_env_only_security_gates_default_closed(self):
"""Every operator gate in ``env_definitions/security.py`` is off.
These are the environment-only settings that cannot be flipped
through the user-writable settings API: NAT64 egress, outbound
webhooks, private search-engine URLs, unprotected egress,
plaintext PDF storage, shared library, legacy cross-tenant read.
The invariant is structural rather than a list of names, so a new
gate that defaults on fails immediately.
"""
gates = ALL_SETTINGS["security"]
assert len(gates) >= 11, (
f"only {len(gates)} security env settings registered; the "
f"registry did not load"
)
open_by_default = [
s.key
for s in gates
if isinstance(s, BooleanSetting) and s.default is not False
]
assert not open_by_default, (
f"environment-only security gates that default to open: "
f"{open_by_default}"
)
# Origin allowlists must be unset (same-origin only), never "*".
permissive_origins = [
s.key
for s in gates
if isinstance(s, StringSetting) and s.default is not None
]
assert not permissive_origins, (
f"origin/allowlist env settings ship a non-empty default: "
f"{permissive_origins}"
)
# Name the ones whose absence would be a silent loss of coverage.
by_key = {s.key: s for s in gates}
for key in (
"notifications.allow_outbound",
"notifications.allow_private_ips",
"policy.allow_unprotected_egress",
"research_library.allow_filesystem_pdf_storage",
"research_library.allow_shared_library",
"search.allow_private_engine_urls",
"search.allow_private_result_fetch",
"security.allow_nat64",
):
assert key in by_key, f"{key} is no longer registered"
assert by_key[key].default is False
for key in (
"security.cors.allowed_origins",
"security.websocket.allowed_origins",
"search.private_engine_url_allowlist",
):
assert key in by_key, f"{key} is no longer registered"
assert by_key[key].default is None
def test_bootstrap_and_crypto_env_defaults_are_safe(self):
by_key = {
setting.key: setting
for group in ALL_SETTINGS.values()
for setting in group
}
assert len(by_key) >= MIN_ENV_SETTINGS
# Encryption must not be optional by default.
assert by_key["bootstrap.allow_unencrypted"].default is False
# Test/dev switches must be off in a shipped build.
assert by_key["testing.test_mode"].default is False
assert by_key["vite.dev_mode"].default is False
kdf = by_key["db_config.kdf_algorithm"]
assert isinstance(kdf, EnumSetting)
assert kdf.default == "PBKDF2_HMAC_SHA512"
hmac = by_key["db_config.hmac_algorithm"]
assert isinstance(hmac, EnumSetting)
assert hmac.default == "HMAC_SHA512"
iterations = by_key["db_config.kdf_iterations"]
assert isinstance(iterations, IntegerSetting)
# OWASP's floor for PBKDF2-HMAC-SHA512 is 210k; SQLCipher 4's own
# default is 256k. Anything materially lower weakens every
# at-rest database.
assert iterations.default >= 210_000, (
f"KDF iterations default dropped to {iterations.default}"
)
def test_no_shipped_default_points_egress_at_a_non_local_third_party(
self,
):
"""URL-valued defaults must be loopback or a documented vendor.
Guards against a contributor's private host or tunnel leaking into
the shipped defaults, which would make every fresh install talk to
it.
"""
defaults = raw_defaults()
allowed_hosts = {
"localhost",
"127.0.0.1",
"openrouter.ai",
}
offenders = []
checked = 0
for key, meta in defaults.items():
blob = json.dumps(meta.get("value"))
for match in re.finditer(r"https?://([^/\"\\,\s]+)", blob):
checked += 1
host = match.group(1).split(":")[0].lower()
if host not in allowed_hosts:
offenders.append((key, host))
assert checked >= 8, (
f"only {checked} URL-valued defaults examined; the sweep is "
f"not finding the engine/provider URLs"
)
assert not offenders, (
f"shipped defaults point at unexpected hosts: {offenders}"
)
def test_notification_link_metadata_pins_external_url_and_fallbacks(self):
"""Notification URL metadata never promises unavailable detection.
``app.external_url`` must be configured explicitly for external
deployments; when blank, ``app.host`` and ``app.port`` provide the
notification-link fallback. The actual listener is configured through
``LDR_WEB_*`` environment variables (or the deprecated legacy JSON
fallback); ``web.*`` names are internal keys, not persisted listener
controls. Pin the user-facing metadata because confusing these pairs
can cause an operator to make the opposite network-exposure decision
from the one they intend.
"""
defaults = raw_defaults()
assert defaults["app.external_url"]["value"] == ""
assert defaults["app.host"]["value"] == "0.0.0.0"
assert defaults["app.port"]["value"] == 5000
assert defaults["web.host"]["value"] == "127.0.0.1"
assert defaults["web.port"]["value"] == 5000
external_url = defaults["app.external_url"]
app_host = defaults["app.host"]
app_port = defaults["app.port"]
assert app_host["name"] == "Notification Link Host"
assert app_port["name"] == "Notification Link Port"
external_url_description = external_url["description"].lower()
host_description = app_host["description"].lower()
port_description = app_port["description"].lower()
assert "notification links" in external_url_description
assert "set this explicitly" in external_url_description
assert "reverse proxies" in external_url_description
assert "external deployments" in external_url_description
assert "when blank" in external_url_description
assert "fall back to app.host and app.port" in external_url_description
assert (
"auto-detect from request context" not in external_url_description
)
assert "request context" not in external_url_description
assert "notification links" in host_description
assert "app.external_url is unset" in host_description
assert "does not control the web listener" in host_description
assert "set ldr_web_host" in host_description
assert "ldr_web_host" in host_description
assert "web.host is the corresponding internal key" in host_description
assert "not a user-editable listener control" in host_description
assert "notification links" in port_description
assert "app.external_url is unset" in port_description
assert "does not control the web listener" in port_description
assert "set ldr_web_port" in port_description
assert "ldr_web_port" in port_description
assert "web.port is the corresponding internal key" in port_description
assert "not a user-editable listener control" in port_description
# The bind path resolves web.*; app.* is nowhere in its mapping.
host_entry = server_config._LEGACY_KEY_MAP["host"]
port_entry = server_config._LEGACY_KEY_MAP["port"]
assert host_entry[0] == "web.host"
assert host_entry[1] == "LDR_WEB_HOST"
assert port_entry[0] == "web.port"
assert port_entry[1] == "LDR_WEB_PORT"
assert server_config._DEFAULTS["host"] == "127.0.0.1"
assert server_config._DEFAULTS["port"] == 5000
mapped_keys = {v[0] for v in server_config._LEGACY_KEY_MAP.values()}
assert "app.host" not in mapped_keys
assert "app.port" not in mapped_keys
# The app.* fallback pair is consumed by notification URL building.
url_builder = (SRC_DIR / "notifications" / "url_builder.py").read_text(
encoding="utf-8"
)
assert '"app.host"' in url_builder
assert '"app.port"' in url_builder
def test_notification_url_host_and_port_describe_their_actual_role(self):
defaults = raw_defaults()
for key, env_var in (
("app.host", "ldr_web_host"),
("app.port", "ldr_web_port"),
):
description = defaults[key]["description"].lower()
assert "notification" in description, key
assert "does not control the web listener" in description, key
assert env_var in description, key
def test_web_use_https_does_not_claim_to_enable_tls(self):
description = raw_defaults()["web.use_https"]["description"].lower()
assert "does not enable https" in description
assert "terminate tls at a reverse proxy" in description
# Its only observable effect is the startup warning in web/app.py.
assert "startup warning" in description
assert "no generated link depends on it" in description
def test_web_use_https_fallback_url_has_no_consumer(self):
"""Pin the code facts behind the web.use_https description.
The description says that, apart from the startup warning, the
setting only feeds an internal fallback URL that nothing uses.
get_server_url() builds that URL. Its one caller stores the result
on SearchEngineLibrary.server_url, and nothing reads that
attribute. If either fact changes, update the description to say
which links now use the setting, then update this test.
"""
callers = set()
readers = set()
for rel_path, text in _src_texts():
if "server_url" not in text:
continue
rel_path = rel_path.replace(os.sep, "/")
if not rel_path.endswith(".py"):
readers.add(rel_path)
continue
for node in ast.walk(ast.parse(text)):
if isinstance(node, ast.Call):
func = node.func
name = getattr(func, "id", None) or getattr(
func, "attr", None
)
if name == "get_server_url":
callers.add(rel_path)
elif (
isinstance(node, ast.Attribute)
and node.attr == "server_url"
and isinstance(node.ctx, ast.Load)
):
readers.add(rel_path)
assert callers == {
"web_search_engines/engines/search_engine_library.py"
}
assert readers == set()
# ---------------------------------------------------------------------------
# 2. Every default is loadable
# ---------------------------------------------------------------------------
_UNSET = object()
# Optional numerics whose correct shipped value is null. `validate_setting`
# treats None as the unset state (the column is nullable and no "required"
# flag exists on the Setting model), so these are not defects.
NULLABLE_NUMERIC_DEFAULTS = {
"embeddings.openai.dimensions": (
"Optional; its description says 'Leave blank to use the model's "
"native dimensionality'."
),
}
class TestEveryDefaultIsLoadable:
"""A shipped default that fails its own validation is a live bug."""
def test_every_ui_element_is_in_the_canonical_type_map(self):
defaults = raw_defaults()
in_use = {meta["ui_element"] for meta in defaults.values()}
assert len(in_use) >= 8, (
f"only {len(in_use)} distinct ui_elements seen; the corpus did "
f"not load"
)
unknown = sorted(in_use - set(UI_ELEMENT_TO_SETTING_TYPE))
assert not unknown, (
f"shipped defaults use ui_elements absent from "
f"UI_ELEMENT_TO_SETTING_TYPE: {unknown}. "
f"get_typed_setting_value returns the caller's `default` for an "
f"unknown ui_element, so every such key reads as None/default "
f"no matter what is stored."
)
# `slider` is the specific latent gap. Three call sites --
# settings/manager.py's validate_imported_setting_value,
# web/routers/settings.py's validate_setting, and
# web/services/settings_service.py -- branch on
# ("number", "slider", "range") as if all three were numeric, but
# UI_ELEMENT_TO_SETTING_TYPE has entries only for "number" and
# "range". Nothing ships "slider" today, which is the only reason
# the mismatch is harmless; the day one does, it reads as None.
assert "slider" not in UI_ELEMENT_TO_SETTING_TYPE
assert "slider" not in in_use, (
"a shipped default now uses ui_element='slider', which is "
"missing from UI_ELEMENT_TO_SETTING_TYPE -- it will read as "
"the caller's default instead of its stored value. Add "
"'slider': _parse_number to the map."
)
def test_get_typed_setting_value_accepts_every_shipped_default(self):
"""Replay the app's own read-path coercion over every default.
``check_env=False`` so the assertion is about the shipped value,
not about the ambient environment.
"""
defaults = raw_defaults()
assert len(defaults) >= MIN_DEFAULT_KEYS
failures = []
coerced = 0
for key, meta in defaults.items():
value = meta["value"]
if value is None:
# A null shipped value reads back as the caller's default
# by design (`if value is None: return default`), so it
# cannot distinguish "unset" from "rejected". The unset
# optionals are covered by the type/bounds tests instead.
continue
coerced += 1
typed = get_typed_setting_value(
key=key,
value=value,
ui_element=meta["ui_element"],
default=_UNSET,
check_env=False,
)
if typed is _UNSET:
failures.append((key, meta["ui_element"], value))
assert coerced >= 500, (
f"only {coerced} non-null defaults were coerced; the sweep is "
f"not exercising the read path"
)
assert not failures, (
f"{len(failures)} shipped defaults are rejected by the app's "
f"own get_typed_setting_value (they would read back as the "
f"caller's default, not the shipped value): {failures[:10]}"
)
def test_value_type_matches_declared_ui_element(self):
defaults = raw_defaults()
mismatches = []
checked = 0
for key, meta in defaults.items():
element, value = meta["ui_element"], meta["value"]
checked += 1
if element == "checkbox" and not isinstance(value, bool):
mismatches.append((key, element, value))
elif element in ("number", "range"):
if value is None:
if key not in NULLABLE_NUMERIC_DEFAULTS:
mismatches.append((key, element, value))
elif isinstance(value, bool) or not isinstance(
value, (int, float)
):
mismatches.append((key, element, value))
elif element == "multiselect" and not isinstance(value, list):
mismatches.append((key, element, value))
elif element in ("text", "password", "textarea", "select"):
if value is not None and not isinstance(value, str):
mismatches.append((key, element, value))
assert checked >= MIN_DEFAULT_KEYS
assert not mismatches, (
f"shipped values disagree with their declared ui_element: "
f"{mismatches}"
)
def test_numeric_defaults_are_inside_their_own_bounds(self):
defaults = raw_defaults()
violations = []
bounded = 0
for key, meta in defaults.items():
low, high = meta.get("min_value"), meta.get("max_value")
if low is None and high is None:
continue
bounded += 1
if low is not None and high is not None and low > high:
violations.append((key, "min_value > max_value", low, high))
value = meta["value"]
if not isinstance(value, (int, float)) or isinstance(value, bool):
# Bounds on a non-numeric default are inert -- the
# min/max branch of validate_setting only runs for
# number/slider/range -- so this is metadata noise rather
# than a loadability bug, except when the element IS
# numeric, which the type test above already covers.
continue
if low is not None and value < low:
violations.append((key, "below min_value", value, low))
if high is not None and value > high:
violations.append((key, "above max_value", value, high))
assert bounded >= 40, (
f"only {bounded} defaults declare bounds; the sweep is not "
f"reaching the numeric settings"
)
assert not violations, (
f"shipped defaults fall outside their own declared bounds: "
f"{violations}"
)
def test_select_defaults_are_within_their_own_options(self):
"""Run against the *resolved* defaults.
``SettingsManager.default_settings`` replaces the options of a few
selects at load time (``app.theme`` from the theme registry,
``search.search_strategy`` from ``constants``). Validating the raw
JSON would report those as broken and miss real breakage in them.
"""
resolved = resolved_defaults()
offenders = []
checked = 0
for key, meta in resolved.items():
if not isinstance(meta, dict) or meta.get("ui_element") != "select":
continue
options = meta.get("options")
if not options or key in DYNAMIC_SETTINGS:
# DYNAMIC_SETTINGS is imported from the app, not restated:
# those dropdowns accept free text and their static option
# lists are only a sample.
continue
checked += 1
allowed = [
opt.get("value") if isinstance(opt, dict) else opt
for opt in options
]
if meta["value"] not in allowed:
offenders.append((key, meta["value"], allowed))
assert checked >= 30, (
f"only {checked} select settings validated; expected the full "
f"dropdown corpus"
)
assert not offenders, (
f"select defaults name a value that is not among their own "
f"options: {offenders}"
)
@pytest.mark.xfail(
strict=True,
reason=(
"DEFECT: report.export_formats ships "
"['markdown','latex','quarto','ris'] while its own options "
"list offers only markdown/latex/quarto -- 'ris' is a value "
"the UI cannot render or round-trip. It survives because "
"validate_setting (web/routers/settings.py) and "
"validate_imported_setting_value (settings/manager.py) have "
"no 'multiselect' branch at all: multiselect values are never "
"checked against their options on any path. Fix by adding "
"'ris' to the options list (or dropping it from the value) "
"AND giving both validators a multiselect branch."
),
)
def test_multiselect_defaults_are_within_their_own_options(self):
defaults = raw_defaults()
multiselects = {
key: meta
for key, meta in defaults.items()
if meta["ui_element"] == "multiselect"
}
assert multiselects, "no multiselect settings found to validate"
offenders = []
for key, meta in multiselects.items():
options = meta.get("options")
if not options:
continue
allowed = {
opt.get("value") if isinstance(opt, dict) else opt
for opt in options
}
extra = sorted(set(meta["value"]) - allowed)
if extra:
offenders.append((key, extra, sorted(allowed)))
assert not offenders, (
f"multiselect defaults contain values outside their own "
f"options: {offenders}"
)
# ---------------------------------------------------------------------------
# 3. Rate-limit defaults are restrictive enough to matter
# ---------------------------------------------------------------------------
# Ceilings expressed as requests-per-minute, computed from the parsed
# limit rather than string-matched, so "5 per 15 minutes" and
# "1 per 3 minutes" are comparable and a unit change cannot slip past.
# Every window in a multi-limit string must satisfy the ceiling.
RATE_LIMIT_CEILINGS: Dict[str, float] = {
# Brute-force surface. 5 attempts / 15 min = 0.34/min.
"security.rate_limit_login": 0.5,
# Allow form retries and shared-IP signups, with a short burst cap.
# The dedicated contract below also requires a <=300/hour window.
"security.rate_limit_registration": 10.0,
# Settings mutation, per authenticated user.
"security.rate_limit_settings": 30.0,
# Upload endpoints, per user and per IP.
"security.rate_limit_upload_user": 60.0,
"security.rate_limit_upload_ip": 60.0,
# Global fallback for everything else. 5000/hour = 83/min.
"security.rate_limit_default": 100.0,
}
def _per_minute(limit_string: str) -> List[Tuple[int, float]]:
"""Parse with the same library slowapi uses, and normalise to /min.
Using ``limits.parse_many`` rather than a hand-rolled regex means a
malformed shipped string ("5 per 15 minutess") fails here instead of
at server start-up.
"""
out = []
for item in parse_many(limit_string):
window = item.get_expiry()
out.append((item.amount, item.amount / (window / 60.0)))
return out
class TestRateLimitDefaults:
def test_shipped_rate_limits_parse_and_stay_under_their_ceiling(self):
defaults = raw_defaults()
assert len(RATE_LIMIT_CEILINGS) == 6
windows_checked = 0
too_loose = []
for key, ceiling in RATE_LIMIT_CEILINGS.items():
assert key in defaults, f"{key} no longer ships"
raw = defaults[key]["value"]
assert isinstance(raw, str) and raw.strip(), (
f"{key} ships an empty limit, which disables the limiter "
f"for that endpoint group"
)
parsed = _per_minute(raw)
assert parsed, f"{key} parsed to no limits at all: {raw!r}"
for amount, rate in parsed:
windows_checked += 1
assert amount >= 1, (
f"{key} window allows {amount} requests, which blocks "
f"the endpoint entirely"
)
if rate > ceiling:
too_loose.append((key, raw, round(rate, 3), ceiling))
assert windows_checked >= 8, (
f"only {windows_checked} limit windows parsed; expected the "
f"multi-window strings to contribute more than one each"
)
assert not too_loose, (
f"rate-limit defaults are too permissive to matter "
f"(key, string, req/min, ceiling): {too_loose}"
)
def test_registration_keeps_a_sustained_hourly_cap(self):
"""Allow shared-IP signups and form retries within a bounded hour.
300/hour/IP is the deliberate allowance for groups sharing an IP
and retrying registration forms. Successful and failed submissions
both count; the separate 10/minute window also bounds bursts.
"""
raw = raw_defaults()["security.rate_limit_registration"]["value"]
hourly_limits = [
item for item in parse_many(raw) if item.get_expiry() == 3600
]
assert hourly_limits, (
f"registration must retain an explicit hourly window: {raw!r}"
)
for item in hourly_limits:
assert 1 <= item.amount <= 300, (
f"registration must allow at most 300 attempts per hour: {raw!r}"
)
def test_auth_limits_are_stricter_than_the_global_fallback(self):
"""The auth endpoints must not be governed by the generic budget.
A login limit that merely matches the default limit is the same as
having no login limit, which is how a shared bucket erases a
documented number.
"""
defaults = raw_defaults()
login = min(
r
for _, r in _per_minute(
defaults["security.rate_limit_login"]["value"]
)
)
register = min(
r
for _, r in _per_minute(
defaults["security.rate_limit_registration"]["value"]
)
)
fallback = min(
r
for _, r in _per_minute(
defaults["security.rate_limit_default"]["value"]
)
)
assert login < fallback / 10
# Registration permits form retries while login retains the
# stricter budget needed to limit password guessing.
assert register < fallback
def test_json_rate_limits_match_the_defaults_the_server_actually_uses(
self,
):
"""Two sources of truth for the same numbers -- pin them together.
``load_server_config`` resolves each limit as
``env > legacy server_config.json > web/server_config._DEFAULTS``.
It never consults ``defaults/settings_security.json``. So editing
the JSON alone changes what the settings UI *displays* and nothing
about what the limiter *enforces*. They agree today; this fails the
moment they drift.
"""
defaults = raw_defaults()
pairs = {
"rate_limit_default": "security.rate_limit_default",
"rate_limit_login": "security.rate_limit_login",
"rate_limit_registration": "security.rate_limit_registration",
"rate_limit_settings": "security.rate_limit_settings",
"rate_limit_upload_user": "security.rate_limit_upload_user",
"rate_limit_upload_ip": "security.rate_limit_upload_ip",
}
assert len(pairs) == 6
drift = {
config_key: (
server_config._DEFAULTS[config_key],
defaults[setting_key]["value"],
)
for config_key, setting_key in pairs.items()
if server_config._DEFAULTS[config_key]
!= defaults[setting_key]["value"]
}
assert not drift, (
"web/server_config._DEFAULTS (what the limiter enforces) has "
"drifted from defaults/settings_security.json (what the UI "
"shows) -- (server_config, json): " + repr(drift)
)
def test_session_and_lockout_defaults_resolve_through_the_real_loader(
self,
):
"""``get_security_default`` is what auth and SessionMiddleware call.
It reads ``defaults/settings_security.json`` directly (not through
SettingsManager), so this exercises the actual path rather than the
JSON in isolation.
"""
threshold = get_security_default(
"security.account_lockout_threshold", 0
)
duration = get_security_default(
"security.account_lockout_duration_minutes", 0
)
remember = get_security_default("security.session_remember_me_days", 0)
timeout = get_security_default("security.session_timeout_hours", 0)
# The `0` sentinels above would come back unchanged if the loader
# silently failed to find the file.
assert threshold and duration and remember and timeout
assert 3 <= threshold <= 10, (
f"account lockout after {threshold} failures is too loose to "
f"blunt online password guessing"
)
assert duration >= 15, (
f"a {duration}-minute lockout barely slows an attacker down"
)
assert 1 <= timeout <= 24, f"session timeout {timeout}h out of range"
assert 1 <= remember <= 30, (
f"remember-me window of {remember} days exceeds the 30-day "
f"ceiling its own schema declares; it is also the session "
f"cookie max_age handed to SessionMiddleware"
)
def test_notification_rate_limits_are_bounded(self):
defaults = raw_defaults()
per_hour = defaults["notifications.rate_limit_per_hour"]["value"]
per_day = defaults["notifications.rate_limit_per_day"]["value"]
assert 1 <= per_hour <= 60
assert per_hour <= per_day <= 24 * per_hour, (
f"per-day notification cap ({per_day}) is inconsistent with "
f"the per-hour cap ({per_hour})"
)
# ---------------------------------------------------------------------------
# 4. No credential or personal value ships
# ---------------------------------------------------------------------------
# Assembled from character classes; none of these patterns matches its own
# text, so nothing here is a credential-shaped literal.
CREDENTIAL_SHAPES = [
("openai-style key", re.compile(r"sk-[A-Za-z0-9]{16,}")),
("github token", re.compile(r"gh[pousr]_[A-Za-z0-9]{20,}")),
("aws access key id", re.compile(r"AKIA[0-9A-Z]{16}")),
("google api key", re.compile(r"AIza[0-9A-Za-z_\-]{30,}")),
("slack token", re.compile(r"xox[baprs]-[0-9A-Za-z\-]{10,}")),
("jwt", re.compile(r"eyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\.")),
("bearer header", re.compile(r"[Bb]earer\s+[A-Za-z0-9._\-]{20,}")),
("pem private key", re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY")),
("basic-auth url", re.compile(r"://[^/\s:\"]+:[^/\s@\"]+@")),
]
PERSONAL_SHAPES = [
("home directory", re.compile(r"/home/[a-z][a-z0-9_\-]*")),
("macos home", re.compile(r"/Users/[A-Za-z]")),
("windows profile", re.compile(r"[Cc]:\\+Users")),
(
"e-mail address",
re.compile(r"[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}"),
),
]
# Last dot-segment names that carry secret material.
SECRET_SEGMENT = re.compile(
r"(?:^|\.)(?:api_key|secret|secret_key|token|access_token|password|"
r"passwd|private_key|client_secret)$"
)
class TestNoShippedCredentials:
def test_no_password_element_ships_a_value(self):
defaults = raw_defaults()
password_keys = [
key
for key, meta in defaults.items()
if meta["ui_element"] == "password"
]
assert len(password_keys) >= 25, (
f"only {len(password_keys)} password-typed settings found; the "
f"provider/engine credential fields are missing"
)
populated = {
key: defaults[key]["value"]
for key in password_keys
if defaults[key]["value"] not in ("", None)
}
assert not populated, (
f"password-typed settings ship a non-empty default: "
f"{sorted(populated)}"
)
def test_secret_named_keys_are_password_typed_and_empty(self):
"""A secret stored behind ``ui_element='text'`` is rendered in the
clear in the settings UI and is not redacted by the export path.
"""
defaults = raw_defaults()
secret_keys = [key for key in defaults if SECRET_SEGMENT.search(key)]
assert len(secret_keys) >= 25, (
f"only {len(secret_keys)} secret-named keys matched; the "
f"classifier is not seeing the corpus"
)
wrong_element = [
key
for key in secret_keys
if defaults[key]["ui_element"] != "password"
]
assert not wrong_element, (
f"secret-named settings not typed as password: {wrong_element}"
)
populated = [
key
for key in secret_keys
if defaults[key]["value"] not in ("", None)
]
assert not populated, f"secret-named settings ship a value: {populated}"
def test_no_credential_shaped_string_in_any_default_value(self):
defaults = raw_defaults()
strings_scanned = 0
hits = []
for key, meta in defaults.items():
for path, text in _walk_strings(meta.get("value")):
strings_scanned += 1
for label, pattern in CREDENTIAL_SHAPES:
if pattern.search(text):
hits.append((key, path, label))
assert strings_scanned >= 150, (
f"only {strings_scanned} default value strings scanned; the "
f"credential sweep is examining almost nothing"
)
assert not hits, f"credential-shaped default values: {hits}"
def test_no_personal_path_or_address_in_any_default(self):
"""Values *and* option labels -- a developer's home directory has
landed in an options list before.
"""
defaults = raw_defaults()
strings_scanned = 0
hits = []
for key, meta in defaults.items():
payload = (meta.get("value"), meta.get("options"))
for path, text in _walk_strings(payload):
strings_scanned += 1
for label, pattern in PERSONAL_SHAPES:
if pattern.search(text):
hits.append((key, path, label, text[:60]))
assert strings_scanned >= 200
assert not hits, (
f"personal paths or addresses in shipped defaults: {hits}"
)
def _walk_strings(node: Any, path: str = "") -> List[Tuple[str, str]]:
"""Every string reachable from a default's value/options."""
if isinstance(node, str):
return [(path or ".", node)]
if isinstance(node, dict):
out = []
for key, value in node.items():
out.extend(_walk_strings(value, f"{path}/{key}"))
return out
if isinstance(node, (list, tuple)):
out = []
for index, value in enumerate(node):
out.extend(_walk_strings(value, f"{path}[{index}]"))
return out
return []
# ---------------------------------------------------------------------------
# 5. Registry consistency
# ---------------------------------------------------------------------------
# Files that only *define* environment settings, or dispatch generically
# over their categories. A mention there is not a use. ``env_registry.py``
# deliberately stays in scope: its convenience helpers (``is_test_mode``)
# are genuine reads.
ENV_DEFINITION_PATHS = (
"settings/env_definitions/",
"settings/env_settings.py",
)
def _is_env_key_read(setting: Any) -> bool:
"""Whether anything outside the definitions themselves reads this key.
Exact match only -- no ancestor-prefix fallback. Environment settings
are read by exact key through ``registry.get`` or by their ``LDR_``
variable, never by prefix. The prefix fallback used for shipped
defaults made the category list ``["bootstrap", "db_config"]`` in
``env_settings.py``'s own dispatch loop look like a read of every
``bootstrap.*`` key, which masked six dead ones.
"""
needles = [
f'"{setting.key}"',
f"'{setting.key}'",
env_var_for(setting.key),
]
deprecated = getattr(setting, "deprecated_env_var", None)
if deprecated:
needles.append(deprecated)
for rel, text in _src_texts():
normalised = rel.replace("\\", "/")
if normalised.startswith(ENV_DEFINITION_PATHS):
continue
if any(needle in text for needle in needles):
return True
return False
def _is_referenced(key: str) -> bool:
"""Whether anything under ``src/`` reads this *shipped default* key.
Accepts the literal key, its derived ``LDR_`` variable, or a read of
any ancestor prefix -- ``get_setting("search.engine.web")`` really does
return every child, and the engine configs are batch-loaded that way,
so a prefix hit is a genuine reference here. (Environment-registry keys
are never read by prefix; they use ``_is_env_key_read`` instead.)
"""
blob = _src_blob()
if key in blob or env_var_for(key) in blob:
return True
parts = key.split(".")
for depth in range(len(parts) - 1, 0, -1):
prefix = ".".join(parts[:depth])
if f'"{prefix}"' in blob or f"'{prefix}'" in blob:
return True
return False
class TestRegistryConsistency:
def test_every_env_registry_key_is_read_somewhere(self):
registered = [
setting for group in ALL_SETTINGS.values() for setting in group
]
assert len(registered) >= MIN_ENV_SETTINGS, (
f"only {len(registered)} env settings registered; the registry "
f"did not load"
)
# Sanity-check the predicate before trusting its negative
# answers: a key we know is read must come back True.
by_key = {setting.key: setting for setting in registered}
assert _is_env_key_read(by_key["bootstrap.allow_unencrypted"])
assert _is_env_key_read(by_key["security.allow_nat64"])
assert _is_env_key_read(by_key["db_config.kdf_iterations"])
unread = sorted(
setting.key
for setting in registered
if not _is_env_key_read(setting)
)
assert not unread, (
f"environment settings that nothing in src/ reads (the "
f"documented LDR_ variable does nothing): {unread}"
)
def test_no_shipped_default_key_is_unreferenced(self):
"""The reverse direction: dead weight in the shipped defaults.
Deliberately permissive about *how* a key is referenced (literal,
env var, or ancestor prefix) so batch-loaded engine configs and
frontend-only toggles count. A key that matches none of those is
genuinely unreachable.
"""
defaults = raw_defaults()
assert len(defaults) >= MIN_DEFAULT_KEYS
assert len(_src_texts()) >= MIN_SRC_FILES_SWEPT
orphans = sorted(key for key in defaults if not _is_referenced(key))
assert not orphans, (
f"{len(orphans)} shipped default keys are referenced nowhere "
f"in src/: {orphans[:20]}"
)
# test_repair_path_defaults_match_the_shipped_defaults used to live
# here: it AST-walked the /settings "fix corrupted settings" repair
# block (via the private helper _extract_repair_defaults) to catch
# drift between that block's hardcoded defaults and the shipped
# JSON. Both the repair path and the endpoint were removed in favour
# of migration 0031 (src/local_deep_research/database/migrations/
# versions/0031_reconcile_corrupted_settings.py), and the same
# drift-against-shipped-defaults contract is now pinned by
# tests/database/test_migration_0031_reconcile_corrupted_settings.py
# ::test_upgrade_repairs_every_target_with_frozen_current_default.