mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
ENROLLMENT_DAILY_CAP was 50, so any traffic spike past 50 new users in a day was turned away with a rate_limited card. Raise the default to 500 and add ENROLLMENT_HOURLY_CAP=60. The hourly guard is the one that actually protects the shared PAT: a new user costs ~37 REST calls (p90 ~104) against a 5000/hr budget, and a spike arrives inside an hour rather than spread over a day. Counted off users.enrolled_at, so no schema change. Also stop an unbounded enqueue loop. `name` is drawn inside the composite and never persisted standalone, and `achievements` renders nothing until the user authors one, so their SVG is permanently absent and every request re-enqueued a build that could never produce it. Only self-heal when no build has actually landed, keyed off widget_data for the current hash. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BwMoSbmK77Ph7XaV5YnAdS
200 lines
7.6 KiB
Python
200 lines
7.6 KiB
Python
import os
|
|
import tempfile
|
|
import pytest
|
|
from src import db as dbmod
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_dbs(monkeypatch):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db"))
|
|
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db"))
|
|
monkeypatch.setattr(dbmod, "ANALYTICS_DB_PATH", os.path.join(d, "a.db"))
|
|
dbmod.init_dbs()
|
|
yield d
|
|
|
|
|
|
def test_settings_hash_is_deterministic():
|
|
h1 = dbmod.settings_hash({"theme": "dark", "widgets": ["a", "b"]})
|
|
h2 = dbmod.settings_hash({"widgets": ["a", "b"], "theme": "dark"})
|
|
assert h1 == h2
|
|
|
|
|
|
def test_enroll_returns_job_id(tmp_dbs):
|
|
r = dbmod.enroll("alice", {"theme": "dark"})
|
|
assert r["job_id"] > 0
|
|
assert "edit_token" not in r
|
|
|
|
|
|
def test_enroll_twice_noops(tmp_dbs):
|
|
r1 = dbmod.enroll("alice", {"theme": "dark"})
|
|
r2 = dbmod.enroll("alice", {"theme": "dark"})
|
|
assert r1["job_id"] > 0 and r2["job_id"] > 0
|
|
|
|
|
|
def test_enroll_without_build_skips_job(tmp_dbs):
|
|
"""OAuth callback uses this so the build job is only added once the
|
|
fetcher has actually fetched (via /internal/data-ready). Otherwise the
|
|
worker pops a job for a user with no fetcher data yet."""
|
|
r = dbmod.enroll("alice", {"theme": "dark"}, enqueue_build=False)
|
|
assert r["job_id"] is None
|
|
assert dbmod.pending_job_count() == 0
|
|
# The user row itself was still created so callbacks can find them.
|
|
assert dbmod.get_settings("alice") is not None
|
|
|
|
|
|
def test_enroll_with_github_profile(tmp_dbs):
|
|
dbmod.enroll("alice", {"theme": "dark"}, github_id=1, github_avatar_url="https://x/a.png")
|
|
with dbmod._settings_conn() as c:
|
|
row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone()
|
|
assert row["github_id"] == 1
|
|
assert row["github_avatar_url"] == "https://x/a.png"
|
|
|
|
|
|
def test_enrollments_daily_counter(tmp_dbs):
|
|
dbmod.enroll("alice", {"theme": "dark"})
|
|
dbmod.enroll("bob", {"theme": "dark"})
|
|
assert dbmod.enrollments_today() == 2
|
|
|
|
|
|
def test_enrollments_last_hour_counts_only_recent(tmp_dbs):
|
|
dbmod.enroll("alice", {"theme": "dark"})
|
|
dbmod.enroll("bob", {"theme": "dark"})
|
|
assert dbmod.enrollments_last_hour() == 2
|
|
# Age one of them past the window; it must drop out of the burst count
|
|
# while still counting toward the daily total.
|
|
with dbmod._settings_conn() as c:
|
|
c.execute("UPDATE users SET enrolled_at='2020-01-01T00:00:00Z' WHERE username='alice'")
|
|
c.commit()
|
|
assert dbmod.enrollments_last_hour() == 1
|
|
|
|
|
|
def test_enrollment_rank_is_case_insensitive(tmp_dbs):
|
|
"""GitHub returns logins with original casing ("ShayManor") but the users
|
|
table stores them lowercased. Without normalization the founder-tag rule
|
|
silently misses everyone whose login has capitals."""
|
|
dbmod.enroll("shaymanor", {"theme": "dark"})
|
|
# Mixed / upper case must find the same row as the lowercased insert.
|
|
assert dbmod.enrollment_rank("ShayManor") is not None
|
|
assert dbmod.enrollment_rank("SHAYMANOR") == dbmod.enrollment_rank("shaymanor")
|
|
assert dbmod.enrollment_rank("missing") is None
|
|
|
|
|
|
def test_update_settings_changes_hash(tmp_dbs):
|
|
dbmod.enroll("alice", {"theme": "dark"})
|
|
h_before = dbmod.get_settings("alice")["settings_hash"]
|
|
dbmod.update_settings("alice", {"theme": "light"})
|
|
h_after = dbmod.get_settings("alice")["settings_hash"]
|
|
assert h_before != h_after
|
|
|
|
|
|
def test_claim_job_marks_running(tmp_dbs):
|
|
job_id = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
|
|
job = dbmod.claim_next_job()
|
|
assert job["id"] == job_id
|
|
assert job["status"] == "running"
|
|
assert dbmod.claim_next_job() is None # already running
|
|
|
|
|
|
def test_complete_job(tmp_dbs):
|
|
jid = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
|
|
dbmod.claim_next_job()
|
|
dbmod.complete_job(jid)
|
|
with dbmod._settings_conn() as c:
|
|
status = c.execute("SELECT status FROM jobs WHERE id=?", (jid,)).fetchone()[0]
|
|
assert status == "done"
|
|
|
|
|
|
def test_widgets_put_and_get(tmp_dbs):
|
|
dbmod.put_widgets("alice", "hash1", {"composite": "<svg/>", "grade": "<svg/>"})
|
|
row = dbmod.get_current_widget("alice", "composite")
|
|
assert row == "<svg/>"
|
|
|
|
|
|
def test_current_widget_flips_atomically(tmp_dbs):
|
|
dbmod.put_widgets("alice", "hash1", {"composite": "<svg>v1</svg>"})
|
|
dbmod.put_widgets("alice", "hash2", {"composite": "<svg>v2</svg>"})
|
|
assert dbmod.get_current_widget("alice", "composite") == "<svg>v2</svg>"
|
|
|
|
|
|
def test_lru_trim_keeps_n_newest(tmp_dbs):
|
|
for i in range(12):
|
|
dbmod.put_widgets("alice", f"hash{i}", {"composite": f"<svg>{i}</svg>"})
|
|
dbmod.lru_trim("alice", keep=10)
|
|
with dbmod._widgets_conn() as c:
|
|
n = c.execute("SELECT COUNT(DISTINCT settings_hash) FROM widgets WHERE username='alice'").fetchone()[0]
|
|
assert n == 10
|
|
|
|
|
|
def test_reclaim_stuck_running_jobs(tmp_dbs):
|
|
jid = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
|
|
dbmod.claim_next_job()
|
|
# Backdate updated_at to simulate dead worker
|
|
with dbmod._settings_conn() as c:
|
|
c.execute("UPDATE jobs SET updated_at='2020-01-01T00:00:00Z' WHERE id=?", (jid,))
|
|
c.commit()
|
|
dbmod.reclaim_stuck_jobs(older_than_minutes=10)
|
|
job = dbmod.claim_next_job()
|
|
assert job["id"] == jid
|
|
|
|
|
|
def test_set_github_profile_round_trip(tmp_dbs):
|
|
dbmod.enroll("alice", {"theme": "dark"})
|
|
dbmod.set_github_profile("alice", github_id=42, github_avatar_url="https://example.com/a.png")
|
|
with dbmod._settings_conn() as c:
|
|
row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone()
|
|
assert row["github_id"] == 42
|
|
assert row["github_avatar_url"] == "https://example.com/a.png"
|
|
|
|
|
|
def test_get_user_streak_missing_returns_none(tmp_dbs):
|
|
assert dbmod.get_user_streak("nobody") is None
|
|
|
|
|
|
def test_put_and_get_user_streak_roundtrip(tmp_dbs):
|
|
dbmod.put_user_streak("alice", {
|
|
"current_streak": 12,
|
|
"current_start": "2026-04-08",
|
|
"last_active_date": "2026-04-19",
|
|
"max_streak": 42,
|
|
"max_start": "2025-11-01",
|
|
"max_end": "2025-12-12",
|
|
})
|
|
row = dbmod.get_user_streak("alice")
|
|
assert row["current_streak"] == 12
|
|
assert row["current_start"] == "2026-04-08"
|
|
assert row["last_active_date"] == "2026-04-19"
|
|
assert row["max_streak"] == 42
|
|
assert row["max_start"] == "2025-11-01"
|
|
assert row["max_end"] == "2025-12-12"
|
|
assert row["updated_at"] # set on write
|
|
|
|
|
|
def test_put_user_streak_upserts(tmp_dbs):
|
|
dbmod.put_user_streak("alice", {
|
|
"current_streak": 1, "current_start": "2026-04-19",
|
|
"last_active_date": "2026-04-19",
|
|
"max_streak": 1, "max_start": "2026-04-19", "max_end": "2026-04-19",
|
|
})
|
|
dbmod.put_user_streak("alice", {
|
|
"current_streak": 2, "current_start": "2026-04-18",
|
|
"last_active_date": "2026-04-19",
|
|
"max_streak": 5, "max_start": "2024-01-01", "max_end": "2024-01-05",
|
|
})
|
|
row = dbmod.get_user_streak("alice")
|
|
assert row["current_streak"] == 2
|
|
assert row["max_streak"] == 5
|
|
assert row["max_start"] == "2024-01-01"
|
|
|
|
|
|
def test_init_dbs_creates_analytics_schema(tmp_path, monkeypatch):
|
|
from src import db as dbmod
|
|
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", str(tmp_path / "s.db"))
|
|
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", str(tmp_path / "w.db"))
|
|
monkeypatch.setattr(dbmod, "ANALYTICS_DB_PATH", str(tmp_path / "a.db"))
|
|
dbmod.init_dbs()
|
|
with dbmod.analytics_conn() as c:
|
|
cols = {r["name"] for r in c.execute("PRAGMA table_info(events)").fetchall()}
|
|
assert cols == {"id", "ts", "service", "kind", "username",
|
|
"endpoint", "widget", "status", "latency_ms", "cache_hit"}
|