107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
"""Tests for interactive mode."""
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
@pytest.fixture
|
|
def setup_test_env(tmp_path, sample_docker_yaml, sample_git_yaml):
|
|
"""Set up test environment."""
|
|
docker_file = tmp_path / "docker.yaml"
|
|
docker_file.write_text(sample_docker_yaml)
|
|
|
|
git_file = tmp_path / "git.yaml"
|
|
git_file.write_text(sample_git_yaml)
|
|
|
|
os.environ["SHELL_SPEAK_DATA_DIR"] = str(tmp_path)
|
|
os.environ["SHELL_SPEAK_HISTORY_FILE"] = str(tmp_path / "history.json")
|
|
os.environ["SHELL_SPEAK_CORRECTIONS_FILE"] = str(tmp_path / "corrections.json")
|
|
|
|
|
|
class TestInteractiveSession:
|
|
"""Tests for interactive session functionality."""
|
|
|
|
def test_session_prompt(self):
|
|
"""Test that session prompt is created."""
|
|
from shell_speak.interactive import ShellSpeakCompleter
|
|
|
|
completer = ShellSpeakCompleter()
|
|
assert completer is not None
|
|
|
|
def test_key_bindings(self):
|
|
"""Test key bindings creation."""
|
|
from shell_speak.interactive import create_key_bindings
|
|
|
|
kb = create_key_bindings()
|
|
assert kb is not None
|
|
|
|
def test_detect_tool_docker(self):
|
|
"""Test tool detection for docker."""
|
|
from shell_speak.interactive import _detect_tool
|
|
|
|
tool = _detect_tool("list running containers")
|
|
assert tool == "docker"
|
|
|
|
def test_detect_tool_git(self):
|
|
"""Test tool detection for git."""
|
|
from shell_speak.interactive import _detect_tool
|
|
|
|
tool = _detect_tool("commit changes with message")
|
|
assert tool == "git"
|
|
|
|
def test_detect_tool_kubectl(self):
|
|
"""Test tool detection for kubectl."""
|
|
from shell_speak.interactive import _detect_tool
|
|
|
|
tool = _detect_tool("get pods in namespace default")
|
|
assert tool == "kubectl"
|
|
|
|
def test_detect_tool_unknown(self):
|
|
"""Test tool detection for unknown query."""
|
|
from shell_speak.interactive import _detect_tool
|
|
|
|
tool = _detect_tool("random query that matches nothing")
|
|
assert tool is None
|
|
|
|
|
|
class TestInteractiveHelp:
|
|
"""Tests for interactive help."""
|
|
|
|
def test_help_display(self):
|
|
"""Test help message display."""
|
|
from shell_speak.interactive import _show_interactive_help
|
|
from shell_speak.output import console
|
|
|
|
with patch.object(console, 'print') as mock_print:
|
|
_show_interactive_help()
|
|
assert mock_print.called
|
|
|
|
|
|
class TestProcessQuery:
|
|
"""Tests for query processing."""
|
|
|
|
def test_process_query_success(self, setup_test_env):
|
|
"""Test successful query processing."""
|
|
from shell_speak.interactive import _process_query
|
|
from shell_speak.matcher import get_matcher
|
|
|
|
matcher = get_matcher()
|
|
match = _process_query("list running containers", "docker")
|
|
|
|
if match:
|
|
assert "docker" in match.command.lower()
|
|
|
|
def test_process_query_failure(self, setup_test_env):
|
|
"""Test failed query processing."""
|
|
from shell_speak.interactive import _process_query
|
|
|
|
match = _process_query("xyz unknown query xyz", None)
|
|
assert match is None
|