"""Tests for CLI Command Memory search functionality.""" from datetime import datetime, timedelta from cli_memory.search import SearchEngine from cli_memory.models import Command, CommandType class MockCommand: def __init__(self, id, command, command_type, timestamp=None, project_id=None, exit_code=0, duration_ms=None, working_directory="/tmp"): self.id = id self.command = command self.command_type = command_type self.timestamp = timestamp or datetime.utcnow() self.project_id = project_id self.exit_code = exit_code self.duration_ms = duration_ms self.working_directory = working_directory def test_search_engine_init(): """Test SearchEngine initialization.""" engine = SearchEngine() assert engine is not None def test_matches_query_exact(): """Test exact query matching.""" engine = SearchEngine() assert engine._matches_query("git status", "git status") assert not engine._matches_query("git status", "docker ps") def test_matches_query_partial(): """Test partial query matching.""" engine = SearchEngine() assert engine._matches_query("git status", "status") assert engine._matches_query("git status", "git") def test_search_commands_by_type(): """Test search filtering by command type.""" commands = [ MockCommand(1, "git status", CommandType.GIT), MockCommand(2, "git add .", CommandType.GIT), MockCommand(3, "docker ps", CommandType.DOCKER), ] engine = SearchEngine() results = engine.search_commands(commands, "", command_type="docker") assert len(results) == 1 def test_search_recent(): """Test recent command search.""" now = datetime.utcnow() hour_ago = now - timedelta(hours=1) day_ago = now - timedelta(days=1) commands = [ MockCommand(1, "recent cmd", CommandType.OTHER, timestamp=hour_ago), MockCommand(2, "old cmd", CommandType.OTHER, timestamp=day_ago), ] engine = SearchEngine() results = engine.search_recent(commands, hours=24) assert len(results) == 1 def test_get_command_statistics(): """Test command statistics calculation.""" commands = [ MockCommand(1, "git status", CommandType.GIT), MockCommand(2, "git add .", CommandType.GIT), MockCommand(3, "docker ps", CommandType.DOCKER), ] engine = SearchEngine() stats = engine.get_command_statistics(commands) assert stats["total_commands"] == 3 assert "git" in stats["by_type"] assert stats["by_type"]["git"] == 2