"""Tests for CLI interface.""" import pytest import tempfile import os import sys from click.testing import CliRunner sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from shell_memory.cli import main @pytest.fixture def runner(): return CliRunner() @pytest.fixture def temp_db_path(): with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: db_path = f.name yield db_path if os.path.exists(db_path): os.unlink(db_path) class TestCLIVersion: def test_version(self, runner): result = runner.invoke(main, ["version"]) assert result.exit_code == 0 assert "Shell Memory" in result.output class TestCLICommandGroup: def test_cmd_add(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "cmd", "add", "ls -la", "-d", "List files"]) if result.exit_code != 0: print(f"Exception: {result.exception}") print(f"Output: {result.output}") assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "saved" in result.output.lower() or "ID" in result.output def test_cmd_list(self, runner, temp_db_path): runner.invoke(main, ["--db", temp_db_path, "cmd", "add", "pwd"]) result = runner.invoke(main, ["--db", temp_db_path, "cmd", "list"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "pwd" in result.output def test_cmd_search(self, runner, temp_db_path): runner.invoke(main, ["--db", temp_db_path, "cmd", "add", "git status", "-t", "git"]) result = runner.invoke(main, ["--db", temp_db_path, "cmd", "search", "git"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "git" in result.output.lower() def test_cmd_delete(self, runner, temp_db_path): runner.invoke(main, ["--db", temp_db_path, "cmd", "add", "to_delete"]) result = runner.invoke(main, ["--db", temp_db_path, "cmd", "delete", "1"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "deleted" in result.output.lower() or "✓" in result.output class TestCLIPatternGroup: def test_pattern_detect(self, runner, temp_db_path): for i in range(5): runner.invoke(main, ["--db", temp_db_path, "cmd", "add", f"cmd_{i}"]) result = runner.invoke(main, ["--db", temp_db_path, "pattern", "detect"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" def test_pattern_stats(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "pattern", "stats"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "Statistics" in result.output or "Commands" in result.output class TestCLISessionGroup: def test_session_start(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "session", "start", "test-session"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "started" in result.output.lower() or "Session" in result.output def test_session_list(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "session", "list"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" class TestCLIScriptGroup: def test_script_templates(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "script", "templates"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" def test_script_generate(self, runner, temp_db_path): result = runner.invoke(main, ["--db", temp_db_path, "script", "generate", "deploy application"]) assert result.exit_code == 0, f"Exit code: {result.exit_code}, Output: {result.output}" assert "#!/bin/bash" in result.output or "deploy" in result.output.lower()