"""Tests for CLI interface.""" import os import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).parent.parent)) @pytest.fixture def cli_runner(): """Create a CLI runner for testing.""" from typer.testing import CliRunner from shell_speak.main import app return CliRunner(), app @pytest.fixture def setup_test_env(tmp_path, sample_docker_yaml, sample_git_yaml): """Set up test environment with sample libraries.""" 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 TestCLIConvert: """Tests for the convert command.""" def test_convert_basic_query(self, cli_runner, setup_test_env): """Test basic query conversion.""" runner, app = cli_runner result = runner.invoke( app, ["convert", "list running containers"] ) assert result.exit_code == 0 assert "docker ps" in result.stdout or "docker" in result.stdout.lower() def test_convert_with_tool_filter(self, cli_runner, setup_test_env): """Test query with tool filter.""" runner, app = cli_runner result = runner.invoke( app, ["convert", "--tool", "docker", "list running containers"] ) assert result.exit_code == 0 assert "docker" in result.stdout.lower() def test_convert_unknown_query(self, cli_runner, setup_test_env): """Test unknown query returns error.""" runner, app = cli_runner result = runner.invoke( app, ["convert", "xyz unknown query"] ) assert result.exit_code == 0 assert "not found" in result.stdout.lower() or "could not" in result.stdout.lower() class TestCLIHistory: """Tests for the history command.""" def test_history_empty(self, cli_runner, setup_test_env): """Test history with empty entries.""" runner, app = cli_runner result = runner.invoke( app, ["history"] ) assert result.exit_code == 0 def test_history_with_limit(self, cli_runner, setup_test_env): """Test history with limit option.""" runner, app = cli_runner result = runner.invoke( app, ["history", "--limit", "10"] ) assert result.exit_code == 0 class TestCLILearn: """Tests for the learn command.""" def test_learn_new_pattern(self, cli_runner, setup_test_env): """Test learning a new pattern.""" runner, app = cli_runner result = runner.invoke( app, ["learn", "test query", "echo test", "--tool", "unix"] ) assert result.exit_code == 0 assert "learned" in result.stdout.lower() or "test query" in result.stdout class TestCLIForget: """Tests for the forget command.""" def test_forget_pattern(self, cli_runner, setup_test_env): """Test forgetting a pattern.""" runner, app = cli_runner result = runner.invoke( app, ["forget", "test query", "--tool", "unix"] ) assert result.exit_code == 0 class TestCLIReload: """Tests for the reload command.""" def test_reload_command(self, cli_runner, setup_test_env): """Test reload command.""" runner, app = cli_runner result = runner.invoke( app, ["reload"] ) assert result.exit_code == 0 assert "reloaded" in result.stdout.lower() class TestCLITools: """Tests for the tools command.""" def test_tools_command(self, cli_runner): """Test listing available tools.""" runner, app = cli_runner result = runner.invoke( app, ["tools"] ) assert result.exit_code == 0 assert "docker" in result.stdout.lower() class TestCLIVersion: """Tests for version option.""" def test_version_flag(self, cli_runner): """Test --version flag.""" runner, app = cli_runner result = runner.invoke( app, ["--version"] ) assert result.exit_code == 0 assert "shell speak" in result.stdout.lower() or "version" in result.stdout.lower()