返回 last30days-skill
test_meta_ads.py
根目录 / tests / test_meta_ads.py
1 """Tests for the Meta Ad Library source.
2
3 Nothing here spawns a subprocess or touches the network. Fixture shapes mirror
4 live ScrapeCreators payloads captured 2026-09-14 (envelope keys, snapshot
5 fields, the searchResults/results asymmetry, null collation ids); brand names
6 are invented.
7 """
8
9 import datetime
10 from unittest import mock
11
12 import pytest
13
14 from lib import meta_ads
15 from lib.meta_ads import (
16 NO_CANDIDATES,
17 RESOLVED,
18 UNRESOLVED,
19 build_item,
20 dedupe_key,
21 extract_promo_code,
22 has_video,
23 launch_date,
24 names_match,
25 resolve_page,
26 search_meta_ads,
27 )
28
29 FROM_DATE = "2026-08-15"
30 TO_DATE = "2026-09-14"
31 TOKEN = "fake-sc-key"
32
33
34 def ad_row(**over):
35 """One ad row in the live shape (trimmed of media blobs)."""
36 base = {
37 "ad_archive_id": "1000000000000001",
38 "collation_id": "col-1",
39 "collation_count": 3,
40 "page_id": "300000000000001",
41 "page_name": "Brightpan",
42 "is_active": True,
43 "start_date_string": "2026-09-01T07:00:00.000Z",
44 "end_date_string": "2026-09-14T07:00:00.000Z",
45 "publisher_platform": ["FACEBOOK", "INSTAGRAM", "THREADS"],
46 "url": "https://www.facebook.com/ads/library/?id=1000000000000001",
47 "reach_estimate": None,
48 "spend": None,
49 "snapshot": {
50 "body": {"text": "Better mornings start here. Shop the Brightpan kettle."},
51 "title": "",
52 "cta_text": "Shop now",
53 "display_format": "DCO",
54 "link_url": "https://brightpan.example/collections/kettles",
55 "cards": [],
56 "videos": [],
57 },
58 }
59 base.update(over)
60 return base
61
62
63 def envelope(rows, key="searchResults", total=None, cursor=""):
64 return {
65 "success": True,
66 "credits_remaining": 999,
67 "credits_charged": 1,
68 key: rows,
69 "searchResultsCount": len(rows) if total is None else total,
70 "cursor": cursor,
71 }
72
73
74 def company_row(page_id, name, likes=100):
75 return {
76 "page_id": page_id,
77 "name": name,
78 "likes": likes,
79 "category": "Appliances",
80 "verification": "NOT_VERIFIED",
81 "ig_username": "",
82 }
83
84
85 class TestNameMatching:
86 def test_exact_brand_matches(self):
87 assert names_match("Brightpan", "Brightpan")
88
89 def test_product_page_matches_umbrella_topic_by_shared_token(self):
90 # The umbrella brand advertises under product-line pages; containment
91 # in either direction is what resolves them.
92 assert names_match("BrightpanCo", "Brightpan Kitchen")
93
94 def test_short_token_never_matches(self):
95 # "ai" is below the four-character floor, so an unrelated advertiser
96 # that shares only that word must not match.
97 assert not names_match("Vantage AI", "Jasper AI")
98
99 def test_no_synonym_expansion(self):
100 # The shared scoring tokenizer expands "ai" to artificial/intelligence.
101 # Matching must not, or this pair would resolve as the same brand.
102 assert not names_match("Vantage AI", "Artificial Intelligence Labs")
103
104 def test_unrelated_names_do_not_match(self):
105 assert not names_match("Brightpan", "Coastal Realty Group")
106
107 def test_case_and_spacing_are_normalized(self):
108 assert names_match("Bright iQ", "brightiq")
109
110
111 class TestResolvePage:
112 def test_umbrella_brand_resolves_dominant_product_page(self):
113 rows = (
114 [ad_row(page_id="1", page_name="Brightpan Kitchen") for _ in range(14)]
115 + [ad_row(page_id="2", page_name="Brightpan Beauty") for _ in range(5)]
116 + [ad_row(page_id="3", page_name="Brightpan Home") for _ in range(4)]
117 + [ad_row(page_id="9", page_name="Unrelated Deals Co") for _ in range(20)]
118 )
119 page, runner_ups, top, _strength = resolve_page("BrightpanCo", rows)
120 assert page["name"] == "Brightpan Kitchen"
121 assert page["id"] == "1"
122 assert runner_ups == ["Brightpan Beauty", "Brightpan Home"]
123 assert top == ""
124
125 def test_exact_name_beats_busier_partial_match(self):
126 # A reseller running more ads than the brand must not claim the topic.
127 rows = [ad_row(page_id="1", page_name="Brightpan") for _ in range(11)] + [
128 ad_row(page_id="2", page_name="Brightpan Outlet") for _ in range(18)
129 ]
130 page, _runner_ups, _top, strength = resolve_page("Brightpan", rows)
131 assert page["name"] == "Brightpan"
132 assert page["id"] == "1"
133 assert strength == meta_ads.MATCH_EXACT
134
135 def test_no_match_returns_top_unmatched_candidate(self):
136 rows = [ad_row(page_id="9", page_name="Jasper AI") for _ in range(30)]
137 page, runner_ups, top, _strength = resolve_page("Vantage AI", rows)
138 assert page is None
139 assert runner_ups == []
140 assert top == "Jasper AI"
141
142 def test_no_rows_returns_no_candidate_name(self):
143 page, runner_ups, top, _strength = resolve_page("Brightpan", [])
144 assert page is None
145 assert runner_ups == []
146 assert top == ""
147
148 def test_rows_without_page_id_are_ignored(self):
149 page, _runner_ups, top, _strength = resolve_page(
150 "Brightpan", [ad_row(page_id="", page_name="Brightpan")]
151 )
152 assert page is None
153 assert top == ""
154
155
156 class TestRowFields:
157 def test_launch_date_from_iso_string(self):
158 assert launch_date(ad_row()) == "2026-09-01"
159
160 def test_launch_date_falls_back_to_epoch(self):
161 epoch = int(
162 datetime.datetime(2026, 9, 1, tzinfo=datetime.timezone.utc).timestamp()
163 )
164 row = ad_row(start_date_string="", start_date=epoch)
165 assert launch_date(row) == "2026-09-01"
166
167 def test_launch_date_missing_is_none(self):
168 assert launch_date(ad_row(start_date_string="", start_date=None)) is None
169
170 def test_dedupe_key_prefers_collation(self):
171 assert dedupe_key(ad_row(collation_id="col-9")) == "collation:col-9"
172
173 def test_dedupe_key_falls_back_to_archive_id(self):
174 row = ad_row(collation_id=None, ad_archive_id="777")
175 assert dedupe_key(row) == "ad:777"
176
177 def test_has_video_detects_snapshot_videos(self):
178 row = ad_row()
179 row["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/v.mp4"}]
180 assert has_video(row)
181
182 def test_has_video_detects_card_video(self):
183 row = ad_row()
184 row["snapshot"]["cards"] = [{"video_sd_url": "https://cdn.example/v.mp4"}]
185 assert has_video(row)
186
187 def test_has_video_false_for_image_ad(self):
188 assert not has_video(ad_row())
189
190 def test_promo_code_found_after_code_keyword(self):
191 assert extract_promo_code("Take 30% off sitewide. Use code SUMMER30.") == "SUMMER30"
192
193 def test_promo_code_ignores_model_numbers(self):
194 assert extract_promo_code("The Brightpan E-325 is here.") is None
195
196 def test_promo_code_ignores_lowercase_words_after_code(self):
197 assert extract_promo_code("Our code of conduct is published.") is None
198
199 def test_build_item_carries_every_rendered_field(self):
200 item = build_item(ad_row(), {"id": "300000000000001", "name": "Brightpan"})
201 assert item["title"].startswith("Better mornings")
202 assert item["date"] == "2026-09-01"
203 assert item["cta"] == "Shop now"
204 assert item["display_format"] == "DCO"
205 assert item["landing_url"] == "https://brightpan.example/collections/kettles"
206 assert item["placements"] == ["FACEBOOK", "INSTAGRAM", "THREADS"]
207 assert item["variants"] == 3
208 assert item["advertiser"] == "Brightpan"
209 assert item["url"].endswith("id=1000000000000001")
210
211 def test_build_item_uses_card_link_when_snapshot_link_absent(self):
212 row = ad_row()
213 row["snapshot"]["link_url"] = ""
214 row["snapshot"]["cards"] = [{"link_url": "https://brightpan.example/p/1"}]
215 item = build_item(row, {"id": "1", "name": "Brightpan"})
216 assert item["landing_url"] == "https://brightpan.example/p/1"
217
218 def test_build_item_synthesizes_permalink_when_url_missing(self):
219 item = build_item(ad_row(url=""), {"id": "1", "name": "Brightpan"})
220 assert item["url"] == "https://www.facebook.com/ads/library/?id=1000000000000001"
221
222
223 class TestSearchMetaAds:
224 def _run(self, responses, **kwargs):
225 """Drive the lane with a scripted sequence of http.get returns."""
226 calls = []
227
228 def fake_get(url, **call_kwargs):
229 calls.append((url, call_kwargs.get("params") or {}))
230 nxt = responses.pop(0)
231 if isinstance(nxt, Exception):
232 raise nxt
233 return nxt
234
235 with mock.patch("lib.meta_ads.http.get", side_effect=fake_get):
236 result = search_meta_ads(
237 kwargs.pop("topic", "Brightpan"),
238 FROM_DATE,
239 TO_DATE,
240 token=kwargs.pop("token", TOKEN),
241 **kwargs,
242 )
243 return result, calls
244
245 def test_missing_token_makes_no_calls(self):
246 with mock.patch("lib.meta_ads.http.get") as get:
247 result = search_meta_ads("Brightpan", FROM_DATE, TO_DATE, token="")
248 get.assert_not_called()
249 assert result["ads"] == []
250 assert result["page"] is None
251
252 def test_happy_path_resolves_and_classifies(self):
253 discovery = envelope([ad_row() for _ in range(11)])
254 window = envelope(
255 [
256 ad_row(ad_archive_id="1", collation_id="a", start_date_string="2026-09-01"),
257 ad_row(ad_archive_id="2", collation_id="b", start_date_string="2026-08-20"),
258 ad_row(ad_archive_id="3", collation_id="c", start_date_string="2026-04-03"),
259 ],
260 key="results",
261 )
262 result, calls = self._run([discovery, window])
263 assert result["page"]["name"] == "Brightpan"
264 assert [item["date"] for item in result["ads"]] == ["2026-09-01", "2026-08-20"]
265 tally = result["tally"]
266 assert tally["resolution"] == RESOLVED
267 assert tally["launched_in_window"] == 2
268 assert tally["still_running"] == 1
269 assert tally["promo_codes"] == []
270 assert tally["placements"] == ["FACEBOOK", "INSTAGRAM", "THREADS"]
271 assert calls[0][0] == meta_ads.SEARCH_ADS_URL
272 assert calls[1][0] == meta_ads.COMPANY_ADS_URL
273
274 def test_window_fetch_requests_all_statuses(self):
275 # Without this the endpoint's ACTIVE default hides creatives that
276 # launched inside the window and already ended.
277 result, calls = self._run(
278 [envelope([ad_row()]), envelope([ad_row()], key="results")]
279 )
280 assert calls[1][1]["status"] == meta_ads.ENRICHMENT_STATUS
281 assert calls[1][1]["start_date"] == FROM_DATE
282 assert calls[1][1]["end_date"] == TO_DATE
283 assert result["tally"]["launched_in_window"] == 1
284
285 def test_ended_in_window_creative_is_kept_and_flagged(self):
286 window = envelope(
287 [ad_row(is_active=False, end_date_string="2026-09-05T07:00:00.000Z")],
288 key="results",
289 )
290 result, _calls = self._run([envelope([ad_row()]), window])
291 assert len(result["ads"]) == 1
292 assert result["ads"][0]["is_active"] is False
293 assert result["ads"][0]["ended_on"] == "2026-09-05"
294
295 def test_company_search_fallback_resolves(self):
296 discovery = envelope([ad_row(page_id="9", page_name="Unrelated Deals Co")])
297 companies = envelope(
298 [company_row("55", "Brightpan"), company_row("56", "Other Co")],
299 key="results",
300 )
301 window = envelope([ad_row()], key="results")
302 result, calls = self._run([discovery, companies, window])
303 assert result["page"]["id"] == "55"
304 assert calls[1][0] == meta_ads.SEARCH_COMPANIES_URL
305 assert calls[2][1]["pageId"] == "55"
306
307 def test_unresolved_names_top_candidate_and_skips_enrichment(self):
308 discovery = envelope(
309 [ad_row(page_id="9", page_name="Jasper AI") for _ in range(30)]
310 )
311 companies = envelope([company_row("7", "Jasper AI")], key="results")
312 result, calls = self._run([discovery, companies], topic="Vantage AI")
313 assert result["ads"] == []
314 assert result["page"] is None
315 assert result["tally"]["resolution"] == UNRESOLVED
316 assert result["tally"]["top_candidate"] == "Jasper AI"
317 assert all(call[0] != meta_ads.COMPANY_ADS_URL for call in calls)
318
319 def test_zero_candidates_is_its_own_state(self):
320 result, calls = self._run(
321 [envelope([]), envelope([], key="results")], topic="Nonexistent Brand"
322 )
323 assert result["tally"]["resolution"] == NO_CANDIDATES
324 assert result["tally"]["top_candidate"] == ""
325 assert len(calls) == 2
326
327 def test_page_override_skips_resolution(self):
328 window = envelope([ad_row()], key="results")
329 result, calls = self._run([window], page_override="300000000000001")
330 assert len(calls) == 1
331 assert calls[0][0] == meta_ads.COMPANY_ADS_URL
332 assert result["page"]["id"] == "300000000000001"
333
334 def test_variants_of_one_creative_collapse(self):
335 window = envelope(
336 [
337 ad_row(ad_archive_id="1", collation_id="same", collation_count=4),
338 ad_row(ad_archive_id="2", collation_id="same", collation_count=4),
339 ],
340 key="results",
341 )
342 result, _calls = self._run([envelope([ad_row()]), window])
343 assert len(result["ads"]) == 1
344 assert result["ads"][0]["variants"] == 4
345
346 def test_null_collation_ids_stay_distinct(self):
347 window = envelope(
348 [
349 ad_row(ad_archive_id="1", collation_id=None),
350 ad_row(ad_archive_id="2", collation_id=None),
351 ],
352 key="results",
353 )
354 result, _calls = self._run([envelope([ad_row()]), window])
355 assert len(result["ads"]) == 2
356
357 def test_pagination_stops_at_depth_cap_and_flags_more(self):
358 discovery = envelope([ad_row()])
359 pages = [
360 envelope([ad_row(ad_archive_id=str(i), collation_id=f"c{i}")],
361 key="results", total=222, cursor=f"cur{i}")
362 for i in range(5)
363 ]
364 result, calls = self._run([discovery] + pages, depth="default")
365 window_calls = [c for c in calls if c[0] == meta_ads.COMPANY_ADS_URL]
366 assert len(window_calls) == 2 # default depth cap
367 assert result["tally"]["cursor_remaining"] is True
368 assert result["tally"]["endpoint_total"] == 222
369
370 def test_pagination_stops_when_cursor_repeats(self):
371 discovery = envelope([ad_row()])
372 repeated = envelope(
373 [ad_row(ad_archive_id="1", collation_id="c1")], key="results", cursor="same"
374 )
375 result, calls = self._run([discovery, repeated, repeated, repeated], depth="deep")
376 window_calls = [c for c in calls if c[0] == meta_ads.COMPANY_ADS_URL]
377 assert len(window_calls) == 2
378 assert result["tally"]["cursor_remaining"] is False
379
380 def test_pagination_stops_on_empty_cursor(self):
381 discovery = envelope([ad_row()])
382 window = envelope([ad_row()], key="results", cursor="")
383 _result, calls = self._run([discovery, window], depth="deep")
384 assert len([c for c in calls if c[0] == meta_ads.COMPANY_ADS_URL]) == 1
385
386 @pytest.mark.parametrize("status", [401, 402, 403, 429])
387 def test_fatal_status_stops_the_lane(self, status):
388 err = meta_ads.http.HTTPError(f"boom {status}", status_code=status)
389 result, calls = self._run([err])
390 assert len(calls) == 1
391 assert result["ads"] == []
392 assert str(status) in result["error"]
393
394 def test_fatal_status_during_enrichment_reports_error(self):
395 err = meta_ads.http.HTTPError("rate limited", status_code=429)
396 result, calls = self._run([envelope([ad_row()]), err])
397 assert len(calls) == 2
398 assert "429" in result["error"]
399 assert result["ads"] == []
400
401 def test_empty_success_response_is_not_an_error(self):
402 result, _calls = self._run([envelope([]), envelope([], key="results")])
403 assert "error" not in result
404 assert result["tally"]["resolution"] == NO_CANDIDATES
405
406 def test_transcripts_cover_newest_videos_across_pages(self):
407 discovery = envelope([ad_row()])
408 older_video = ad_row(
409 ad_archive_id="old", collation_id="old", start_date_string="2026-08-20"
410 )
411 older_video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/a.mp4"}]
412 newer_video = ad_row(
413 ad_archive_id="new", collation_id="new", start_date_string="2026-09-10"
414 )
415 newer_video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/b.mp4"}]
416 page_one = envelope([older_video], key="results", cursor="c1")
417 page_two = envelope([newer_video], key="results", cursor="")
418 transcript = {"transcript_available": True, "transcript": "Say what you need to say."}
419 result, calls = self._run(
420 [discovery, page_one, page_two, transcript, transcript], depth="default"
421 )
422 transcript_calls = [c for c in calls if c[0] == meta_ads.AD_TRANSCRIPT_URL]
423 # Newest first: the page-two creative is transcribed before the older one.
424 assert transcript_calls[0][1]["id"] == "new"
425 assert result["tally"]["transcribed"] == 2
426 assert result["ads"][0]["transcript"].startswith("Say what")
427
428 def test_unavailable_transcript_is_not_an_error(self):
429 discovery = envelope([ad_row()])
430 video = ad_row()
431 video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/a.mp4"}]
432 window = envelope([video], key="results")
433 result, _calls = self._run(
434 [discovery, window, {"transcript_available": False, "transcript": None}]
435 )
436 assert "error" not in result
437 assert result["tally"]["transcribed"] == 0
438 assert result["ads"][0]["transcript"] == ""
439
440 def test_quick_depth_pulls_no_transcripts(self):
441 discovery = envelope([ad_row()])
442 video = ad_row()
443 video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/a.mp4"}]
444 _result, calls = self._run(
445 [discovery, envelope([video], key="results")], depth="quick"
446 )
447 assert all(c[0] != meta_ads.AD_TRANSCRIPT_URL for c in calls)
448
449 def test_rate_limit_retries_are_disabled_on_every_call(self):
450 # The shared client retries a 429 twice by default, which would both
451 # contradict the no-further-calls contract and burn the lane budget.
452 video = ad_row()
453 video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/a.mp4"}]
454 responses = [
455 envelope([]),
456 envelope([company_row("300000000000001", "Brightpan")], key="results"),
457 envelope([video], key="results", cursor="next"),
458 envelope([ad_row(ad_archive_id="second", collation_id="second")], key="results"),
459 {"transcript_available": True, "transcript": "The kettle makes better mornings."},
460 ]
461 with mock.patch("lib.meta_ads.http.get", side_effect=responses) as get:
462 result = search_meta_ads("Brightpan", FROM_DATE, TO_DATE, token=TOKEN)
463 assert [call.args[0] for call in get.call_args_list] == [
464 meta_ads.SEARCH_ADS_URL,
465 meta_ads.SEARCH_COMPANIES_URL,
466 meta_ads.COMPANY_ADS_URL,
467 meta_ads.COMPANY_ADS_URL,
468 meta_ads.AD_TRANSCRIPT_URL,
469 ]
470 assert {ad["id"] for ad in result["ads"]} == {"1000000000000001", "second"}
471 assert result["tally"]["transcribed"] == 1
472 deadline = get.call_args_list[0].kwargs["deadline_monotonic"]
473 assert deadline is not None
474 for call in get.call_args_list:
475 assert call.kwargs["max_429_retries"] == 0, call.args[0]
476 assert call.kwargs["deadline_monotonic"] == deadline, call.args[0]
477
478 def test_exhausted_budget_keeps_what_was_fetched_and_reports_partial(self):
479 # Running out of time must not discard creatives already in hand: thin
480 # coverage caused by our own clock would otherwise read as a finding
481 # about how little the advertiser is running.
482 discovery = envelope([ad_row()])
483 window = envelope([ad_row()], key="results", cursor="more")
484 ticks = {"n": 0}
485 real_monotonic = meta_ads.time.monotonic
486
487 def creeping_clock():
488 ticks["n"] += 1
489 # Jump past the lane budget once the first window page is in.
490 return real_monotonic() + (0 if ticks["n"] < 6 else 10_000)
491
492 with mock.patch("lib.meta_ads.time.monotonic", side_effect=creeping_clock):
493 with mock.patch("lib.meta_ads.http.get", side_effect=[discovery, window]):
494 result = search_meta_ads(
495 "Brightpan", FROM_DATE, TO_DATE, token=TOKEN, depth="deep"
496 )
497 assert result.get("partial") is True
498 assert "budget" in result["error"]
499 assert len(result["ads"]) == 1
500 assert result["page"]["name"] == "Brightpan"
501 assert result["tally"]["cursor_remaining"] is True
502
503 def test_country_override_is_passed_through(self):
504 _result, calls = self._run(
505 [envelope([ad_row()]), envelope([ad_row()], key="results")], country="GB"
506 )
507 assert calls[0][1]["country"] == "GB"
508 assert calls[1][1]["country"] == "GB"
509
510
511 class TestWindowBoundaries:
512 def test_creative_launched_on_window_edges_is_included(self):
513 window = envelope(
514 [
515 ad_row(ad_archive_id="1", collation_id="a", start_date_string=FROM_DATE),
516 ad_row(ad_archive_id="2", collation_id="b", start_date_string=TO_DATE),
517 ],
518 key="results",
519 )
520 with mock.patch(
521 "lib.meta_ads.http.get", side_effect=[envelope([ad_row()]), window]
522 ):
523 result = search_meta_ads("Brightpan", FROM_DATE, TO_DATE, token=TOKEN)
524 assert result["tally"]["launched_in_window"] == 2
525
526 def test_undated_creative_counts_as_still_running(self):
527 window = envelope(
528 [ad_row(start_date_string="", start_date=None)], key="results"
529 )
530 with mock.patch(
531 "lib.meta_ads.http.get", side_effect=[envelope([ad_row()]), window]
532 ):
533 result = search_meta_ads("Brightpan", FROM_DATE, TO_DATE, token=TOKEN)
534 assert result["ads"] == []
535 assert result["tally"]["still_running"] == 1
536
537
538 class TestTransientVersusFatal:
539 """R11: only credential/account statuses discard the lane's work."""
540
541 def _drive(self, responses, depth="default"):
542 def fake_get(url, **kwargs):
543 nxt = responses.pop(0)
544 if isinstance(nxt, Exception):
545 raise nxt
546 return nxt
547
548 with mock.patch("lib.meta_ads.http.get", side_effect=fake_get):
549 return search_meta_ads(
550 "Brightpan", FROM_DATE, TO_DATE, token=TOKEN, depth=depth
551 )
552
553 def test_transient_error_mid_pagination_keeps_fetched_creatives(self):
554 # A 500 on page two must not throw away page one: those creatives were
555 # already paid for and are real evidence.
556 page_one = envelope([ad_row()], key="results", cursor="more")
557 boom = meta_ads.http.HTTPError("upstream hiccup", status_code=500)
558 result = self._drive([envelope([ad_row()]), page_one, boom], depth="deep")
559 assert len(result["ads"]) == 1
560 assert result["page"]["name"] == "Brightpan"
561 assert result.get("partial") is True
562 assert "500" in result["error"]
563
564 def test_transient_error_during_transcripts_keeps_creatives(self):
565 video = ad_row()
566 video["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/a.mp4"}]
567 boom = meta_ads.http.HTTPError("transcoder down", status_code=503)
568 result = self._drive(
569 [envelope([ad_row()]), envelope([video], key="results"), boom]
570 )
571 assert len(result["ads"]) == 1
572 assert result.get("partial") is True
573
574 def test_fatal_status_mid_enrichment_keeps_paid_creatives(self):
575 # A rate limit or expired credential means "stop calling", not "the
576 # pages that already returned 200 were wrong". Discarding them throws
577 # away evidence the user already paid for.
578 page_one = envelope([ad_row()], key="results", cursor="more")
579 boom = meta_ads.http.HTTPError("rate limited", status_code=429)
580 result = self._drive([envelope([ad_row()]), page_one, boom], depth="deep")
581 assert len(result["ads"]) == 1
582 assert result["page"]["name"] == "Brightpan"
583 assert result.get("partial") is True
584 assert "429" in result["error"]
585
586 def test_fatal_status_during_discovery_has_nothing_to_keep(self):
587 boom = meta_ads.http.HTTPError("rate limited", status_code=429)
588 result = self._drive([boom])
589 assert result["ads"] == []
590 assert result["page"] is None
591 assert "429" in result["error"]
592
593
594 class TestShortBrandNames:
595 """A brand whose every word is under the token floor must still resolve.
596
597 The floor exists to stop a shared short word ("AI") from matching every
598 advertiser that carries it. Applied without an exact-identity escape it
599 also makes an initialism brand unresolvable against its own page, which
600 fails the feature silently for a whole class of well-known names.
601 """
602
603 def test_initialism_brand_matches_its_own_page_exactly(self):
604 assert names_match("KLM", "KLM")
605 assert names_match("BMW", "bmw")
606
607 def test_initialism_brand_still_rejects_an_unrelated_page(self):
608 assert not names_match("KLM", "Kitchen Lighting Market")
609
610 def test_initialism_resolves_through_the_exact_tier(self):
611 rows = [ad_row(page_id="1", page_name="KLM") for _ in range(2)] + [
612 ad_row(page_id="2", page_name="Unrelated Deals Co") for _ in range(30)
613 ]
614 page, _runner_ups, _top, strength = resolve_page("KLM", rows)
615 assert page["name"] == "KLM"
616 assert strength == meta_ads.MATCH_EXACT
617
618
619 class TestResolutionStrength:
620 def test_shared_whole_token_outranks_mere_containment(self):
621 # "Brightpan Kitchen" shares the whole token; the containment-only
622 # page must not win on ad volume alone.
623 rows = [ad_row(page_id="1", page_name="Brightpan Kitchen") for _ in range(2)] + [
624 ad_row(page_id="2", page_name="Superbrightpanel Co") for _ in range(40)
625 ]
626 page, _runner_ups, _top, strength = resolve_page("Brightpan", rows)
627 assert page["name"] == "Brightpan Kitchen"
628 assert strength == meta_ads.MATCH_TOKEN
629
630 def test_containment_tier_is_reported_as_such(self):
631 rows = [ad_row(page_id="1", page_name="Brightpan Kitchen") for _ in range(3)]
632 _page, _runner_ups, _top, strength = resolve_page("BrightpanCo", rows)
633 assert strength == meta_ads.MATCH_CONTAINED
634
635 def test_transcript_cap_bounds_paid_requests_not_successes(self):
636 # Every transcript call costs a credit whether or not one comes back,
637 # so an upstream with no transcripts must not keep the lane calling.
638 videos = []
639 for i in range(10):
640 row = ad_row(ad_archive_id=str(i), collation_id=f"c{i}")
641 row["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/v.mp4"}]
642 videos.append(row)
643 unavailable = {"transcript_available": False, "transcript": None}
644 responses = [envelope([ad_row()]), envelope(videos, key="results")] + [
645 unavailable
646 ] * 10
647 calls = []
648
649 def fake_get(url, **kwargs):
650 calls.append(url)
651 return responses.pop(0)
652
653 with mock.patch("lib.meta_ads.http.get", side_effect=fake_get):
654 result = search_meta_ads(
655 "Brightpan", FROM_DATE, TO_DATE, token=TOKEN, depth="default"
656 )
657 transcript_calls = [c for c in calls if c == meta_ads.AD_TRANSCRIPT_URL]
658 assert len(transcript_calls) == 3 # the default-depth cap, not 10
659 assert result["tally"]["transcribed"] == 0
660
661 def test_default_depth_run_stays_inside_its_documented_credit_ceiling(self):
662 videos = []
663 for i in range(10):
664 row = ad_row(ad_archive_id=str(i), collation_id=f"c{i}")
665 row["snapshot"]["videos"] = [{"video_hd_url": "https://cdn.example/v.mp4"}]
666 videos.append(row)
667 responses = [
668 envelope([ad_row(page_id="9", page_name="Unrelated Deals Co")]),
669 envelope([company_row("55", "Brightpan")], key="results"),
670 envelope(videos, key="results", cursor="more"),
671 envelope(videos, key="results", cursor="more"),
672 ] + [{"transcript_available": True, "transcript": "hello"}] * 5
673 calls = []
674
675 def fake_get(url, **kwargs):
676 calls.append(url)
677 return responses.pop(0)
678
679 with mock.patch("lib.meta_ads.http.get", side_effect=fake_get):
680 search_meta_ads("Brightpan", FROM_DATE, TO_DATE, token=TOKEN, depth="default")
681 # 1 discovery + 1 company fallback + 2 pages + 3 transcripts = 7
682 assert len(calls) <= 7
683
684
685 class TestGenericTokens:
686 """A word several advertisers share is a category, not an identity."""
687
688 def test_category_word_does_not_resolve_an_unrelated_advertiser(self):
689 rows = (
690 [ad_row(page_id="1", page_name="Acme Kitchen") for _ in range(3)]
691 + [ad_row(page_id="2", page_name="Kitchen World") for _ in range(40)]
692 + [ad_row(page_id="3", page_name="Kitchen Depot") for _ in range(20)]
693 )
694 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
695 assert page["name"] == "Acme Kitchen"
696
697 def test_a_word_only_one_advertiser_uses_still_identifies(self):
698 rows = [ad_row(page_id="1", page_name="Brightpan Supply") for _ in range(2)] + [
699 ad_row(page_id="2", page_name="Unrelated Deals Co") for _ in range(40)
700 ]
701 page, _runner_ups, _top, strength = resolve_page("Brightpan Supply news", rows)
702 assert page["name"] == "Brightpan Supply"
703 assert strength == meta_ads.MATCH_TOKEN
704
705 def test_shared_category_alone_resolves_nothing(self):
706 rows = [ad_row(page_id="1", page_name="Kitchen World") for _ in range(30)] + [
707 ad_row(page_id="2", page_name="Kitchen Depot") for _ in range(20)
708 ]
709 page, _runner_ups, top, _strength = resolve_page("Acme Kitchen", rows)
710 assert page is None
711 assert top == "Kitchen World"
712
713
714 class TestHeadTokenIdentity:
715 """A multi-word topic carries its identity in the leading word.
716
717 Counting how many advertisers share a word cannot carry this: a result set
718 holding one lookalike makes its category word look perfectly distinctive.
719 """
720
721 def test_lone_category_lookalike_resolves_nothing(self):
722 rows = [ad_row(page_id="9", page_name="Kitchen World") for _ in range(30)]
723 page, _runner_ups, top, _strength = resolve_page("Acme Kitchen", rows)
724 assert page is None
725 assert top == "Kitchen World"
726
727 def test_brand_page_wins_over_a_busier_category_lookalike(self):
728 rows = [ad_row(page_id="1", page_name="Acme Kitchen") for _ in range(3)] + [
729 ad_row(page_id="9", page_name="Kitchen World") for _ in range(40)
730 ]
731 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
732 assert page["name"] == "Acme Kitchen"
733
734 def test_a_page_sharing_only_the_brand_word_fails_closed(self):
735 # "Acme Supply" may or may not belong to the brand behind "Acme
736 # Kitchen"; the name alone cannot say. Resolving it would risk
737 # attributing another firm's ads, so the lane declines and names the
738 # candidate instead, which the page override recovers in one flag.
739 rows = [ad_row(page_id="1", page_name="Acme Supply") for _ in range(3)]
740 page, _runner_ups, top, _strength = resolve_page("Acme Kitchen", rows)
741 assert page is None
742 assert top == "Acme Supply"
743
744 def test_single_word_topic_keeps_its_umbrella_reach(self):
745 # Nothing to strip, so the whole name is the head and product-line
746 # pages still resolve.
747 rows = [ad_row(page_id="1", page_name="Brightpan Kitchen") for _ in range(14)]
748 page, _runner_ups, _top, _strength = resolve_page("BrightpanCo", rows)
749 assert page["name"] == "Brightpan Kitchen"
750
751
752 class TestDescriptorLeadingTopics:
753 """A research topic does not always open with the brand."""
754
755 def test_intent_modifier_before_the_brand_still_resolves_it(self):
756 rows = [ad_row(page_id="1", page_name="Acme Grills") for _ in range(5)]
757 page, _runner_ups, _top, _strength = resolve_page("best Acme grills", rows)
758 assert page["name"] == "Acme Grills"
759
760 @pytest.mark.parametrize(
761 "topic,page_name",
762 [
763 ("best Acme grills", "Acme Grills"),
764 ("latest Acme cookware", "Acme Cookware"),
765 ("top Acme deals", "Acme Deals"),
766 ],
767 )
768 def test_common_descriptors_do_not_become_the_identity(self, topic, page_name):
769 rows = [ad_row(page_id="1", page_name=page_name) for _ in range(5)]
770 page, _runner_ups, _top, _strength = resolve_page(topic, rows)
771 assert page["name"] == page_name
772
773 def test_a_category_lookalike_is_still_rejected_under_a_descriptor(self):
774 # "grill" appears inside "grills", but it is the category word, not
775 # the brand, so it must not carry the whole topic with it.
776 rows = [ad_row(page_id="9", page_name="Grill World") for _ in range(50)]
777 page, _runner_ups, _top, _strength = resolve_page("best Acme grills", rows)
778 assert page is None
779
780
781 class TestBrandPositionIndependence:
782 """The brand does not always lead the topic."""
783
784 @pytest.mark.parametrize(
785 "topic,page",
786 [
787 ("Kitchen by Crate and Barrel", "Crate and Barrel"),
788 ("smart home Acme", "Acme Home"),
789 ("cookware from Acme Supply", "Acme Supply"),
790 ],
791 )
792 def test_a_topic_that_spells_out_the_name_resolves_it(self, topic, page):
793 # The topic names the company outright, so where in the phrase that
794 # name sits cannot decide the match.
795 rows = [ad_row(page_id="1", page_name=page) for _ in range(6)]
796 resolved, _runner_ups, _top, _strength = resolve_page(topic, rows)
797 assert resolved["name"] == page
798
799 def test_a_lookalike_is_still_rejected_because_the_topic_omits_a_word(self):
800 # "Kitchen World" carries "world", which a topic about "Acme Kitchen"
801 # never mentions, so the topic is not naming this company.
802 rows = [ad_row(page_id="9", page_name="Kitchen World") for _ in range(30)]
803 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
804 assert page is None
805
806
807 class TestPartialNamesCannotClaimATopic:
808 """A fragment of the topic is not an advertiser's whole name."""
809
810 def test_a_page_named_only_the_category_word_is_rejected(self):
811 # Trivially "covered" by the topic, but it accounts for one word of it
812 # and none of the brand.
813 rows = [ad_row(page_id="9", page_name="Kitchen") for _ in range(30)]
814 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
815 assert page is None
816
817 def test_a_page_named_the_whole_single_word_topic_still_resolves(self):
818 rows = [ad_row(page_id="1", page_name="Acme Co") for _ in range(4)]
819 page, _runner_ups, _top, _strength = resolve_page("Acme", rows)
820 assert page["name"] == "Acme Co"
821
822 def test_coverage_needs_more_than_one_topic_word(self):
823 rows = [ad_row(page_id="9", page_name="Grills") for _ in range(30)]
824 page, _runner_ups, _top, _strength = resolve_page("best Acme grills", rows)
825 assert page is None
826
827
828 class TestCoverageBeatsVolume:
829 def test_the_page_matching_more_of_the_topic_wins(self):
830 # Two pages share the brand word; the one that also matches the rest
831 # of the topic is the better answer however much the other spends.
832 rows = [ad_row(page_id="1", page_name="Acme Kitchen Co") for _ in range(2)] + [
833 ad_row(page_id="9", page_name="Acme AI") for _ in range(80)
834 ]
835 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
836 assert page["name"] == "Acme Kitchen Co"
837
838
839 class TestWholeNameRequired:
840 """Greptile round 7: a partial name must not stand in for a whole one."""
841
842 def test_a_page_with_a_word_the_topic_never_mentions_is_rejected(self):
843 # "Acme AI" and the brand behind "Acme Kitchen" share one word and
844 # nothing else; "ai" appears nowhere in the topic.
845 rows = [ad_row(page_id="9", page_name="Acme AI") for _ in range(80)]
846 page, _runner_ups, _top, _strength = resolve_page("Acme Kitchen", rows)
847 assert page is None
848
849 def test_short_words_of_the_page_name_are_counted(self):
850 # The page name's short word is part of its identity, so ignoring it
851 # would let the name look fully covered when it is not.
852 rows = [ad_row(page_id="9", page_name="Acme AI") for _ in range(5)]
853 page, _runner_ups, _top, _strength = resolve_page("Acme AI", rows)
854 assert page["name"] == "Acme AI"
855
856 def test_word_boundaries_are_respected_in_coverage(self):
857 # "Cart Wheel" is not named by "Acme Cartwheel" merely because its
858 # letters appear inside a longer word.
859 rows = [ad_row(page_id="9", page_name="Cart Wheel") for _ in range(30)]
860 page, _runner_ups, _top, _strength = resolve_page("Acme Cartwheel", rows)
861 assert page is None
862
862 lines PYTHON