~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 migration 0034: per-engine time_period defaults → 'inherit'.
Pins the upgrade/downgrade semantics for the three per-engine keys
(``search.engine.web.{brave,serpapi,tavily}.default_params.time_period``):
- Rows holding the old shipped default (``"y"`` for brave/serpapi,
``"all"`` for tavily) get flipped to ``"inherit"``.
- Rows users explicitly chose are left untouched — including ``"all"`` for
brave/serpapi and ``"y"`` for tavily (each engine only matches its OWN
old default).
- Case/whitespace variants of a choice become the canonical choice.
- Any other legacy value (brave/serpapi were free text) becomes what it
effectively meant on main: ``"all"`` for Brave, which ignored it, and
``"inherit"`` for SerpAPI/Tavily, whose value the global setting shadowed.
No row is left holding a value the strict resolver rejects.
- The global ``search.time_period`` key is not affected.
- Idempotency: a second upgrade is a no-op once values are migrated.
The on-disk encoding is JSON-text (quoted literals), matching production
storage through SQLAlchemy's JSON column type.
"""
import json
import shutil
import pytest
from alembic import command
from sqlalchemy import create_engine, text
from local_deep_research.database.alembic_runner import (
get_alembic_config,
)
BRAVE_KEY = "search.engine.web.brave.default_params.time_period"
SERPAPI_KEY = "search.engine.web.serpapi.default_params.time_period"
TAVILY_KEY = "search.engine.web.tavily.default_params.time_period"
# key -> old shipped default that the migration flips
OLD_DEFAULTS = {BRAVE_KEY: "y", SERPAPI_KEY: "y", TAVILY_KEY: "all"}
ENGINE_KEYS = {
"brave": BRAVE_KEY,
"serpapi": SERPAPI_KEY,
"tavily": TAVILY_KEY,
}
# stored value -> (brave, serpapi, tavily) value after the upgrade. The old
# shipped default flips to inherit, other choices are kept, variants of a
# choice are canonicalized, and anything else becomes what it effectively
# meant on main: "all" for Brave (it ignored the value and the global
# setting), "inherit" for SerpAPI and Tavily (the global setting shadowed it).
LEGACY_VALUES = [
pytest.param("y", "inherit", "inherit", "y", id="y"),
pytest.param("all", "all", "all", "inherit", id="all"),
pytest.param("d", "d", "d", "d", id="d"),
pytest.param("w", "w", "w", "w", id="w"),
pytest.param("m", "m", "m", "m", id="m"),
# A typed "inherit": SerpAPI and Tavily would have followed the global
# setting anyway, but Brave's old text field never knew the sentinel and
# ignored it (unfiltered), so only Brave maps it to "all".
pytest.param("inherit", "all", "inherit", "inherit", id="text-inherit"),
pytest.param("Y", "y", "y", "y", id="upper-y"),
pytest.param(" y", "y", "y", "y", id="leading-space-y"),
pytest.param("y ", "y", "y", "y", id="trailing-space-y"),
pytest.param("\u00a0y\u00a0", "y", "y", "y", id="nbsp-y"),
pytest.param("\tW\n", "w", "w", "w", id="tab-upper-w-newline"),
pytest.param("D", "d", "d", "d", id="upper-d"),
pytest.param(" m ", "m", "m", "m", id="padded-m"),
pytest.param("ALL", "all", "all", "all", id="upper-all"),
pytest.param(" All ", "all", "all", "all", id="padded-mixed-all"),
pytest.param(
"Inherit", "all", "inherit", "inherit", id="text-mixed-inherit"
),
pytest.param(
" INHERIT ", "all", "inherit", "inherit", id="text-padded-inherit"
),
pytest.param("", "all", "inherit", "inherit", id="blank"),
pytest.param(" ", "all", "inherit", "inherit", id="whitespace-only"),
pytest.param("year", "all", "inherit", "inherit", id="word-year"),
pytest.param("py", "all", "inherit", "inherit", id="brave-native-py"),
pytest.param("none", "all", "inherit", "inherit", id="word-none"),
pytest.param("None", "all", "inherit", "inherit", id="python-none"),
pytest.param("undefined", "all", "inherit", "inherit", id="undefined"),
pytest.param("past year", "all", "inherit", "inherit", id="two-words"),
pytest.param("7", "all", "inherit", "inherit", id="numeric-string"),
pytest.param("yy", "all", "inherit", "inherit", id="doubled-code"),
pytest.param("\uff39", "all", "inherit", "inherit", id="fullwidth-y"),
pytest.param(None, "all", "inherit", "inherit", id="json-null"),
pytest.param(7, "all", "inherit", "inherit", id="json-int"),
pytest.param(1.5, "all", "inherit", "inherit", id="json-float"),
pytest.param(True, "all", "inherit", "inherit", id="json-true"),
pytest.param(False, "all", "inherit", "inherit", id="json-false"),
pytest.param(["y"], "all", "inherit", "inherit", id="json-list"),
pytest.param({"a": 1}, "all", "inherit", "inherit", id="json-object"),
]
# The rows that are not a recognizable choice at all.
UNRECOGNIZED_VALUES = [
pytest.param(p.values[0], id=p.id)
for p in LEGACY_VALUES
if p.values[1:] == ("all", "inherit", "inherit")
]
def _run_upgrade_to(engine, revision):
config = get_alembic_config(engine)
with engine.begin() as conn:
config.attributes["connection"] = conn
command.upgrade(config, revision)
def _run_downgrade_to(engine, revision):
config = get_alembic_config(engine)
with engine.begin() as conn:
config.attributes["connection"] = conn
command.downgrade(config, revision)
def _seed_setting(engine, key, value, ui_element="select", options=None):
"""Insert a setting matching production's JSON-text storage."""
with engine.begin() as conn:
conn.execute(
text(
"INSERT INTO settings "
"(key, value, type, name, ui_element, options, "
"visible, editable) "
"VALUES (:key, :value, 'search', :name, :ui_element, "
":options, 1, 1)"
),
{
"key": key,
"value": json.dumps(value),
"name": key,
"ui_element": ui_element,
"options": json.dumps(options) if options is not None else None,
},
)
def _seed_raw(engine, key, raw, ui_element="text"):
"""Insert a settings row whose value cell is exactly ``raw`` (SQL NULL
for None), bypassing JSON encoding."""
with engine.begin() as conn:
conn.execute(
text(
"INSERT INTO settings "
"(key, value, type, name, ui_element, visible, editable) "
"VALUES (:key, :value, 'search', :name, :ui_element, 1, 1)"
),
{"key": key, "value": raw, "name": key, "ui_element": ui_element},
)
def _read_raw(engine, key):
with engine.begin() as conn:
row = conn.execute(
text("SELECT value FROM settings WHERE key = :key"),
{"key": key},
).fetchone()
return row[0]
def _read_metadata(engine, key):
"""Return (ui_element, parsed options) for a settings row."""
with engine.begin() as conn:
row = conn.execute(
text("SELECT ui_element, options FROM settings WHERE key = :key"),
{"key": key},
).fetchone()
if row is None:
return None
ui_element, raw_options = row
options = (
json.loads(raw_options) if isinstance(raw_options, str) else raw_options
)
return ui_element, options
def _read_setting(engine, key):
with engine.begin() as conn:
row = conn.execute(
text("SELECT value FROM settings WHERE key = :key"),
{"key": key},
).fetchone()
if row is None:
return None
raw = row[0]
return json.loads(raw) if isinstance(raw, str) else raw
@pytest.fixture(scope="module")
def _migrated_to_0033_template(tmp_path_factory):
"""Template DB fully migrated through 0033, built once per module.
Same cost-amortization pattern as test_migration_0030's template: the
full 0001->0033 chain is built once; each test copies the file.
"""
template_dir = tmp_path_factory.mktemp("migration_0034_template")
db_path = template_dir / "template_0033.db"
engine = create_engine(f"sqlite:///{db_path}")
_run_upgrade_to(engine, "0033")
engine.dispose()
return db_path
@pytest.fixture
def migrated_to_0033_engine(tmp_path, _migrated_to_0033_template):
"""Per-test isolated database at revision 0033 (the one before 0034)."""
db_path = tmp_path / "test_0034.db"
shutil.copy2(_migrated_to_0033_template, db_path)
engine = create_engine(f"sqlite:///{db_path}")
yield engine
engine.dispose()
class TestMigration0034Upgrade:
@pytest.mark.parametrize("key", [BRAVE_KEY, SERPAPI_KEY, TAVILY_KEY])
def test_old_default_is_migrated_to_inherit(
self, migrated_to_0033_engine, key
):
engine = migrated_to_0033_engine
_seed_setting(engine, key, OLD_DEFAULTS[key])
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, key) == "inherit"
@pytest.mark.parametrize(
("key", "explicit_value"),
[
# Values that differ from each engine's own old default must
# survive — including the OTHER engines' defaults.
(BRAVE_KEY, "d"),
(BRAVE_KEY, "w"),
(BRAVE_KEY, "all"),
(SERPAPI_KEY, "m"),
(SERPAPI_KEY, "all"),
(TAVILY_KEY, "y"),
(TAVILY_KEY, "d"),
],
)
def test_explicit_choices_are_preserved(
self, migrated_to_0033_engine, key, explicit_value
):
engine = migrated_to_0033_engine
_seed_setting(engine, key, explicit_value)
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, key) == explicit_value
def test_global_time_period_key_is_not_touched(
self, migrated_to_0033_engine
):
"""The global search.time_period holds 'y'-adjacent values too; the
per-engine migration must never rewrite it."""
engine = migrated_to_0033_engine
_seed_setting(engine, "search.time_period", "y")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, "search.time_period") == "y"
def test_upgrade_is_idempotent(self, migrated_to_0033_engine):
engine = migrated_to_0033_engine
_seed_setting(engine, SERPAPI_KEY, "y")
_run_upgrade_to(engine, "0034")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, SERPAPI_KEY) == "inherit"
def test_no_rows_does_not_error(self, migrated_to_0033_engine):
"""A DB without any per-engine rows migrates cleanly (no-op)."""
engine = migrated_to_0033_engine
_run_upgrade_to(engine, "0034")
for key in OLD_DEFAULTS:
assert _read_setting(engine, key) is None
def test_upgrade_refreshes_select_metadata(self, migrated_to_0033_engine):
"""The migration itself sets ui_element/options for the three keys:
the defaults-JSON reconciliation only runs on a version bump, so
without this a user upgrading between bumps would see a text box
containing the literal string 'inherit' (#5588 review)."""
engine = migrated_to_0033_engine
_seed_setting(engine, BRAVE_KEY, "y", ui_element="text", options=None)
_run_upgrade_to(engine, "0034")
ui_element, options = _read_metadata(engine, BRAVE_KEY)
assert ui_element == "select"
assert [o["value"] for o in options] == [
"inherit",
"d",
"w",
"m",
"y",
"all",
]
assert options[0]["label"] == "Use global default"
def test_metadata_refresh_applies_even_to_explicit_values(
self, migrated_to_0033_engine
):
"""Metadata is refreshed unconditionally — a row with an explicit
(preserved) value still becomes a proper select."""
engine = migrated_to_0033_engine
_seed_setting(engine, SERPAPI_KEY, "d", ui_element="text", options=None)
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, SERPAPI_KEY) == "d"
ui_element, options = _read_metadata(engine, SERPAPI_KEY)
assert ui_element == "select"
assert len(options) == 6
@pytest.mark.parametrize(
("stored", "brave", "serpapi", "tavily"), LEGACY_VALUES
)
def test_legacy_value_becomes_a_valid_choice(
self, migrated_to_0033_engine, stored, brave, serpapi, tavily
):
"""Brave and SerpAPI were free-text fields, so a row can hold any
JSON value. Each ends up as a choice the strict resolver accepts:
variants are canonicalized and anything unrecognised becomes what it
effectively meant on main (Brave: unfiltered; SerpAPI and Tavily:
shadowed by the global setting)."""
engine = migrated_to_0033_engine
for name, key in ENGINE_KEYS.items():
_seed_setting(
engine,
key,
stored,
ui_element="select" if name == "tavily" else "text",
)
_run_upgrade_to(engine, "0034")
assert [_read_setting(engine, key) for key in ENGINE_KEYS.values()] == [
brave,
serpapi,
tavily,
]
@pytest.mark.parametrize(
("key", "expected"),
[
(BRAVE_KEY, "all"),
(SERPAPI_KEY, "inherit"),
(TAVILY_KEY, "inherit"),
],
)
def test_sql_null_cell_is_treated_like_json_null(
self, migrated_to_0033_engine, key, expected
):
engine = migrated_to_0033_engine
_seed_raw(engine, key, None)
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, key) == expected
def test_blob_cell_holding_json_is_migrated(self, migrated_to_0033_engine):
"""Some drivers hand TEXT back as bytes; the cell is still JSON."""
engine = migrated_to_0033_engine
_seed_raw(engine, BRAVE_KEY, b'"year"')
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, BRAVE_KEY) == "all"
def test_undecodable_blob_cell_is_left_alone(self, migrated_to_0033_engine):
engine = migrated_to_0033_engine
_seed_raw(engine, BRAVE_KEY, b"\xff\xfe")
_run_upgrade_to(engine, "0034")
assert _read_raw(engine, BRAVE_KEY) == b"\xff\xfe"
def test_other_engines_time_period_vocabulary_is_not_touched(
self, migrated_to_0033_engine
):
"""Serper's per-engine time_period (day/week/month/year or null) is a
different vocabulary outside this scheme and must pass through."""
engine = migrated_to_0033_engine
serper_key = "search.engine.web.serper.default_params.time_period"
_seed_setting(engine, serper_key, "day", ui_element="select")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, serper_key) == "day"
@pytest.mark.parametrize("stored", ["inherit", "Inherit", " INHERIT "])
def test_brave_text_inherit_is_unrecognized_text_not_the_sentinel(
self, migrated_to_0033_engine, stored
):
"""Brave's old free-text field never knew the sentinel and ignored a
typed "inherit" (unfiltered, the global setting never applied), so the
migration writes "all" for it. SerpAPI and Tavily, whose value the
global setting shadowed, keep "inherit"."""
engine = migrated_to_0033_engine
for name, key in ENGINE_KEYS.items():
_seed_setting(
engine,
key,
stored,
ui_element="select" if name == "tavily" else "text",
)
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, BRAVE_KEY) == "all"
assert _read_setting(engine, SERPAPI_KEY) == "inherit"
assert _read_setting(engine, TAVILY_KEY) == "inherit"
@pytest.mark.parametrize("stored", UNRECOGNIZED_VALUES)
def test_unrecognized_values_keep_their_effective_period(
self, migrated_to_0033_engine, monkeypatch, stored
):
"""With a global of "d", Brave searched unfiltered (it ignored both
the stored value and the global setting) while SerpAPI and Tavily
followed the global one (it shadowed their stored value). The
migrated rows resolve to the same periods instead of raising."""
from local_deep_research.web_search_engines.search_engine_factory import (
_resolve_time_period,
)
monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
engine = migrated_to_0033_engine
for name, key in ENGINE_KEYS.items():
monkeypatch.delenv(
f"LDR_SEARCH_ENGINE_WEB_{name.upper()}_DEFAULT_PARAMS_TIME_PERIOD",
raising=False,
)
_seed_setting(engine, key, stored, ui_element="text")
_run_upgrade_to(engine, "0034")
periods = {
name: _resolve_time_period(
name,
{"default_params": {"time_period": _read_setting(engine, key)}},
{"search.time_period": "d"},
{},
)
for name, key in ENGINE_KEYS.items()
}
assert periods == {"brave": "all", "serpapi": "d", "tavily": "d"}
def test_legacy_blank_migration_is_idempotent(
self, migrated_to_0033_engine
):
engine = migrated_to_0033_engine
_seed_setting(engine, BRAVE_KEY, "")
_run_upgrade_to(engine, "0034")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, BRAVE_KEY) == "all"
def test_malformed_value_is_left_alone(self, migrated_to_0033_engine):
"""A cell that is not valid JSON (here the bare text ``y``) must not
be rewritten (same safe-no-op posture as migration 0030)."""
engine = migrated_to_0033_engine
with engine.begin() as conn:
conn.execute(
text(
"INSERT INTO settings "
"(key, value, type, name, ui_element, visible, editable) "
"VALUES (:key, 'y', 'search', 'period', 'select', 1, 1)"
),
{"key": BRAVE_KEY},
)
_run_upgrade_to(engine, "0034")
with engine.begin() as conn:
row = conn.execute(
text("SELECT value FROM settings WHERE key = :key"),
{"key": BRAVE_KEY},
).fetchone()
assert row[0] == "y" # bare bytes untouched
class TestMigration0034Downgrade:
@pytest.mark.parametrize("key", [BRAVE_KEY, SERPAPI_KEY, TAVILY_KEY])
def test_downgrade_reverts_inherit_to_old_default(
self, migrated_to_0033_engine, key
):
engine = migrated_to_0033_engine
_seed_setting(engine, key, OLD_DEFAULTS[key])
_run_upgrade_to(engine, "0034")
_run_downgrade_to(engine, "0033")
assert _read_setting(engine, key) == OLD_DEFAULTS[key]
def test_downgrade_preserves_explicit_choices(
self, migrated_to_0033_engine
):
engine = migrated_to_0033_engine
_seed_setting(engine, BRAVE_KEY, "d")
_run_upgrade_to(engine, "0034")
_run_downgrade_to(engine, "0033")
assert _read_setting(engine, BRAVE_KEY) == "d"
def test_downgrade_reverts_brave_all_to_blank(
self, migrated_to_0033_engine
):
"""The upgrade writes "all" for the values Brave ignored; the old
free-text field had no distinct "all", so downgrade restores the
blank text that meant no filter."""
engine = migrated_to_0033_engine
_seed_setting(engine, BRAVE_KEY, "year", ui_element="text")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, BRAVE_KEY) == "all"
_run_downgrade_to(engine, "0033")
assert _read_setting(engine, BRAVE_KEY) == ""
@pytest.mark.parametrize("key", [SERPAPI_KEY, TAVILY_KEY])
def test_downgrade_keeps_all_for_serpapi_and_tavily(
self, migrated_to_0033_engine, key
):
""" "all" was a documented choice for these two before, and the
upgrade never invents it for them, so downgrade leaves it alone."""
engine = migrated_to_0033_engine
_seed_setting(engine, key, " All ")
_run_upgrade_to(engine, "0034")
_run_downgrade_to(engine, "0033")
assert _read_setting(engine, key) == "all"
def test_downgrade_sends_unrecognized_serpapi_value_to_old_default(
self, migrated_to_0033_engine
):
engine = migrated_to_0033_engine
_seed_setting(engine, SERPAPI_KEY, "year", ui_element="text")
_run_upgrade_to(engine, "0034")
assert _read_setting(engine, SERPAPI_KEY) == "inherit"
_run_downgrade_to(engine, "0033")
assert _read_setting(engine, SERPAPI_KEY) == "y"
def test_downgrade_restores_old_metadata(self, migrated_to_0033_engine):
"""Downgrade reverts metadata to the pre-upgrade shapes: text/no
options for brave/serpapi, the old five-option select for tavily."""
engine = migrated_to_0033_engine
_seed_setting(engine, BRAVE_KEY, "y", ui_element="text", options=None)
_seed_setting(
engine, TAVILY_KEY, "all", ui_element="select", options=None
)
_run_upgrade_to(engine, "0034")
_run_downgrade_to(engine, "0033")
brave_ui, brave_options = _read_metadata(engine, BRAVE_KEY)
assert brave_ui == "text"
assert brave_options is None
tavily_ui, tavily_options = _read_metadata(engine, TAVILY_KEY)
assert tavily_ui == "select"
assert [o["value"] for o in tavily_options] == [
"d",
"w",
"m",
"y",
"all",
]
class TestMigration0034HeadAlignment:
"""Chain-anchoring guard; deliberately no literal head pin (see the
rationale in test_migration_0030_default_time_period_all.py)."""
def test_0034_chains_correctly_to_0033(self):
from alembic.config import Config
from alembic.script import ScriptDirectory
from local_deep_research.database.alembic_runner import (
get_migrations_dir,
)
config = Config()
config.set_main_option("script_location", str(get_migrations_dir()))
script = ScriptDirectory.from_config(config)
rev_0034 = script.get_revision("0034")
assert rev_0034.down_revision == "0033"
def _load_migration_0034_module():
"""Import the 0034 migration module by path (its name starts with a digit,
so it can't be imported normally) to read its module-level constants."""
import importlib.util
from pathlib import Path
path = (
Path(__file__).resolve().parents[2]
/ "src/local_deep_research/database/migrations/versions"
/ "0034_per_engine_time_period_inherit.py"
)
spec = importlib.util.spec_from_file_location("_migration_0034", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class TestMigrationConstantsMatchDefaults:
"""Guard against silent drift between the migration's hardcoded select
options and defaults/default_settings.json — the two are hand-mirrored, so
a change to one that misses the other would ship a broken dropdown."""
def test_new_options_json_matches_default_settings(self):
from pathlib import Path
mig = _load_migration_0034_module()
new_options = json.loads(mig.NEW_OPTIONS_JSON)
defaults = json.loads(
(
Path(__file__).resolve().parents[2]
/ "src/local_deep_research/defaults/default_settings.json"
).read_text()
)
for engine in ("brave", "serpapi", "tavily"):
key = f"search.engine.web.{engine}.default_params.time_period"
assert new_options == defaults[key]["options"], (
f"migration 0034 NEW_OPTIONS_JSON drifted from "
f"default_settings.json for {key}"
)