feat(notion-search): add query expansion via Claude Haiku
Generates up to 5 query variants (synonyms + cross-language KR↔EN) so later Notion API search can union over them. Permissive failure modes: LLM error or non-JSON response falls back to [original] with stderr warning. Dedupes and caps variants. 4 tests: variants list, dedup+cap, JSON-parse fallback, exception fallback. 12 passing total. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -122,6 +122,47 @@ def test_cache_handles_corrupted_file():
|
||||
_assert(result is None, "corrupted cache file returns None")
|
||||
|
||||
|
||||
def test_expand_query_returns_variants_with_original_first():
|
||||
"""LLM mock returns variants; expansion includes original verbatim as first item."""
|
||||
import notion_search
|
||||
|
||||
fake_llm = lambda prompt, **kw: '["AI agents", "multi-agent systems", "autonomous LLMs"]'
|
||||
variants = notion_search.expand_query("AI agents", llm_caller=fake_llm)
|
||||
_assert(variants[0] == "AI agents", "original query is first variant")
|
||||
_assert(len(variants) >= 2, "at least 2 variants returned")
|
||||
_assert("multi-agent systems" in variants, "LLM-suggested variant included")
|
||||
|
||||
|
||||
def test_expand_query_dedupes_and_caps():
|
||||
"""Duplicates removed; total ≤ max_variants."""
|
||||
import notion_search
|
||||
|
||||
fake_llm = lambda prompt, **kw: '["AI", "AI", "agents", "agents", "systems", "tools", "models"]'
|
||||
variants = notion_search.expand_query("AI", llm_caller=fake_llm, max_variants=3)
|
||||
_assert(len(variants) <= 3, "capped at max_variants=3")
|
||||
_assert(len(variants) == len(set(variants)), "no duplicates in result")
|
||||
|
||||
|
||||
def test_expand_query_falls_back_on_invalid_json():
|
||||
"""LLM returns prose instead of JSON → fall back to [original]."""
|
||||
import notion_search
|
||||
|
||||
fake_llm = lambda prompt, **kw: "I'm sorry, I can't help with that."
|
||||
variants = notion_search.expand_query("AI agents", llm_caller=fake_llm)
|
||||
_assert(variants == ["AI agents"], "non-JSON response falls back to original only")
|
||||
|
||||
|
||||
def test_expand_query_falls_back_on_llm_exception():
|
||||
"""LLM raises → fall back to [original]."""
|
||||
import notion_search
|
||||
|
||||
def boom(prompt, **kw):
|
||||
raise RuntimeError("API error")
|
||||
|
||||
variants = notion_search.expand_query("AI agents", llm_caller=boom)
|
||||
_assert(variants == ["AI agents"], "LLM exception falls back to original only")
|
||||
|
||||
|
||||
def run_all():
|
||||
tests = [
|
||||
test_call_claude_dispatches_to_sdk_when_available,
|
||||
@@ -132,6 +173,10 @@ def run_all():
|
||||
test_cache_miss_on_different_candidates,
|
||||
test_cache_miss_after_ttl_expires,
|
||||
test_cache_handles_corrupted_file,
|
||||
test_expand_query_returns_variants_with_original_first,
|
||||
test_expand_query_dedupes_and_caps,
|
||||
test_expand_query_falls_back_on_invalid_json,
|
||||
test_expand_query_falls_back_on_llm_exception,
|
||||
]
|
||||
for t in tests:
|
||||
print(f"\n{t.__name__}")
|
||||
|
||||
Reference in New Issue
Block a user