| 1 | """Tests for scripts/lib/fanout.run_competitor_fanout.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import io |
| 6 | import threading |
| 7 | import time |
| 8 | import unittest |
| 9 | from contextlib import redirect_stderr |
| 10 | from unittest import mock |
| 11 | |
| 12 | from lib import fanout |
| 13 | |
| 14 | |
| 15 | def _fake_report(topic: str): |
| 16 | """Build a lightweight Report stand-in. Tests only check identity.""" |
| 17 | class _R: |
| 18 | pass |
| 19 | |
| 20 | r = _R() |
| 21 | r.topic = topic |
| 22 | return r |
| 23 | |
| 24 | |
| 25 | class FanoutOrchestratorTests(unittest.TestCase): |
| 26 | def test_main_plus_two_competitors_all_succeed(self): |
| 27 | def main_runner(): |
| 28 | return _fake_report("OpenAI") |
| 29 | |
| 30 | def comp_runner(entity): |
| 31 | return _fake_report(entity) |
| 32 | |
| 33 | err = io.StringIO() |
| 34 | with redirect_stderr(err): |
| 35 | results = fanout.run_competitor_fanout( |
| 36 | main_topic="OpenAI", |
| 37 | main_runner=main_runner, |
| 38 | competitors=["Anthropic", "xAI"], |
| 39 | competitor_runner=comp_runner, |
| 40 | ) |
| 41 | labels = [label for label, _ in results] |
| 42 | self.assertEqual(labels, ["OpenAI", "Anthropic", "xAI"]) |
| 43 | self.assertEqual(results[0][1].topic, "OpenAI") |
| 44 | self.assertEqual(results[1][1].topic, "Anthropic") |
| 45 | |
| 46 | def test_one_competitor_failure_degrades_gracefully(self): |
| 47 | def main_runner(): |
| 48 | return _fake_report("OpenAI") |
| 49 | |
| 50 | def comp_runner(entity): |
| 51 | if entity == "BrokenCo": |
| 52 | raise RuntimeError("upstream offline") |
| 53 | return _fake_report(entity) |
| 54 | |
| 55 | err = io.StringIO() |
| 56 | with redirect_stderr(err): |
| 57 | results = fanout.run_competitor_fanout( |
| 58 | main_topic="OpenAI", |
| 59 | main_runner=main_runner, |
| 60 | competitors=["Anthropic", "BrokenCo", "xAI"], |
| 61 | competitor_runner=comp_runner, |
| 62 | ) |
| 63 | labels = [label for label, _ in results] |
| 64 | self.assertEqual(labels, ["OpenAI", "Anthropic", "xAI"]) |
| 65 | self.assertIn("BrokenCo", err.getvalue()) |
| 66 | self.assertIn("upstream offline", err.getvalue()) |
| 67 | |
| 68 | def test_main_topic_failure_leaves_only_competitors(self): |
| 69 | def main_runner(): |
| 70 | raise RuntimeError("main exploded") |
| 71 | |
| 72 | def comp_runner(entity): |
| 73 | return _fake_report(entity) |
| 74 | |
| 75 | err = io.StringIO() |
| 76 | with redirect_stderr(err): |
| 77 | results = fanout.run_competitor_fanout( |
| 78 | main_topic="OpenAI", |
| 79 | main_runner=main_runner, |
| 80 | competitors=["Anthropic", "xAI"], |
| 81 | competitor_runner=comp_runner, |
| 82 | ) |
| 83 | labels = [label for label, _ in results] |
| 84 | self.assertEqual(labels, ["Anthropic", "xAI"]) |
| 85 | self.assertIn("main exploded", err.getvalue()) |
| 86 | |
| 87 | def test_empty_competitor_list_runs_only_main(self): |
| 88 | def main_runner(): |
| 89 | return _fake_report("OpenAI") |
| 90 | |
| 91 | def comp_runner(_entity): |
| 92 | raise AssertionError("should not be called when competitors=[]") |
| 93 | |
| 94 | err = io.StringIO() |
| 95 | with redirect_stderr(err): |
| 96 | results = fanout.run_competitor_fanout( |
| 97 | main_topic="OpenAI", |
| 98 | main_runner=main_runner, |
| 99 | competitors=[], |
| 100 | competitor_runner=comp_runner, |
| 101 | ) |
| 102 | self.assertEqual([label for label, _ in results], ["OpenAI"]) |
| 103 | |
| 104 | def test_fanout_clears_youtube_search_cache_before_parallel_work(self): |
| 105 | """Comparison mode must not inherit a prior run's ytsearch cache.""" |
| 106 | from lib import youtube_yt |
| 107 | |
| 108 | stale_key = ("prior", 8, "2026-01-01") |
| 109 | youtube_yt._search_cache[stale_key] = {"items": []} |
| 110 | observed = {} |
| 111 | barrier = threading.Barrier(2, timeout=3) |
| 112 | |
| 113 | def runner(topic): |
| 114 | observed[topic] = stale_key in youtube_yt._search_cache |
| 115 | barrier.wait() |
| 116 | return _fake_report(topic) |
| 117 | |
| 118 | with mock.patch.object( |
| 119 | youtube_yt, "reset_search_cache", wraps=youtube_yt.reset_search_cache |
| 120 | ) as reset_mock, redirect_stderr(io.StringIO()): |
| 121 | results = fanout.run_competitor_fanout( |
| 122 | main_topic="OpenAI", |
| 123 | main_runner=lambda: runner("OpenAI"), |
| 124 | competitors=["Anthropic"], |
| 125 | competitor_runner=runner, |
| 126 | ) |
| 127 | reset_mock.assert_called_once_with() |
| 128 | self.assertEqual(observed, {"OpenAI": False, "Anthropic": False}) |
| 129 | self.assertEqual([label for label, _ in results], ["OpenAI", "Anthropic"]) |
| 130 | self.assertNotIn(stale_key, youtube_yt._search_cache) |
| 131 | |
| 132 | def test_sub_runs_execute_in_parallel(self): |
| 133 | """Wall clock should be closer to max(latency) than sum(latency).""" |
| 134 | delay = 0.2 |
| 135 | call_count = 3 # main + 2 competitors |
| 136 | |
| 137 | def make_runner(_label): |
| 138 | def runner(): |
| 139 | time.sleep(delay) |
| 140 | return _fake_report(_label) |
| 141 | return runner |
| 142 | |
| 143 | def comp_runner(entity): |
| 144 | return make_runner(entity)() |
| 145 | |
| 146 | start = time.monotonic() |
| 147 | with redirect_stderr(io.StringIO()): |
| 148 | results = fanout.run_competitor_fanout( |
| 149 | main_topic="OpenAI", |
| 150 | main_runner=make_runner("OpenAI"), |
| 151 | competitors=["Anthropic", "xAI"], |
| 152 | competitor_runner=comp_runner, |
| 153 | ) |
| 154 | elapsed = time.monotonic() - start |
| 155 | self.assertEqual(len(results), 3) |
| 156 | # Generous margin: parallel execution should finish well under |
| 157 | # sum(call_count * delay) == 0.6s. We accept anything under 0.5s. |
| 158 | self.assertLess( |
| 159 | elapsed, delay * call_count, |
| 160 | f"Expected parallel execution < {delay * call_count:.2f}s, " |
| 161 | f"got {elapsed:.2f}s (sub-runs likely serialized)", |
| 162 | ) |
| 163 | |
| 164 | def test_all_competitors_fail_leaves_main_only(self): |
| 165 | def main_runner(): |
| 166 | return _fake_report("OpenAI") |
| 167 | |
| 168 | def comp_runner(_entity): |
| 169 | raise RuntimeError("all offline") |
| 170 | |
| 171 | with redirect_stderr(io.StringIO()): |
| 172 | results = fanout.run_competitor_fanout( |
| 173 | main_topic="OpenAI", |
| 174 | main_runner=main_runner, |
| 175 | competitors=["A", "B", "C"], |
| 176 | competitor_runner=comp_runner, |
| 177 | ) |
| 178 | self.assertEqual([label for label, _ in results], ["OpenAI"]) |
| 179 | |
| 180 | if __name__ == "__main__": |
| 181 | unittest.main() |
| 182 |