Files
meshcore-bot/tests/test_command_prefix_module.py
T
agessaman 95b0601980 feat(command_prefix, config): enhance command prefix handling and documentation
- Updated `config.ini.example` to support single, multiple, and decorative command prefixes, improving flexibility in command invocation.
- Refactored `CommandManager` and `BaseCommand` to utilize new command prefix parsing logic, allowing for better handling of multiple prefixes and optional bare commands.
- Enhanced documentation to clarify command prefix configuration options and behavior, ensuring users understand how to set and use prefixes effectively.
- Added unit tests to verify the correct functionality of command prefix handling, including support for permissive and strict prefix modes.
2026-07-07 09:37:28 -07:00

85 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Unit tests for modules.command_prefix parsing and normalization."""
from configparser import ConfigParser
from modules.command_prefix import (
find_matching_prefix,
load_command_prefix_settings,
normalize_command_content,
parse_command_prefixes,
)
class TestParseCommandPrefixes:
def test_empty(self):
assert parse_command_prefixes('') == []
assert parse_command_prefixes(' ') == []
def test_single_char(self):
assert parse_command_prefixes('!') == ['!']
def test_single_multi_char(self):
assert parse_command_prefixes('abc') == ['abc']
def test_decorative_concatenated(self):
assert parse_command_prefixes('!~.') == ['!', '~', '.']
def test_comma_separated(self):
assert parse_command_prefixes('!, ~, .') == ['!', '~', '.']
def test_comma_separated_multi_char(self):
assert parse_command_prefixes('abc, xyz') == ['abc', 'xyz']
class TestFindMatchingPrefix:
def test_longest_match_wins(self):
prefixes = ['ab', 'abc']
assert find_matching_prefix('abctest', prefixes) == 'abc'
def test_no_match(self):
assert find_matching_prefix('ping', ['!']) is None
class TestNormalizeCommandContent:
def test_legacy_bang_when_no_prefixes(self):
assert normalize_command_content('!ping', [], require_prefix=False) == 'ping'
assert normalize_command_content('ping', [], require_prefix=False) == 'ping'
def test_strict_single_prefix(self):
assert normalize_command_content('!ping', ['!'], require_prefix=True) == 'ping'
assert normalize_command_content('ping', ['!'], require_prefix=True) is None
def test_permissive_single_prefix(self):
assert normalize_command_content('!ping', ['!'], require_prefix=False) == 'ping'
assert normalize_command_content('ping', ['!'], require_prefix=False) == 'ping'
def test_multi_prefix_strict(self):
prefixes = ['!', '~', '.']
assert normalize_command_content('~ping', prefixes, require_prefix=True) == 'ping'
assert normalize_command_content('ping', prefixes, require_prefix=True) is None
def test_multi_prefix_permissive(self):
prefixes = ['!', '~', '.']
assert normalize_command_content('ping', prefixes, require_prefix=False) == 'ping'
assert normalize_command_content('.ping', prefixes, require_prefix=False) == 'ping'
class TestLoadCommandPrefixSettings:
def test_defaults_require_true(self):
config = ConfigParser()
config.add_section('Bot')
config.set('Bot', 'command_prefix', '!')
prefixes, require = load_command_prefix_settings(config)
assert prefixes == ['!']
assert require is True
def test_require_false_from_config(self):
config = ConfigParser()
config.add_section('Bot')
config.set('Bot', 'command_prefix', '~')
config.set('Bot', 'require_command_prefix', 'false')
prefixes, require = load_command_prefix_settings(config)
assert prefixes == ['~']
assert require is False