from __future__ import annotations
from msagent.utils.patterns import (
matches_patterns,
mcp_server_matcher,
three_part_matcher,
two_part_matcher,
)
def test_matches_patterns_requires_positive_match_and_rejects_negatives() -> None:
def matcher(pattern: str) -> bool:
return pattern in {"cli:*", "cli:tools", "mcp:server:*"}
assert matches_patterns(["cli:*"], matcher) is True
assert matches_patterns(["cli:*", "!cli:tools"], matcher) is False
assert matches_patterns(["!cli:*"], matcher) is False
def test_two_part_matcher_supports_globs_and_reports_invalid_patterns() -> None:
invalid: list[str] = []
matcher = two_part_matcher(name="run_command", module="cli.handlers", on_invalid=invalid.append)
assert matcher("cli.*:run_*") is True
assert matcher("invalid-pattern") is False
assert invalid == ["invalid-pattern"]
def test_three_part_matcher_supports_globs_and_reports_invalid_patterns() -> None:
invalid: list[str] = []
matcher = three_part_matcher(
name="run_command",
module="cli.handlers",
category="tool",
on_invalid=invalid.append,
)
assert matcher("tool:cli.*:run_*") is True
assert matcher("tool:cli.handlers") is False
assert invalid == ["tool:cli.handlers"]
def test_mcp_server_matcher_validates_category_and_wildcard_tool_name() -> None:
invalid: list[str] = []
matcher = mcp_server_matcher("profiler", "mcp", on_invalid=invalid.append)
assert matcher("mcp") is False
assert matcher("tool:profiler:*") is False
assert matcher("mcp:profiler:run") is False
assert matcher("mcp:prof*: *".replace(" ", "")) is True
assert invalid == ["mcp:profiler:run"]