| 1 | """Published evidence on GitHub cards must respect the report's UTC cutoff.""" |
| 2 | |
| 3 | from urllib.parse import parse_qs, urlsplit |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from lib import github, normalize |
| 8 | |
| 9 | |
| 10 | START, END = "2024-03-01", "2024-03-31" |
| 11 | |
| 12 | |
| 13 | def release(tag, published): |
| 14 | return {"tag_name": tag, "published_at": published} |
| 15 | |
| 16 | |
| 17 | @pytest.mark.parametrize("older_on_first_page", [False, True], ids=["all-newer", "delayed-publication"]) |
| 18 | def test_pinned_project_reads_past_newer_releases_and_keeps_utc_boundaries(monkeypatch, older_on_first_page): |
| 19 | pages = [] |
| 20 | |
| 21 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 22 | parsed = urlsplit(url) |
| 23 | params = parse_qs(parsed.query) |
| 24 | if parsed.path.endswith("/releases"): |
| 25 | assert token is None |
| 26 | page = int(params.get("page", ["1"])[0]) |
| 27 | pages.append(page) |
| 28 | if page == 1: |
| 29 | size = int(params["per_page"][0]) |
| 30 | rows = [release(f"future-{i}", "2024-04-01T00:00:00Z") for i in range(size)] |
| 31 | if older_on_first_page: |
| 32 | rows[:3] = [release(f"older-{i}", "2024-02-29T00:00:00Z") for i in range(3)] |
| 33 | return rows |
| 34 | return [ |
| 35 | release("start", "2024-03-01T00:00:00Z"), |
| 36 | release("no-date", None), |
| 37 | {**release("draft", "2024-03-31T23:59:59Z"), "draft": True}, |
| 38 | release("end", "2024-04-01T01:59:59+02:00"), |
| 39 | ] |
| 40 | if parsed.path == "/search/issues": |
| 41 | return {"items": [], "total_count": 0} |
| 42 | assert parsed.path == "/repos/example/pinned" |
| 43 | return {"visibility": "public", "created_at": "2024-03-01T00:00:00Z", "stargazers_count": 42} |
| 44 | |
| 45 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 46 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: "Long current README " * 25) |
| 47 | [item] = github.search_github_project(["example/pinned"], START, END, token="token") |
| 48 | |
| 49 | assert pages == [1, 2] |
| 50 | assert item["date"] == END |
| 51 | assert item["metadata"]["date_is_snapshot"] is False |
| 52 | assert "Latest release: end (2024-03-31)" in item["snippet"] |
| 53 | assert "Latest release: start (2024-03-01)" in item["snippet"] |
| 54 | assert "future-" not in item["snippet"] and "draft" not in item["snippet"] |
| 55 | assert "at retrieval" in item["snippet"] |
| 56 | [normalized] = normalize.normalize_source_items("github", [item], START, END) |
| 57 | assert "Latest release: end (2024-03-31)" in normalized.snippet |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize("created,expected", [("2024-04-01", 0), ("2024-03-31", 1)]) |
| 61 | def test_repository_creation_and_old_context_are_distinct_from_recent_activity(monkeypatch, created, expected): |
| 62 | enrichment = [] |
| 63 | |
| 64 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 65 | path = urlsplit(url).path |
| 66 | if path == "/repos/alice/project": |
| 67 | return {"visibility": "public", "created_at": created + "T00:00:00Z"} |
| 68 | enrichment.append(path) |
| 69 | if path.endswith("/releases"): |
| 70 | return [release("future", "2024-04-01"), release("old", "2024-02-29")] |
| 71 | return {"items": [], "total_count": 0} |
| 72 | |
| 73 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 74 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 75 | items = github.search_github_project(["alice/project"], START, END, token="token") |
| 76 | assert len(items) == expected |
| 77 | if expected: |
| 78 | assert items[0]["date"] == END and items[0]["metadata"]["date_is_snapshot"] |
| 79 | assert "Latest release: old (2024-02-29)" in items[0]["snippet"] |
| 80 | assert "future" not in items[0]["snippet"] |
| 81 | else: |
| 82 | assert enrichment == [] |
| 83 | |
| 84 | |
| 85 | def test_person_counts_and_both_repository_card_types_share_the_cutoff(monkeypatch): |
| 86 | repos = [ |
| 87 | {"full_name": "alice/future", "created_at": "2024-04-01"}, |
| 88 | {"full_name": "alice/own", "created_at": "2023-01-01"}, |
| 89 | ] |
| 90 | queries = [] |
| 91 | |
| 92 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 93 | parsed = urlsplit(url) |
| 94 | query = parse_qs(parsed.query).get("q", [""])[0] |
| 95 | if parsed.path == "/search/issues": |
| 96 | queries.append(query) |
| 97 | if "type:pr" in query: |
| 98 | hits = [{"html_url": "https://github.com/team/external/pull/1"}] if "is:merged" in query else [] |
| 99 | return {"total_count": 1, "items": hits} |
| 100 | return {"total_count": 0, "items": []} |
| 101 | if parsed.path == "/search/repositories": |
| 102 | assert token == "token" |
| 103 | assert query == f"is:public user:alice created:<={END}" |
| 104 | assert parse_qs(parsed.query)["sort"] == ["stars"] |
| 105 | return {"items": repos} |
| 106 | if parsed.path.endswith("/releases"): |
| 107 | return [release("future-version", "2024-04-01"), release("in-window", "2024-03-20")] |
| 108 | assert parsed.path == "/repos/team/external" |
| 109 | return {"visibility": "public", "created_at": "2022-12-01"} |
| 110 | |
| 111 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 112 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 113 | items = github.search_github_person("alice", START, END, token="token") |
| 114 | by_repo = {item["container"]: item for item in items} |
| 115 | assert set(by_repo) == {"@alice", "team/external", "alice/own"} |
| 116 | assert any(f"merged:<={END}" in q for q in queries) |
| 117 | assert all(f"created:<={END}" in q for q in queries if "is:issue" in q) |
| 118 | assert "still open at retrieval" in by_repo["@alice"]["snippet"] |
| 119 | for repo in ("team/external", "alice/own"): |
| 120 | assert by_repo[repo]["date"] == "2024-03-20" |
| 121 | assert not by_repo[repo]["metadata"]["date_is_snapshot"] |
| 122 | assert "future-version" not in by_repo[repo]["snippet"] |
| 123 | |
| 124 | |
| 125 | def test_live_project_still_fetches_one_small_release_page(monkeypatch): |
| 126 | queries = [] |
| 127 | pages = [] |
| 128 | _, today = github.dates.get_date_range(0) |
| 129 | |
| 130 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 131 | parsed = urlsplit(url) |
| 132 | params = parse_qs(parsed.query) |
| 133 | if parsed.path.endswith("/releases"): |
| 134 | pages.append(params) |
| 135 | return [release("now", today)] |
| 136 | if parsed.path == "/search/issues": |
| 137 | queries.append(params["q"][0]) |
| 138 | return {"items": [], "total_count": 0} |
| 139 | return {"visibility": "public"} |
| 140 | |
| 141 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 142 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 143 | [item] = github.search_github_project(["owner/name"], today, today, token="token") |
| 144 | assert pages == [{"per_page": ["3"]}] |
| 145 | assert all("created:" not in q for q in queries) |
| 146 | assert "at retrieval" not in item["snippet"] |
| 147 | assert item["date"] == today and not item["metadata"]["date_is_snapshot"] |
| 148 | |
| 149 | |
| 150 | def test_live_person_selects_popular_original_projects_before_the_limit(monkeypatch): |
| 151 | start, today = github.dates.get_date_range(30) |
| 152 | |
| 153 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 154 | parsed = urlsplit(url) |
| 155 | if parsed.path == "/search/issues": |
| 156 | query = parse_qs(parsed.query)["q"][0] |
| 157 | if "is:merged" in query: |
| 158 | return {"total_count": 2, "items": [ |
| 159 | {"html_url": "https://github.com/Alice/small/pull/1"}, |
| 160 | {"html_url": "https://github.com/team/external/pull/2"}, |
| 161 | ]} |
| 162 | return {"total_count": 2, "items": []} |
| 163 | if parsed.path == "/users/alice/repos": |
| 164 | return [{"full_name": "alice/a-fork", "fork": True}] |
| 165 | if parsed.path == "/search/repositories": |
| 166 | params = parse_qs(parsed.query) |
| 167 | assert params["q"] == ["is:public user:alice"] |
| 168 | assert params["sort"] == ["stars"] and params["order"] == ["desc"] |
| 169 | return {"items": [ |
| 170 | {"full_name": "alice/popular", "fork": False, "stargazers_count": 75000}, |
| 171 | {"full_name": "alice/also-popular", "fork": False, "stargazers_count": 3000}, |
| 172 | ]} |
| 173 | if parsed.path.endswith("/releases"): |
| 174 | return [] |
| 175 | if parsed.path.startswith("/repos/"): |
| 176 | return {"visibility": "public"} |
| 177 | raise AssertionError(parsed.path) |
| 178 | |
| 179 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 180 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 181 | monkeypatch.setattr(github, "as_completed", lambda futures: reversed(list(futures))) |
| 182 | cards = github.search_github_person("alice", start, today, token="token") |
| 183 | assert "velocity" in cards[0]["metadata"]["labels"] |
| 184 | own = [item for item in cards if "own-repo" in item["metadata"]["labels"]] |
| 185 | assert [(x["container"], x["engagement"]["stars"]) for x in own] == [ |
| 186 | ("alice/popular", 75000), ("alice/also-popular", 3000), |
| 187 | ] |
| 188 | assert [item["container"] for item in cards if "external-repo" in item["metadata"]["labels"]] == ["team/external"] |
| 189 | |
| 190 | |
| 191 | def test_busy_repository_historical_lookup_stops_without_dating_a_future_release(monkeypatch): |
| 192 | pages = [] |
| 193 | |
| 194 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 195 | parsed = urlsplit(url) |
| 196 | if parsed.path.endswith("/releases"): |
| 197 | params = parse_qs(parsed.query) |
| 198 | pages.append(int(params.get("page", ["1"])[0])) |
| 199 | return [release(f"new-{pages[-1]}-{i}", "2024-04-01") for i in range(int(params["per_page"][0]))] |
| 200 | if parsed.path == "/search/issues": |
| 201 | return {"items": [], "total_count": 0} |
| 202 | return {"visibility": "public"} |
| 203 | |
| 204 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 205 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 206 | [item] = github.search_github_project(["owner/busy"], START, END, token="token") |
| 207 | assert pages == [1, 2, 3] |
| 208 | assert item["metadata"]["date_is_snapshot"] |
| 209 | assert "No verified release evidence through 2024-03-31" in item["snippet"] |
| 210 | assert "new-" not in item["snippet"] |
| 211 | |
| 212 | |
| 213 | def test_push_card_keeps_event_date_and_omits_later_releases(monkeypatch): |
| 214 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 215 | path = urlsplit(url).path |
| 216 | if path.endswith("/events/public"): |
| 217 | return [{ |
| 218 | "type": "PushEvent", "id": "1", "created_at": "2024-03-18T12:00:00Z", |
| 219 | "actor": {"login": "alice"}, "repo": {"name": "alice/tool"}, |
| 220 | }] |
| 221 | if path.endswith("/releases"): |
| 222 | return [release("future-version", "2024-04-01"), release("in-window", "2024-03-30")] |
| 223 | if path == "/search/issues": |
| 224 | return {"items": [], "total_count": 0} |
| 225 | return {"visibility": "public"} |
| 226 | |
| 227 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 228 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda *a, **kw: None) |
| 229 | [item] = github.search_github_person("alice", START, END, token="token") |
| 230 | assert item["date"] == "2024-03-18" |
| 231 | assert "in-window" in item["snippet"] and "future-version" not in item["snippet"] |
| 232 | assert "at retrieval" in item["snippet"] |
| 233 | |
| 234 | |
| 235 | def test_keyword_research_ranks_only_comment_text_available_by_the_cutoff(monkeypatch): |
| 236 | url = "https://github.com/example/repo/issues/41" |
| 237 | comments = [ |
| 238 | ("later reply", "2024-04-01T00:00:00Z", "2024-04-01T00:00:00Z", 120), |
| 239 | ("later edit", "2024-03-02T00:00:00Z", "2024-04-01T00:00:00Z", 110), |
| 240 | ("undated", None, None, 100), |
| 241 | ("last second", "2024-04-01T01:59:59+02:00", "2024-03-31T23:59:59Z", 3), |
| 242 | ] |
| 243 | def fetch(request_url, *, timeout): |
| 244 | assert request_url.endswith("/issues/41/comments?per_page=100") |
| 245 | return [ |
| 246 | {"body": text, "created_at": created, "updated_at": updated, "user": {"login": "alice"}, |
| 247 | "reactions": {"total_count": score}, "html_url": f"{url}#issuecomment-{index}"} |
| 248 | for index, (text, created, updated, score) in enumerate(comments) |
| 249 | ] |
| 250 | |
| 251 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 252 | [item] = github.enrich_with_comments([{"url": url, "metadata": {}}], "quick", to_date=END) |
| 253 | assert [(c["excerpt"], c["date"], c["url"]) for c in item["metadata"]["top_comments"]] == [ |
| 254 | ("last second", END, f"{url}#issuecomment-3"), |
| 255 | ] |
| 256 |