# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The operator's switches: defaults, overrides, and what "off" means.""" from __future__ import annotations from typing import TYPE_CHECKING import pytest from signal_bot.command_settings import CommandSetting if TYPE_CHECKING: from signal_bot.store import Store NOW = 1_000.0 def test_a_command_that_was_never_touched_is_on_with_no_override(store: Store) -> None: setting = store.command_settings.get("cat") assert setting == CommandSetting("cat") assert not setting.off_at(NOW) assert store.command_settings.cooldown_for("cat", 30.0) == 30.0 assert store.command_settings.all() == {} def test_a_setting_round_trips_through_the_database(store: Store) -> None: setting = CommandSetting("cat", enabled=False, disabled_until=NOW + 60, cooldown_seconds=5) store.command_settings.put(setting) assert store.command_settings.get("cat") == setting assert store.command_settings.all() == {"cat": setting} def test_a_later_put_replaces_the_row(store: Store) -> None: """One row per command: switching back on must not leave the old row behind.""" store.command_settings.put(CommandSetting("cat", enabled=False)) store.command_settings.put(CommandSetting("cat", cooldown_seconds=1)) assert store.command_settings.get("cat") == CommandSetting("cat", cooldown_seconds=1) assert len(store.command_settings.all()) == 1 def test_the_operator_cooldown_wins_over_the_deployment_one(store: Store) -> None: store.command_settings.put(CommandSetting("cat", cooldown_seconds=0)) assert store.command_settings.cooldown_for("cat", 30.0) == 0 store.command_settings.put(CommandSetting("cat", cooldown_seconds=None)) assert store.command_settings.cooldown_for("cat", 30.0) == 30.0 @pytest.mark.parametrize( ("setting", "off"), [ (CommandSetting("cat"), False), (CommandSetting("cat", enabled=False), True), (CommandSetting("cat", disabled_until=NOW + 1), True), # Exclusive at the deadline, like a mute. (CommandSetting("cat", disabled_until=NOW), False), (CommandSetting("cat", disabled_until=NOW - 1), False), # The hard switch is not lifted by a deadline that has passed. (CommandSetting("cat", enabled=False, disabled_until=NOW - 1), True), ], ) def test_off_is_either_switch(setting: CommandSetting, *, off: bool) -> None: assert setting.off_at(NOW) is off