| 1 | """Supplemental handle discovery reaches the first-party ranking boundaries.""" |
| 2 | |
| 3 | import inspect |
| 4 | import threading |
| 5 | from unittest.mock import patch |
| 6 | |
| 7 | from lib import pipeline, providers, schema |
| 8 | |
| 9 | |
| 10 | def _plan(): |
| 11 | return schema.QueryPlan( |
| 12 | intent="exploration", freshness_mode="balanced_recent", cluster_mode="topic", |
| 13 | raw_topic="Example research", source_weights={"x": 1.0}, |
| 14 | subqueries=[schema.SubQuery(label="primary", search_query="Example research", |
| 15 | ranking_query="Example research", sources=["x"])], |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def test_out_param_is_part_of_the_contract(): |
| 20 | parameter = inspect.signature(pipeline._run_supplemental_searches).parameters["resolved_handles_out"] |
| 21 | assert parameter.default is None |
| 22 | |
| 23 | |
| 24 | def test_collector_normalizes_deduplicates_and_requires_corroboration(): |
| 25 | output = ["existing"] |
| 26 | with patch.object(pipeline.entity_extract, "extract_entities", return_value={ |
| 27 | "x_handles": ["@ExampleDev", "EXAMPLEDEV", "commentator", "@Existing"], |
| 28 | "x_hashtags": [], "reddit_subreddits": [], |
| 29 | }), patch.object(pipeline.env, "x_backend_chain", return_value=[]): |
| 30 | pipeline._run_supplemental_searches( |
| 31 | topic="Example research", bundle=schema.RetrievalBundle(), plan=_plan(), |
| 32 | config={}, depth="default", date_range=("2026-03-01", "2026-03-31"), |
| 33 | runtime=providers.mock_runtime({}, "default"), mock=False, |
| 34 | rate_limited_sources=set(), rate_limit_lock=threading.Lock(), |
| 35 | x_handle="@Explicit", x_related=["@Related", "related"], |
| 36 | resolved_handles_out=output, |
| 37 | ) |
| 38 | assert output == ["existing", "explicit", "exampledev", "related"] |
| 39 | |
| 40 | |
| 41 | def _resolve(topic, extracted): |
| 42 | """Run the Phase 2 collector with a fixed extracted-handle list; return the |
| 43 | handles it resolved beyond the explicit anchor handle (the collector returns |
| 44 | early with no X items and no explicit handle, so one anchor keeps it running).""" |
| 45 | output = [] |
| 46 | with patch.object(pipeline.entity_extract, "extract_entities", return_value={ |
| 47 | "x_handles": extracted, "x_hashtags": [], "reddit_subreddits": [], |
| 48 | }), patch.object(pipeline.env, "x_backend_chain", return_value=[]): |
| 49 | pipeline._run_supplemental_searches( |
| 50 | topic=topic, bundle=schema.RetrievalBundle(), plan=_plan(), |
| 51 | config={}, depth="default", date_range=("2026-03-01", "2026-03-31"), |
| 52 | runtime=providers.mock_runtime({}, "default"), mock=False, |
| 53 | rate_limited_sources=set(), rate_limit_lock=threading.Lock(), |
| 54 | x_handle="anchor_handle", resolved_handles_out=output, |
| 55 | ) |
| 56 | return [h for h in output if h != "anchor_handle"] |
| 57 | |
| 58 | |
| 59 | def test_short_handles_are_not_corroborated_by_substring_overlap(): |
| 60 | """An extracted handle that is merely a short fragment of a topic word ("go" |
| 61 | in "Google", "ai" in "Chair") does not name the topic and must not earn |
| 62 | first-party status.""" |
| 63 | assert _resolve("Google Gemini", ["go", "google_ai_dev"]) == ["google_ai_dev"] |
| 64 | assert _resolve("Chair review", ["ai"]) == [] |
| 65 | |
| 66 | |
| 67 | def test_handle_containing_a_full_three_letter_topic_word_is_corroborated(): |
| 68 | """A short subject name ("Vue") inside a longer handle ("vuejs") is still |
| 69 | the subject, while a short fragment of a longer topic word ("go" in |
| 70 | "Google") is not.""" |
| 71 | assert _resolve("Vue", ["vuejs", "go"]) == ["vuejs"] |
| 72 | assert _resolve("Google Gemini", ["go"]) == [] |
| 73 | # Scaffolding words never name the subject, even when contained in a handle. |
| 74 | assert _resolve("Cursor review", ["reviewbot"]) == [] |
| 75 | |
| 76 | |
| 77 | def test_handle_spelled_from_the_topic_name_is_still_corroborated(): |
| 78 | assert _resolve("Peter Steinberger", ["steipete", "commentator"]) == ["steipete"] |
| 79 | |
| 80 | |
| 81 | def test_discovered_handles_reach_real_fusion_and_reranking(): |
| 82 | raw = [ |
| 83 | {"id": "subject", "text": "A new release shipped today", |
| 84 | "author_handle": "ExampleDev", "url": "https://x.com/ExampleDev/status/1", |
| 85 | "date": "2026-03-15", "engagement": {"likes": 100}}, |
| 86 | {"id": "mention", "text": "Example research by ExampleDev is detailed", |
| 87 | "author_handle": "observer", "url": "https://x.com/observer/status/2", |
| 88 | "date": "2026-03-16", "engagement": {"likes": 20}}, |
| 89 | ] |
| 90 | runtime = providers.mock_runtime({}, "default") |
| 91 | with patch.object(pipeline.providers, "resolve_runtime", return_value=(runtime, None)), \ |
| 92 | patch.object(pipeline, "available_sources", return_value=["x"]), \ |
| 93 | patch.object(pipeline.env, "x_backend_chain", return_value=[]), \ |
| 94 | patch.object(pipeline, "_retrieve_stream", return_value=(raw, {})), \ |
| 95 | patch.object(pipeline, "_run_supplemental_searches", wraps=pipeline._run_supplemental_searches) as supplements, \ |
| 96 | patch.object(pipeline, "weighted_rrf", wraps=pipeline.weighted_rrf) as fuse, \ |
| 97 | patch.object(pipeline.rerank, "rerank_candidates", wraps=pipeline.rerank.rerank_candidates) as rank: |
| 98 | report = pipeline.run( |
| 99 | topic="Example research", config={}, depth="default", mock=False, |
| 100 | requested_sources=["x"], web_backend="none", as_of_date="2026-03-31", |
| 101 | external_plan=schema.to_dict(_plan()), |
| 102 | ) |
| 103 | assert supplements.call_count == 1 |
| 104 | assert supplements.call_args.kwargs["resolved_handles_out"] == ["exampledev"] |
| 105 | assert "exampledev" in fuse.call_args.kwargs["first_party_handles"] |
| 106 | assert "observer" not in fuse.call_args.kwargs["first_party_handles"] |
| 107 | assert rank.call_count == 2 |
| 108 | assert all("exampledev" in call.kwargs["resolved_handles"] for call in rank.call_args_list) |
| 109 | assert any(item.author == "ExampleDev" for candidate in report.ranked_candidates |
| 110 | for item in candidate.source_items) |
| 111 |