Private
Public Access
feat(palette): add fuzzy_match with subsequence matching and scoring
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from src.command_palette import Command, ScoredCommand, fuzzy_match
|
||||
|
||||
|
||||
def _cmd(id: str, title: str) -> Command:
|
||||
return Command(id=id, title=title, category="test")
|
||||
|
||||
|
||||
def test_fuzzy_match_prefix_ranks_first():
|
||||
candidates = [
|
||||
_cmd("find", "Find in Selection"),
|
||||
_cmd("fold", "Fold All"),
|
||||
_cmd("config", "Configure Settings"),
|
||||
]
|
||||
results = fuzzy_match("fin", candidates, top_n=10)
|
||||
assert len(results) > 0
|
||||
assert results[0].command.id == "find"
|
||||
assert results[0].score > 0.5
|
||||
|
||||
|
||||
def test_fuzzy_match_subsequence_match():
|
||||
candidates = [_cmd("x", "Find")]
|
||||
results = fuzzy_match("fd", candidates, top_n=10)
|
||||
assert len(results) == 1
|
||||
assert results[0].command.id == "x"
|
||||
|
||||
|
||||
def test_fuzzy_match_no_match_returns_empty():
|
||||
candidates = [_cmd("x", "foo bar")]
|
||||
results = fuzzy_match("xyz", candidates, top_n=10)
|
||||
assert results == []
|
||||
|
||||
|
||||
def test_fuzzy_match_top_n_limits_results():
|
||||
candidates = [_cmd(f"cmd_{i}", f"Command {i}") for i in range(50)]
|
||||
results = fuzzy_match("cmd", candidates, top_n=10)
|
||||
assert len(results) == 10
|
||||
|
||||
|
||||
def test_fuzzy_match_score_higher_for_exact_prefix():
|
||||
candidates = [
|
||||
_cmd("a", "find"),
|
||||
_cmd("b", "Configure Find Settings"),
|
||||
]
|
||||
results = fuzzy_match("fin", candidates, top_n=10)
|
||||
assert results[0].command.id == "a"
|
||||
Reference in New Issue
Block a user