# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """Every attempt becomes a row, and a refusal still does not move the cooldown. The second half is the one that matters. ``CommandRefusedError``'s docstring records the bug: recording a refused run slid the cooldown window forward every time somebody was told to wait, so waiting the stated time never helped. The command history now takes refusals *and* crashes, which makes merging the two writes look like an obvious simplification. It is not, and test_a_refusal_does_not_move_the_cooldown is what says so. """ from __future__ import annotations import datetime as dt from typing import TYPE_CHECKING, Any import pytest from signal_bot.analytics import probe from signal_bot.dispatcher import Dispatcher from signal_bot.registry import CommandRegistry from signal_bot.registry_holder import RegistryHolder from tests.conftest import WATCHED_GROUP, envelope if TYPE_CHECKING: from collections.abc import Callable from signal_bot.config import Config from signal_bot.context import CommandContext from signal_bot.discovery import GroupDirectory from signal_bot.store import Store from signal_bot.transport_fake import FakeTransport NOW = dt.datetime(2026, 8, 29, 12, 0, tzinfo=dt.UTC) SENDER = "uuid-alice" def history(store: Store) -> list[dict[str, Any]]: """Every recorded attempt, oldest first.""" return [ dict(row) for row in store.commands.connection.execute( "SELECT * FROM command_events ORDER BY invoked_at_ms" ) ] def calls(store: Store) -> list[dict[str, Any]]: """Every recorded upstream call.""" return [ dict(row) for row in store.commands.connection.execute("SELECT * FROM upstream_calls ORDER BY seq") ] def build( config: Config, directory: GroupDirectory, registry: CommandRegistry, store: Store, transport: FakeTransport, clock: Callable[[], dt.datetime] = lambda: NOW, ) -> Dispatcher: """A dispatcher over one caller-supplied registry.""" return Dispatcher( config=config, directory=directory, holder=RegistryHolder(lambda: registry), store=store, transport=transport, clock=clock, ) @pytest.fixture def ok_registry() -> CommandRegistry: """One command that succeeds and reports what it did on the way.""" async def handler(_ctx: CommandContext) -> None: probe.note_upstream("api.example.test", 200, 40) probe.note_send(120) probe.note_item("Q42") reg = CommandRegistry() reg.register("cat", ["!cat"], handler, "kot") return reg async def test_a_success_is_recorded_with_what_it_cost( config: Config, directory: GroupDirectory, ok_registry: CommandRegistry, store: Store, transport: FakeTransport, ) -> None: """The row nothing wrote before: outcome, item, and where the time went.""" dispatcher = build(config, directory, ok_registry, store, transport) await dispatcher.handle(envelope(text="!cat")) (row,) = history(store) assert row["command"] == "cat" assert row["outcome"] == "ok" assert row["item_id"] == "Q42" assert row["send_ms"] == 120 assert row["source"] == "live" assert row["handler_ms"] >= 0 assert calls(store) == [ { "group_id": WATCHED_GROUP, "invoked_at_ms": row["invoked_at_ms"], "command": "cat", "host": "api.example.test", "status": 200, "duration_ms": 40, "seq": 0, } ] async def test_the_history_and_the_message_share_a_key( config: Config, directory: GroupDirectory, ok_registry: CommandRegistry, store: Store, transport: FakeTransport, ) -> None: """Both tables key on the envelope's own timestamp, or nothing joins.""" dispatcher = build(config, directory, ok_registry, store, transport) await dispatcher.handle(envelope(text="!cat")) (event,) = history(store) row = store.events.connection.execute( "SELECT sent_at_ms, local_hour, day FROM message_events WHERE is_command = 1" ).fetchone() assert event["invoked_at_ms"] == row["sent_at_ms"] assert event["local_hour"] == row["local_hour"] assert event["day"] == row["day"]