2026-06-30 00:36:29 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from nanobot.bus.events import InboundMessage
|
2026-11-03 22:47:00 +08:00
|
|
|
from nanobot.command.application import build_help_text, register_builtin_commands
|
2026-06-30 00:36:29 +08:00
|
|
|
from nanobot.command.router import CommandContext, CommandRouter
|
2026-06-30 14:36:51 +08:00
|
|
|
from nanobot.triggers.local_store import LocalTriggerStore
|
2026-06-30 00:36:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_trigger_command_creates_session_bound_local_trigger(tmp_path: Path) -> None:
|
|
|
|
|
router = CommandRouter()
|
|
|
|
|
register_builtin_commands(router)
|
2026-06-30 14:36:51 +08:00
|
|
|
store = LocalTriggerStore(tmp_path)
|
|
|
|
|
loop = SimpleNamespace(workspace=tmp_path, local_trigger_store=store)
|
2026-06-30 00:36:29 +08:00
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="chat-1",
|
|
|
|
|
content="/trigger@nanobot_bot PR review",
|
|
|
|
|
metadata={"webui": True},
|
|
|
|
|
)
|
|
|
|
|
ctx = CommandContext(
|
|
|
|
|
msg=msg,
|
|
|
|
|
session=None,
|
|
|
|
|
key="websocket:chat-1",
|
|
|
|
|
raw="/trigger@nanobot_bot PR review",
|
|
|
|
|
loop=loop,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert router.is_dispatchable_command("/trigger@nanobot_bot PR review") is True
|
|
|
|
|
response = await router.dispatch(ctx)
|
|
|
|
|
|
|
|
|
|
assert response is not None
|
|
|
|
|
assert "Trigger created: PR review" in response.content
|
|
|
|
|
trigger = store.list_for_session("websocket:chat-1")[0]
|
|
|
|
|
assert trigger.name == "PR review"
|
|
|
|
|
assert trigger.channel == "websocket"
|
|
|
|
|
assert trigger.chat_id == "chat-1"
|
|
|
|
|
assert trigger.session_key == "websocket:chat-1"
|
|
|
|
|
assert f"nanobot trigger {trigger.id} \"message\"" in response.content
|
|
|
|
|
|
|
|
|
|
|
2026-07-01 21:09:36 +08:00
|
|
|
@pytest.mark.asyncio
|
2026-11-12 09:55:55 +08:00
|
|
|
async def test_trigger_command_binds_explicit_topic_session(
|
2026-07-01 21:09:36 +08:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
router = CommandRouter()
|
|
|
|
|
register_builtin_commands(router)
|
|
|
|
|
store = LocalTriggerStore(tmp_path)
|
|
|
|
|
loop = SimpleNamespace(workspace=tmp_path, local_trigger_store=store)
|
|
|
|
|
msg = InboundMessage(
|
2026-11-12 09:55:55 +08:00
|
|
|
channel="telegram",
|
2026-07-01 21:09:36 +08:00
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="chat-1",
|
|
|
|
|
content="/trigger PR review",
|
2026-11-12 09:55:55 +08:00
|
|
|
session_key_override="telegram:chat-1:topic:5",
|
2026-07-01 21:09:36 +08:00
|
|
|
)
|
|
|
|
|
ctx = CommandContext(
|
|
|
|
|
msg=msg,
|
|
|
|
|
session=None,
|
2026-11-12 09:55:55 +08:00
|
|
|
key=msg.session_key,
|
2026-07-01 21:09:36 +08:00
|
|
|
raw="/trigger PR review",
|
|
|
|
|
loop=loop,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response = await router.dispatch(ctx)
|
|
|
|
|
|
|
|
|
|
assert response is not None
|
2026-11-12 09:55:55 +08:00
|
|
|
trigger = store.list_for_session("telegram:chat-1:topic:5")[0]
|
|
|
|
|
assert trigger.session_key == "telegram:chat-1:topic:5"
|
|
|
|
|
assert store.list_for_session("telegram:chat-1") == []
|
2026-07-01 21:09:36 +08:00
|
|
|
|
|
|
|
|
|
2026-06-30 00:44:42 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_trigger_command_without_name_returns_usage_only(tmp_path: Path) -> None:
|
|
|
|
|
router = CommandRouter()
|
|
|
|
|
register_builtin_commands(router)
|
2026-06-30 14:36:51 +08:00
|
|
|
store = LocalTriggerStore(tmp_path)
|
|
|
|
|
loop = SimpleNamespace(workspace=tmp_path, local_trigger_store=store)
|
2026-06-30 00:44:42 +08:00
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="chat-1",
|
|
|
|
|
content="/trigger@nanobot_bot",
|
|
|
|
|
metadata={"webui": True},
|
|
|
|
|
)
|
|
|
|
|
ctx = CommandContext(
|
|
|
|
|
msg=msg,
|
|
|
|
|
session=None,
|
|
|
|
|
key="websocket:chat-1",
|
|
|
|
|
raw="/trigger@nanobot_bot",
|
|
|
|
|
loop=loop,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response = await router.dispatch(ctx)
|
|
|
|
|
|
|
|
|
|
assert response is not None
|
|
|
|
|
assert "Usage: /trigger <name>" in response.content
|
|
|
|
|
assert store.list_for_session("websocket:chat-1") == []
|
|
|
|
|
|
|
|
|
|
|
2026-06-30 00:36:29 +08:00
|
|
|
def test_trigger_command_is_in_help_text() -> None:
|
2026-06-30 00:44:42 +08:00
|
|
|
assert "/trigger <name>" in build_help_text()
|