mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-11 19:54:05 +01:00
The queries the statistics are made of, all derived rather than cached -- two sources of truth for one number is how a leaderboard comes to disagree with itself, and this history is small enough that recomputing is free. Periods take an inclusive span of stored Warsaw day strings, never a range of timestamps, so a daylight-saving change cannot move a boundary. Trend reports None rather than 0 when there is no previous period: "nothing to compare against yet" and "no change" are different statements and the group will read them differently. A tie shares the crown and keeps every tied run alive, per kuhy's call. Streaks count *recorded* days, so a day nobody spoke does not end a run -- there was no crown to lose -- and neither does a day the bot was offline for, which it could not have judged. Every record excludes the backfilled day. It is a 22-hour window filed as a day, and leaving it in would hand somebody a personal best they never set. Two bugs found by running the code rather than reading it: - current_streaks reported every streak as zero. Ending somebody's run dropped them from the tally along with the length they had already accumulated, so the surviving dict was empty by the time it returned. Now the run ends and the count it reached is kept. - daily_winners depended on SQLite's grouping order to find a day's leader. It happened to work because uuids sorted conveniently in the fixtures. Ordering by messages DESC makes the first row per day the leader by construction, which is both correct and obviously so. No suppressions: the period queries compare an exclusion parameter against '' instead of pasting half a WHERE clause into an f-string, which is a SQL-injection shape even when every input is a constant. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
119 lines
4.5 KiB
Python
119 lines
4.5 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The crown: who holds it, for how long, and what a tie does to a run."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from signal_bot.stats.streaks import (
|
|
current_streaks,
|
|
daily_winners,
|
|
held_days,
|
|
longest_ever,
|
|
names_of,
|
|
)
|
|
from tests.stats_seed import GROUP, IGA, KUHY, MITHRA, wrote
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.store import Store
|
|
|
|
|
|
def test_no_history_has_no_crown(store: Store) -> None:
|
|
winners = daily_winners(store.events, GROUP)
|
|
assert winners == {}
|
|
assert current_streaks(winners) == []
|
|
assert longest_ever(winners) is None
|
|
assert held_days(winners, "u-kuhy") == 0
|
|
|
|
|
|
def test_the_busiest_person_holds_the_day(store: Store) -> None:
|
|
wrote(store, "2026-09-01", MITHRA, 10)
|
|
wrote(store, "2026-09-01", KUHY, 5)
|
|
winners = daily_winners(store.events, GROUP)
|
|
assert names_of(winners["2026-09-01"]) == ["Mithra"]
|
|
|
|
|
|
def test_a_tie_shares_the_crown(store: Store) -> None:
|
|
"""kuhy's call: shared, because the alternative starts arguments."""
|
|
wrote(store, "2026-09-01", MITHRA, 4)
|
|
wrote(store, "2026-09-01", KUHY, 4)
|
|
wrote(store, "2026-09-01", IGA, 1)
|
|
winners = daily_winners(store.events, GROUP)
|
|
assert names_of(winners["2026-09-01"]) == ["Mithra", "kuhy"]
|
|
|
|
|
|
def test_a_tie_keeps_every_tied_run_alive(store: Store) -> None:
|
|
"""Three days, both tied throughout: both hold a three-day streak."""
|
|
for day in ("2026-09-01", "2026-09-02", "2026-09-03"):
|
|
wrote(store, day, MITHRA, 3)
|
|
wrote(store, day, KUHY, 3)
|
|
streaks = current_streaks(daily_winners(store.events, GROUP))
|
|
assert [(s.display_name, s.length) for s in streaks] == [("Mithra", 3), ("kuhy", 3)]
|
|
|
|
|
|
def test_a_run_survives_a_day_nobody_spoke(store: Store) -> None:
|
|
"""There was no crown to lose on a silent day, so the run continues."""
|
|
wrote(store, "2026-09-01", KUHY, 5)
|
|
# 09-02 and 09-03 silent.
|
|
wrote(store, "2026-09-04", KUHY, 5)
|
|
streaks = current_streaks(daily_winners(store.events, GROUP))
|
|
assert [(s.display_name, s.length) for s in streaks] == [("kuhy", 2)]
|
|
|
|
|
|
def test_losing_a_day_ends_a_run_but_keeps_what_it_reached(store: Store) -> None:
|
|
"""The bug this test exists for reported every streak as zero."""
|
|
wrote(store, "2026-09-01", MITHRA, 10)
|
|
wrote(store, "2026-09-02", MITHRA, 10)
|
|
wrote(store, "2026-09-03", MITHRA, 4)
|
|
wrote(store, "2026-09-03", KUHY, 4)
|
|
wrote(store, "2026-09-06", KUHY, 9)
|
|
streaks = current_streaks(daily_winners(store.events, GROUP))
|
|
assert [(s.display_name, s.length) for s in streaks] == [("kuhy", 2)]
|
|
assert streaks[0].last_day == "2026-09-06"
|
|
|
|
|
|
def test_longest_ever_survives_the_run_that_set_it_ending(store: Store) -> None:
|
|
wrote(store, "2026-09-01", MITHRA, 10)
|
|
wrote(store, "2026-09-02", MITHRA, 10)
|
|
wrote(store, "2026-09-03", MITHRA, 10)
|
|
wrote(store, "2026-09-04", KUHY, 10)
|
|
best = longest_ever(daily_winners(store.events, GROUP))
|
|
assert (best.display_name, best.length, best.last_day) == ("Mithra", 3, "2026-09-03")
|
|
|
|
|
|
def test_held_days_counts_shared_days_for_everyone_tied(store: Store) -> None:
|
|
wrote(store, "2026-09-01", MITHRA, 10)
|
|
wrote(store, "2026-09-02", MITHRA, 4)
|
|
wrote(store, "2026-09-02", KUHY, 4)
|
|
winners = daily_winners(store.events, GROUP)
|
|
assert held_days(winners, "u-mithra") == 2
|
|
assert held_days(winners, "u-kuhy") == 1
|
|
assert held_days(winners, "u-iga") == 0
|
|
|
|
|
|
def test_somebody_who_merely_took_part_holds_nothing(store: Store) -> None:
|
|
"""Neither leader nor tied: the common case, and the one worth pinning."""
|
|
wrote(store, "2026-09-01", MITHRA, 10)
|
|
wrote(store, "2026-09-01", KUHY, 4)
|
|
wrote(store, "2026-09-01", IGA, 1)
|
|
winners = daily_winners(store.events, GROUP)
|
|
assert names_of(winners["2026-09-01"]) == ["Mithra"]
|
|
assert held_days(winners, "u-iga") == 0
|
|
|
|
|
|
def test_streak_order_is_stable(store: Store) -> None:
|
|
"""Longest first, then by name -- not whichever the dict happened to yield."""
|
|
wrote(store, "2026-09-01", MITHRA, 3)
|
|
wrote(store, "2026-09-01", KUHY, 3)
|
|
wrote(store, "2026-09-01", IGA, 3)
|
|
streaks = current_streaks(daily_winners(store.events, GROUP))
|
|
assert [s.display_name for s in streaks] == ["Iga", "Mithra", "kuhy"]
|
|
|
|
|
|
def test_the_backfilled_day_can_be_left_out(store: Store) -> None:
|
|
wrote(store, "2026-08-30", MITHRA, 98)
|
|
wrote(store, "2026-09-01", KUHY, 5)
|
|
winners = daily_winners(store.events, GROUP, exclude="2026-08-30")
|
|
assert "2026-08-30" not in winners
|
|
assert [s.display_name for s in current_streaks(winners)] == ["kuhy"]
|