| 1 | """Provider JSON may decode successfully without matching the plan shape.""" |
| 2 | |
| 3 | from unittest.mock import Mock |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from lib import planner |
| 8 | |
| 9 | |
| 10 | def _plan(payload, depth="quick"): |
| 11 | provider = Mock() |
| 12 | provider.generate_json.return_value = payload |
| 13 | return planner.plan_query( |
| 14 | topic="SQLite incremental vacuum", |
| 15 | available_sources=["hackernews", "reddit"], |
| 16 | requested_sources=["hackernews"], |
| 17 | depth=depth, |
| 18 | provider=provider, |
| 19 | model="fixture", |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | @pytest.mark.parametrize("subqueries", [{"primary": "vacuum"}, 42]) |
| 24 | def test_invalid_subquery_collection_falls_back(subqueries): |
| 25 | plan = _plan({"subqueries": subqueries}) |
| 26 | assert plan.subqueries[0].sources == ["hackernews"] |
| 27 | assert "fallback-plan" in " ".join(plan.notes) |
| 28 | |
| 29 | |
| 30 | def test_malformed_fields_do_not_discard_a_valid_provider_query(): |
| 31 | plan = _plan({ |
| 32 | "notes": 42, |
| 33 | "subqueries": [ |
| 34 | { |
| 35 | "search_query": {"keywords": "SQLite"}, |
| 36 | "ranking_query": "Wrong shape", |
| 37 | "sources": ["hackernews"], |
| 38 | }, |
| 39 | { |
| 40 | "search_query": "sqlite vacuum api", |
| 41 | "ranking_query": "How do SQLite vacuum modes work?", |
| 42 | "sources": [{"name": "reddit"}, "hackernews"], |
| 43 | "weight": float("inf"), |
| 44 | }, |
| 45 | ], |
| 46 | }) |
| 47 | assert plan.notes == [] |
| 48 | assert len(plan.subqueries) == 1 |
| 49 | subquery = plan.subqueries[0] |
| 50 | assert subquery.search_query == "sqlite vacuum api" |
| 51 | assert subquery.sources == ["hackernews"] |
| 52 | assert subquery.weight == 1.0 |
| 53 | |
| 54 | |
| 55 | def _query(text, **kwargs): |
| 56 | return { |
| 57 | "search_query": text, |
| 58 | "ranking_query": f"What can we learn about {text}?", |
| 59 | "sources": ["hackernews"], |
| 60 | **kwargs, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | def test_invalid_rows_do_not_take_slots_from_usable_provider_queries(): |
| 65 | plan = _plan({ |
| 66 | "intent": "concept", |
| 67 | "subqueries": [ |
| 68 | None, |
| 69 | _query({"query": "wrong shape"}), |
| 70 | _query("usable first"), |
| 71 | _query("usable second"), |
| 72 | _query("beyond the cap"), |
| 73 | ], |
| 74 | }, depth="default") |
| 75 | assert [query.search_query for query in plan.subqueries] == [ |
| 76 | "usable first", "usable second", |
| 77 | ] |
| 78 | |
| 79 | |
| 80 | @pytest.mark.parametrize("invalid_weight", [float("inf"), "invalid"]) |
| 81 | def test_invalid_weight_normalizes_like_an_omitted_weight(invalid_weight): |
| 82 | control = _query("first") |
| 83 | second = _query("second", weight=3.0) |
| 84 | expected = _plan({"subqueries": [control, second]}, depth="default") |
| 85 | actual = _plan( |
| 86 | {"subqueries": [{**control, "weight": invalid_weight}, second]}, |
| 87 | depth="default", |
| 88 | ) |
| 89 | assert [query.weight for query in actual.subqueries] == [ |
| 90 | query.weight for query in expected.subqueries |
| 91 | ] == [0.25, 0.75] |
| 92 | |
| 93 | |
| 94 | def test_repeated_provider_labels_do_not_share_a_retrieval_stream(): |
| 95 | plan = _plan({ |
| 96 | "intent": "comparison", |
| 97 | "subqueries": [ |
| 98 | _query("SQLite in applications", label="research"), |
| 99 | _query("Postgres in applications", label="research"), |
| 100 | _query("migration tradeoffs", label="research-2"), |
| 101 | ], |
| 102 | }, depth="deep") |
| 103 | assert [query.label for query in plan.subqueries] == ["research", "research-3", "research-2"] |
| 104 |