# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The alias editor: written to one file, made live by the bot, or undone.""" from __future__ import annotations import json from typing import TYPE_CHECKING import httpx import pytest from signal_bot.custom_aliases import load from tests.manage_fixture import bot_is_down, client_for, deps_at if TYPE_CHECKING: from pathlib import Path RELOADED = {"status": "reloaded", "commands": 24} def _bot_that_reloads(request: httpx.Request) -> httpx.Response: assert request.method == "POST" assert request.url.path == "/_reload" return httpx.Response(200, json=RELOADED) def test_the_table_starts_empty_and_a_put_fills_it(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) with client_for(deps) as client: assert client.get("/api/aliases").json() == {"aliases": {}} answer = client.put("/api/aliases/cat", json={"triggers": ["kotek", "!kicia", "kotek"]}) listed = client.get("/api/aliases").json() assert answer.status_code == 200 assert answer.json() == {"name": "cat", "triggers": ["!kotek", "!kicia"], **RELOADED} assert listed == {"aliases": {"cat": ["!kotek", "!kicia"]}} assert load(deps.aliases_path) == {"cat": ("!kotek", "!kicia")} def test_an_empty_list_removes_the_command_from_the_file(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) with client_for(deps) as client: client.put("/api/aliases/cat", json={"triggers": ["!kotek"]}) client.put("/api/aliases/dog", json={"triggers": ["!piesek"]}) answer = client.put("/api/aliases/cat", json={"triggers": []}) assert answer.status_code == 200 assert load(deps.aliases_path) == {"dog": ("!piesek",)} def test_a_trigger_of_several_words_is_refused_before_anything_is_written( tmp_path: Path, ) -> None: deps = deps_at(tmp_path, _bot_that_reloads) with client_for(deps) as client: answer = client.put("/api/aliases/cat", json={"triggers": ["!kot ładny"]}) assert answer.status_code == 400 assert "one word" in answer.json()["detail"] assert not deps.aliases_path.exists() @pytest.mark.parametrize( ("code", "body", "expected"), [ (409, {"status": "kept", "commands": 24, "reason": "trigger '!kot' is taken"}, "taken"), (503, {"status": "connecting", "reason": "attempt 2: refused"}, "attempt 2"), ], ) def test_a_reload_the_bot_refuses_puts_the_file_back( tmp_path: Path, code: int, body: dict[str, object], expected: str ) -> None: def bot(_request: httpx.Request) -> httpx.Response: return httpx.Response(code, json=body) deps = deps_at(tmp_path, bot) before = json.dumps({"dog": ["!piesek"]}) deps.aliases_path.write_text(before, encoding="utf-8") with client_for(deps) as client: answer = client.put("/api/aliases/cat", json={"triggers": ["!kot"]}) assert answer.status_code == code assert expected in answer.json()["detail"] assert deps.aliases_path.read_text(encoding="utf-8") == before def test_a_bot_that_does_not_answer_leaves_no_file_behind(tmp_path: Path) -> None: deps = deps_at(tmp_path, bot_is_down) with client_for(deps) as client: answer = client.put("/api/aliases/cat", json={"triggers": ["!kotek"]}) assert answer.status_code == 502 assert not deps.aliases_path.exists() def test_a_file_edited_by_hand_into_nonsense_is_reported_not_hidden(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) deps.aliases_path.write_text("{oops", encoding="utf-8") with client_for(deps) as client: listed = client.get("/api/aliases") put = client.put("/api/aliases/cat", json={"triggers": ["!kotek"]}) assert listed.status_code == 409 assert "cannot be read" in listed.json()["detail"] assert put.status_code == 409 assert deps.aliases_path.read_text(encoding="utf-8") == "{oops"