| 1 | """GitHub keyword search keeps only results that are about the topic. |
| 2 | |
| 3 | GitHub's issue search matches on body text, so a non-tech topic pulls in |
| 4 | keyword-stuffed spam issues and incidental mentions in unrelated repos. Their |
| 5 | search rank is high, so a rank-weighted score cannot tell them from real hits. |
| 6 | A result must name the topic in its title or live in a repo named for it. |
| 7 | """ |
| 8 | |
| 9 | from unittest import mock |
| 10 | |
| 11 | from lib import github, pipeline, schema |
| 12 | |
| 13 | _FROM = "2026-10-11" |
| 14 | _TO = "2026-11-10" |
| 15 | |
| 16 | |
| 17 | def _raw(repo, number, title, *, body="", reactions=0, comments=0, pr=False): |
| 18 | kind = "pull" if pr else "issues" |
| 19 | item = { |
| 20 | "id": hash((repo, number)), |
| 21 | "html_url": f"https://github.com/{repo}/{kind}/{number}", |
| 22 | "title": title, |
| 23 | "body": body, |
| 24 | "created_at": "2026-10-20T10:00:00Z", |
| 25 | "state": "open", |
| 26 | "comments": comments, |
| 27 | "reactions": {"total_count": reactions}, |
| 28 | "labels": [], |
| 29 | "user": {"login": "someone"}, |
| 30 | } |
| 31 | if pr: |
| 32 | item["pull_request"] = {"url": "..."} |
| 33 | return item |
| 34 | |
| 35 | |
| 36 | def _envelope(core, raw_items): |
| 37 | return { |
| 38 | "items": raw_items, |
| 39 | "context": {"core": core, "from_date": _FROM, "to_date": _TO, "count": 25}, |
| 40 | } |
| 41 | |
| 42 | |
| 43 | _KANYE_RAW = [ |
| 44 | _raw( |
| 45 | "qa-helpdesk-7/tickets", 8922, |
| 46 | "Bug report: MESSAGE #L8922 — THE PROMOCODE — $5000 LEARN MORE", |
| 47 | body="kanye west kanye west promo code claim now", |
| 48 | reactions=40, |
| 49 | ), |
| 50 | _raw( |
| 51 | "qa-helpdesk-7/tickets", 9285, |
| 52 | "Bug report: ticket №F9285 ★ PROMO CODE ◆ $5000 CHECK", |
| 53 | body="kanye west bonus promo", |
| 54 | reactions=35, |
| 55 | ), |
| 56 | _raw( |
| 57 | "stablehand/horse-counter", 12, |
| 58 | "Weekly count is off by one for the north paddock", |
| 59 | body="Repro: name two horses Kanye and West, then run the weekly count.", |
| 60 | comments=3, |
| 61 | ), |
| 62 | ] |
| 63 | |
| 64 | |
| 65 | def test_non_tech_topic_with_spam_and_incidental_matches_returns_nothing(): |
| 66 | items = github.parse_github_response( |
| 67 | _envelope("kanye west", _KANYE_RAW), topic="Kanye West", |
| 68 | ) |
| 69 | assert items == [] |
| 70 | |
| 71 | |
| 72 | def test_floor_applies_without_an_explicit_topic(): |
| 73 | """The search's own subject is the fallback when no topic is passed.""" |
| 74 | assert github.parse_github_response(_envelope("kanye west", _KANYE_RAW)) == [] |
| 75 | |
| 76 | |
| 77 | def test_floor_does_not_depend_on_result_rank_or_engagement(): |
| 78 | spam_first = _raw( |
| 79 | "qa-helpdesk-7/tickets", 1, |
| 80 | "Bug report: MESSAGE #L8922 — THE PROMOCODE — $5000 LEARN MORE", |
| 81 | reactions=5000, comments=900, |
| 82 | ) |
| 83 | real_last = _raw("yeezy-fans/discography", 4, "Add Kanye West 2026 tour dates") |
| 84 | filler = [ |
| 85 | _raw("qa-helpdesk-7/tickets", n, f"Bug report: ticket #{n} PROMO CODE") |
| 86 | for n in range(2, 22) |
| 87 | ] |
| 88 | items = github.parse_github_response( |
| 89 | _envelope("kanye west", [spam_first, *filler, real_last]), topic="Kanye West", |
| 90 | ) |
| 91 | assert [i["url"] for i in items] == [ |
| 92 | "https://github.com/yeezy-fans/discography/issues/4", |
| 93 | ] |
| 94 | |
| 95 | |
| 96 | def test_tech_topic_named_in_titles_is_still_returned(): |
| 97 | raw = [ |
| 98 | _raw("anthropics/claude-code", 4101, |
| 99 | "Claude Code hangs after resuming a long session", reactions=30), |
| 100 | _raw("microsoft/vscode", 880, |
| 101 | "Terminal flickers when Claude Code streams output", comments=6), |
| 102 | _raw("vercel/next.js", 77, |
| 103 | "docs: add a Claude Code setup guide", pr=True), |
| 104 | _raw("community/community", 150, |
| 105 | "Discussion: is Claude Code worth it for monorepos?", comments=40), |
| 106 | ] |
| 107 | items = github.parse_github_response( |
| 108 | _envelope("claude code", raw), topic="Claude Code", |
| 109 | ) |
| 110 | assert {i["url"] for i in items} == { |
| 111 | "https://github.com/anthropics/claude-code/issues/4101", |
| 112 | "https://github.com/microsoft/vscode/issues/880", |
| 113 | "https://github.com/vercel/next.js/pull/77", |
| 114 | "https://github.com/community/community/issues/150", |
| 115 | } |
| 116 | |
| 117 | |
| 118 | def test_results_in_the_topics_own_repo_are_returned_whatever_the_title(): |
| 119 | raw = [ |
| 120 | _raw("openclaw/openclaw", 501, "Gateway drops websocket on restart"), |
| 121 | _raw("openclaw/openclaw", 502, "Fix flaky cron scheduler test", pr=True), |
| 122 | ] |
| 123 | items = github.parse_github_response(_envelope("openclaw", raw), topic="OpenClaw") |
| 124 | assert len(items) == 2 |
| 125 | |
| 126 | |
| 127 | def test_results_in_the_topics_org_are_returned_whatever_the_title(): |
| 128 | raw = [_raw("vercel/next.js", 9001, "Turbopack build fails on Windows paths")] |
| 129 | items = github.parse_github_response(_envelope("vercel", raw), topic="Vercel") |
| 130 | assert len(items) == 1 |
| 131 | |
| 132 | |
| 133 | def test_hyphenated_repo_name_matches_a_multi_word_topic(): |
| 134 | raw = [_raw("anthropics/claude-code", 4102, "Hooks stop firing after compaction")] |
| 135 | items = github.parse_github_response( |
| 136 | _envelope("claude code", raw), topic="Claude Code", |
| 137 | ) |
| 138 | assert len(items) == 1 |
| 139 | |
| 140 | |
| 141 | # ---- the pipeline stream ---- |
| 142 | |
| 143 | def _github_stream(topic, search_query, raw_items): |
| 144 | subquery = schema.SubQuery( |
| 145 | label="primary", search_query=search_query, |
| 146 | ranking_query=topic, sources=["github"], weight=1.0, |
| 147 | ) |
| 148 | with mock.patch.object(github, "_resolve_token", return_value="t"), \ |
| 149 | mock.patch.object(github, "resolve_token", return_value="t"), \ |
| 150 | mock.patch.object( |
| 151 | github, "_fetch_json", |
| 152 | side_effect=lambda url, **kw: ( |
| 153 | {"items": raw_items} if "is%3Aissue" in url else {"items": []} |
| 154 | ), |
| 155 | ), \ |
| 156 | mock.patch.object( |
| 157 | github, "enrich_with_comments", side_effect=lambda items, **kw: items, |
| 158 | ): |
| 159 | return pipeline._retrieve_stream_impl( |
| 160 | topic=topic, subquery=subquery, source="github", |
| 161 | config={}, depth="quick", date_range=(_FROM, _TO), |
| 162 | runtime=mock.Mock(), mock=False, raw_topic=topic, |
| 163 | ) |
| 164 | |
| 165 | |
| 166 | def test_stream_reports_an_off_topic_search_as_empty_not_as_an_error(): |
| 167 | items, artifact = _github_stream("Kanye West", "kanye west bully", _KANYE_RAW) |
| 168 | assert items == [] |
| 169 | # No outcome artifact means the stream is recorded as a clean no-results |
| 170 | # run; an error would carry a `_source_outcome` here. |
| 171 | assert artifact == {} |
| 172 | |
| 173 | |
| 174 | def test_stream_judges_against_the_research_topic_not_the_narrowed_subquery(): |
| 175 | """A planner subquery adds angle words the title need not repeat.""" |
| 176 | raw = [_raw("microsoft/vscode", 881, "Claude loses the terminal session on reload")] |
| 177 | items, artifact = _github_stream( |
| 178 | "Claude Code", "claude code hooks subagents worktrees release", raw, |
| 179 | ) |
| 180 | assert [i["url"] for i in items] == ["https://github.com/microsoft/vscode/issues/881"] |
| 181 | assert artifact == {} |
| 182 | |
| 183 | |
| 184 | # ---- person and project modes take no topical floor ---- |
| 185 | |
| 186 | def _refuse(*args, **kwargs): |
| 187 | raise AssertionError("the topical floor must not run in person or project mode") |
| 188 | |
| 189 | |
| 190 | def test_person_mode_does_not_apply_the_topical_floor(): |
| 191 | event = { |
| 192 | "id": "1", |
| 193 | "type": "PushEvent", |
| 194 | "actor": {"login": "kurt"}, |
| 195 | "repo": {"name": "stablehand/horse-counter"}, |
| 196 | "created_at": "2026-10-22T20:28:18Z", |
| 197 | } |
| 198 | |
| 199 | def fetch(url, **kwargs): |
| 200 | return None if "search/issues" in url else [event] |
| 201 | |
| 202 | with mock.patch.object(github, "_clears_topical_floor", _refuse, create=True), \ |
| 203 | mock.patch.object(github, "_resolve_token", return_value="t"), \ |
| 204 | mock.patch.object(github, "_enrich_own_repo", return_value={}), \ |
| 205 | mock.patch.object(github, "_fetch_repo_info", return_value={ |
| 206 | "stars": 3, "forks": 0, "description": "Counts horses", |
| 207 | "language": "Python", "open_issues": 1, |
| 208 | }), \ |
| 209 | mock.patch.object(github, "_fetch_json", side_effect=fetch): |
| 210 | items = github.search_github_person("kurt", _FROM, _TO, token="t") |
| 211 | |
| 212 | assert [i["container"] for i in items] == ["stablehand/horse-counter"] |
| 213 | |
| 214 | |
| 215 | def test_project_mode_does_not_apply_the_topical_floor(): |
| 216 | enrichment = { |
| 217 | "info": {"stars": 12, "open_issues": 2, "description": "Counts horses", |
| 218 | "language": "Python"}, |
| 219 | "readme": "Counts horses per paddock.", |
| 220 | "releases": [{"name": "v1.2", "date": "2026-10-30", "body": ""}], |
| 221 | "top_issues": {}, |
| 222 | } |
| 223 | with mock.patch.object(github, "_clears_topical_floor", _refuse, create=True), \ |
| 224 | mock.patch.object(github, "_resolve_token", return_value="t"), \ |
| 225 | mock.patch.object(github, "_enrich_project_repo", return_value=enrichment): |
| 226 | items = github.search_github_project(["stablehand/horse-counter"], _FROM, _TO) |
| 227 | |
| 228 | assert [i["container"] for i in items] == ["stablehand/horse-counter"] |
| 229 |