signal-bot/tests/stats_seed.py
Krzysztof kuhy Rudnicki a9ffd02f33 feat: periods, records and the crown
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
2026-08-30 19:38:54 +02:00

65 lines
1.9 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""A deterministic history to run the period, record and streak queries over.
Not a conftest fixture: it is a builder several test modules call with a
different history each, rather than one shared shape. Counts come from
arithmetic, never from ``random`` -- a fixture that changes between runs turns
a real failure into a flake.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from signal_bot.stats.events import MessageEvent
if TYPE_CHECKING:
from signal_bot.store import Store
GROUP = "group-a=="
MITHRA = ("u-ala", "Ala")
KUHY = ("u-kuhy", "kuhy")
IGA = ("u-iga", "Basia")
@dataclass(frozen=True, slots=True)
class Shape:
"""The optional detail of a seeded day, so ``wrote`` stays legible."""
media: int = 0
hour: int = 12
chars: int = 10
commands: int = 0
group_id: str = GROUP
def wrote(
store: Store,
day: str,
who: tuple[str, str],
count: int,
shape: Shape | None = None,
) -> None:
"""Put ``count`` messages from one person into one group on one day."""
shape = shape or Shape()
uuid, name = who
for index in range(count):
store.events.record(
MessageEvent(
group_id=shape.group_id,
uuid=uuid,
display_name=name,
# Unique within (group, uuid) so the primary key does not
# absorb them, and stable so two runs agree.
sent_at_ms=int(day.replace("-", "")) * 1000 + index,
day=day,
local_hour=shape.hour,
media_kind="image" if index < shape.media else "",
media_count=1 if index < shape.media else 0,
char_len=shape.chars,
is_command=index < shape.commands,
)
)
store.events.note_person(shape.group_id, uuid, name, "", day)