From ce0ca2388ec52f090bc6968af086cb5eaf708e54 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sat, 31 Jan 2026 05:31:23 +0000 Subject: [PATCH] Initial upload: shell-speak CLI tool with natural language to shell command conversion --- tests/test_pattern_matching.py | 140 +++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/test_pattern_matching.py diff --git a/tests/test_pattern_matching.py b/tests/test_pattern_matching.py new file mode 100644 index 0000000..614a3fc --- /dev/null +++ b/tests/test_pattern_matching.py @@ -0,0 +1,140 @@ +"""Tests for pattern matching engine.""" + +import os +import sys +import tempfile +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent)) + + +class TestNLP: + """Tests for NLP preprocessing functions.""" + + def test_normalize_text(self): + from shell_speak.nlp import normalize_text + + assert normalize_text(" Hello World ") == "hello world" + assert normalize_text("Hello\tWorld") == "hello world" + assert normalize_text("multiple spaces") == "multiple spaces" + + def test_tokenize(self): + from shell_speak.nlp import tokenize + + assert tokenize("hello world") == ["hello", "world"] + assert tokenize("List running containers") == ["list", "running", "containers"] + assert tokenize("") == [] + + def test_extract_keywords(self): + from shell_speak.nlp import extract_keywords + + keywords = extract_keywords("list running containers") + assert "list" in keywords + assert "running" in keywords + assert "containers" in keywords + + def test_extract_keywords_removes_stopwords(self): + from shell_speak.nlp import extract_keywords + + keywords = extract_keywords("the a is are") + assert len(keywords) == 0 + + def test_calculate_similarity(self): + from shell_speak.nlp import calculate_similarity + + assert calculate_similarity("list containers", "list containers") == 1.0 + assert calculate_similarity("list containers", "show running containers") > 0.1 + assert calculate_similarity("list containers", "git commit") == 0.0 + + +class TestPatternMatcher: + """Tests for pattern matching functionality.""" + + @pytest.fixture(autouse=True) + def setup(self, tmp_path, sample_docker_yaml): + from shell_speak.config import ensure_data_dir + from shell_speak.library import get_loader + + docker_file = tmp_path / "docker.yaml" + docker_file.write_text(sample_docker_yaml) + + os.environ["SHELL_SPEAK_DATA_DIR"] = str(tmp_path) + + loader = get_loader() + loader._loaded = False + loader._patterns = [] + + def test_match_existing_pattern(self): + from shell_speak.matcher import get_matcher + + matcher = get_matcher() + match = matcher.match("list running containers") + + assert match is not None + assert match.command == "docker ps" + assert match.confidence > 0.5 + + def test_match_partial_pattern(self): + from shell_speak.matcher import get_matcher + + matcher = get_matcher() + match = matcher.match("show running containers") + + assert match is not None + assert "docker" in match.command.lower() + + def test_match_no_match(self): + from shell_speak.matcher import get_matcher + + matcher = get_matcher() + match = matcher.match("xyz unknown query that does not exist") + + assert match is None + + def test_match_with_tool_filter(self): + from shell_speak.matcher import get_matcher + + matcher = get_matcher() + match = matcher.match("list running containers", tool="docker") + + assert match is not None + assert match.pattern.tool == "docker" + + def test_template_substitution(self): + from shell_speak.matcher import get_matcher + + matcher = get_matcher() + match = matcher.match("run a container named test with image nginx") + + assert match is not None + assert "docker" in match.command.lower() + + +class TestCommandMatch: + """Tests for CommandMatch dataclass.""" + + def test_command_match_creation(self): + from shell_speak.models import CommandMatch, CommandPattern + + pattern = CommandPattern( + name="test_pattern", + tool="test", + description="Test pattern", + patterns=["test query"], + template="echo test", + explanation="Test explanation", + ) + + match = CommandMatch( + pattern=pattern, + confidence=0.9, + matched_query="test query", + command="echo test", + explanation="Test explanation", + ) + + assert match.pattern.name == "test_pattern" + assert match.confidence == 0.9 + assert match.command == "echo test"