signal-bot/tests/test_stats_render_period.py
Krzysztof kuhy Rudnicki bfc2bcb3b8 feat: nine commands for asking the statistics questions
!staty, !wczoraj, !tydzień, !miesiąc, !rok, !rekordy, !korona, !dni and
!ja, under 29 triggers with both Polish spellings of each.

None takes an argument. registry.resolve matches the whole folded body
against a trigger, so "!ja ala" resolves to nothing; teaching it to
split on whitespace would silently make "!kot pls" fire where today it
does nothing. Each question gets its own trigger instead.

!wczoraj renders through the same code path as the nightly post, which
is not a convenience: the bot cannot trigger its own 21:37 report, so
without byte-identical output there is no way to check the headline
feature of this release short of waiting for the evening.

Two guards worth naming:

- test_every_trigger_is_unique builds the whole registry.
  DuplicateTriggerError subclasses ValueError, and ValueError is in
  startup_retry.RETRYABLE -- so a colliding alias would not crash the
  bot, it would retry forever logging "signal-cli not reachable yet".
  Checked at test time instead of never.
- No trigger may contain a digit: fold() maps 0134578 onto letters, so
  "!7dni" would resolve as "!tdni". Also asserted.

Every command answers on an empty history rather than crashing, which is
the state the group will actually be in for the next few days: records,
the crown and the weekday table all say "za mało danych" because their
only day so far is the backfilled one, and they exclude it.

Verified against a copy of the live database -- all nine render
correctly on the real group's data.

One test bug caught while writing it: it seeded "today" and excluded
2026-08-30, which on the day it was written were the same date, so it
passed for the wrong reason. Fixed dates now.

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

121 lines
4.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Rendering a period, and the on-demand answers."""
from __future__ import annotations
from signal_bot.stats import periods
from signal_bot.stats.events import Person
from signal_bot.stats.queries_day import DayRow
from signal_bot.stats.queries_period import Trend
from signal_bot.stats.records import DayRecord, Silence
from signal_bot.stats.render_extra import (
NOTHING_YET,
render_crown,
render_me,
render_records,
render_weekdays,
)
from signal_bot.stats.render_period import MAX_ROWS, bar, render_period, trend_line
WEEK = periods.week_of("2026-09-01")
def rows(spec: list[tuple[str, int]]) -> list[DayRow]:
"""Build period rows from (name, messages) pairs."""
return [
DayRow(f"u{i}", name, messages, 0, 0, messages * 10)
for i, (name, messages) in enumerate(spec)
]
def test_a_period_with_nobody_in_it() -> None:
rendered = render_period(WEEK, [], Trend(0, 0))
assert "Nikt nic nie napisał." in rendered
def test_the_bar_fills_with_the_share() -> None:
assert bar(0) == "░" * 10
assert bar(100) == "█" * 10
assert bar(50) == "█████░░░░░"
def test_trend_says_first_rather_than_no_change() -> None:
"""Zero percent and "nothing to compare against" are different claims."""
assert "pierwszy taki okres" in trend_line(Trend(now=10, before=0), "poprzednio")
assert "dokładnie tyle samo" in trend_line(Trend(now=10, before=10), "poprzednio")
assert "↑ 50% więcej" in trend_line(Trend(now=15, before=10), "poprzednio")
assert "↓ 50% mniej" in trend_line(Trend(now=5, before=10), "poprzednio")
def test_a_long_period_is_truncated_with_its_total() -> None:
many = rows([(f"Osoba {i:02d}", 20 - i) for i in range(MAX_ROWS + 4)])
rendered = render_period(WEEK, many, Trend(100, 90))
assert "…i 4 osoby więcej" in rendered
assert len([line for line in rendered.split("\n") if line.startswith(("1.", "2."))]) >= 1
def test_medals_only_appear_on_a_podium() -> None:
three = rows([("A", 3), ("B", 2), ("C", 1)])
assert "🥇" in render_period(WEEK, three, Trend(6, 5), podium=True)
assert "🥇" not in render_period(WEEK, three, Trend(6, 5))
def test_records_with_nothing_recorded_says_so() -> None:
assert render_records({}, None, None, []) == NOTHING_YET
def test_records_reports_each_kind_it_has() -> None:
rendered = render_records(
{"2026-09-01": 10, "2026-09-03": 2},
DayRecord("2026-09-01", 4),
Silence("2026-09-04", "2026-09-06", 3),
[("Ala", DayRecord("2026-09-01", 9))],
)
assert "Najwięcej w jeden dzień: 10" in rendered
assert "Najciszej (ale ktoś pisał): 2" in rendered
assert "Najwięcej zdjęć" in rendered
assert "Najdłuższa cisza: 3 dni" in rendered
assert "Ala — 9" in rendered
def test_the_crown_with_no_history() -> None:
assert render_crown({}, []) == NOTHING_YET
def test_the_crown_reports_now_longest_and_totals() -> None:
winners = {
"2026-09-01": [("u1", "Ala")],
"2026-09-02": [("u1", "Ala")],
"2026-09-03": [("u2", "kuhy")],
}
people = [Person("u1", "Ala", ""), Person("u2", "kuhy", "")]
rendered = render_crown(winners, people)
assert "Teraz: kuhy — 1 dzień z rzędu" in rendered
assert "Najdłuższa seria: Ala — 2 dni" in rendered
assert "Ala — 2 dni" in rendered
def test_weekdays_with_no_history() -> None:
assert render_weekdays({}, 0) == NOTHING_YET
def test_weekdays_skips_days_never_recorded() -> None:
rendered = render_weekdays({0: [10, 20], 4: [5]}, 4)
assert "poniedziałki: średnio 15" in rendered
assert "piątki: średnio 5" in rendered
assert "← dziś" in rendered
assert "wtorki" not in rendered
def test_me_omits_what_it_does_not_know() -> None:
"""No record, no favourite hour, no streak: three clauses that vanish."""
rendered = render_me("kuhy", 3, None, 10, None, 0)
assert rendered == "kuhy — 3 wiadomości w historii\nUdział w rozmowie: 10%"
def test_me_reports_everything_it_does_know() -> None:
rendered = render_me("kuhy", 30, DayRecord("2026-09-01", 12), 40, 23, 2)
assert "Rekord życiowy: 12 (1 września)" in rendered
assert "Najczęściej piszesz o 23:00" in rendered
assert "Korona: 2 dni z rzędu" in rendered