An open source, non-profit web search engine
Super Search: address recipe review feedback
Daoud Clarke37c54d56080a
diff --git a/mwmbl/tinysearchengine/super_search_sources/recipe.py b/mwmbl/tinysearchengine/super_search_sources/recipe.pyindex 5767284..b720063 100644--- a/mwmbl/tinysearchengine/super_search_sources/recipe.py+++ b/mwmbl/tinysearchengine/super_search_sources/recipe.py@@ -44,6 +44,7 @@ class Recipe:response: dictdomain: str = ""field: str = ""+ smoke: dict | None = None@propertydef response_format(self) -> str:@@ -54,28 +55,26 @@ def load_recipe(path: Path | str) -> Recipe:data = yaml.safe_load(Path(path).read_text())return Recipe(name=data["name"],- domain=data.get("domain", ""),- field=data.get("field", ""),+ domain=data["domain"],+ field=data["field"],request=data["request"],response=data["response"],+ smoke=data["smoke"],)def load_recipes(directory: Path | str = RECIPES_DIR) -> dict[str, Recipe]:"""Load every ``*.yaml`` recipe in ``directory``, keyed by recipe name.- Invalid recipes are logged and skipped rather than failing the import.+ A malformed recipe (missing required keys, bad YAML) raises rather than+ being silently skipped, so a broken recipe fails loudly at load time."""recipes: dict[str, Recipe] = {}directory = Path(directory)if not directory.is_dir():return recipesfor path in sorted(directory.glob("*.yaml")):- try:- recipe = load_recipe(path)- except (yaml.YAMLError, KeyError, OSError) as e:- logger.warning("Skipping invalid recipe %s: %s", path, e)- continue+ recipe = load_recipe(path)recipes[recipe.name] = recipereturn recipesdiff --git a/mwmbl/tinysearchengine/super_search_sources/recipes/archive_org.yaml b/mwmbl/tinysearchengine/super_search_sources/recipes/archive_org.yamlindex 22480b1..b6d0a89 100644--- a/mwmbl/tinysearchengine/super_search_sources/recipes/archive_org.yaml+++ b/mwmbl/tinysearchengine/super_search_sources/recipes/archive_org.yaml@@ -21,3 +21,6 @@ response:extract: descriptionurl:template: "https://archive.org/details/{identifier}"+smoke:+ query: frankenstein+ expect_title_contains: Frankensteindiff --git a/mwmbl/tinysearchengine/super_search_sources/recipes/gutenberg.yaml b/mwmbl/tinysearchengine/super_search_sources/recipes/gutenberg.yamlindex f80139c..c776be4 100644--- a/mwmbl/tinysearchengine/super_search_sources/recipes/gutenberg.yaml+++ b/mwmbl/tinysearchengine/super_search_sources/recipes/gutenberg.yaml@@ -20,3 +20,6 @@ response:url:selector: "a.link"attr: href+smoke:+ query: frankenstein+ expect_title_contains: Frankensteindiff --git a/mwmbl/tinysearchengine/super_search_sources/recipes/wiktionary.yaml b/mwmbl/tinysearchengine/super_search_sources/recipes/wiktionary.yamlindex 9b3ba0c..37b31ac 100644--- a/mwmbl/tinysearchengine/super_search_sources/recipes/wiktionary.yaml+++ b/mwmbl/tinysearchengine/super_search_sources/recipes/wiktionary.yaml@@ -23,3 +23,6 @@ response:template: "https://en.wiktionary.org/wiki/{title}"strip_html:- extract+smoke:+ query: frankenstein+ expect_title_contains: Frankensteindiff --git a/test/test_super_search_recipes.py b/test/test_super_search_recipes.pyindex 290a590..30868af 100644--- a/test/test_super_search_recipes.py+++ b/test/test_super_search_recipes.py@@ -9,6 +9,7 @@ import refrom pathlib import Pathimport httpx+import pytestfrom mwmbl.tinysearchengine.super_search_sources.recipe import (RECIPES_DIR,@@ -38,9 +39,15 @@ def test_recipes_registered_in_sources():assert "wiktionary" in SOURCES and "gutenberg" in SOURCES-def test_load_recipes_skips_invalid(tmp_path):+def test_load_recipes_raises_on_invalid(tmp_path):(tmp_path / "broken.yaml").write_text("name: x\nrequest: {}\n") # no response- assert load_recipes(tmp_path) == {}+ with pytest.raises(KeyError):+ load_recipes(tmp_path)++[email protected]("recipe", RECIPES.values(), ids=lambda r: r.name)+def test_recipe_has_smoke_block(recipe):+ assert recipe.smoke and recipe.smoke["query"] and recipe.smoke["expect_title_contains"]# ---------------------------------------------------------------------------diff --git a/test/test_super_search_smoke.py b/test/test_super_search_smoke.pyindex 4c459db..9db77e4 100644--- a/test/test_super_search_smoke.py+++ b/test/test_super_search_smoke.py@@ -20,7 +20,6 @@ from mwmbl.tinysearchengine.super_search_sources.recipe import load_recipes, seapytestmark = pytest.mark.live-QUERY = "frankenstein"USER_AGENT = "mwmbl-super-search/0.1 (+https://mwmbl.org)"RECIPES = load_recipes()@@ -30,26 +29,15 @@ def _client() -> httpx.AsyncClient:return httpx.AsyncClient(timeout=20.0, headers={"User-Agent": USER_AGENT})-def _assert_usable(docs, expected_url_substr: str):+def _assert_usable(docs, expected_title_substr: str):assert docs, "recipe returned no results — the site may have changed its format or blocked us"- top = docs[0]- assert top.url and expected_url_substr in top.url, f"unexpected result URL: {top.url!r}"- assert top.title, "top result has an empty title"+ assert docs[0].url and docs[0].title, f"top result missing url/title: {docs[0]!r}"+ assert any(expected_title_substr in d.title for d in docs), \+ f"no result title contains {expected_title_substr!r}; titles={[d.title for d in docs]}"-async def test_wiktionary_live():[email protected]("recipe", RECIPES.values(), ids=lambda r: r.name)+async def test_recipe_live(recipe):async with _client() as client:- docs = await search_with_recipe(client, RECIPES["wiktionary"], QUERY, 5)- _assert_usable(docs, "en.wiktionary.org/wiki/")---async def test_archive_org_live():- async with _client() as client:- docs = await search_with_recipe(client, RECIPES["archive_org"], QUERY, 5)- _assert_usable(docs, "archive.org/details/")---async def test_gutenberg_live():- async with _client() as client:- docs = await search_with_recipe(client, RECIPES["gutenberg"], QUERY, 5)- _assert_usable(docs, "gutenberg.org/ebooks/")+ docs = await search_with_recipe(client, recipe, recipe.smoke["query"], 5)+ _assert_usable(docs, recipe.smoke["expect_title_contains"])