signal-bot/tests/test_store.py
Krzysztof kuhy Rudnicki dc1995b558 feat: the message event log, and the nightly report built on it
The tally was one integer per person, deleted after each post. Nothing
dated survived it, so weeks, months, records, streaks and weekday
averages were all unanswerable. This replaces it with an append-only
per-message log holding metadata only -- never a message body -- and
rebuilds the nightly report on top.

What changes for the group: a photo posted without a caption now counts
(it was worth zero), stickers count, the bot's own messages no longer
do, people who said nothing appear with a 0, and the report covers the
previous full calendar day in Europe/Warsaw instead of "since the last
post" -- which used to merge two days whenever a group went quiet.

Notable decisions, each with the trap it avoids:

- Events and people are PERMANENT_TABLES: neither forget_group nor a
  future SCHEMA_VERSION bump may drop them. Both paths used to cost a
  day's counter and would now cost the entire history, and forget_group
  fires on a listing that merely omits members.
- is_own comes from which envelope branch carried the payload, not from
  comparing identities. The account is spelled as a uuid on some frames
  and a +E.164 number on others, so any comparison misses one of them.
- The roster is stashed in refresh(), not _adopt(), which returns early
  once the watched set stops changing -- i.e. always, in the steady
  state.
- The roster matches on uuid OR number because /v1/groups spells kuhy as
  +48123456789 while his messages carry an ACI uuid. Joining on uuid
  alone would have listed him twice every night, once with 0.
- Members whose display name we have never seen are counted, not named:
  their only spellings are a uuid or a phone number.
- (group, uuid, sent_at_ms) is the primary key, so a websocket
  redelivery after a reconnect cannot inflate the counts.
- Days come from a stored Warsaw date string, never a timestamp range:
  2026-03-29 is 23 hours long and 2026-10-25 is 25.
- Polish number agreement is a real rule with a 12-14 exception, so
  "44 znaki" and "45 znaków" are both spelled correctly.

config.py and startup.py were two lines under the 250-line cap and both
had to grow, so config_env and startup_retry come out of them first, as
pure moves.

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

192 lines
6.7 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""SQLite persistence, including survival across a restart."""
from __future__ import annotations
import sqlite3
from typing import TYPE_CHECKING
from signal_bot.schema import SCHEMA_VERSION
from signal_bot.stats.events import MessageEvent
from signal_bot.store import Store, open_store
if TYPE_CHECKING:
from pathlib import Path
GROUP = "group-a=="
OTHER = "group-b=="
def event(
uuid: str = "u1",
name: str = "Alice",
at: int = 1,
day: str = "2026-08-29",
group_id: str = GROUP,
) -> MessageEvent:
"""One minimal event. ``at`` also disambiguates the primary key."""
return MessageEvent(
group_id=group_id,
uuid=uuid,
display_name=name,
sent_at_ms=at,
day=day,
local_hour=12,
)
def counted(store: Store, group_id: str) -> int:
"""How many events one group holds."""
row = store.events.connection.execute(
"SELECT COUNT(*) AS n FROM message_events WHERE group_id = ?", (group_id,)
).fetchone()
return int(row["n"])
def test_last_used_is_none_before_first_use(store: Store) -> None:
assert store.last_used(GROUP, "u1", "cat") is None
def test_record_use_tracks_cooldown_and_daily(store: Store) -> None:
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
assert store.last_used(GROUP, "u1", "cat") == 100.0
assert store.uses_today(GROUP, "u1", "cat", "2026-08-29") == 1
store.record_use(GROUP, "u1", "cat", 200.0, "2026-08-29")
assert store.last_used(GROUP, "u1", "cat") == 200.0
assert store.uses_today(GROUP, "u1", "cat", "2026-08-29") == 2
def test_daily_counter_is_per_day(store: Store) -> None:
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
assert store.uses_today(GROUP, "u1", "cat", "2026-08-30") == 0
def test_uses_today_is_per_user_and_command(store: Store) -> None:
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
assert store.uses_today(GROUP, "u2", "cat", "2026-08-29") == 0
assert store.uses_today(GROUP, "u1", "dog", "2026-08-29") == 0
def test_votes_are_deduplicated_per_person(store: Store) -> None:
store.add_vote(GROUP, "kot", "u1", 100.0)
store.add_vote(GROUP, "kot", "u1", 101.0)
store.add_vote(GROUP, "kot", "u2", 102.0)
assert store.count_votes(GROUP, "kot", 0.0) == 2
def test_votes_outside_the_window_are_ignored(store: Store) -> None:
store.add_vote(GROUP, "cmd", "u1", 100.0)
assert store.count_votes(GROUP, "cmd", 150.0) == 0
def test_clear_votes(store: Store) -> None:
store.add_vote(GROUP, "cmd", "u1", 100.0)
store.clear_votes(GROUP, "cmd")
assert store.count_votes(GROUP, "cmd", 0.0) == 0
def test_store_is_a_context_manager(tmp_path: Path) -> None:
with Store(str(tmp_path / "x.sqlite3")) as opened:
opened.events.record(event())
assert opened.meta.__self__ is opened
def test_state_is_scoped_per_group(store: Store) -> None:
"""The whole point of the group key: two groups never share a counter."""
store.events.record(event(at=1))
store.events.record(event(at=1, group_id=OTHER))
store.events.record(event(at=2, group_id=OTHER))
assert counted(store, GROUP) == 1
assert counted(store, OTHER) == 2
def test_forget_group_keeps_the_history(store: Store) -> None:
"""The reversal 0.1.0 turns on, and why it matters.
A group listing that transiently omits members makes discovery drop the
group, which calls this. Before 0.1.0 that cost a day's counter; against
a permanent history it would delete every statistic the group has.
"""
store.events.record(event())
store.events.note_person(GROUP, "u1", "Alice", "", "2026-08-29")
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
store.add_vote(GROUP, "cmd", "u1", 100.0)
store.set_meta(GROUP, "announced_version", "0.0.7")
store.forget_group(GROUP)
assert store.last_used(GROUP, "u1", "cat") is None
assert store.uses_today(GROUP, "u1", "cat", "2026-08-29") == 0
assert store.count_votes(GROUP, "cmd", 0.0) == 0
assert store.meta(GROUP, "announced_version") is None
assert counted(store, GROUP) == 1
assert [p.display_name for p in store.events.people(GROUP)] == ["Alice"]
def test_rate_limits_are_scoped_per_group(store: Store) -> None:
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
assert store.last_used(OTHER, "u1", "cat") is None
assert store.uses_today(OTHER, "u1", "cat", "2026-08-29") == 0
def test_votes_are_scoped_per_group(store: Store) -> None:
store.add_vote(GROUP, "cmd", "u1", 100.0)
store.add_vote(OTHER, "cmd", "u2", 100.0)
store.clear_votes(GROUP, "cmd")
assert store.count_votes(GROUP, "cmd", 0.0) == 0
assert store.count_votes(OTHER, "cmd", 0.0) == 1
def test_a_pre_group_database_is_rebuilt(tmp_path: Path) -> None:
"""CREATE TABLE IF NOT EXISTS is a no-op against the wrong columns.
A database written before the group key would survive startup untouched
and then fail on every INSERT: a bot that boots clean and answers
nothing. PRAGMA user_version is the only part of the file that knows.
"""
db = str(tmp_path / "old.sqlite3")
legacy = sqlite3.connect(db)
legacy.execute("CREATE TABLE command_uses (uuid TEXT PRIMARY KEY, used_at REAL NOT NULL)")
legacy.execute("INSERT INTO command_uses VALUES ('u1', 7.0)")
legacy.commit()
legacy.close()
with open_store(db) as store:
store.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
assert store.last_used(GROUP, "u1", "cat") == 100.0
check = sqlite3.connect(db)
assert int(check.execute("PRAGMA user_version").fetchone()[0]) == SCHEMA_VERSION
check.close()
def test_a_schema_rebuild_never_drops_the_history(tmp_path: Path) -> None:
"""The second destructive path the EPHEMERAL/PERMANENT split governs.
forget_group is the obvious one. This is the other: a future session
bumping SCHEMA_VERSION for some cooldown table must not take years of
message history with it.
"""
db = str(tmp_path / "bump.sqlite3")
with open_store(db) as first:
first.events.record(event())
first.record_use(GROUP, "u1", "cat", 100.0, "2026-08-29")
aged = sqlite3.connect(db)
aged.execute("PRAGMA user_version = 0")
aged.commit()
aged.close()
with open_store(db) as second:
assert counted(second, GROUP) == 1
assert second.last_used(GROUP, "u1", "cat") is None
def test_a_current_database_is_left_alone(tmp_path: Path) -> None:
"""Reopening must not wipe the history on every restart."""
db = str(tmp_path / "current.sqlite3")
with open_store(db) as first:
first.events.record(event())
with open_store(db) as second:
assert counted(second, GROUP) == 1