| 1 | """Supplemental X lanes must stay inside the effective research plan.""" |
| 2 | |
| 3 | from unittest.mock import patch |
| 4 | from uuid import uuid4 |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from lib import dates, pipeline, x_envelope |
| 9 | |
| 10 | |
| 11 | @pytest.mark.parametrize("depth", ["default", "deep"]) |
| 12 | @pytest.mark.parametrize("lane", ["backend", "envelope"]) |
| 13 | @pytest.mark.parametrize( |
| 14 | "requested,excluded,planned,allowed", |
| 15 | [ |
| 16 | pytest.param(["reddit"], "x", ["reddit"], False, id="reddit-and-exclusion"), |
| 17 | pytest.param(["reddit"], "", ["reddit", "x"], False, id="requested-reddit"), |
| 18 | pytest.param(None, "x", ["reddit", "x"], False, id="excluded-x"), |
| 19 | pytest.param(["reddit", "x"], "x", ["reddit", "x"], False, id="exclusion-wins"), |
| 20 | pytest.param(["reddit", "x"], "", ["reddit"], False, id="plan-omits-x"), |
| 21 | pytest.param(None, "", ["reddit", "x"], True, id="available-x"), |
| 22 | pytest.param(["reddit", "x"], "", ["reddit", "x"], True, id="requested-x"), |
| 23 | pytest.param(["xquik"], "", ["reddit", "x"], True, id="requested-alias"), |
| 24 | ], |
| 25 | ) |
| 26 | def test_supplemental_lanes_obey_effective_sources(depth, lane, requested, excluded, planned, allowed): |
| 27 | topic = "widget development" |
| 28 | marker = uuid4().hex |
| 29 | window = dates.get_date_range(30) |
| 30 | posts = [ |
| 31 | { |
| 32 | "id": str(index), |
| 33 | "url": f"https://x.com/example/status/{index}", |
| 34 | "author_handle": "example", |
| 35 | "text": f"{topic} {marker}: {label}", |
| 36 | "date": window[1], |
| 37 | "engagement": {"likes": 120, "retweets": 10, "replies": 5}, |
| 38 | "relevance": 1.0, |
| 39 | } |
| 40 | for index, label in enumerate([ |
| 41 | "shipping a compiler release with faster incremental builds and reduced memory consumption", |
| 42 | "community feedback suggests clearer tutorials explaining deployment authentication errors for beginners", |
| 43 | ], start=1) |
| 44 | ] |
| 45 | envelope = None |
| 46 | if lane == "envelope": |
| 47 | envelope = x_envelope.Envelope( |
| 48 | path="fixture.json", sha256="fixture", status="ok", error_category="", |
| 49 | provider="x-connector", window=window, topic_items=[], |
| 50 | lane_calls=[ |
| 51 | x_envelope.EnvelopeCall(0, "from", ["example"], [posts[0]]), |
| 52 | x_envelope.EnvelopeCall(1, "mention", ["example"], [posts[1]]), |
| 53 | ], |
| 54 | counters={}, accepted=2, total=2, |
| 55 | lane_counts={"from": 1, "mention": 1}, call_lanes=["from", "mention"], |
| 56 | ) |
| 57 | config = { |
| 58 | "X_BEARER_TOKEN": "dummy-bearer", |
| 59 | "LAST30DAYS_X_BACKEND": "xapi", |
| 60 | "EXCLUDE_SOURCES": excluded, |
| 61 | } |
| 62 | plan = { |
| 63 | "intent": "opinion", "freshness_mode": "balanced_recent", "cluster_mode": "debate", |
| 64 | "subqueries": [{ |
| 65 | "label": "primary", "search_query": topic, |
| 66 | "ranking_query": topic, "sources": planned, |
| 67 | }], |
| 68 | } |
| 69 | with ( |
| 70 | patch.object(pipeline.reddit_public, "search_reddit_public", autospec=True, return_value=[]), |
| 71 | patch.object(pipeline.x_api, "search_x", autospec=True, return_value={"items": []}), |
| 72 | patch.object(pipeline.x_api, "search_handles", autospec=True, return_value=[posts[0]]) as from_lane, |
| 73 | patch.object(pipeline.x_api, "search_mentions", autospec=True, return_value=[posts[1]]) as about_lane, |
| 74 | ): |
| 75 | report = pipeline.run( |
| 76 | topic=topic, config=config, depth=depth, requested_sources=requested, |
| 77 | external_plan=plan, x_handle="example", x_posts=envelope, |
| 78 | web_backend="none", save_dir="", |
| 79 | ) |
| 80 | |
| 81 | evidence = report.items_by_source.get("x", []) |
| 82 | if allowed: |
| 83 | assert {item.url for item in evidence} == {post["url"] for post in posts} |
| 84 | assert all(marker in item.body for item in evidence) |
| 85 | else: |
| 86 | assert not evidence |
| 87 | if allowed and lane == "backend": |
| 88 | from_lane.assert_called_once() |
| 89 | about_lane.assert_called_once() |
| 90 | else: |
| 91 | from_lane.assert_not_called() |
| 92 | about_lane.assert_not_called() |
| 93 | if envelope is not None: |
| 94 | remaining = envelope.take_lanes() |
| 95 | if allowed: |
| 96 | assert remaining is None |
| 97 | else: |
| 98 | assert remaining == envelope.lane_calls |
| 99 |