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")) 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_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": "", "grade": ""}) row = dbmod.get_current_widget("alice", "composite") assert row == "" def test_current_widget_flips_atomically(tmp_dbs): dbmod.put_widgets("alice", "hash1", {"composite": "v1"}) dbmod.put_widgets("alice", "hash2", {"composite": "v2"}) assert dbmod.get_current_widget("alice", "composite") == "v2" def test_lru_trim_keeps_n_newest(tmp_dbs): for i in range(12): dbmod.put_widgets("alice", f"hash{i}", {"composite": f"{i}"}) 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"