Implement prefix matching in dispatch

This commit is contained in:
Jared Miller 2026-02-08 13:33:22 -05:00
parent 7d3b02f6ff
commit 7c313ae307
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C

View file

@ -80,21 +80,17 @@ async def test_prefix_match_resolves_unique_prefix(player):
async def test_ambiguous_prefix_two_matches(player): async def test_ambiguous_prefix_two_matches(player):
"""Test that ambiguous prefix with 2 matches shows 'A or B?' message.""" """Test that ambiguous prefix with 2 matches shows 'A or B?' message."""
commands.register( commands.register(
CommandDefinition(name="sweep", handler=dummy_handler, mode="normal") CommandDefinition(name="swoop", handler=dummy_handler, mode="normal")
) )
commands.register( commands.register(
CommandDefinition( CommandDefinition(name="swallow", handler=dummy_handler, mode="normal")
name="southwest", handler=dummy_handler, mode="normal"
)
) )
await commands.dispatch(player, "sw") await commands.dispatch(player, "sw")
output = "".join([call[0][0] for call in player.writer.write.call_args_list]) output = "".join([call[0][0] for call in player.writer.write.call_args_list])
# Check for either order # Check for either order
assert ( assert "swallow or swoop?" in output or "swoop or swallow?" in output
"sweep or southwest?" in output or "southwest or sweep?" in output
)
assert "Handler called" not in output assert "Handler called" not in output
@ -108,9 +104,7 @@ async def test_ambiguous_prefix_three_plus_matches(player):
CommandDefinition(name="set", handler=dummy_handler, mode="normal") CommandDefinition(name="set", handler=dummy_handler, mode="normal")
) )
commands.register( commands.register(
CommandDefinition( CommandDefinition(name="settings", handler=dummy_handler, mode="normal")
name="settings", handler=dummy_handler, mode="normal"
)
) )
await commands.dispatch(player, "se") await commands.dispatch(player, "se")
@ -183,8 +177,8 @@ async def test_prefix_match_deduplicates_aliases(player):
"""Test that aliases to the same command don't create multiple matches.""" """Test that aliases to the same command don't create multiple matches."""
commands.register( commands.register(
CommandDefinition( CommandDefinition(
name="look", name="long",
aliases=["l", "lo"], aliases=["l", "lon"],
handler=dummy_handler, handler=dummy_handler,
mode="normal", mode="normal",
) )
@ -196,16 +190,14 @@ async def test_prefix_match_deduplicates_aliases(player):
await commands.dispatch(player, "lo") await commands.dispatch(player, "lo")
output = "".join([call[0][0] for call in player.writer.write.call_args_list]) output = "".join([call[0][0] for call in player.writer.write.call_args_list])
# Should show just "lock or look?" not multiple entries for look's aliases # Should show just "lock or long?" not multiple entries for long's aliases
assert "lock" in output assert "lock" in output
assert "look" in output assert "long" in output
# Should not list look multiple times # Should not list long multiple times
output_lower = output.lower() output_lower = output.lower()
# Count occurrences of the word "look" (not as substring) # Count occurrences of the word "long" (not as substring like "lock")
look_count = len( # Looking for "long" as a standalone word in disambiguation
[word for word in output_lower.split() if word.startswith("look")] assert output_lower.count("long") == 1
)
assert look_count == 1
@pytest.mark.asyncio @pytest.mark.asyncio