# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The append-only message log: dedupe, naming, and the roster join.""" from __future__ import annotations from typing import TYPE_CHECKING from signal_bot.store import Store, open_store from tests.test_store import GROUP, counted, event if TYPE_CHECKING: from pathlib import Path def test_the_event_table_holds_no_message_body(store: Store) -> None: """The privacy guarantee, pinned so a later edit cannot quietly break it. The statistics need to know somebody wrote 140 characters at 23:41, never what they wrote. Any new column here is a deliberate decision, not a convenient place to stash a body. """ columns = { row["name"] for row in store.events.connection.execute("PRAGMA table_info(message_events)").fetchall() } assert columns == { "group_id", "uuid", "display_name", "sent_at_ms", "day", "local_hour", "media_kind", "media_count", "char_len", "is_command", "is_reply", } def test_events_accumulate(store: Store) -> None: store.events.record(event(at=1)) store.events.record(event(at=2)) store.events.record(event(uuid="u2", name="Bob", at=3)) assert counted(store, GROUP) == 3 def test_a_replayed_frame_is_recorded_once(store: Store) -> None: """signal-cli replays undelivered frames when the bot reconnects. Without ON CONFLICT DO NOTHING every restart would inflate the counts the bot had just published, and nothing in the output would look wrong. """ store.events.record(event(at=7)) store.events.record(event(at=7)) assert counted(store, GROUP) == 1 def test_a_rename_does_not_rewrite_past_attribution(store: Store) -> None: """group_people follows the new name; the events keep the old one.""" store.events.record(event(name="Alice", at=1)) store.events.note_person(GROUP, "u1", "Alice", "", "2026-08-29") store.events.record(event(name="Alicja", at=2)) store.events.note_person(GROUP, "u1", "Alicja", "", "2026-08-30") assert store.events.people(GROUP)[0].display_name == "Alicja" rows = store.events.connection.execute( "SELECT display_name FROM message_events WHERE group_id = ? ORDER BY sent_at_ms", (GROUP,), ).fetchall() assert [r["display_name"] for r in rows] == ["Alice", "Alicja"] def test_a_known_number_is_not_erased_by_an_empty_one(store: Store) -> None: """The roster join exists for exactly the people spelled by number.""" store.events.note_person(GROUP, "u1", "Alice", "+15550001111", "2026-08-29") store.events.note_person(GROUP, "u1", "Alice", "", "2026-08-30") assert store.events.people(GROUP)[0].number == "+15550001111" def test_roster_matches_either_spelling(store: Store) -> None: """The listing gives a uuid for some members and a number for others.""" store.events.note_person(GROUP, "u1", "Alice", "+15550001111", "2026-08-29") store.events.note_person(GROUP, "u2", "Bob", "", "2026-08-29") assert [p.display_name for p in store.events.roster(GROUP, ["+15550001111"])] == ["Alice"] assert [p.display_name for p in store.events.roster(GROUP, ["u2"])] == ["Bob"] assert store.events.roster(GROUP, []) == [] assert store.events.roster(GROUP, ["nobody"]) == [] def test_first_day_is_none_without_history(store: Store) -> None: assert store.events.first_day(GROUP) is None store.events.record(event(day="2026-08-29")) store.events.record(event(at=2, day="2026-08-30")) assert store.events.first_day(GROUP) == "2026-08-29" def test_events_survive_a_restart(tmp_path: Path) -> None: db = str(tmp_path / "restart.sqlite3") with open_store(db) as first: first.events.record(event()) with open_store(db) as second: assert counted(second, GROUP) == 1