From 72ebdd972589ad4258ee4ac895ba8e8df57a1291 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:38:46 +0000 Subject: [PATCH] support bot light: /feed sends a message to all chats (#8134) * support bot light: /feed sends a message to all chats * support bot light: --broadcasters as in the main support bot * support bot light: broadcasters and queue size in config.toml --------- Co-authored-by: sh-agents <350651734+sh-agents@users.noreply.github.com> --- .github/workflows/build.yml | 1 + apps/simplex-support-bot-light/README.md | 21 ++++ .../bot-config/config.toml.example | 4 + .../config.toml.example | 4 + .../src/support_bot_light/__main__.py | 37 +++++- .../src/support_bot_light/commands.py | 2 + .../src/support_bot_light/config.py | 35 ++++++ .../src/support_bot_light/context.py | 1 + .../src/support_bot_light/handlers.py | 48 +++++++- .../src/support_bot_light/messages.py | 17 ++- .../tests/conftest.py | 14 +++ .../tests/test_boundaries.py | 1 + .../tests/test_commands.py | 8 +- .../tests/test_config.py | 49 +++++++- .../tests/test_handlers.py | 106 +++++++++++++++++- .../tests/test_main.py | 82 ++++++++++++-- .../tests/test_messages.py | 13 ++- scripts/ci/README.md | 9 +- scripts/ci/packages_release.py | 1 + scripts/ci/scope-tests.sh | 11 +- scripts/ci/test_packages_release.py | 8 ++ 21 files changed, 448 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c90accfbf2..28d7aa7747 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -43,6 +43,7 @@ on: - "website/package.json" - "packages/simplex-chat-mcp/**" - "apps/simplex-support-bot/**" + - "apps/simplex-support-bot-light/**" types: [opened, synchronize, reopened, ready_for_review] workflow_dispatch: inputs: diff --git a/apps/simplex-support-bot-light/README.md b/apps/simplex-support-bot-light/README.md index 058f539d3f..b2a2ec5839 100644 --- a/apps/simplex-support-bot-light/README.md +++ b/apps/simplex-support-bot-light/README.md @@ -69,8 +69,10 @@ the bot; a second `Ctrl+C` exits immediately. | `bot.image` | no | Profile image path (`.png`, `.jpg`, `.jpeg`). Relative paths resolve against the directory containing `config.toml`. The encoded image must not exceed 12500 characters, roughly a 128x128 avatar. | | `bot.db_prefix` | yes | SQLite path prefix. Creates `_chat.db` and `_agent.db`. Under Docker it must point inside `/data`. | | `bot.welcome` | yes | Message posted into each new business chat, sent as the address auto-reply. Multi-line TOML strings are supported. | +| `bot.queue_size` | no | Internal queue size of the chat core, as the CLI's `--queue-size`. Defaults to the core's `1024`. | | `roster.group_name` | yes | Name of the roster group, applied when it is created. | | `roster.member_role` | no | Role roster members receive in business chats: `observer`, `author`, `member`, `moderator`, `admin` or `owner`. Defaults to `owner`. | +| `roster.broadcasters` | no | Contacts allowed to use `/feed`, as `"ID:name"` strings, such as `["12:Alice", "15:Bob"]`. See [Feed](#feed). | | `health.enabled` | no | Set `false` to switch the monitoring endpoint off. On by default. | | `health.host` | no | Interface the endpoint binds. Defaults to `127.0.0.1`; `0.0.0.0` under Docker. | | `health.port` | no | Port for the endpoint. Defaults to `8080`. Setting either key makes a bind failure fatal. | @@ -107,11 +109,30 @@ Available in the roster group. | `/list` | List active members, members who are no longer reachable, and those pending a contact request. | | `/leave` | Leave the roster. Chats already joined are unaffected. | | `/help` | Summarise the above. | +| `/feed ` | Send a message to every chat. Only for `roster.broadcasters`. | Leaving the roster group, or being removed from it, also takes a member off the roster. The bot is the group's only owner, so removing another member requires a client signed in as the bot. +## Feed + +A contact listed in `roster.broadcasters` sends `/feed ` in the roster group. +The bot sends the text as a feed message to every business chat and every +direct contact of the bot, replies that it is being sent, and posts again when +it is sent to all chats or fails. The text may span several lines. + +Feeds are part of the core, so `/feed` needs a `libsimplex` built from this +tree, as the Docker image builds it; no released version has them. A manual +installation sets `SIMPLEX_LIBS_DIR` to the output of +`scripts/desktop/build-lib-linux.sh`, in +`apps/multiplatform/common/src/commonMain/cpp/desktop/libs/linux-$(uname -m)`. + +`roster.broadcasters` lists `"ID:name"` strings, such as `["12:Alice", "15:Bob"]`. +A broadcaster must be a contact of the bot; `/feed` from anyone else replies +with their contact id, or asks them to run `/dm` when they have no contact. The +bot exits at start if an id is not a contact or its display name differs. + ## State All state is in the databases at `bot.db_prefix`. Roster membership is stored in diff --git a/apps/simplex-support-bot-light/bot-config/config.toml.example b/apps/simplex-support-bot-light/bot-config/config.toml.example index 08228a47b3..653a5a77c5 100644 --- a/apps/simplex-support-bot-light/bot-config/config.toml.example +++ b/apps/simplex-support-bot-light/bot-config/config.toml.example @@ -11,6 +11,8 @@ display_name = "Support" # _agent.db, which hold the bot's identity. db_prefix = "/data/support_bot_light" welcome = "Hi! Someone from the team will join this chat in a moment." +# Optional. Internal queue size of the chat core; defaults to 1024. +# queue_size = 65536 [roster] # Renaming the group later has no effect: it is found by a marker in its custom @@ -19,6 +21,8 @@ group_name = "Invite roster" # One of: observer, author, member, moderator, admin, owner. "relay" is also # accepted by the core but is an infrastructure role. member_role = "owner" +# Optional. Contacts allowed to use /feed in the roster group, as "ID:name". +# broadcasters = ["12:Alice", "15:Bob"] # Monitoring endpoint: GET /health answers 200 while the chat controller # responds to a command, 503 when it does not. It must bind 0.0.0.0 to be diff --git a/apps/simplex-support-bot-light/config.toml.example b/apps/simplex-support-bot-light/config.toml.example index 7c7b3e6942..573a2cdf0a 100644 --- a/apps/simplex-support-bot-light/config.toml.example +++ b/apps/simplex-support-bot-light/config.toml.example @@ -7,6 +7,8 @@ display_name = "Support" # Creates _chat.db and _agent.db. db_prefix = "./support_bot_light" welcome = "Hi! Someone from the team will join this chat in a moment." +# Optional. Internal queue size of the chat core; defaults to 1024. +# queue_size = 65536 [roster] # Renaming the group later has no effect: it is found by a marker in its custom @@ -15,6 +17,8 @@ group_name = "Invite roster" # One of: observer, author, member, moderator, admin, owner. "relay" is also # accepted by the core but is an infrastructure role. member_role = "owner" +# Optional. Contacts allowed to use /feed in the roster group, as "ID:name". +# broadcasters = ["12:Alice", "15:Bob"] # Monitoring endpoint: GET /health answers 200 while the chat controller # responds to a command, 503 when it does not. On by default, at the values diff --git a/apps/simplex-support-bot-light/src/support_bot_light/__main__.py b/apps/simplex-support-bot-light/src/support_bot_light/__main__.py index a7476643a2..b4c09fe93b 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/__main__.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/__main__.py @@ -10,13 +10,14 @@ import stat import sys from pathlib import Path -from simplex_chat import Bot, BotProfile, ChatError, SqliteDb +from simplex_chat import Bot, BotProfile, ChatApi, ChatError, SqliteDb from simplex_chat.core import ChatInitError from simplex_chat.types import CEvt from . import business, commands, handlers, health, setup -from .config import Config, ConfigError, load_config +from .config import Config, ConfigError, IdName, load_config from .context import BotContext +from .text import safe_name log = logging.getLogger("support_bot_light") @@ -49,6 +50,10 @@ def _register(bot: Bot, ctx: BotContext) -> None: async def _help(msg, _cmd): await handlers.help_cmd(ctx, msg) + @bot.on_command(commands.FEED, group_id=group_id) + async def _feed(msg, _cmd): + await handlers.feed(ctx, msg) + @bot.on_event("acceptingBusinessRequest") async def _business(evt: CEvt.AcceptingBusinessRequest): await business.on_business_request(ctx, evt) @@ -69,6 +74,10 @@ def _register(bot: Bot, ctx: BotContext) -> None: async def _left_member(evt: CEvt.LeftMember): await handlers.member_gone(ctx, evt["groupInfo"]["groupId"], evt["member"]) + @bot.on_event("chatItemsStatusesUpdated") + async def _statuses(evt: CEvt.ChatItemsStatusesUpdated): + await handlers.feed_status(ctx, evt) + def startup_error(e: Exception) -> str: """What the operator can act on, from an exception that names only a tag. @@ -89,6 +98,23 @@ def startup_error(e: Exception) -> str: return f"{e} {chat_error}" if chat_error else str(e) +async def check_broadcasters(api: ChatApi, user_id: int, broadcasters: tuple[IdName, ...]) -> None: + if not broadcasters: + return + contacts = {c["contactId"]: c for c in await api.api_list_contacts(user_id)} + for b in broadcasters: + contact = contacts.get(b.id) + if contact is None: + raise ConfigError(f"broadcaster not found: ID={b.id}") + name = contact["profile"]["displayName"] + if name != b.name: + raise ConfigError( + f'broadcaster name mismatch: expected "{b.name}", ' + f'got "{safe_name(name)}" (ID={b.id})' + ) + log.info("broadcaster validated: %d:%s", b.id, safe_name(b.name)) + + def bot_profile(config: Config) -> BotProfile: return BotProfile(display_name=config.display_name, image=config.image) @@ -106,6 +132,7 @@ def build_bot(config: Config) -> Bot: return Bot( profile=bot_profile(config), db=SqliteDb(file_prefix=config.db_prefix), + queue_size=config.queue_size, welcome=config.welcome, business_address=True, auto_accept=True, @@ -146,6 +173,7 @@ async def _serve(config: Config, bot: Bot) -> None: if user is None: raise RuntimeError("no active user after start") user_id = user["userId"] + await check_broadcasters(bot.api, user_id, config.broadcasters) await _apply_profile(bot) roster_group_id = await setup.ensure_roster_group(bot.api, user_id, config) ctx = BotContext( @@ -195,8 +223,9 @@ def main() -> int: try: asyncio.run(_run(config)) except ConfigError as e: - # Raised past load_config only by the health endpoint, which cannot know - # its port is taken until it binds. + # Raised past load_config by the health endpoint, which cannot know its + # port is taken until it binds, and by the broadcaster check, which needs + # the database. log.error("%s", e) return 2 except STARTUP_ERRORS as e: diff --git a/apps/simplex-support-bot-light/src/support_bot_light/commands.py b/apps/simplex-support-bot-light/src/support_bot_light/commands.py index c081a40e37..6f8f65e507 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/commands.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/commands.py @@ -11,12 +11,14 @@ DM = "dm" LIST = "list" LEAVE = "leave" HELP = "help" +FEED = "feed" COMMANDS: tuple[BotCommand, ...] = ( BotCommand(keyword=DM, label="Add me to incoming chats"), BotCommand(keyword=LIST, label="Who gets invited"), BotCommand(keyword=LEAVE, label="Stop adding me"), BotCommand(keyword=HELP, label="How this works"), + BotCommand(keyword=FEED, label="Send to all chats", params="text"), ) diff --git a/apps/simplex-support-bot-light/src/support_bot_light/config.py b/apps/simplex-support-bot-light/src/support_bot_light/config.py index 79dd3bfefb..ed6b8238be 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/config.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/config.py @@ -52,6 +52,12 @@ class Health: configured: bool = False +@dataclass(frozen=True, slots=True) +class IdName: + id: int + name: str + + @dataclass(frozen=True, slots=True) class Config: """Validated settings loaded from `config.toml`.""" @@ -63,6 +69,8 @@ class Config: member_role: T.GroupMemberRole image: str | None = None health: Health | None = None + broadcasters: tuple[IdName, ...] = () + queue_size: int | None = None def load_config(path: Path) -> Config: @@ -97,9 +105,36 @@ def load_config(path: Path) -> Config: member_role=role, image=_image(bot, path.parent), health=_health(raw), + broadcasters=_broadcasters(roster), + queue_size=_queue_size(bot), ) +def _broadcasters(roster: dict[str, Any]) -> tuple[IdName, ...]: + value = roster.get("broadcasters", []) + if not isinstance(value, list): + raise ConfigError('roster.broadcasters must be a list of "ID:name" strings') + return tuple(_id_name(item) for item in value) + + +def _id_name(item: Any) -> IdName: + if isinstance(item, str): + contact_id, sep, name = item.partition(":") + if sep and contact_id.strip().isdigit(): + return IdName(int(contact_id), name) + raise ConfigError(f'roster.broadcasters entries must be "ID:name", got {item!r}') + + +def _queue_size(bot: dict[str, Any]) -> int | None: + value = bot.get("queue_size") + if value is None: + return None + # bool is an int, and TOML has booleans. + if not isinstance(value, int) or isinstance(value, bool) or value < 1: + raise ConfigError(f"bot.queue_size must be a positive integer, got {value!r}") + return value + + def _health(raw: dict[str, Any]) -> Health | None: """Where the endpoint listens, or None when `health.enabled` switches it off.""" health = raw.get("health", {}) diff --git a/apps/simplex-support-bot-light/src/support_bot_light/context.py b/apps/simplex-support-bot-light/src/support_bot_light/context.py index d168763557..b7904e04fb 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/context.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/context.py @@ -23,6 +23,7 @@ class BotContext: # Business chats repaired by the startup pass, so the queued event for the # same chat does not report the customer a second time. repaired: set[int] = field(default_factory=set) + feed_items: set[int] = field(default_factory=set) async def post_to_roster(self, text: str) -> None: """Send a message to the roster group. diff --git a/apps/simplex-support-bot-light/src/support_bot_light/handlers.py b/apps/simplex-support-bot-light/src/support_bot_light/handlers.py index 6870c57126..130573ee4e 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/handlers.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/handlers.py @@ -3,11 +3,12 @@ from __future__ import annotations import functools +import json import logging from collections.abc import Awaitable, Callable -from simplex_chat import ChatError, Message -from simplex_chat.types import T +from simplex_chat import ChatCommandError, ChatError, Message, util +from simplex_chat.types import CEvt, T from . import messages, roster, setup from .context import BotContext @@ -298,3 +299,46 @@ async def leave(ctx: BotContext, msg: Message) -> None: async def help_cmd(ctx: BotContext, msg: Message) -> None: """Reply with the help text.""" await msg.reply(messages.HELP) + + +@_reply_on_error +async def feed(ctx: BotContext, msg: Message) -> None: + member = group_sender(msg) + if member is None: + return + contact = await _contact_of(ctx, member) + if contact is None: + await msg.reply(messages.FEED_NO_CONTACT) + return + if not any(b.id == contact["contactId"] for b in ctx.config.broadcasters): + await msg.reply(messages.FEED_NOT_ALLOWED.format(contact_id=contact["contactId"])) + return + command = util.ci_bot_command(msg.chat_item["chatItem"]) + text = command[1] if command else "" + if not text: + await msg.reply(messages.FEED_EMPTY) + return + r = await ctx.api.send_chat_cmd(f"/feed {json.dumps(text, ensure_ascii=False)}") + if r["type"] != "newChatItems": + raise ChatCommandError("unexpected response to /feed", r) + item_id = r["chatItems"][0]["chatItem"]["meta"]["itemId"] + ctx.feed_items.add(item_id) + await msg.reply(messages.FEED_QUEUED.format(item_id=item_id)) + + +async def feed_status(ctx: BotContext, evt: CEvt.ChatItemsStatusesUpdated) -> None: + for item in evt["chatItems"]: + if item["chatInfo"]["type"] != "feed": + continue + item_id = item["chatItem"]["meta"]["itemId"] + if item_id not in ctx.feed_items: + continue + status = item["chatItem"]["meta"]["itemStatus"] + if status["type"] == "sndSent" and status["sndProgress"] == "complete": + report = messages.FEED_SENT.format(item_id=item_id) + elif status["type"] == "sndError": + report = messages.feed_failed(item_id, status["agentError"]) + else: + continue + ctx.feed_items.discard(item_id) + await ctx.post_to_roster(report) diff --git a/apps/simplex-support-bot-light/src/support_bot_light/messages.py b/apps/simplex-support-bot-light/src/support_bot_light/messages.py index f6e136e7ba..cc58638841 100644 --- a/apps/simplex-support-bot-light/src/support_bot_light/messages.py +++ b/apps/simplex-support-bot-light/src/support_bot_light/messages.py @@ -2,6 +2,7 @@ from __future__ import annotations +import json from collections.abc import Sequence from .roster import ACTIVE, RosterEntry @@ -38,13 +39,23 @@ NOBODY_NEW_LOG = "Connected: {customer} → everyone on the roster was already i COMMAND_FAILED = "The command failed. Try again." REVOKE_FAILED = "Could not take {name} off the roster — retrying on the next restart." BUSINESS_FAILED_LOG = "Connected: {customer} → could not set up the chat, nobody added" +FEED_QUEUED = "Feed message {item_id} is being sent to all chats." +FEED_SENT = "Feed message {item_id} sent to all chats." +FEED_FAILED = "Feed message {item_id} failed: {error}" +FEED_EMPTY = "Add the message text after /feed." +FEED_NOT_ALLOWED = ( + "Only broadcasters can use /feed. Your contact id is {contact_id}: " + "the operator adds it to roster.broadcasters in config.toml." +) +FEED_NO_CONTACT = "Only broadcasters can use /feed, and they need a direct contact: run /dm first." HELP = ( "I add roster members to chats started by anyone who connects to my address.\n\n" "/dm — join the roster. Without a direct contact I send a contact request; " "you join the roster once you accept it.\n" "/list — roster members, and contact requests not yet accepted.\n" - "/leave — leave the roster. Chats you have already joined are unchanged." + "/leave — leave the roster. Chats you have already joined are unchanged.\n" + "/feed — send a message to every chat; only for broadcasters set by the operator." ) @@ -111,3 +122,7 @@ def invite_log(customer: str, added: Sequence[str], failed: Sequence[str]) -> st if failed: line += f" (failed: {', '.join(failed)})" return _bounded(line) + + +def feed_failed(item_id: int, error: object) -> str: + return _bounded(FEED_FAILED.format(item_id=item_id, error=json.dumps(error))) diff --git a/apps/simplex-support-bot-light/tests/conftest.py b/apps/simplex-support-bot-light/tests/conftest.py index d13e012589..a636383d02 100644 --- a/apps/simplex-support-bot-light/tests/conftest.py +++ b/apps/simplex-support-bot-light/tests/conftest.py @@ -41,6 +41,7 @@ class FakeChatApi: self._next_item_id = 1000 self._member_contacts_created: set[tuple[int, int]] = set() self.accepted_member_contacts: list[int] = [] + self.commands: list[str] = [] def _check(self, name: str) -> None: if name in self.fail_on: @@ -173,6 +174,12 @@ class FakeChatApi: except KeyError: raise ChatAPIError("no group link", {"type": "chatCmdError"}) from None + async def send_chat_cmd(self, cmd: str) -> dict: + self._check("send_chat_cmd") + self.commands.append(cmd) + self._next_item_id += 1 + return {"type": "newChatItems", "chatItems": [make_feed_item(self._next_item_id)]} + def make_contact( contact_id: int, @@ -241,6 +248,13 @@ def make_group( return group +def make_feed_item(item_id: int, status: dict | None = None) -> dict: + return { + "chatInfo": {"type": "feed", "feed": {"feedId": 1}}, + "chatItem": {"meta": {"itemId": item_id, "itemStatus": status or {"type": "sndNew"}}}, + } + + def join_roster_group(api: FakeChatApi) -> None: """Put every contact in the roster group. diff --git a/apps/simplex-support-bot-light/tests/test_boundaries.py b/apps/simplex-support-bot-light/tests/test_boundaries.py index b34158c336..4d28326c2a 100644 --- a/apps/simplex-support-bot-light/tests/test_boundaries.py +++ b/apps/simplex-support-bot-light/tests/test_boundaries.py @@ -121,4 +121,5 @@ def test_the_command_menu_carries_every_command(): commands.LIST, commands.LEAVE, commands.HELP, + commands.FEED, ] diff --git a/apps/simplex-support-bot-light/tests/test_commands.py b/apps/simplex-support-bot-light/tests/test_commands.py index 83c435aacb..926f5a933d 100644 --- a/apps/simplex-support-bot-light/tests/test_commands.py +++ b/apps/simplex-support-bot-light/tests/test_commands.py @@ -3,13 +3,13 @@ from simplex_chat import BotCommand from support_bot_light.commands import COMMANDS, to_wire -def test_declares_four_commands(): - assert tuple(c.keyword for c in COMMANDS) == ("dm", "list", "leave", "help") +def test_declares_five_commands(): + assert tuple(c.keyword for c in COMMANDS) == ("dm", "list", "leave", "help", "feed") -def test_no_command_takes_params(): +def test_only_feed_takes_params(): # Zero-argument commands send on tap instead of pasting a placeholder. - assert all(c.params is None for c in COMMANDS) + assert [c.keyword for c in COMMANDS if c.params is not None] == ["feed"] def test_to_wire_omits_params_when_none(): diff --git a/apps/simplex-support-bot-light/tests/test_config.py b/apps/simplex-support-bot-light/tests/test_config.py index e3c3a7fed5..3f74ba2732 100644 --- a/apps/simplex-support-bot-light/tests/test_config.py +++ b/apps/simplex-support-bot-light/tests/test_config.py @@ -2,7 +2,7 @@ import base64 import pytest -from support_bot_light.config import Config, ConfigError, Health, load_config +from support_bot_light.config import Config, ConfigError, Health, IdName, load_config VALID = """ [bot] @@ -272,6 +272,53 @@ def test_invalid_health_settings_are_rejected(tmp_path, section): load_config(write(tmp_path, VALID + "\n" + section)) +def with_roster(line): + return VALID + line + "\n" + + +def with_bot(line): + return VALID.replace("[roster]", line + "\n\n[roster]") + + +def test_broadcasters_and_queue_size_default_to_none(tmp_path): + cfg = load_config(write(tmp_path, VALID)) + assert cfg.broadcasters == () + assert cfg.queue_size is None + + +def test_broadcasters_are_read_as_id_name_pairs(tmp_path): + cfg = load_config(write(tmp_path, with_roster('broadcasters = ["12:Alice", " 15:Bob"]'))) + assert cfg.broadcasters == (IdName(12, "Alice"), IdName(15, "Bob")) + + +def test_a_broadcaster_name_may_contain_a_colon(tmp_path): + cfg = load_config(write(tmp_path, with_roster('broadcasters = ["12:a:b"]'))) + assert cfg.broadcasters == (IdName(12, "a:b"),) + + +@pytest.mark.parametrize("value", ['"Alice"', '":Alice"', '"x:Alice"', '"12"', "12"]) +def test_invalid_broadcaster_entries_are_rejected(tmp_path, value): + with pytest.raises(ConfigError, match='roster.broadcasters entries must be "ID:name"'): + load_config(write(tmp_path, with_roster(f"broadcasters = [{value}]"))) + + +def test_broadcasters_must_be_a_list(tmp_path): + with pytest.raises( + ConfigError, match='roster.broadcasters must be a list of "ID:name" strings' + ): + load_config(write(tmp_path, with_roster('broadcasters = "12:Alice"'))) + + +def test_queue_size_is_read(tmp_path): + assert load_config(write(tmp_path, with_bot("queue_size = 65536"))).queue_size == 65536 + + +@pytest.mark.parametrize("value", ["0", "-1", '"65536"', "true", "1.5"]) +def test_invalid_queue_size_is_rejected(tmp_path, value): + with pytest.raises(ConfigError, match="bot.queue_size must be a positive integer"): + load_config(write(tmp_path, with_bot(f"queue_size = {value}"))) + + def test_a_missing_config_points_at_the_template(tmp_path): # Under Docker this is a restart loop until the operator acts, so the error # has to say what the action is. diff --git a/apps/simplex-support-bot-light/tests/test_handlers.py b/apps/simplex-support-bot-light/tests/test_handlers.py index dca98dc2d7..41b23158c3 100644 --- a/apps/simplex-support-bot-light/tests/test_handlers.py +++ b/apps/simplex-support-bot-light/tests/test_handlers.py @@ -1,13 +1,16 @@ +import dataclasses + import pytest from simplex_chat import ChatCommandError from support_bot_light import handlers, messages, roster -from support_bot_light.config import Config +from support_bot_light.config import Config, IdName from support_bot_light.context import BotContext from tests.conftest import ( ROSTER_GROUP_ID, USER_ID, make_contact, + make_feed_item, make_group, make_group_message, make_member, @@ -718,3 +721,104 @@ async def test_dm_on_an_already_active_member_announces_nothing(ctx, api): msg = make_group_message(api, make_member(1, contact_id=7, name="sh"), "/dm") await handlers.dm(ctx, msg) assert api.sent == [] + + +@pytest.fixture +def feed_ctx(ctx, api): + ctx.config = dataclasses.replace(CONFIG, broadcasters=(IdName(7, "sh"),)) + api.contacts.append(make_contact(7, "sh", connected=True)) + return ctx + + +async def test_feed_from_a_sender_is_sent_as_json(feed_ctx, api): + text = '/feed Привет\n"all"' + await handlers.feed(feed_ctx, make_group_message(api, make_member(1, contact_id=7), text)) + assert api.commands == ['/feed "Привет\\n\\"all\\""'] + assert feed_ctx.feed_items == {1001} + assert api.replies == [messages.FEED_QUEUED.format(item_id=1001)] + + +async def test_feed_from_a_contact_not_in_the_config_is_refused(feed_ctx, api): + api.contacts.append(make_contact(8, "Alex", connected=True)) + msg = make_group_message(api, make_member(2, contact_id=8), "/feed hi") + await handlers.feed(feed_ctx, msg) + assert api.commands == [] + assert api.replies == [messages.FEED_NOT_ALLOWED.format(contact_id=8)] + + +async def test_feed_without_a_direct_contact_is_refused(feed_ctx, api): + await handlers.feed(feed_ctx, make_group_message(api, make_member(2), "/feed hi")) + assert api.commands == [] + assert api.replies == [messages.FEED_NO_CONTACT] + + +async def test_feed_is_refused_to_everyone_without_broadcasters(ctx, api): + api.contacts.append(make_contact(7, "sh", connected=True)) + await handlers.feed(ctx, make_group_message(api, make_member(1, contact_id=7), "/feed hi")) + assert api.commands == [] + assert api.replies == [messages.FEED_NOT_ALLOWED.format(contact_id=7)] + + +@pytest.mark.parametrize("text", ["/feed", "/feed \n "]) +async def test_feed_without_text_asks_for_it(feed_ctx, api, text): + await handlers.feed(feed_ctx, make_group_message(api, make_member(1, contact_id=7), text)) + assert api.commands == [] + assert api.replies == [messages.FEED_EMPTY] + + +async def test_feed_replies_command_failed_when_the_core_refuses(feed_ctx, api): + api.fail_on.add("send_chat_cmd") + await handlers.feed(feed_ctx, make_group_message(api, make_member(1, contact_id=7), "/feed hi")) + assert feed_ctx.feed_items == set() + assert api.replies == [messages.COMMAND_FAILED] + + +async def test_feed_with_an_unexpected_response_replies_command_failed(feed_ctx, api): + async def cmd_ok(cmd): + return {"type": "cmdOk"} + + api.send_chat_cmd = cmd_ok + await handlers.feed(feed_ctx, make_group_message(api, make_member(1, contact_id=7), "/feed hi")) + assert feed_ctx.feed_items == set() + assert api.replies == [messages.COMMAND_FAILED] + + +def statuses(*items): + return { + "type": "chatItemsStatusesUpdated", + "user": {"userId": USER_ID}, + "chatItems": list(items), + } + + +async def test_feed_status_reports_completion_once(ctx, api): + ctx.feed_items.add(5) + await handlers.feed_status( + ctx, statuses(make_feed_item(5, {"type": "sndSent", "sndProgress": "partial"})) + ) + assert api.sent == [] + complete = make_feed_item(5, {"type": "sndSent", "sndProgress": "complete"}) + await handlers.feed_status(ctx, statuses(complete)) + await handlers.feed_status(ctx, statuses(complete)) + assert api.sent == [(["group", ROSTER_GROUP_ID], messages.FEED_SENT.format(item_id=5))] + assert ctx.feed_items == set() + + +async def test_feed_status_reports_an_error(ctx, api): + ctx.feed_items.add(5) + error = {"type": "other", "sndError": "feed job 1 failed"} + await handlers.feed_status( + ctx, statuses(make_feed_item(5, {"type": "sndError", "agentError": error})) + ) + assert api.sent == [(["group", ROSTER_GROUP_ID], messages.feed_failed(5, error))] + assert ctx.feed_items == set() + + +async def test_feed_status_ignores_items_it_did_not_send(ctx, api): + ctx.feed_items.add(5) + complete = {"type": "sndSent", "sndProgress": "complete"} + other_chat = make_feed_item(5, complete) + other_chat["chatInfo"] = {"type": "group", "groupInfo": make_group(3, {"displayName": "c"})} + await handlers.feed_status(ctx, statuses(make_feed_item(6, complete), other_chat)) + assert api.sent == [] + assert ctx.feed_items == {5} diff --git a/apps/simplex-support-bot-light/tests/test_main.py b/apps/simplex-support-bot-light/tests/test_main.py index c49b01dbcb..09ff1803fd 100644 --- a/apps/simplex-support-bot-light/tests/test_main.py +++ b/apps/simplex-support-bot-light/tests/test_main.py @@ -1,3 +1,6 @@ +import dataclasses +import sys + import pytest from simplex_chat import Bot, BotProfile, SqliteDb from simplex_chat.core import ChatAPIError @@ -9,17 +12,20 @@ from support_bot_light.__main__ import ( _serve, bot_profile, build_bot, + main, startup_error, ) -from support_bot_light.config import Config, Health +from support_bot_light.config import Config, ConfigError, Health, IdName from support_bot_light.context import BotContext from tests.conftest import ( ROSTER_GROUP_ID, USER_ID, + make_contact, make_group, make_group_message, make_member, ) +from tests.test_config import VALID CONFIG = Config("Support", "./x", "hi", "Invite roster", "owner") OTHER_GROUP_ID = 99 @@ -40,13 +46,13 @@ def registered(api) -> Bot: return bot -def test_registers_all_four_commands(api): +def test_registers_all_five_commands(api): bot = registered(api) keywords = [names for names, _predicate, _handler in bot._command_handlers] - assert keywords == [("dm",), ("list",), ("leave",), ("help",)] + assert keywords == [("dm",), ("list",), ("leave",), ("help",), ("feed",)] -def test_registers_connection_and_business_events(api): +def test_registers_connection_business_and_status_events(api): bot = registered(api) assert set(bot._event_handlers) == { "acceptingBusinessRequest", @@ -54,6 +60,7 @@ def test_registers_connection_and_business_events(api): "contactSndReady", "deletedMember", "leftMember", + "chatItemsStatusesUpdated", } @@ -84,7 +91,9 @@ def test_bot_profile_without_image(): assert bot_profile(CONFIG).image is None -@pytest.mark.parametrize("index,keyword", [(0, "dm"), (1, "list"), (2, "leave"), (3, "help")]) +@pytest.mark.parametrize( + "index,keyword", [(0, "dm"), (1, "list"), (2, "leave"), (3, "help"), (4, "feed")] +) def test_every_command_is_scoped_to_the_roster_group(api, index, keyword): # A /list answered in a business chat would show the roster to a customer. bot = registered(api) @@ -107,11 +116,11 @@ async def test_registered_handlers_call_the_matching_handler(api, monkeypatch): return handler - for name in ("dm", "list_roster", "leave", "help_cmd"): + for name in ("dm", "list_roster", "leave", "help_cmd", "feed"): monkeypatch.setattr(handlers, name, spy(name)) for keywords, _predicate, handler in bot._command_handlers: await handler(make_group_message(api, make_member(1), f"/{keywords[0]}"), None) - assert called == ["dm", "list_roster", "leave", "help_cmd"] + assert called == ["dm", "list_roster", "leave", "help_cmd", "feed"] def test_a_taken_display_name_is_explained(): @@ -148,6 +157,11 @@ def test_the_bot_opens_a_business_address(): assert bot._welcome == "hi" +def test_the_bot_passes_the_queue_size_to_the_core(): + assert build_bot(CONFIG)._queue_size is None + assert build_bot(dataclasses.replace(CONFIG, queue_size=65536))._queue_size == 65536 + + def test_the_bot_does_not_apply_its_profile_while_starting(): # The name the core will accept is only knowable from the database, which # nothing can read until the client has started. _apply_profile does it. @@ -227,13 +241,14 @@ async def test_serve_wires_the_handlers_and_serves(api): bot = FakeBot(serve_api(api)) await _serve(CONFIG, bot) assert bot.served == 1 - assert len(bot._command_handlers) == 4 # nothing is delivered without these + assert len(bot._command_handlers) == 5 # nothing is delivered without these assert set(bot._event_handlers) == { "acceptingBusinessRequest", "contactConnected", "contactSndReady", "deletedMember", "leftMember", + "chatItemsStatusesUpdated", } @@ -294,6 +309,57 @@ async def test_the_profile_is_applied_after_start(api, monkeypatch): assert bot.syncs == 1 +BROADCASTING = dataclasses.replace(CONFIG, broadcasters=(IdName(7, "sh"),)) + + +async def test_serve_checks_the_broadcasters_and_serves(api): + api.contacts.append(make_contact(7, "sh", connected=True)) + bot = FakeBot(serve_api(api)) + await _serve(BROADCASTING, bot) + assert bot.served == 1 + + +async def test_serve_stops_on_a_broadcaster_who_is_not_a_contact(api): + bot = FakeBot(serve_api(api)) + with pytest.raises(ConfigError, match="broadcaster not found: ID=7"): + await _serve(BROADCASTING, bot) + assert bot.served == 0 + + +async def test_serve_stops_on_a_broadcaster_with_another_name(api): + api.contacts.append(make_contact(7, "Alex", connected=True)) + bot = FakeBot(serve_api(api)) + with pytest.raises(ConfigError, match='expected "sh", got "Alex"'): + await _serve(BROADCASTING, bot) + assert bot.served == 0 + + +def test_broadcasters_and_queue_size_are_read_from_the_config_file(tmp_path, monkeypatch): + text = VALID.replace("[roster]", "queue_size = 65536\n\n[roster]") + (tmp_path / "config.toml").write_text( + text + 'broadcasters = ["7:sh", "9:Alex"]\n', encoding="utf-8" + ) + configs: list[Config] = [] + + async def run(config): + configs.append(config) + + monkeypatch.setattr("support_bot_light.__main__._run", run) + monkeypatch.setattr("support_bot_light.__main__.os.umask", lambda _mask: 0) + monkeypatch.setattr( + sys, "argv", ["support-bot-light", "--config", str(tmp_path / "config.toml")] + ) + assert main() == 0 + assert configs[0].broadcasters == (IdName(7, "sh"), IdName(9, "Alex")) + assert configs[0].queue_size == 65536 + + +def test_the_command_line_rejects_broadcasters(monkeypatch): + monkeypatch.setattr(sys, "argv", ["support-bot-light", "--broadcasters", "7:sh"]) + with pytest.raises(SystemExit): + main() + + async def test_run_installs_signal_handlers_before_starting(monkeypatch): # Startup runs migrations and address creation; a signal there would # otherwise kill the process mid-write. diff --git a/apps/simplex-support-bot-light/tests/test_messages.py b/apps/simplex-support-bot-light/tests/test_messages.py index e9b73ab8af..1342befb78 100644 --- a/apps/simplex-support-bot-light/tests/test_messages.py +++ b/apps/simplex-support-bot-light/tests/test_messages.py @@ -46,7 +46,7 @@ def test_invite_log_reports_failures(): def test_help_mentions_every_command(): - for keyword in ("dm", "list", "leave"): + for keyword in ("dm", "list", "leave", "feed"): assert f"/{keyword}" in messages.HELP @@ -83,3 +83,14 @@ def test_invite_log_is_bounded_in_bytes(): out = messages.invite_log("Alex", names, []) assert len(out.encode()) <= messages.MAX_REPLY_BYTES assert out.endswith(messages.TRUNCATED) + + +def test_feed_failure_quotes_the_error(): + out = messages.feed_failed(5, {"type": "other", "sndError": "x"}) + assert out == 'Feed message 5 failed: {"type": "other", "sndError": "x"}' + + +def test_feed_failure_is_bounded_in_bytes(): + out = messages.feed_failed(5, {"type": "other", "sndError": "e" * 20000}) + assert len(out.encode()) <= messages.MAX_REPLY_BYTES + assert out.endswith(messages.TRUNCATED) diff --git a/scripts/ci/README.md b/scripts/ci/README.md index 0693d4e90d..d4814235bb 100644 --- a/scripts/ci/README.md +++ b/scripts/ci/README.md @@ -153,6 +153,14 @@ The reduced build runs one Linux job, build-linux (ubuntu-22.04 x86_64), for eve - The Docker image (scripts/support-bot/Dockerfile) is not built in CI. - Local: `scripts/ci/scope-tests.sh support-bot`. +#### support-bot-light + +- Globs: + - apps/simplex-support-bot-light/** + - packages/simplex-chat-python/** +- CI, in build-linux: `scope-tests.sh support-bot-light` (step "Test changed scopes"): the Python library of the branch and the bot installed in a virtual environment, then ruff, pyright and pytest. The tests use a fake API, without libsimplex. +- Local: `scripts/ci/scope-tests.sh support-bot-light`. + #### Scopes lacking CI tests | Scope | Paths | CI | @@ -162,7 +170,6 @@ The reduced build runs one Linux job, build-linux (ubuntu-22.04 x86_64), for eve | ts-client | packages/simplex-chat-client/typescript/** | - | | webrtc | packages/simplex-chat-webrtc/** | - | | calculator-bot | apps/simplex-calculator-bot/** | - | -| support-bot-light | apps/simplex-support-bot-light/** | - | | prices | scripts/prices/** | - | | nft | eth/nft/** | - | diff --git a/scripts/ci/packages_release.py b/scripts/ci/packages_release.py index 15c00501f7..dae1b14a4b 100644 --- a/scripts/ci/packages_release.py +++ b/scripts/ci/packages_release.py @@ -91,6 +91,7 @@ SCOPE_CHANGES = { "packages/simplex-chat-client/types/typescript/**", ], "support-bot": ["apps/simplex-support-bot/**"], + "support-bot-light": ["apps/simplex-support-bot-light/**", "packages/simplex-chat-python/**"], } CABAL_FILE = "simplex-chat.cabal" CABAL_VERSION = re.compile(r"^(version:\s*)(\S+)", re.M) diff --git a/scripts/ci/scope-tests.sh b/scripts/ci/scope-tests.sh index 74c04c2842..eca861c925 100755 --- a/scripts/ci/scope-tests.sh +++ b/scripts/ci/scope-tests.sh @@ -28,8 +28,17 @@ case "${1:-}" in support-bot) (cd apps/simplex-support-bot && npm ci --no-audit --no-fund && npm run build && npm test) ;; + support-bot-light) + cd apps/simplex-support-bot-light + python3 -m venv .venv + .venv/bin/pip install -e ../../packages/simplex-chat-python -e '.[dev]' + .venv/bin/ruff check + .venv/bin/ruff format --check src tests + .venv/bin/pyright + .venv/bin/pytest tests + ;; *) - echo "usage: $0 badge-web | ci-scripts | website | mcp | support-bot" >&2 + echo "usage: $0 badge-web | ci-scripts | website | mcp | support-bot | support-bot-light" >&2 exit 1 ;; esac diff --git a/scripts/ci/test_packages_release.py b/scripts/ci/test_packages_release.py index e5bfac4823..b1bea26fdf 100644 --- a/scripts/ci/test_packages_release.py +++ b/scripts/ci/test_packages_release.py @@ -531,6 +531,14 @@ class ChangesTest(GitRepoTest): self.assertEqual(self.builds("apps/simplex-support-bot/src/bot.ts"), set()) self.assertEqual(self.values["scopes"], "support-bot") + def test_support_bot_light_change_tests_its_scope(self): + self.assertEqual(self.builds("apps/simplex-support-bot-light/src/support_bot_light/handlers.py"), set()) + self.assertEqual(self.values["scopes"], "support-bot-light") + + def test_python_library_change_builds_packages_and_tests_the_support_bot_light_scope(self): + self.assertEqual(self.builds("packages/simplex-chat-python/src/simplex_chat/client.py"), {"packages"}) + self.assertEqual(self.values["scopes"], "support-bot-light") + class PackageStateTest(GitRepoTest): def setUp(self):