返回 last30days-skill
test_polymarket_regression_set.py
根目录 / tests / test_polymarket_regression_set.py
1 """Regression set for Polymarket entity market selection, on live data.
2
3 ``fixtures/polymarket/regression`` holds live Gamma ``public-search`` events
4 captured on 2026-11-16 for 25 research topics and 3 negative controls.
5 ``events.json`` maps an event key to the event, trimmed to the fields the
6 parser reads (a market that does not name the subject keeps only its price
7 fields). ``topics.json`` lists each topic's events in search order. Events
8 that name nothing for a topic were dropped, and so was each topic's long tail
9 below its tenth-ranked event, except ground-truth events. An event captured at
10 two moments with different prices is stored once per capture (``"<id>.2"``).
11
12 Ground truth was labelled by hand after seeing which markets exist and before
13 scoring: the event-title prefixes a knowledgeable fan would want shown, the
14 first group being the headline question. Hints are what the host planner would
15 pass as ``polymarket_hints``. The negative controls are topics whose saved runs
16 showed only off-topic markets.
17 """
18
19 import json
20 from pathlib import Path
21
22 import pytest
23
24 from lib import polymarket
25
26 FIXTURES = Path(__file__).parent / "fixtures" / "polymarket" / "regression"
27 TO_DATE = "2026-11-16"
28
29 TOP1_THRESHOLD = 22
30 TOP3_THRESHOLD = 23
31
32 _WARRIORS = ["Stephen Curry", "Draymond Green", "Jimmy Butler", "Yaxel Lendeborg", "Jonathan Kuminga"]
33 _GRIZZLIES = ["Ja Morant", "Jaren Jackson Jr.", "Ty Jerome", "Zach Edey", "Santi Aldama"]
34
35 HINTS = {
36 "49ers": {"kind": "team", "aliases": ["San Francisco 49ers"]},
37 "Benfica": {"kind": "team"},
38 "Lakers": {"kind": "team", "aliases": ["Los Angeles Lakers"]},
39 "Real Madrid": {"kind": "team"},
40 "Formula 1": {"aliases": ["F1"]},
41 "Anthropic": {"kind": "company"},
42 "OpenAI": {"kind": "company"},
43 "Nvidia": {"kind": "company"},
44 "Microsoft": {"kind": "company"},
45 "Apple": {"kind": "company"},
46 "Tesla": {"kind": "company"},
47 "Palantir": {"kind": "company"},
48 "Netflix": {"kind": "company"},
49 "Waymo": {"kind": "company"},
50 "Elon Musk": {"kind": "person"},
51 "Jensen Huang": {"kind": "person"},
52 "Taylor Swift": {"kind": "person"},
53 "Kanye West": {"kind": "person"},
54 "Bitcoin": {"kind": "other"},
55 "Claude Code": {"kind": "product", "aliases": ["Claude"]},
56 "Gavin Newsom": {"kind": "person"},
57 "Ukraine": {"kind": "other"},
58 "Iran": {"kind": "other"},
59 "Warriors": {
60 "kind": "team", "aliases": ["Golden State Warriors"],
61 "related": [{"name": name, "relation": "player"} for name in _WARRIORS],
62 },
63 "Memphis Grizzlies": {
64 "kind": "team", "aliases": ["Grizzlies"],
65 "related": [{"name": name, "relation": "player"} for name in _GRIZZLIES],
66 },
67 }
68
69 GROUND_TRUTH = {
70 "49ers": ["Pro Football: 2027 Champion", "Pro Football: 2027 NFC Champion", "Pro Football: NFC West Champion"],
71 "Benfica": ["Primeira Liga: 2027 Champion", "UEFA Europa League: 2027 Champion", "UEFA Europa League: League Phase Winner"],
72 "Lakers": ["NBA: 2027 Champion", "NBA: 2027 Western Conference Champion", "NBA: Team to Make Playoffs"],
73 "Real Madrid": ["UEFA Champions League: 2027 Champion", "LALIGA: 2027 Champion"],
74 "Formula 1": ["Las Vegas Grand Prix: Driver Winner", "F1 Drivers' Champion: 2nd Place"],
75 "Anthropic": ["Anthropic IPO Closing Market Cap", "Which company has best AI model end of 2026", "Will Anthropic’s valuation hit"],
76 "OpenAI": ["OpenAI IPO closing market cap above", "Which company has best AI model end of 2026", "OpenAI IPO by"],
77 "Nvidia": ["Next CEO of NVIDIA", "Largest Company end of December 2026", "Nvidia’s Market Cap end of 2026"],
78 "Microsoft": ["Largest Company end of December 2026", "3rd Largest Company end of December 2026", "2nd Largest Company end of December 2026"],
79 "Apple": ["Largest Company end of December 2026", "Apple’s Market Cap end of 2026"],
80 "Tesla": ["Tesla and SpaceX merger officially announced", "How many Tesla deliveries in Q4 2026", "Will Tesla sell a Cybercab"],
81 "Palantir": ["What will Palantir Technologies Inc. (PLTR) hit in November 2026", "Will Palantir (PLTR) close above ___ end of November"],
82 "Netflix": ["What will Netflix, Inc. (NFLX) hit in November 2026", "What will be the top global Netflix movie this week"],
83 "Waymo": ["How many cities will Waymo operate in", "Which cities will Waymo launch in"],
84 "Elon Musk": ["Elon Musk Net Worth on November 30", "Richest person on December 31, 2026", "Tesla and SpaceX merger officially announced"],
85 "Jensen Huang": ["Jensen Huang rejoins NVIDIA", "Next CEO of NVIDIA", "Where will Jensen Huang work next"],
86 "Taylor Swift": ["Grammys 2027: Album of the Year Winner", "Top Spotify Artist 2026", "Taylor Swift pregnant by"],
87 "Kanye West": ["Will Kanye West and Bianca Censori separate", "Who will be in the 2027 Big Game halftime show"],
88 "Bitcoin": ["What price will Bitcoin hit in 2026", "What price will Bitcoin hit in November"],
89 "Claude Code": ["Next Claude Opus (5.6+) released by", "Claude 6 released by", "Which company has the best Code Arena WebDev AI model end of"],
90 "Gavin Newsom": ["Democratic Presidential Nominee 2028", "Presidential Election Winner 2028"],
91 "Ukraine": ["Russia x Ukraine ceasefire agreement by", "Ukraine signs peace deal with Russia by", "Zelenskyy out as Ukraine president"],
92 "Iran": ["Will the U.S. invade Iran before 2027", "US-Iran Final Nuclear Deal", "Iran leader end of 2026", "US announces end of Iranian blockade"],
93 "Warriors": ["NBA: 2027 Champion", "NBA: 2026-27 Rookie of the Year", "NBA: 2027 Western Conference Champion", "NBA: Team to Make Playoffs"],
94 "Memphis Grizzlies": ["NBA: 2027 Champion", "NBA: 2026-27 Most Improved Player", "NBA: 2027 Western Conference Champion", "NBA: Team to Make Playoffs"],
95 }
96
97 NEGATIVE_CONTROLS = {
98 "June Oven": {"kind": "product"},
99 "Fox One": {"kind": "product"},
100 "Nothing Android": {"kind": "product"},
101 }
102
103 BEST_AI_MODEL = "Which company has best AI model end of 2026"
104 CLAUDE_RELEASES = ("Next Claude Opus (5.6+) released", "Claude 6 released by")
105
106
107 @pytest.fixture(scope="module")
108 def runs():
109 events = json.loads((FIXTURES / "events.json").read_text())
110 topics = json.loads((FIXTURES / "topics.json").read_text())
111 out = {}
112 for topic, hints in {**HINTS, **NEGATIVE_CONTROLS}.items():
113 out[topic] = polymarket.parse_polymarket_response(
114 {"events": [events[key] for key in topics[topic]]},
115 topic, hints=hints, to_date=TO_DATE,
116 )
117 return out
118
119
120 def _is_ground_truth(topic, title):
121 title = title.strip().casefold()
122 return any(title.startswith(prefix.casefold()) for prefix in GROUND_TRUTH[topic])
123
124
125 def _titles(items, n=3):
126 return [item["title"].strip() for item in items[:n]]
127
128
129 def _top(runs, topic, title_prefix, n=3):
130 return next(
131 (item for item in runs[topic][:n] if item["title"].startswith(title_prefix)), None,
132 )
133
134
135 def _misses(runs, k):
136 return {
137 topic: _titles(runs[topic])
138 for topic in GROUND_TRUTH
139 if not any(_is_ground_truth(topic, title) for title in _titles(runs[topic], k))
140 }
141
142
143 def test_every_topic_has_fixture_events():
144 topics = json.loads((FIXTURES / "topics.json").read_text())
145 assert set(topics) == set(HINTS) | set(NEGATIVE_CONTROLS)
146 assert all(topics[topic] for topic in topics)
147
148
149 def test_top_event_matches_ground_truth(runs):
150 misses = _misses(runs, 1)
151 hits = len(GROUND_TRUTH) - len(misses)
152 assert hits >= TOP1_THRESHOLD, f"top-1 {hits}/{len(GROUND_TRUTH)}; misses: {misses}"
153
154
155 def test_ground_truth_in_top_three(runs):
156 misses = _misses(runs, 3)
157 hits = len(GROUND_TRUTH) - len(misses)
158 assert hits >= TOP3_THRESHOLD, f"top-3 {hits}/{len(GROUND_TRUTH)}; misses: {misses}"
159
160
161 def test_49ers_lead_with_the_champion_market(runs):
162 first = runs["49ers"][0]
163 assert first["title"].startswith("Pro Football: 2027 Champion")
164 assert "San Francisco 49ers" in first["headline"]["question"]
165 assert first["matched_market"]["label"] == "San Francisco 49ers"
166
167
168 def test_warriors_top_three_has_title_odds_and_lendeborg(runs):
169 champion = _top(runs, "Warriors", "NBA: 2027 Champion")
170 rookie = _top(runs, "Warriors", "NBA: 2026-27 Rookie of the Year")
171 assert champion is not None, _titles(runs["Warriors"])
172 assert rookie is not None, _titles(runs["Warriors"])
173 assert rookie["headline"] is not None and "Yaxel Lendeborg" in rookie["headline"]["question"]
174 assert rookie["matched_market"]["related_name"] == "Yaxel Lendeborg"
175
176
177 def test_grizzlies_top_three_has_title_odds_and_jerome(runs):
178 champion = _top(runs, "Memphis Grizzlies", "NBA: 2027 Champion")
179 improved = _top(runs, "Memphis Grizzlies", "NBA: 2026-27 Most Improved Player")
180 assert champion is not None, _titles(runs["Memphis Grizzlies"])
181 assert improved is not None, _titles(runs["Memphis Grizzlies"])
182 assert improved["matched_market"]["related_name"] == "Ty Jerome"
183 assert improved["headline"] is not None and "Ty Jerome" in improved["headline"]["question"]
184
185
186 def test_claude_code_top_three_are_claude_model_markets_led_by_a_release(runs):
187 titles = _titles(runs["Claude Code"])
188 assert len(titles) == 3
189 assert titles[0].startswith(CLAUDE_RELEASES), titles
190 assert all("Claude" in title and "Giroux" not in title for title in titles), titles
191
192
193 @pytest.mark.parametrize("topic", ["Anthropic", "OpenAI"])
194 def test_ai_labs_show_the_best_ai_model_market(runs, topic):
195 titles = [item["title"] for item in runs[topic][: polymarket.RESULT_CAP["default"]]]
196 assert any(title.startswith(BEST_AI_MODEL) for title in titles), titles
197
198
199 @pytest.mark.parametrize("topic", sorted(NEGATIVE_CONTROLS))
200 def test_negative_controls_show_no_markets(runs, topic):
201 assert runs[topic] == []
202
202 lines PYTHON