mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 19:27:20 +01:00
One row per live command on a new Commands tab of the management page,
each with an on/off switch, an optional "off until" moment and a cooldown
that overrides the bot's default for that command alone. Every switch is
live: the bot keeps them in its own SQLite (command_settings, a permanent
table) and reads the row on every attempt, so nothing goes through
/_reload and nothing restarts.
A command that is off answers "BEEP BOOP !kot WYŁĄCZONE." (with "DO HH:MM"
for a timed switch) rather than going quiet, so nobody reports it broken;
the refusal is filed as refused_off, which the analytics count with the
other refusals, and it neither starts the cooldown nor counts as a use.
The bot grows GET /_commands and PUT /_commands/{name}; the management app
proxies them under /api/commands and passes the bot's 404/503 through
unwrapped. !cisza and !odcisz carry no cooldown and the page says so
instead of accepting a number the bot would ignore (test_wiring_ungated
keeps that set honest against the real table).
Proven end to end: scripts/e2e.sh now brackets a PUT with switch_off.yaml
and switch_on.yaml, the same actor refused and then served, with no reload
between them. Also bumps vitest and @vitest/coverage-v8 to 5.0.0 and
@types/react-dom to 19.2.7, which the freshness gate asked for.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
138 lines
4.2 KiB
Python
138 lines
4.2 KiB
Python
# 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"]
|