# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The operator's alias file, read by the real table builder.""" from __future__ import annotations import json from typing import TYPE_CHECKING import pytest from signal_bot.config import DEFAULT_COMMONS_API_URL from signal_bot.custom_aliases import ENV_VAR from signal_bot.groups import ConfigError from signal_bot.portraits import PortraitCache from signal_bot.registry import DuplicateTriggerError from signal_bot.wiring import build_registry from tests.test_help_command import _offline_fetcher if TYPE_CHECKING: from pathlib import Path from signal_bot.config import Config from signal_bot.registry import CommandRegistry def _build(config: Config) -> CommandRegistry: fetcher = _offline_fetcher() return build_registry(config, fetcher, PortraitCache(fetcher, DEFAULT_COMMONS_API_URL)) @pytest.fixture def aliases_file(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: path = tmp_path / "aliases_custom.json" monkeypatch.setenv(ENV_VAR, str(path)) return path def test_without_a_file_the_table_is_the_shipped_one(config: Config, aliases_file: Path) -> None: assert not aliases_file.exists() assert _build(config).resolve("!kotek") is None def test_an_alias_in_the_file_resolves_and_pomoc_keeps_the_shipped_spelling( config: Config, aliases_file: Path ) -> None: aliases_file.write_text(json.dumps({"cat": ["!kotek"]}), encoding="utf-8") registry = _build(config) command = registry.resolve("!kotek") assert command is not None assert command.name == "cat" assert command.primary == "!kot" def test_an_alias_for_a_command_that_does_not_exist_refuses_the_whole_table( config: Config, aliases_file: Path ) -> None: aliases_file.write_text(json.dumps({"pack:p9": ["!nobody"]}), encoding="utf-8") with pytest.raises(ConfigError, match="pack:p9"): _build(config) def test_an_alias_that_is_already_a_word_is_the_usual_clash( config: Config, aliases_file: Path ) -> None: aliases_file.write_text(json.dumps({"dog": ["!kot"]}), encoding="utf-8") with pytest.raises(DuplicateTriggerError, match="'cat'"): _build(config) def test_a_file_that_does_not_parse_is_bad_data_not_a_crash( config: Config, aliases_file: Path ) -> None: aliases_file.write_text("{oops", encoding="utf-8") with pytest.raises(ConfigError, match="cannot be read"): _build(config)