mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-07 21:08:34 +03:00
fix(commands): reject invalid slash commands
This commit is contained in:
@@ -218,6 +218,47 @@ async def test_new_with_bot_suffix_does_not_persist_command(tmp_path: Path) -> N
|
||||
assert session.messages == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("content", "expected"),
|
||||
[
|
||||
("/neaw", 'Unknown command "/neaw". Did you mean "/new"?'),
|
||||
(
|
||||
"/status now",
|
||||
'Command "/status" does not accept arguments. Did you mean "/status"?',
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_invalid_slash_command_is_rejected_without_calling_provider(
|
||||
tmp_path: Path,
|
||||
content: str,
|
||||
expected: str,
|
||||
) -> None:
|
||||
loop = _make_full_loop(tmp_path)
|
||||
|
||||
response = await loop._process_message(
|
||||
InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="user",
|
||||
chat_id="chat-1",
|
||||
content=content,
|
||||
)
|
||||
)
|
||||
|
||||
assert response is not None
|
||||
assert response.content == expected
|
||||
loop.provider.chat_with_retry.assert_not_awaited()
|
||||
session = loop.sessions.get_or_create("websocket:chat-1")
|
||||
persisted = [
|
||||
(message["role"], message["content"], message.get("_command"))
|
||||
for message in session.messages
|
||||
]
|
||||
assert persisted == [
|
||||
("user", content, True),
|
||||
("assistant", response.content, True),
|
||||
]
|
||||
|
||||
|
||||
def test_clean_generated_title_strips_reasoning_tags() -> None:
|
||||
assert clean_generated_title("<think>reasoning</think> WebUI polish") == "WebUI polish"
|
||||
assert clean_generated_title("Title: <think> The user said hello") == ""
|
||||
|
||||
@@ -70,9 +70,12 @@ class TestIsDispatchableCommand:
|
||||
assert router.is_dispatchable_command(" /new ")
|
||||
assert router.is_dispatchable_command(" /pairing list ")
|
||||
|
||||
def test_unknown_slash_command_not_matched(self, router: CommandRouter) -> None:
|
||||
assert not router.is_dispatchable_command("/unknown")
|
||||
assert not router.is_dispatchable_command("/foo bar")
|
||||
def test_invalid_slash_commands_match_for_explicit_rejection(
|
||||
self, router: CommandRouter,
|
||||
) -> None:
|
||||
assert router.is_dispatchable_command("/unknown")
|
||||
assert router.is_dispatchable_command("/foo bar")
|
||||
assert router.is_dispatchable_command("/status now")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -183,6 +186,57 @@ class TestMidTurnCommandDispatchedDirectly:
|
||||
result = await router.dispatch(ctx)
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unknown_command_suggests_close_match(
|
||||
self, router: CommandRouter, fake_loop: MagicMock, fake_msg: MagicMock,
|
||||
) -> None:
|
||||
fake_msg.content = "/neaw"
|
||||
ctx = CommandContext(
|
||||
msg=fake_msg, session=None,
|
||||
key="test:chat1", raw="/neaw", loop=fake_loop,
|
||||
)
|
||||
|
||||
result = await router.dispatch(ctx)
|
||||
|
||||
assert result is not None
|
||||
assert result.content == 'Unknown command "/neaw". Did you mean "/new"?'
|
||||
assert result.metadata["render_as"] == "text"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exact_command_with_arguments_suggests_valid_form(
|
||||
self, router: CommandRouter, fake_loop: MagicMock, fake_msg: MagicMock,
|
||||
) -> None:
|
||||
fake_msg.content = "/status now"
|
||||
ctx = CommandContext(
|
||||
msg=fake_msg, session=None,
|
||||
key="test:chat1", raw="/status now", loop=fake_loop,
|
||||
)
|
||||
|
||||
result = await router.dispatch(ctx)
|
||||
|
||||
assert result is not None
|
||||
assert result.content == (
|
||||
'Command "/status" does not accept arguments. Did you mean "/status"?'
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unknown_command_without_close_match_points_to_help(
|
||||
self, router: CommandRouter, fake_loop: MagicMock, fake_msg: MagicMock,
|
||||
) -> None:
|
||||
fake_msg.content = "/totally-unknown-command"
|
||||
ctx = CommandContext(
|
||||
msg=fake_msg, session=None,
|
||||
key="test:chat1", raw="/totally-unknown-command", loop=fake_loop,
|
||||
)
|
||||
|
||||
result = await router.dispatch(ctx)
|
||||
|
||||
assert result is not None
|
||||
assert result.content == (
|
||||
'Unknown command "/totally-unknown-command". '
|
||||
'Use "/help" to list available commands.'
|
||||
)
|
||||
|
||||
|
||||
class TestPairingCommandDispatch:
|
||||
"""Verify /pairing works via CommandRouter."""
|
||||
|
||||
Reference in New Issue
Block a user