| 1 | from urllib.parse import parse_qs, urlsplit |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from lib import github |
| 6 | |
| 7 | |
| 8 | START, END = "2026-03-01", "2026-03-31" |
| 9 | TOKEN = "dummy-github-token" |
| 10 | |
| 11 | |
| 12 | @pytest.mark.parametrize("username", ["alice", "alice_org"]) |
| 13 | def test_person_partition_queries_are_public_and_keep_auth(monkeypatch, username): |
| 14 | queries = [] |
| 15 | |
| 16 | def fetch(url, *, token, timeout): |
| 17 | path = urlsplit(url).path |
| 18 | if path == "/search/issues": |
| 19 | query = parse_qs(urlsplit(url).query)["q"][0] |
| 20 | queries.append(query) |
| 21 | assert token == TOKEN |
| 22 | return {"total_count": 0 if query.endswith(" is:open") else 1, "items": []} |
| 23 | assert path == "/search/repositories" |
| 24 | assert parse_qs(urlsplit(url).query)["q"] == [f"is:public user:{username} created:<={END}"] |
| 25 | assert token == TOKEN |
| 26 | return {"items": []} |
| 27 | |
| 28 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 29 | |
| 30 | results = github.search_github_person(username, START, END, token=TOKEN) |
| 31 | |
| 32 | base = f"is:public author:{username} type:pr created:{START}..{END}" |
| 33 | assert queries == [base, f"{base} merged:<={END} is:merged", f"{base} is:open"] |
| 34 | assert results[0]["engagement"]["total_prs"] == 1 |
| 35 | |
| 36 | |
| 37 | def test_public_project_keeps_authenticated_metadata_and_public_release(monkeypatch): |
| 38 | calls = [] |
| 39 | |
| 40 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 41 | parsed = urlsplit(url) |
| 42 | if parsed.path == "/repos/alice/project": |
| 43 | assert token == TOKEN |
| 44 | return {"visibility": "public", "stargazers_count": 42} |
| 45 | if parsed.path == "/repos/alice/project/releases": |
| 46 | assert token is None |
| 47 | return [{"tag_name": "v1", "published_at": "2026-03-14T10:00:00Z"}] |
| 48 | assert parsed.path == "/search/issues" |
| 49 | assert token == TOKEN |
| 50 | calls.append(parse_qs(parsed.query)["q"][0]) |
| 51 | return {"total_count": 0, "items": []} |
| 52 | |
| 53 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 54 | monkeypatch.setattr(github, "_fetch_readme_snippet", lambda repo, *, max_chars: "Public README") |
| 55 | |
| 56 | results = github.search_github_project(["alice/project"], START, END, token=TOKEN) |
| 57 | |
| 58 | assert len(results) == 1 |
| 59 | assert results[0]["engagement"]["stars"] == 42 |
| 60 | assert results[0]["date"] == "2026-03-14" |
| 61 | assert "Public README" in results[0]["snippet"] |
| 62 | assert "Latest release: v1" in results[0]["snippet"] |
| 63 | assert calls == [ |
| 64 | f"is:public repo:alice/project is:issue is:open created:<={END} label:enhancement", |
| 65 | f"is:public repo:alice/project is:issue is:open created:<={END}", |
| 66 | f"is:public repo:alice/project is:issue is:open created:<={END}", |
| 67 | ] |
| 68 | |
| 69 | |
| 70 | @pytest.mark.parametrize("metadata", [ |
| 71 | {"visibility": "private", "private": True}, |
| 72 | {"visibility": "internal"}, |
| 73 | {"private": False}, |
| 74 | ]) |
| 75 | def test_project_without_confirmed_public_metadata_skips_enrichment(monkeypatch, metadata): |
| 76 | urls = [] |
| 77 | |
| 78 | def fetch(url, *, token=None, timeout, failure_out=None): |
| 79 | urls.append(url) |
| 80 | return metadata |
| 81 | |
| 82 | def unexpected_readme(*args, **kwargs): |
| 83 | pytest.fail("README fetch requires confirmed public metadata") |
| 84 | |
| 85 | monkeypatch.setattr(github, "_fetch_json", fetch) |
| 86 | monkeypatch.setattr(github, "_fetch_readme_snippet", unexpected_readme) |
| 87 | |
| 88 | assert github.search_github_project(["alice/project"], START, END, token=TOKEN) == [] |
| 89 | assert urls == ["https://api.github.com/repos/alice/project"] |
| 90 |