| 1 | """Tests for the Techmeme source adapter (lib/techmeme.py). |
| 2 | |
| 3 | Covers the --json (not --agent) surface choice, header-row filtering, |
| 4 | field mapping, date windowing to the research range, old-binary prose |
| 5 | tolerance, and graceful degradation. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import json |
| 11 | from datetime import datetime, timezone |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | from lib import techmeme |
| 16 | |
| 17 | |
| 18 | class _FakeProc: |
| 19 | returncode = 0 |
| 20 | stdout = "{}" |
| 21 | stderr = "" |
| 22 | |
| 23 | def __init__(self, stdout: str = "{}", returncode: int = 0, stderr: str = ""): |
| 24 | self.stdout = stdout |
| 25 | self.returncode = returncode |
| 26 | self.stderr = stderr |
| 27 | |
| 28 | |
| 29 | # ---- surface choice ---- |
| 30 | |
| 31 | def test_search_args_use_json_not_agent(): |
| 32 | args = techmeme._build_search_args("AI agents") |
| 33 | assert "--json" in args |
| 34 | # --agent implies --compact, which blanked records pre-PR-1383. |
| 35 | assert "--agent" not in args |
| 36 | assert "--compact" not in args |
| 37 | # Techmeme `search` has no result-limit flag; --max-results breaks it. |
| 38 | assert "--max-results" not in args |
| 39 | assert "search" in args and "AI agents" in args |
| 40 | |
| 41 | |
| 42 | def test_search_invokes_only_search_no_sync(monkeypatch): |
| 43 | """search_techmeme must issue exactly one subprocess call -- the search -- |
| 44 | and never a `sync` (search hits Techmeme's live archive, not the cache).""" |
| 45 | calls = [] |
| 46 | |
| 47 | def fake_run(cmd, timeout, **_kwargs): |
| 48 | calls.append(list(cmd)) |
| 49 | return _FakeProc(stdout=json.dumps([ |
| 50 | {"num": 1, "source": "a.com", |
| 51 | "headline": "A real in-window headline about the topic", |
| 52 | "link": "https://t.co/a", "date": "2026-06-15"}, |
| 53 | ])) |
| 54 | |
| 55 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 56 | monkeypatch.setattr(techmeme.subproc, "run_with_timeout", fake_run) |
| 57 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27") |
| 58 | assert len(out["results"]) == 1 |
| 59 | assert calls == [ |
| 60 | [techmeme.CLI_BIN, "search", "--json", "--pages", "2", "--", |
| 61 | "topic date:[6/1/26 TO 6/27/26]"], |
| 62 | ] |
| 63 | assert not any("sync" in c for c in calls) |
| 64 | |
| 65 | |
| 66 | # ---- server-side date window (#1449) ---- |
| 67 | |
| 68 | def test_search_args_carry_the_run_window_as_a_date_clause(): |
| 69 | args = techmeme._build_search_args("Microsoft", "2026-10-16", "2026-11-15") |
| 70 | # No space after the colon and no zero padding: the live-verified form. |
| 71 | assert args == [ |
| 72 | techmeme.CLI_BIN, "search", "--json", "--", "Microsoft date:[10/16/26 TO 11/15/26]", |
| 73 | ] |
| 74 | |
| 75 | |
| 76 | def test_date_clause_handles_single_digit_dates_and_year_rollover(): |
| 77 | args = techmeme._build_search_args("AI", "2026-12-05", "2027-01-04") |
| 78 | assert args[-2:] == ["--", "AI date:[12/5/26 TO 1/4/27]"] |
| 79 | |
| 80 | |
| 81 | def test_no_date_clause_without_a_valid_window(): |
| 82 | assert techmeme._build_search_args("AI")[-2:] == ["--", "AI"] |
| 83 | assert techmeme._build_search_args("AI", "2026-06-01", None)[-2:] == ["--", "AI"] |
| 84 | assert techmeme._build_search_args("AI", "June 1", "2026-06-27")[-2:] == ["--", "AI"] |
| 85 | |
| 86 | |
| 87 | def test_topic_with_its_own_date_operator_is_left_alone(): |
| 88 | args = techmeme._build_search_args("AI date:[1/1/26 TO 2/1/26]", "2026-06-01", "2026-06-27") |
| 89 | assert args[-2:] == ["--", "AI date:[1/1/26 TO 2/1/26]"] |
| 90 | |
| 91 | |
| 92 | def test_topic_with_another_operator_keeps_it_and_gains_the_window(): |
| 93 | args = techmeme._build_search_args("sourcename:verge AI", "2026-06-01", "2026-06-27") |
| 94 | assert args[-2:] == ["--", "sourcename:verge AI date:[6/1/26 TO 6/27/26]"] |
| 95 | |
| 96 | |
| 97 | def test_client_side_window_still_drops_rows_the_server_did_not_scope(monkeypatch): |
| 98 | """A helper or server that ignores the operator still gets windowed.""" |
| 99 | out = _search_with_records(monkeypatch, [ |
| 100 | {"num": 1, "source": "a.com", "headline": "Fresh in-window topic story", |
| 101 | "link": "https://t.co/a", "date": "2026-06-15"}, |
| 102 | {"num": 2, "source": "b.com", "headline": "Years-old archive topic story", |
| 103 | "link": "https://t.co/b", "date": "2019-03-02"}, |
| 104 | ]) |
| 105 | assert [r["link"] for r in out["results"]] == ["https://t.co/a"] |
| 106 | |
| 107 | |
| 108 | # ---- date windowing ---- |
| 109 | |
| 110 | def _search_with_records(monkeypatch, records, from_date="2026-06-01", to_date="2026-06-27"): |
| 111 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 112 | monkeypatch.setattr( |
| 113 | techmeme.subproc, "run_with_timeout", |
| 114 | lambda cmd, timeout, **_kwargs: _FakeProc(stdout=json.dumps(records)), |
| 115 | ) |
| 116 | return techmeme.search_techmeme("topic", from_date, to_date) |
| 117 | |
| 118 | |
| 119 | def test_in_window_record_kept_with_real_date(monkeypatch): |
| 120 | out = _search_with_records(monkeypatch, [ |
| 121 | {"num": 1, "source": "a.com", |
| 122 | "headline": "Fresh in-window story about the research topic", |
| 123 | "link": "https://t.co/a", "date": "2026-06-15"}, |
| 124 | ]) |
| 125 | assert len(out["results"]) == 1 |
| 126 | items = techmeme.parse_techmeme_response(out, query="topic") |
| 127 | assert len(items) == 1 |
| 128 | # The item carries the record's real date, never today's. |
| 129 | assert items[0]["date"] == "2026-06-15" |
| 130 | |
| 131 | |
| 132 | def test_out_of_window_record_dropped(monkeypatch): |
| 133 | out = _search_with_records(monkeypatch, [ |
| 134 | {"num": 1, "source": "a.com", |
| 135 | "headline": "Stale Parler acquisition story from years ago", |
| 136 | "link": "https://t.co/old", "date": "2022-12-02"}, |
| 137 | {"num": 2, "source": "b.com", |
| 138 | "headline": "Fresh in-window story about the research topic", |
| 139 | "link": "https://t.co/new", "date": "2026-06-15"}, |
| 140 | ]) |
| 141 | links = [r["link"] for r in out["results"]] |
| 142 | assert links == ["https://t.co/new"] |
| 143 | |
| 144 | |
| 145 | def test_zero_in_window_records_means_zero_results(monkeypatch): |
| 146 | """No keep-all fallback: staleness is the bug, so all-stale means empty.""" |
| 147 | out = _search_with_records(monkeypatch, [ |
| 148 | {"num": 1, "source": "a.com", |
| 149 | "headline": "Stale story number one from the archive", |
| 150 | "link": "https://t.co/1", "date": "2022-12-02"}, |
| 151 | {"num": 2, "source": "b.com", |
| 152 | "headline": "Stale story number two from the archive", |
| 153 | "link": "https://t.co/2", "date": "2023-08-24"}, |
| 154 | ]) |
| 155 | assert out["results"] == [] |
| 156 | |
| 157 | |
| 158 | def test_window_endpoints_inclusive(monkeypatch): |
| 159 | """Records dated exactly from_date or to_date survive the window; a |
| 160 | strict-< regression would silently shave both edges.""" |
| 161 | out = _search_with_records(monkeypatch, [ |
| 162 | {"num": 1, "source": "a.com", |
| 163 | "headline": "Story published exactly on the window start date", |
| 164 | "link": "https://t.co/start", "date": "2026-06-01"}, |
| 165 | {"num": 2, "source": "b.com", |
| 166 | "headline": "Story published exactly on the window end date", |
| 167 | "link": "https://t.co/end", "date": "2026-06-27"}, |
| 168 | ]) |
| 169 | assert [r["link"] for r in out["results"]] == ["https://t.co/start", "https://t.co/end"] |
| 170 | |
| 171 | |
| 172 | def test_non_string_and_whitespace_dates(monkeypatch): |
| 173 | """Non-string date values (int/null) are treated as undated and kept; |
| 174 | a valid ISO date with surrounding whitespace still parses.""" |
| 175 | out = _search_with_records(monkeypatch, [ |
| 176 | {"num": 1, "source": "a.com", |
| 177 | "headline": "Record carrying an integer where the date belongs", |
| 178 | "link": "https://t.co/int", "date": 20260615}, |
| 179 | {"num": 2, "source": "b.com", |
| 180 | "headline": "Record carrying an explicit null date value", |
| 181 | "link": "https://t.co/null", "date": None}, |
| 182 | {"num": 3, "source": "c.com", |
| 183 | "headline": "Record with whitespace padded valid ISO date", |
| 184 | "link": "https://t.co/ws", "date": " 2026-06-15 "}, |
| 185 | ]) |
| 186 | assert len(out["results"]) == 3 |
| 187 | items = techmeme.parse_techmeme_response(out, query="record") |
| 188 | by_url = {it["url"]: it["date"] for it in items} |
| 189 | assert by_url["https://t.co/int"] is None |
| 190 | assert by_url["https://t.co/null"] is None |
| 191 | assert by_url["https://t.co/ws"] == "2026-06-15" |
| 192 | |
| 193 | |
| 194 | def test_dropped_records_logged(monkeypatch): |
| 195 | logs = [] |
| 196 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 197 | monkeypatch.setattr(techmeme, "_log", lambda msg: logs.append(msg)) |
| 198 | monkeypatch.setattr( |
| 199 | techmeme.subproc, "run_with_timeout", |
| 200 | lambda cmd, timeout, **_kwargs: _FakeProc(stdout=json.dumps([ |
| 201 | {"num": 1, "source": "a.com", |
| 202 | "headline": "Stale story from the deep archive years back", |
| 203 | "link": "https://t.co/old", "date": "2022-12-02"}, |
| 204 | ])), |
| 205 | ) |
| 206 | techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27") |
| 207 | assert "dropped 1 records outside 2026-06-01..2026-06-27" in logs |
| 208 | |
| 209 | |
| 210 | def test_undated_records_kept_and_never_stamped_today(monkeypatch): |
| 211 | """Old binaries emit no date key; unparseable dates come through as "" or |
| 212 | junk. All are kept by the window, and parse yields date None -- never |
| 213 | today's date.""" |
| 214 | out = _search_with_records(monkeypatch, [ |
| 215 | {"num": 1, "source": "a.com", |
| 216 | "headline": "Headline with no date key from an old binary", |
| 217 | "link": "https://t.co/nodate"}, |
| 218 | {"num": 2, "source": "b.com", |
| 219 | "headline": "Headline whose date was unparseable upstream", |
| 220 | "link": "https://t.co/empty", "date": ""}, |
| 221 | {"num": 3, "source": "c.com", |
| 222 | "headline": "Headline carrying non ISO junk in the date", |
| 223 | "link": "https://t.co/junk", "date": "May 22, 2025"}, |
| 224 | ]) |
| 225 | assert len(out["results"]) == 3 |
| 226 | items = techmeme.parse_techmeme_response(out, query="headline") |
| 227 | assert len(items) == 3 |
| 228 | today = datetime.now(timezone.utc).date().isoformat() |
| 229 | for item in items: |
| 230 | assert item["date"] is None |
| 231 | assert item["date"] != today |
| 232 | |
| 233 | |
| 234 | # ---- old-binary prose tolerance ---- |
| 235 | |
| 236 | def test_no_results_prose_parses_as_empty_without_error(monkeypatch): |
| 237 | """Old binaries print `No results for "q"` prose to stdout with exit 0 in |
| 238 | JSON mode. That is a zero-hit response, not a decode failure.""" |
| 239 | logs = [] |
| 240 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 241 | monkeypatch.setattr(techmeme, "_log", lambda msg: logs.append(msg)) |
| 242 | monkeypatch.setattr( |
| 243 | techmeme.subproc, "run_with_timeout", |
| 244 | lambda cmd, timeout, **_kwargs: _FakeProc(stdout='No results for "whatever query"\n'), |
| 245 | ) |
| 246 | resp = techmeme._run_cli([techmeme.CLI_BIN, "search", "--json", "--", "whatever query"], |
| 247 | timeout=techmeme.SEARCH_TIMEOUT) |
| 248 | assert resp == {"results": []} |
| 249 | assert "error" not in resp |
| 250 | assert not any("decode" in m.lower() for m in logs) |
| 251 | |
| 252 | |
| 253 | def test_malformed_stdout_still_yields_decode_error(monkeypatch): |
| 254 | """Genuinely malformed non-JSON stdout (not the prose sentinel) keeps the |
| 255 | existing decode-error envelope.""" |
| 256 | logs = [] |
| 257 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 258 | monkeypatch.setattr(techmeme, "_log", lambda msg: logs.append(msg)) |
| 259 | monkeypatch.setattr( |
| 260 | techmeme.subproc, "run_with_timeout", |
| 261 | lambda cmd, timeout, **_kwargs: _FakeProc(stdout="garbage <<<"), |
| 262 | ) |
| 263 | resp = techmeme._run_cli([techmeme.CLI_BIN, "search", "--json", "--", "topic"], |
| 264 | timeout=techmeme.SEARCH_TIMEOUT) |
| 265 | assert resp["results"] == [] |
| 266 | assert "json decode" in resp.get("error", "") |
| 267 | assert any("decode" in m.lower() for m in logs) |
| 268 | |
| 269 | |
| 270 | # ---- header-row filtering ---- |
| 271 | |
| 272 | def test_story_headline_accepts_sentence(): |
| 273 | assert techmeme._is_story_headline("OpenAI ships a new coding agent today", "techcrunch.com") |
| 274 | |
| 275 | |
| 276 | def test_story_headline_rejects_publication_name_rows(): |
| 277 | # Short, publication-name-only rows are section headers, not stories. |
| 278 | assert not techmeme._is_story_headline("TechCrunch", "techcrunch.com") |
| 279 | assert not techmeme._is_story_headline("New York Times", "nytimes.com") |
| 280 | |
| 281 | |
| 282 | def test_parse_drops_header_rows_keeps_stories(): |
| 283 | resp = { |
| 284 | "results": [ |
| 285 | {"num": 1, "source": "techcrunch.com", "headline": "TechCrunch", |
| 286 | "link": "http://techcrunch.com/", "date": "2026-06-27"}, |
| 287 | {"num": 2, "source": "techcrunch.com", |
| 288 | "headline": "Sakana AI's Fugu claims to rival frontier models", |
| 289 | "link": "https://www.techmeme.com/260627/p2", "date": "2026-06-27"}, |
| 290 | ] |
| 291 | } |
| 292 | items = techmeme.parse_techmeme_response(resp, query="AI") |
| 293 | assert len(items) == 1 |
| 294 | assert items[0]["title"].startswith("Sakana AI") |
| 295 | assert items[0]["url"] == "https://www.techmeme.com/260627/p2" |
| 296 | assert items[0]["source_name"] == "techcrunch.com" |
| 297 | assert items[0]["date"] == "2026-06-27" |
| 298 | |
| 299 | |
| 300 | def test_parse_drops_records_without_link(): |
| 301 | resp = {"results": [{"num": 1, "source": "x.com", "headline": "A real headline sentence here", |
| 302 | "link": "", "date": "2026-06-27"}]} |
| 303 | assert techmeme.parse_techmeme_response(resp, query="x") == [] |
| 304 | |
| 305 | |
| 306 | # ---- relevance ranking ---- |
| 307 | |
| 308 | def test_more_relevant_headline_ranks_higher(): |
| 309 | resp = { |
| 310 | "results": [ |
| 311 | {"num": 1, "source": "a.com", "headline": "Unrelated quarterly earnings report released today", |
| 312 | "link": "https://t.co/a", "date": "2026-06-20"}, |
| 313 | {"num": 2, "source": "b.com", "headline": "New AI agent framework launches for developers", |
| 314 | "link": "https://t.co/b", "date": "2026-06-20"}, |
| 315 | ] |
| 316 | } |
| 317 | items = techmeme.parse_techmeme_response(resp, query="AI agent framework") |
| 318 | by_url = {it["url"]: it["relevance"] for it in items} |
| 319 | assert by_url["https://t.co/b"] > by_url["https://t.co/a"] |
| 320 | |
| 321 | |
| 322 | # ---- envelope tolerance ---- |
| 323 | |
| 324 | def test_coerce_list_handles_bare_array_and_wrapped(): |
| 325 | assert techmeme._coerce_list([{"a": 1}]) == [{"a": 1}] |
| 326 | assert techmeme._coerce_list({"results": [{"a": 1}]}) == [{"a": 1}] |
| 327 | assert techmeme._coerce_list({"nope": 1}) == [] |
| 328 | |
| 329 | |
| 330 | # ---- depth cap + degradation ---- |
| 331 | |
| 332 | @pytest.mark.parametrize("depth,cap", [("quick", 8), ("default", 16), ("deep", 30)]) |
| 333 | def test_depth_cap_truncates_client_side(monkeypatch, depth, cap): |
| 334 | """Techmeme `search` has no limit flag, so the depth cap is applied after |
| 335 | windowing -- regression guard for the other half of the --max-results fix. |
| 336 | All records are in-window, so the cap alone decides the count.""" |
| 337 | records = [ |
| 338 | {"num": i, "source": "x.com", "headline": f"A real headline sentence number {i}", |
| 339 | "link": f"https://t.co/{i}", "date": "2026-06-15"} |
| 340 | for i in range(cap + 12) |
| 341 | ] |
| 342 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 343 | monkeypatch.setattr(techmeme, "_run_cli", lambda cmd, timeout: {"results": list(records)}) |
| 344 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27", depth=depth) |
| 345 | assert len(out["results"]) == cap |
| 346 | |
| 347 | |
| 348 | def test_depth_cap_applies_after_windowing(monkeypatch): |
| 349 | """Stale records must not consume cap slots: with cap in-window records |
| 350 | plus stale ones interleaved ahead of them, every in-window record |
| 351 | survives.""" |
| 352 | cap = techmeme.DEPTH_CONFIG["quick"] |
| 353 | stale = [ |
| 354 | {"num": i, "source": "old.com", "headline": f"Stale archive headline number {i}", |
| 355 | "link": f"https://t.co/old{i}", "date": "2022-01-10"} |
| 356 | for i in range(cap) |
| 357 | ] |
| 358 | fresh = [ |
| 359 | {"num": 100 + i, "source": "new.com", "headline": f"Fresh in window headline number {i}", |
| 360 | "link": f"https://t.co/new{i}", "date": "2026-06-15"} |
| 361 | for i in range(cap) |
| 362 | ] |
| 363 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 364 | monkeypatch.setattr(techmeme, "_run_cli", |
| 365 | lambda cmd, timeout: {"results": stale + fresh}) |
| 366 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27", depth="quick") |
| 367 | links = [r["link"] for r in out["results"]] |
| 368 | assert links == [f"https://t.co/new{i}" for i in range(cap)] |
| 369 | |
| 370 | |
| 371 | def test_dated_records_take_cap_slots_before_undated(monkeypatch): |
| 372 | """Undated archive hits must never evict confirmed in-window stories: |
| 373 | dated in-window records fill cap slots first, undated fill the rest.""" |
| 374 | cap = techmeme.DEPTH_CONFIG["quick"] |
| 375 | undated = [ |
| 376 | {"num": i, "source": "old.com", "headline": f"Undated old binary headline number {i}", |
| 377 | "link": f"https://t.co/u{i}"} |
| 378 | for i in range(cap) |
| 379 | ] |
| 380 | dated = [ |
| 381 | {"num": 100 + i, "source": "new.com", "headline": f"Dated in window headline number {i}", |
| 382 | "link": f"https://t.co/d{i}", "date": "2026-06-15"} |
| 383 | for i in range(cap) |
| 384 | ] |
| 385 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 386 | # Undated arrive first in CLI order; dated in-window must still win the cap. |
| 387 | monkeypatch.setattr(techmeme, "_run_cli", |
| 388 | lambda cmd, timeout: {"results": undated + dated}) |
| 389 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27", depth="quick") |
| 390 | links = [r["link"] for r in out["results"]] |
| 391 | assert links == [f"https://t.co/d{i}" for i in range(cap)] |
| 392 | |
| 393 | |
| 394 | def test_all_undated_flood_capped_with_windowing_inactive_hint(monkeypatch): |
| 395 | """The realistic old-binary case: every record undated. The cap still |
| 396 | applies, and a windowing-inactive hint is logged.""" |
| 397 | cap = techmeme.DEPTH_CONFIG["quick"] |
| 398 | logs = [] |
| 399 | records = [ |
| 400 | {"num": i, "source": "x.com", "headline": f"Undated archive headline number {i}", |
| 401 | "link": f"https://t.co/{i}"} |
| 402 | for i in range(cap + 12) |
| 403 | ] |
| 404 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 405 | monkeypatch.setattr(techmeme, "_log", lambda msg: logs.append(msg)) |
| 406 | monkeypatch.setattr(techmeme, "_run_cli", lambda cmd, timeout: {"results": records}) |
| 407 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27", depth="quick") |
| 408 | assert len(out["results"]) == cap |
| 409 | assert any("date windowing inactive" in m for m in logs) |
| 410 | |
| 411 | |
| 412 | def test_windowing_inactive_hint_absent_when_dates_present(monkeypatch): |
| 413 | """The hint must not fire when the binary emits dates -- even if every |
| 414 | dated record is out of window (windowing IS active there).""" |
| 415 | logs = [] |
| 416 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 417 | monkeypatch.setattr(techmeme, "_log", lambda msg: logs.append(msg)) |
| 418 | monkeypatch.setattr(techmeme, "_run_cli", lambda cmd, timeout: {"results": [ |
| 419 | {"num": 1, "source": "a.com", "headline": "Stale but properly dated archive story", |
| 420 | "link": "https://t.co/old", "date": "2022-12-02"}, |
| 421 | {"num": 2, "source": "b.com", "headline": "Undated companion record from mixed output", |
| 422 | "link": "https://t.co/mixed"}, |
| 423 | ]}) |
| 424 | techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27") |
| 425 | assert not any("date windowing inactive" in m for m in logs) |
| 426 | |
| 427 | |
| 428 | @pytest.mark.parametrize("words,expected", [(3, False), (4, True)]) |
| 429 | def test_story_headline_word_count_boundary(words, expected): |
| 430 | headline = " ".join(["word"] * words) |
| 431 | assert techmeme._is_story_headline(headline, "x.com") is expected |
| 432 | |
| 433 | |
| 434 | def test_story_headline_rejects_when_equal_to_source(): |
| 435 | # A >=4-word headline that exactly equals its source is still a header row. |
| 436 | assert not techmeme._is_story_headline("the daily example tribune", "the daily example tribune") |
| 437 | |
| 438 | |
| 439 | def test_binary_absent_returns_empty(monkeypatch): |
| 440 | monkeypatch.setattr(techmeme.shutil, "which", lambda _bin: None) |
| 441 | resp = techmeme.search_techmeme("anything", "2026-06-01", "2026-06-27") |
| 442 | assert resp["results"] == [] |
| 443 | assert "error" in resp |
| 444 | |
| 445 | |
| 446 | def test_empty_topic_returns_empty(): |
| 447 | assert techmeme.search_techmeme(" ", "2026-06-01", "2026-06-27") == {"results": []} |
| 448 | |
| 449 | |
| 450 | def test_parse_handles_non_list_results(): |
| 451 | assert techmeme.parse_techmeme_response({"results": "oops"}, query="x") == [] |
| 452 | assert techmeme.parse_techmeme_response({}, query="x") == [] |
| 453 | |
| 454 | |
| 455 | # ---- pagination past the first 10 hits (#1449) ---- |
| 456 | |
| 457 | @pytest.mark.parametrize("depth, pages", [("quick", 1), ("default", 2), ("deep", 3)]) |
| 458 | def test_pages_fill_the_depth_cap(monkeypatch, depth, pages): |
| 459 | calls = [] |
| 460 | |
| 461 | def fake_run(cmd, timeout, **_kwargs): |
| 462 | calls.append((list(cmd), timeout)) |
| 463 | return _FakeProc(stdout="[]") |
| 464 | |
| 465 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 466 | monkeypatch.setattr(techmeme.subproc, "run_with_timeout", fake_run) |
| 467 | techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27", depth=depth) |
| 468 | |
| 469 | (cmd, timeout), = calls |
| 470 | if pages == 1: |
| 471 | assert "--pages" not in cmd |
| 472 | else: |
| 473 | idx = cmd.index("--pages") |
| 474 | assert cmd[idx:idx + 2] == ["--pages", str(pages)] |
| 475 | assert idx < cmd.index("--") |
| 476 | assert timeout == techmeme.SEARCH_TIMEOUT + techmeme.SEARCH_PAGE_TIMEOUT * (pages - 1) |
| 477 | |
| 478 | |
| 479 | def test_old_helper_without_pages_falls_back_to_one_page(monkeypatch): |
| 480 | calls = [] |
| 481 | record = {"num": 1, "source": "a.com", |
| 482 | "headline": "A real in-window headline about the topic", |
| 483 | "link": "https://t.co/a", "date": "2026-06-15"} |
| 484 | |
| 485 | def fake_run(cmd, timeout, **_kwargs): |
| 486 | calls.append(list(cmd)) |
| 487 | if "--pages" in cmd: |
| 488 | return _FakeProc(stdout="", returncode=1, |
| 489 | stderr="Error: unknown flag: --pages\nunknown flag: --pages\n") |
| 490 | return _FakeProc(stdout=json.dumps([record])) |
| 491 | |
| 492 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 493 | monkeypatch.setattr(techmeme.subproc, "run_with_timeout", fake_run) |
| 494 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27") |
| 495 | |
| 496 | assert calls == [ |
| 497 | [techmeme.CLI_BIN, "search", "--json", "--pages", "2", "--", "topic date:[6/1/26 TO 6/27/26]"], |
| 498 | [techmeme.CLI_BIN, "search", "--json", "--", "topic date:[6/1/26 TO 6/27/26]"], |
| 499 | ] |
| 500 | assert out.get("error") is None |
| 501 | assert [r["link"] for r in out["results"]] == ["https://t.co/a"] |
| 502 | |
| 503 | |
| 504 | def test_other_helper_errors_are_not_retried(monkeypatch): |
| 505 | calls = [] |
| 506 | |
| 507 | def fake_run(cmd, timeout, **_kwargs): |
| 508 | calls.append(list(cmd)) |
| 509 | return _FakeProc(stdout="", returncode=1, stderr="Error: rate limited\n") |
| 510 | |
| 511 | monkeypatch.setattr(techmeme, "_is_available", lambda: True) |
| 512 | monkeypatch.setattr(techmeme.subproc, "run_with_timeout", fake_run) |
| 513 | out = techmeme.search_techmeme("topic", "2026-06-01", "2026-06-27") |
| 514 | |
| 515 | assert len(calls) == 1 |
| 516 | assert out["error"] == "Error: rate limited" |
| 517 |