fix: resolve CI test failures (config access, mocks, imports)
This commit is contained in:
@@ -1,101 +1,237 @@
|
|||||||
"""Tests for history learning module."""
|
"""Tests for ShellGenius history module."""
|
||||||
|
|
||||||
import pytest
|
|
||||||
import tempfile
|
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
import tempfile
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from shellgenius.history import HistoryStorage, HistoryLearner, HistoryEntry
|
from shellgenius.history import HistoryEntry, HistoryLearner, HistoryStorage
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryEntry:
|
||||||
|
"""Test history entry dataclass."""
|
||||||
|
|
||||||
|
def test_entry_creation(self):
|
||||||
|
"""Test creating a history entry."""
|
||||||
|
entry = HistoryEntry(
|
||||||
|
id="test-id",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
|
description="Test description",
|
||||||
|
commands=["cmd1", "cmd2"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
assert entry.id == "test-id"
|
||||||
|
assert len(entry.commands) == 2
|
||||||
|
assert entry.usage_count == 1
|
||||||
|
|
||||||
|
|
||||||
class TestHistoryStorage:
|
class TestHistoryStorage:
|
||||||
@pytest.fixture
|
"""Test history storage."""
|
||||||
def temp_storage(self):
|
|
||||||
"""Create temporary storage for testing."""
|
def test_add_and_get_entry(self):
|
||||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
"""Test adding and retrieving entries."""
|
||||||
f.write("entries: []\nmetadata:\n version: '1.0'")
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
f.flush()
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
yield f.name
|
storage = HistoryStorage(storage_path)
|
||||||
os.unlink(f.name)
|
|
||||||
|
entry = HistoryEntry(
|
||||||
def test_init_storage(self, temp_storage):
|
id="test-id",
|
||||||
"""Test storage initialization."""
|
timestamp="2024-01-01T00:00:00",
|
||||||
storage = HistoryStorage(temp_storage)
|
description="Test",
|
||||||
|
commands=["echo hello"],
|
||||||
assert storage.storage_path == temp_storage
|
shell_type="bash",
|
||||||
|
)
|
||||||
def test_add_and_get_entry(self, temp_storage):
|
|
||||||
"""Test adding and retrieving history entries."""
|
storage.add_entry(entry)
|
||||||
storage = HistoryStorage(temp_storage)
|
entries = storage.get_entries()
|
||||||
|
|
||||||
entry = HistoryEntry(
|
assert len(entries) == 1
|
||||||
id="test-id",
|
assert entries[0].description == "Test"
|
||||||
timestamp="2024-01-01T00:00:00",
|
|
||||||
description="Test command",
|
def test_search_entries(self):
|
||||||
commands=["echo hello"],
|
"""Test searching entries."""
|
||||||
shell_type="bash",
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
)
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
storage = HistoryStorage(storage_path)
|
||||||
storage.add_entry(entry)
|
|
||||||
entries = storage.get_entries()
|
entry = HistoryEntry(
|
||||||
|
id="test-id",
|
||||||
assert len(entries) == 1
|
timestamp="2024-01-01T00:00:00",
|
||||||
assert entries[0].description == "Test command"
|
description="Find Python files",
|
||||||
|
commands=["find . -name *.py"],
|
||||||
def test_search_history(self, temp_storage):
|
shell_type="bash",
|
||||||
"""Test searching history."""
|
)
|
||||||
storage = HistoryStorage(temp_storage)
|
|
||||||
|
storage.add_entry(entry)
|
||||||
entry = HistoryEntry(
|
results = storage.search("python", limit=10)
|
||||||
id="test-id",
|
|
||||||
timestamp="2024-01-01T00:00:00",
|
assert len(results) == 1
|
||||||
description="List files command",
|
|
||||||
commands=["ls -la"],
|
def test_get_popular(self):
|
||||||
shell_type="bash",
|
"""Test getting popular entries."""
|
||||||
)
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
storage.add_entry(entry)
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
storage = HistoryStorage(storage_path)
|
||||||
results = storage.search("files")
|
|
||||||
|
entry = HistoryEntry(
|
||||||
assert len(results) == 1
|
id="test-id",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
def test_clear_history(self, temp_storage):
|
description="Test",
|
||||||
"""Test clearing history."""
|
commands=["echo hello"],
|
||||||
storage = HistoryStorage(temp_storage)
|
shell_type="bash",
|
||||||
|
usage_count=5,
|
||||||
entry = HistoryEntry(
|
)
|
||||||
id="test-id",
|
|
||||||
timestamp="2024-01-01T00:00:00",
|
storage.add_entry(entry)
|
||||||
description="Test",
|
popular = storage.get_popular(limit=10)
|
||||||
commands=["echo"],
|
|
||||||
shell_type="bash",
|
assert len(popular) == 1
|
||||||
)
|
assert popular[0].usage_count == 5
|
||||||
storage.add_entry(entry)
|
|
||||||
|
def test_update_usage(self):
|
||||||
storage.clear()
|
"""Test updating usage count."""
|
||||||
entries = storage.get_entries()
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
assert len(entries) == 0
|
storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
entry = HistoryEntry(
|
||||||
|
id="test-id",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
|
description="Test",
|
||||||
|
commands=["echo hello"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
|
||||||
|
storage.add_entry(entry)
|
||||||
|
storage.update_usage("test-id")
|
||||||
|
|
||||||
|
entries = storage.get_entries()
|
||||||
|
assert entries[0].usage_count == 2
|
||||||
|
|
||||||
|
def test_delete_entry(self):
|
||||||
|
"""Test deleting entry."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
entry = HistoryEntry(
|
||||||
|
id="test-id",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
|
description="Test",
|
||||||
|
commands=["echo hello"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
|
||||||
|
storage.add_entry(entry)
|
||||||
|
storage.delete_entry("test-id")
|
||||||
|
|
||||||
|
entries = storage.get_entries()
|
||||||
|
assert len(entries) == 0
|
||||||
|
|
||||||
|
def test_clear_history(self):
|
||||||
|
"""Test clearing all history."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
for i in range(3):
|
||||||
|
entry = HistoryEntry(
|
||||||
|
id=f"test-{i}",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
|
description=f"Test {i}",
|
||||||
|
commands=[f"echo {i}"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
storage.add_entry(entry)
|
||||||
|
|
||||||
|
storage.clear()
|
||||||
|
entries = storage.get_entries()
|
||||||
|
|
||||||
|
assert len(entries) == 0
|
||||||
|
|
||||||
|
def test_limit_and_offset(self):
|
||||||
|
"""Test limit and offset for retrieval."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
for i in range(5):
|
||||||
|
entry = HistoryEntry(
|
||||||
|
id=f"test-{i}",
|
||||||
|
timestamp="2024-01-01T00:00:00",
|
||||||
|
description=f"Test {i}",
|
||||||
|
commands=[f"echo {i}"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
storage.add_entry(entry)
|
||||||
|
|
||||||
|
entries = storage.get_entries(limit=2, offset=2)
|
||||||
|
assert len(entries) == 2
|
||||||
|
|
||||||
|
|
||||||
class TestHistoryLearner:
|
class TestHistoryLearner:
|
||||||
def test_learn_command(self):
|
"""Test history learning functionality."""
|
||||||
"""Test learning from generated command."""
|
|
||||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
def test_learn(self):
|
||||||
f.write("entries: []\nmetadata:\n version: '1.0'")
|
"""Test learning from commands."""
|
||||||
f.flush()
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
storage_path = f.name
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
learner = HistoryLearner()
|
||||||
try:
|
learner.storage = HistoryStorage(storage_path)
|
||||||
with patch('shellgenius.history.get_config') as mock_config:
|
|
||||||
mock_config.return_value.get.return_value = storage_path
|
entry = learner.learn(
|
||||||
mock_config.return_value.is_history_enabled.return_value = True
|
description="List files",
|
||||||
|
commands=["ls -la"],
|
||||||
learner = HistoryLearner()
|
shell_type="bash",
|
||||||
entry = learner.learn("list files", ["ls -la"], "bash")
|
)
|
||||||
|
|
||||||
assert entry.description == "list files"
|
assert entry is not None
|
||||||
assert "ls -la" in entry.commands
|
assert "List files" in entry.description
|
||||||
finally:
|
|
||||||
os.unlink(storage_path)
|
def test_find_similar(self):
|
||||||
|
"""Test finding similar commands."""
|
||||||
|
n with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
learner = HistoryLearner()
|
||||||
|
learner.storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
learner.learn(
|
||||||
|
description="Find Python files",
|
||||||
|
commands=["find . -name *.py"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
|
||||||
|
similar = learner.find_similar("python", limit=5)
|
||||||
|
assert len(similar) == 1
|
||||||
|
|
||||||
|
def test_suggest(self):
|
||||||
|
"""Test getting suggestions."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
learner = HistoryLearner()
|
||||||
|
learner.storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
learner.learn(
|
||||||
|
description="Find Python files",
|
||||||
|
commands=["find . -name *.py"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
|
||||||
|
suggestions = learner.suggest("python files", limit=3)
|
||||||
|
assert len(suggestions) >= 1
|
||||||
|
|
||||||
|
def test_get_frequent_patterns(self):
|
||||||
|
"""Test analyzing frequent patterns."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
storage_path = os.path.join(tmpdir, "history.yaml")
|
||||||
|
learner = HistoryLearner()
|
||||||
|
learner.storage = HistoryStorage(storage_path)
|
||||||
|
|
||||||
|
learner.learn(
|
||||||
|
description="Find files",
|
||||||
|
commands=["find . -type f"],
|
||||||
|
shell_type="bash",
|
||||||
|
)
|
||||||
|
|
||||||
|
patterns = learner.get_frequent_patterns()
|
||||||
|
assert "common_commands" in patterns
|
||||||
|
assert "shell_types" in patterns
|
||||||
|
|||||||
Reference in New Issue
Block a user