"""Tests for pattern detection.""" 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.patterns import PatternDetector from shell_memory.commands import CommandLibrary @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) for i in range(10): lib.add(f"command_{i}") yield db os.unlink(db_path) class TestPatternDetector: def test_analyze_recent_commands_empty(self, db_with_commands): detector = PatternDetector(db_with_commands) patterns = detector.analyze_recent_commands(limit=10) assert isinstance(patterns, list) def test_analyze_recent_commands_with_data(self, db_with_commands): detector = PatternDetector(db_with_commands) patterns = detector.analyze_recent_commands(limit=50) assert isinstance(patterns, list) def test_get_detected_patterns(self, db_with_commands): detector = PatternDetector(db_with_commands) patterns = detector.get_detected_patterns() assert isinstance(patterns, list) def test_suggest_shortcuts(self, db_with_commands): detector = PatternDetector(db_with_commands) shortcuts = detector.suggest_shortcuts() assert isinstance(shortcuts, list) def test_get_workflow_stats(self, db_with_commands): detector = PatternDetector(db_with_commands) stats = detector.get_workflow_stats(days=7) assert "total_commands" in stats assert "patterns_found" in stats class TestPatternNgramDetection: def test_ngram_patterns_creation(self, db_with_commands): detector = PatternDetector(db_with_commands) detector.min_frequency = 2 detector.window_size = 3 commands = db_with_commands.get_all_commands() command_ids = [cmd.id for cmd in commands if cmd.id is not None] if len(command_ids) >= 3: result = detector._find_ngram_patterns(command_ids) assert isinstance(result, list)