Add AI search query expansion
test / pytest (push) Successful in 1m20s
docker-image / build-and-push (push) Successful in 5m6s

This commit is contained in:
2026-06-01 21:28:29 +02:00
parent d36b940981
commit 70b0cf08ee
10 changed files with 1064 additions and 123 deletions
+32 -10
View File
@@ -9,6 +9,7 @@ import pytest
import app.llm as llm_module
from app.llm import LLMResult, expand_query, is_configured
from app.llm import ExpansionResult
from app.models import AppSetting
from app.settings_store import LLMConfig, get_app_settings, save_app_settings
@@ -29,6 +30,7 @@ class TestLLMConfigDefaults:
assert cfg.model == ""
assert cfg.api_key == ""
assert cfg.ai_search_enabled is False
assert cfg.ai_search_extra_hints == ""
# ---------------------------------------------------------------------------
@@ -208,37 +210,40 @@ class TestTestConnection:
class TestExpandQuery:
def test_returns_original_when_not_configured(self):
def test_returns_empty_when_not_configured(self):
cfg = LLMConfig(enabled=False)
result = expand_query(cfg, "")
assert result == [""]
assert result.terms == []
assert result.error is None
@patch("app.llm._call_chat_completion")
def test_expands_query_successfully(self, mock_call):
mock_call.return_value = {
"choices": [
{"message": {"content": "平底锅\n炒锅\n锅具\n厨房锅"}}
{"message": {"content": '["平底锅","炒锅","锅具","厨房锅"]'}}
]
}
cfg = LLMConfig(enabled=True, model="gpt-4o", api_key="sk-key")
result = expand_query(cfg, "")
assert "" in result
assert "平底" in result
assert len(result) >= 4
assert "平底" in result.terms
assert "" in result.terms
assert result.error is None
@patch("app.llm._call_chat_completion")
def test_fallback_on_api_failure(self, mock_call):
mock_call.side_effect = Exception("network down")
cfg = LLMConfig(enabled=True, model="gpt-4o", api_key="sk-key")
result = expand_query(cfg, "")
assert result == [""]
assert result.terms == []
assert result.error is not None
@patch("app.llm._call_chat_completion")
def test_fallback_on_empty_response(self, mock_call):
mock_call.return_value = {"choices": [{"message": {"content": ""}}]}
cfg = LLMConfig(enabled=True, model="gpt-4o", api_key="sk-key")
result = expand_query(cfg, "")
assert result == [""]
assert result.terms == []
assert result.error is None
# ---------------------------------------------------------------------------
@@ -441,16 +446,33 @@ class TestSaveSettingsRoute:
assert cfg.model == "gpt-4o-mini"
assert cfg.api_key == "sk-original"
def test_save_does_not_touch_ai_search_enabled(self, client, db_session):
"""P2 fix: saving LLM settings must not reset ai_search_enabled."""
def test_save_includes_ai_search_enabled_checkbox(self, client, db_session):
"""Saving settings now also persists the ai_search_enabled checkbox."""
# Set ai_search_enabled to true first
db_session.add(AppSetting(key="ai_search_enabled", value="true"))
db_session.commit()
# Save without the checkbox → ai_search_enabled is set to False
client.post(
"/settings",
data={"enabled": "on", "model": "gpt-4o", "api_key": "sk-key"},
follow_redirects=False,
)
cfg = get_app_settings(db_session)
assert cfg.ai_search_enabled is False
def test_save_preserves_ai_search_enabled_when_checked(self, client, db_session):
"""Saving settings with ai_search_enabled checked persists it."""
client.post(
"/settings",
data={
"enabled": "on",
"model": "gpt-4o",
"api_key": "sk-key",
"ai_search_enabled": "on",
},
follow_redirects=False,
)
cfg = get_app_settings(db_session)
assert cfg.ai_search_enabled is True