| 1 | import json |
| 2 | import threading |
| 3 | from datetime import datetime, timedelta, timezone |
| 4 | from unittest import mock |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | import last30days |
| 9 | from lib import bird_x, dates, pipeline, schema, subproc |
| 10 | |
| 11 | |
| 12 | def _response(items): |
| 13 | return subproc.SubprocResult( |
| 14 | returncode=0, stdout=json.dumps({"items": items}), stderr="" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | def _tweet(post_id, created_at, author="analyst"): |
| 19 | return { |
| 20 | "id": post_id, |
| 21 | "url": f"https://x.com/{author}/status/{post_id}", |
| 22 | "author": {"username": author}, |
| 23 | "text": "multi-agent autonomous systems", |
| 24 | "createdAt": created_at, |
| 25 | } |
| 26 | |
| 27 | |
| 28 | def _query_arg(cmd): |
| 29 | assert cmd[-2] == "--" |
| 30 | return cmd[-1] |
| 31 | |
| 32 | |
| 33 | def test_every_topic_retry_keeps_the_inclusive_end_date(): |
| 34 | with mock.patch.object( |
| 35 | bird_x.subproc, "run_with_timeout", autospec=True, return_value=_response([]) |
| 36 | ) as run: |
| 37 | result = bird_x.search_x( |
| 38 | "multi-agent autonomous systems", "2024-02-01", "2024-02-29" |
| 39 | ) |
| 40 | |
| 41 | queries = [_query_arg(call.args[0]) for call in run.call_args_list] |
| 42 | assert result == {"items": []} |
| 43 | assert queries == [ |
| 44 | "multi-agent autonomous systems since:2024-02-01 until:2024-03-01", |
| 45 | '("multi-agent") since:2024-02-01 until:2024-03-01', |
| 46 | "multi-agent autonomous since:2024-02-01 until:2024-03-01", |
| 47 | "multi-agent since:2024-02-01 until:2024-03-01", |
| 48 | ] |
| 49 | |
| 50 | |
| 51 | def test_historical_search_retries_when_recent_posts_are_outside_the_window(): |
| 52 | historical = _tweet("1", "2024-02-29T23:59:59Z") |
| 53 | recent = _tweet("2", "2026-10-04T12:00:00Z") |
| 54 | |
| 55 | def search(cmd, **kwargs): |
| 56 | query = _query_arg(cmd) |
| 57 | if "until:2024-03-01" not in query: |
| 58 | return _response([recent]) |
| 59 | if query.startswith("("): |
| 60 | return _response([historical]) |
| 61 | return _response([]) |
| 62 | |
| 63 | with mock.patch.object( |
| 64 | bird_x.subproc, "run_with_timeout", autospec=True, side_effect=search |
| 65 | ) as run: |
| 66 | result = bird_x.search_x( |
| 67 | "multi-agent autonomous systems", "2024-02-01", "2024-02-29" |
| 68 | ) |
| 69 | |
| 70 | assert result["items"] == [historical] |
| 71 | assert run.call_count == 2 |
| 72 | |
| 73 | |
| 74 | def test_current_day_search_retains_posts_from_the_whole_end_date(): |
| 75 | today = datetime.now(timezone.utc).date() |
| 76 | last_second = _tweet("3", f"{today.isoformat()}T23:59:59Z") |
| 77 | with mock.patch.object( |
| 78 | bird_x.subproc, |
| 79 | "run_with_timeout", |
| 80 | autospec=True, |
| 81 | return_value=_response([last_second]), |
| 82 | ) as run: |
| 83 | result = bird_x.search_x( |
| 84 | "multi-agent", (today - timedelta(days=30)).isoformat(), today.isoformat() |
| 85 | ) |
| 86 | |
| 87 | assert result["items"] == [last_second] |
| 88 | assert run.call_count == 1 |
| 89 | assert f"until:{(today + timedelta(days=1)).isoformat()}" in _query_arg(run.call_args.args[0]) |
| 90 | |
| 91 | |
| 92 | @pytest.mark.parametrize("lane", ["topic", "from", "mentions"]) |
| 93 | @pytest.mark.parametrize( |
| 94 | ("end_date", "expected_filters"), |
| 95 | [ |
| 96 | ("9999-12-31", "since:9999-12-01"), |
| 97 | ("9999-12-30", "since:9999-11-30 until:9999-12-31"), |
| 98 | ], |
| 99 | ) |
| 100 | def test_cli_maximum_end_dates_reach_every_bird_lane(lane, end_date, expected_filters): |
| 101 | args = last30days.build_parser().parse_args(["multi-agent", "--as-of", end_date]) |
| 102 | from_date, to_date = dates.get_date_range(as_of_date=args.as_of_date) |
| 103 | topic = " ".join(args.topic) |
| 104 | tweet = _tweet("5", f"{end_date}T23:59:59Z") |
| 105 | with mock.patch.object( |
| 106 | bird_x.subproc, |
| 107 | "run_with_timeout", |
| 108 | autospec=True, |
| 109 | return_value=_response([tweet]), |
| 110 | ) as run: |
| 111 | if lane == "topic": |
| 112 | result = bird_x.search_x(topic, from_date, to_date) |
| 113 | items = bird_x.parse_bird_response(result, query=topic) |
| 114 | expected = topic |
| 115 | elif lane == "from": |
| 116 | items = bird_x.search_handles(["subject"], topic, from_date, to_date=to_date) |
| 117 | expected = "from:subject" |
| 118 | else: |
| 119 | items = bird_x.search_mentions(["subject"], from_date, to_date=to_date) |
| 120 | expected = "@subject" |
| 121 | |
| 122 | assert [item["url"] for item in items] == [tweet["url"]] |
| 123 | assert [_query_arg(call.args[0]) for call in run.call_args_list] == [ |
| 124 | f"{expected} {expected_filters}" |
| 125 | ] |
| 126 | |
| 127 | |
| 128 | @pytest.mark.parametrize("lane", ["from", "mentions"]) |
| 129 | def test_handle_lanes_bound_the_inclusive_end_date(lane): |
| 130 | tweet = _tweet("4", "2024-12-31T23:59:59Z") |
| 131 | with mock.patch.object( |
| 132 | bird_x.subproc, |
| 133 | "run_with_timeout", |
| 134 | autospec=True, |
| 135 | return_value=_response([tweet]), |
| 136 | ) as run: |
| 137 | if lane == "from": |
| 138 | items = bird_x.search_handles( |
| 139 | ["subject"], "multi-agent", "2024-12-01", to_date="2024-12-31" |
| 140 | ) |
| 141 | expected = "from:subject" |
| 142 | else: |
| 143 | items = bird_x.search_mentions( |
| 144 | ["subject"], "2024-12-01", to_date="2024-12-31" |
| 145 | ) |
| 146 | expected = "@subject" |
| 147 | |
| 148 | assert [item["url"] for item in items] == [tweet["url"]] |
| 149 | assert _query_arg(run.call_args.args[0]) == ( |
| 150 | f"{expected} since:2024-12-01 until:2025-01-01" |
| 151 | ) |
| 152 | |
| 153 | |
| 154 | def test_pipeline_passes_the_end_date_to_both_bird_handle_lanes(): |
| 155 | bundle = schema.RetrievalBundle() |
| 156 | plan = schema.QueryPlan( |
| 157 | intent="news", |
| 158 | freshness_mode="balanced_recent", |
| 159 | cluster_mode="timeline", |
| 160 | raw_topic="subject", |
| 161 | source_weights={"x": 1.0}, |
| 162 | subqueries=[schema.SubQuery( |
| 163 | label="primary", search_query="subject", ranking_query="subject", sources=["x"] |
| 164 | )], |
| 165 | ) |
| 166 | with mock.patch.object( |
| 167 | bird_x.subproc, "run_with_timeout", autospec=True, return_value=_response([]) |
| 168 | ) as run: |
| 169 | pipeline._run_supplemental_searches( |
| 170 | topic="subject", |
| 171 | bundle=bundle, |
| 172 | plan=plan, |
| 173 | config={}, |
| 174 | depth="default", |
| 175 | date_range=("2024-12-01", "2024-12-31"), |
| 176 | runtime=schema.ProviderRuntime( |
| 177 | reasoning_provider="local", planner_model="", rerank_model="", |
| 178 | x_search_backend="bird", |
| 179 | ), |
| 180 | mock=False, |
| 181 | rate_limited_sources=set(), |
| 182 | rate_limit_lock=threading.Lock(), |
| 183 | x_handle="subject", |
| 184 | ) |
| 185 | |
| 186 | assert sorted(_query_arg(call.args[0]) for call in run.call_args_list) == [ |
| 187 | "@subject since:2024-12-01 until:2025-01-01", |
| 188 | "from:subject since:2024-12-01 until:2025-01-01", |
| 189 | ] |
| 190 |