fix(commands): reject invalid slash commands

This commit is contained in:
chengyongru
2026-08-04 17:11:44 +08:00
committed by chengyongru
parent 287fd88fe4
commit f45436b61d
3 changed files with 158 additions and 10 deletions
+57 -3
View File
@@ -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."""