102 lines
4.0 KiB
Python
102 lines
4.0 KiB
Python
"""全服總榜:總簽到次數、總吱吱次數、兩個前 10 名排行榜,不算管理員。"""
|
|
from app import admin, config, db
|
|
|
|
from .conftest import admin_code, admin_verify, login
|
|
|
|
|
|
def seed(users: dict[int, tuple[int, int]], flagged: set[int] = frozenset()) -> None:
|
|
"""直接寫資料庫:{uid: (簽到天數, 吱吱次數)}。"""
|
|
conn = db.connect()
|
|
for uid, (days, clicks) in users.items():
|
|
conn.execute(
|
|
"INSERT INTO users(uid, email, display_name, lang, clicks, flagged_at, created_at) "
|
|
"VALUES (?, ?, ?, 'zh-TW', ?, ?, 0)",
|
|
(uid, f"u{uid}@example.com", f"鼠{uid}", clicks, 1 if uid in flagged else None))
|
|
for d in range(days):
|
|
conn.execute("INSERT INTO checkins(uid, day, created_at) VALUES (?, ?, 0)", (uid, f"2026-10-{d + 1:02d}"))
|
|
conn.close()
|
|
|
|
|
|
def board(client):
|
|
r = client.get("/api/leaderboard")
|
|
assert r.status_code == 200, r.text
|
|
return r.json()
|
|
|
|
|
|
def ranks(entries):
|
|
return [(e["uid"], e["rank"], e["value"]) for e in entries]
|
|
|
|
|
|
def test_empty(client):
|
|
assert board(client) == {"total_checkins": 0, "total_clicks": 0, "by_days": [], "by_clicks": []}
|
|
|
|
|
|
def test_order_ties_and_totals(client):
|
|
seed({1: (3, 10), 2: (5, 40), 3: (3, 40), 4: (1, 5), 5: (0, 0)})
|
|
b = board(client)
|
|
assert b["total_checkins"] == 12 and b["total_clicks"] == 95
|
|
# 同分同名次,同分時 UID 小的在前;0 的不上榜
|
|
assert ranks(b["by_days"]) == [(2, 1, 5), (1, 2, 3), (3, 2, 3), (4, 4, 1)]
|
|
assert ranks(b["by_clicks"]) == [(2, 1, 40), (3, 1, 40), (1, 3, 10), (4, 4, 5)]
|
|
e = b["by_days"][0]
|
|
assert e["uid_str"] == "00000002" and e["display_name"] == "鼠2" and e["avatar_url"] is None
|
|
assert e["title"] == "" and e["title_color"] is None and e["uid_tier"] is None
|
|
assert "email" not in e and "flagged" not in e and "flagged_at" not in e
|
|
|
|
|
|
def test_real_checkins_and_zhi(client, sent, fake_clock):
|
|
login(client, sent)
|
|
client.post("/api/checkin")
|
|
fake_clock.set("2026-11-22T12:00:00+08:00")
|
|
client.post("/api/checkin")
|
|
b = board(client)
|
|
assert b["total_checkins"] == 2 and b["total_clicks"] == 2
|
|
assert ranks(b["by_days"]) == [(1, 1, 2)] and ranks(b["by_clicks"]) == [(1, 1, 2)]
|
|
|
|
|
|
def test_admin_excluded(client, sent, fake_clock):
|
|
r = admin_verify(client, admin_code(fake_clock))
|
|
assert r.status_code == 200, r.text
|
|
client.post("/api/checkin")
|
|
conn = db.connect()
|
|
conn.execute("UPDATE users SET clicks = 999999 WHERE uid = ?", (config.ADMIN_UID,))
|
|
conn.close()
|
|
seed({1: (1, 3)})
|
|
b = board(client)
|
|
assert b["total_checkins"] == 1 and b["total_clicks"] == 3
|
|
assert [e["uid"] for e in b["by_days"] + b["by_clicks"]] == [1, 1]
|
|
|
|
|
|
def test_top_ten_only(client):
|
|
seed({uid: (uid, uid * 100) for uid in range(1, 13)})
|
|
b = board(client)
|
|
assert len(b["by_days"]) == len(b["by_clicks"]) == config.LEADERBOARD_SIZE == 10
|
|
assert [e["uid"] for e in b["by_days"]] == list(range(12, 2, -1))
|
|
assert b["total_checkins"] == sum(range(1, 13)) # 總數還是算全部
|
|
assert b["total_clicks"] == sum(range(1, 13)) * 100
|
|
|
|
|
|
def test_flagged_still_listed_until_cleared(client):
|
|
seed({1: (2, 50), 2: (1, 999_999)}, flagged={2})
|
|
b = board(client)
|
|
assert b["by_clicks"][0]["uid"] == 2 and b["total_clicks"] == 1_000_049
|
|
# 管理員清掉作弊者的次數:總數和名次馬上更新
|
|
conn = db.connect()
|
|
conn.execute("UPDATE users SET clicks = 0 WHERE uid = 2")
|
|
conn.close()
|
|
b = board(client)
|
|
assert ranks(b["by_clicks"]) == [(1, 1, 50)] and b["total_clicks"] == 50
|
|
|
|
|
|
def test_reset_progress_clears_board(client):
|
|
seed({1: (2, 50), 2: (1, 7)})
|
|
admin.main(["reset-progress", "--yes"])
|
|
assert board(client) == {"total_checkins": 0, "total_clicks": 0, "by_days": [], "by_clicks": []}
|
|
|
|
|
|
def test_public_page_and_api(client):
|
|
client.cookies.clear()
|
|
assert client.get("/api/leaderboard").status_code == 200
|
|
r = client.get("/leaderboard")
|
|
assert r.status_code == 200 and 'id="view-leaderboard"' in r.text
|