fix: resolve CI test failures (config access, mocks, imports)
Some checks failed
CI / test (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled

This commit is contained in:
2026-02-04 11:49:13 +00:00
parent c20a178e03
commit 39496586f2

View File

@@ -1,101 +1,237 @@
"""Tests for history learning module."""
"""Tests for ShellGenius history module."""
import pytest
import tempfile
import os
from pathlib import Path
import tempfile
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:
@pytest.fixture
def temp_storage(self):
"""Create temporary storage for testing."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
f.write("entries: []\nmetadata:\n version: '1.0'")
f.flush()
yield f.name
os.unlink(f.name)
def test_init_storage(self, temp_storage):
"""Test storage initialization."""
storage = HistoryStorage(temp_storage)
assert storage.storage_path == temp_storage
def test_add_and_get_entry(self, temp_storage):
"""Test adding and retrieving history entries."""
storage = HistoryStorage(temp_storage)
entry = HistoryEntry(
id="test-id",
timestamp="2024-01-01T00:00:00",
description="Test command",
commands=["echo hello"],
shell_type="bash",
)
storage.add_entry(entry)
entries = storage.get_entries()
assert len(entries) == 1
assert entries[0].description == "Test command"
def test_search_history(self, temp_storage):
"""Test searching history."""
storage = HistoryStorage(temp_storage)
entry = HistoryEntry(
id="test-id",
timestamp="2024-01-01T00:00:00",
description="List files command",
commands=["ls -la"],
shell_type="bash",
)
storage.add_entry(entry)
results = storage.search("files")
assert len(results) == 1
def test_clear_history(self, temp_storage):
"""Test clearing history."""
storage = HistoryStorage(temp_storage)
entry = HistoryEntry(
id="test-id",
timestamp="2024-01-01T00:00:00",
description="Test",
commands=["echo"],
shell_type="bash",
)
storage.add_entry(entry)
storage.clear()
entries = storage.get_entries()
assert len(entries) == 0
"""Test history storage."""
def test_add_and_get_entry(self):
"""Test adding and retrieving entries."""
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)
entries = storage.get_entries()
assert len(entries) == 1
assert entries[0].description == "Test"
def test_search_entries(self):
"""Test searching entries."""
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="Find Python files",
commands=["find . -name *.py"],
shell_type="bash",
)
storage.add_entry(entry)
results = storage.search("python", limit=10)
assert len(results) == 1
def test_get_popular(self):
"""Test getting popular entries."""
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",
usage_count=5,
)
storage.add_entry(entry)
popular = storage.get_popular(limit=10)
assert len(popular) == 1
assert popular[0].usage_count == 5
def test_update_usage(self):
"""Test updating usage count."""
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.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:
def test_learn_command(self):
"""Test learning from generated command."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
f.write("entries: []\nmetadata:\n version: '1.0'")
f.flush()
storage_path = f.name
try:
with patch('shellgenius.history.get_config') as mock_config:
mock_config.return_value.get.return_value = storage_path
mock_config.return_value.is_history_enabled.return_value = True
learner = HistoryLearner()
entry = learner.learn("list files", ["ls -la"], "bash")
assert entry.description == "list files"
assert "ls -la" in entry.commands
finally:
os.unlink(storage_path)
"""Test history learning functionality."""
def test_learn(self):
"""Test learning from commands."""
with tempfile.TemporaryDirectory() as tmpdir:
storage_path = os.path.join(tmpdir, "history.yaml")
learner = HistoryLearner()
learner.storage = HistoryStorage(storage_path)
entry = learner.learn(
description="List files",
commands=["ls -la"],
shell_type="bash",
)
assert entry is not None
assert "List files" in entry.description
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