signal-bot/tests/test_stats_records.py
Krzysztof kuhy Rudnicki 0e0c9ba619 feat: the nightly post notices things
Around twenty things could be said about a day. Saying all of them is a
wall nobody reads; saying whichever three are computed first is
arbitrary. So each candidate declares a priority and a magnitude and the
picker takes the top few, with two rules that make the result readable:
one line per family, so "set a record", "had 60% of the day" and "kept
the crown" do not become three sentences about the same person; and
fully deterministic ordering, so a day renders identically twice.

Nine candidates so far: personal records with what they beat, the
group's busiest day ever, a photo record, crown streaks (naming everyone
when it is shared), a silence broken, a monologue, one person dominating
the day, an unusual showing for that weekday, and a night owl.

Every candidate is (DayFacts) -> Highlight | None with the same
signature. On day one of this release almost all of them return None,
and a shape where "not yet" is the ordinary case is much easier to get
right than one where every guard is bespoke.

Two off-by-ones fixed by writing the sentence out and checking it:

- "odezwał się po 3 dniach ciszy" was counting four. The day somebody
  last spoke is not silent, and neither is the day they came back on, so
  the gap excludes both ends. The function is now named for that.
- A personal record on somebody's first ever day was being announced as
  a record. It is, trivially, and saying so every time is noise.

Verified against the live database, read-only on a copy: the real group
renders correctly at 324 messages, claims no average length for the
backfilled day, and fires no highlights at all -- which is right, since
there is one day of history for them to stand against.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 19:43:55 +02:00

138 lines
5.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""All-time records, and the silences between them."""
from __future__ import annotations
from typing import TYPE_CHECKING
from signal_bot.stats.queries_period import every_day
from signal_bot.stats.records import (
busiest_day,
days_silent_before,
longest_silence,
most_media_day,
personal_best,
previous_best,
quietest_day,
)
from tests.stats_seed import GROUP, IGA, KUHY, MITHRA, Shape, wrote
if TYPE_CHECKING:
from signal_bot.store import Store
def history(store: Store) -> None:
"""Four days, a two-day gap, ties, and somebody who stops writing."""
wrote(store, "2026-09-01", MITHRA, 10, Shape(media=3))
wrote(store, "2026-09-01", KUHY, 5)
wrote(store, "2026-09-02", MITHRA, 4)
wrote(store, "2026-09-02", KUHY, 4)
wrote(store, "2026-09-03", IGA, 2)
# 09-04 and 09-05 silent.
wrote(store, "2026-09-06", KUHY, 9, Shape(media=1))
def test_no_history_yields_no_records(store: Store) -> None:
"""Day one of this release: everything empty, nothing crashing."""
days = every_day(store.events, GROUP)
assert busiest_day(days) is None
assert quietest_day(days) is None
assert longest_silence(days, "2026-09-06") is None
assert most_media_day(store.events, GROUP) is None
assert personal_best(store.events, GROUP, "u-kuhy") is None
def test_busiest_and_quietest(store: Store) -> None:
history(store)
days = every_day(store.events, GROUP)
assert busiest_day(days).day == "2026-09-01"
assert busiest_day(days).value == 15
assert quietest_day(days).day == "2026-09-03"
assert quietest_day(days).value == 2
def test_the_quietest_day_is_one_somebody_spoke_on(store: Store) -> None:
"""A silent day is a silence, not a quiet day, and is measured separately."""
history(store)
days = every_day(store.events, GROUP)
assert quietest_day(days).value > 0
assert "2026-09-04" not in days
def test_a_tie_on_the_busiest_day_goes_to_the_earlier(store: Store) -> None:
wrote(store, "2026-09-02", KUHY, 7)
wrote(store, "2026-09-01", MITHRA, 7)
assert busiest_day(every_day(store.events, GROUP)).day == "2026-09-01"
def test_personal_best_and_what_it_beat(store: Store) -> None:
history(store)
best = personal_best(store.events, GROUP, "u-kuhy")
assert (best.day, best.value) == ("2026-09-06", 9)
previous = previous_best(store.events, GROUP, "u-kuhy", "2026-09-06")
assert (previous.day, previous.value) == ("2026-09-01", 5)
def test_a_first_ever_day_has_nothing_to_beat(store: Store) -> None:
"""The "poprzedni rekord" clause has to be omitted, not printed as 0."""
wrote(store, "2026-09-01", IGA, 3)
assert previous_best(store.events, GROUP, "u-iga", "2026-09-01") is None
def test_most_media_day(store: Store) -> None:
history(store)
record = most_media_day(store.events, GROUP)
assert (record.day, record.value) == ("2026-09-01", 3)
def test_a_history_with_no_media_has_no_media_record(store: Store) -> None:
wrote(store, "2026-09-01", KUHY, 4)
assert most_media_day(store.events, GROUP) is None
def test_longest_silence(store: Store) -> None:
history(store)
gap = longest_silence(every_day(store.events, GROUP), "2026-09-06")
assert (gap.start, gap.end, gap.days) == ("2026-09-04", "2026-09-05", 2)
def test_a_silence_running_to_today_counts(store: Store) -> None:
"""The gap the group is currently in is the one worth mentioning."""
wrote(store, "2026-09-01", KUHY, 1)
gap = longest_silence(every_day(store.events, GROUP), "2026-09-05")
assert (gap.start, gap.end, gap.days) == ("2026-09-02", "2026-09-05", 4)
def test_an_unbroken_history_has_no_silence(store: Store) -> None:
wrote(store, "2026-09-01", KUHY, 1)
wrote(store, "2026-09-02", KUHY, 1)
assert longest_silence(every_day(store.events, GROUP), "2026-09-02") is None
def test_the_gap_excludes_both_the_last_word_and_the_return(store: Store) -> None:
"""What makes "odezwał się po 3 dniach" true instead of off by one."""
history(store)
days = every_day(store.events, GROUP)
ala = {d: n for d, n in days.items() if d in {"2026-09-01", "2026-09-02"}}
# Recorded days are 09-01, 09-02, 09-03 and 09-06. Ala last wrote on
# the 2nd, so only the 3rd sits between that and a return on the 6th.
assert days_silent_before(days, ala, "2026-09-06") == 1
assert days_silent_before(days, {"2026-09-06": 9}, "2026-09-06") == 0
def test_somebody_who_never_wrote_is_silent_for_every_day_before_now(store: Store) -> None:
history(store)
days = every_day(store.events, GROUP)
assert days_silent_before(days, {}, "2026-09-06") == len(days) - 1
assert days_silent_before({}, {}, "2026-09-06") == 0
def test_the_backfilled_day_is_kept_out_of_records(store: Store) -> None:
"""A 22-hour window filed as a day must not become somebody's best."""
wrote(store, "2026-08-30", MITHRA, 98)
wrote(store, "2026-09-01", MITHRA, 10)
assert personal_best(store.events, GROUP, "u-ala").value == 98
excluded = personal_best(store.events, GROUP, "u-ala", exclude="2026-08-30")
assert (excluded.day, excluded.value) == ("2026-09-01", 10)
assert "2026-08-30" not in every_day(store.events, GROUP, exclude="2026-08-30")