diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..2372743 --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,72 @@ +"""Tests for command library operations.""" + +import pytest +import tempfile +import os +import sys +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from shell_memory.database import Database +from shell_memory.commands import CommandLibrary +from shell_memory.models import Command + + +@pytest.fixture +def db_with_commands(): + with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: + db_path = f.name + db = Database(db_path) + lib = CommandLibrary(db) + yield db, lib + os.unlink(db_path) + + +class TestCommandLibrary: + def test_add_command(self, db_with_commands): + db, lib = db_with_commands + cmd_id = lib.add("git status", "Check repository status", ["git", "vcs"]) + assert cmd_id is not None + + def test_list_commands(self, db_with_commands): + db, lib = db_with_commands + lib.add("cmd1") + lib.add("cmd2") + commands = lib.list(limit=10) + assert len(commands) == 2 + + def test_search_commands(self, db_with_commands): + db, lib = db_with_commands + lib.add("git commit", "Commit changes", ["git"]) + lib.add("git push", "Push to remote", ["git"]) + results = lib.search("git") + assert len(results) >= 2 + + def test_get_command(self, db_with_commands): + db, lib = db_with_commands + cmd_id = lib.add("ls -la", "List all files") + retrieved = lib.get(cmd_id) + assert retrieved is not None + assert "ls" in retrieved.command + + def test_delete_command(self, db_with_commands): + db, lib = db_with_commands + cmd_id = lib.add("to_delete") + result = lib.delete(cmd_id) + assert result is True + + def test_increment_usage(self, db_with_commands): + db, lib = db_with_commands + cmd_id = lib.add("frequent_cmd") + lib.increment_usage(cmd_id) + lib.increment_usage(cmd_id) + cmd = lib.get(cmd_id) + assert cmd.usage_count == 2 + + def test_find_similar(self, db_with_commands): + db, lib = db_with_commands + lib.add("git status") + lib.add("git commit") + lib.add("docker ps") + similar = lib.find_similar("git statuss", threshold=50) + assert len(similar) >= 1 + assert any("git" in cmd[0].command for cmd in similar) \ No newline at end of file