Files
shell-history-alias-generator/tests/test_core.py
2026-02-01 08:51:39 +00:00

160 lines
5.4 KiB
Python

"""Tests for core functionality."""
import pytest
from datetime import datetime
from shell_alias_gen.command_analyzer import CommandAnalyzer, AliasSuggestion
from shell_alias_gen.export_manager import ExportManager, BashFormatter, ZshFormatter, FishFormatter
from shell_alias_gen.interactive_ui import InteractiveUI
class TestAliasSuggestion:
"""Tests for AliasSuggestion class."""
def test_to_shell_bash(self):
"""Test alias conversion to bash format."""
suggestion = AliasSuggestion(
alias_name="gco",
original_command="git checkout",
frequency=5,
score=0.8
)
result = suggestion.to_shell("bash")
assert result == "alias gco='git checkout'"
def test_to_shell_fish(self):
"""Test alias conversion to fish format."""
suggestion = AliasSuggestion(
alias_name="gco",
original_command="git checkout",
frequency=5,
score=0.8
)
result = suggestion.to_shell("fish")
assert result == "alias gco='git checkout'"
class TestCommandAnalyzer:
"""Tests for CommandAnalyzer class."""
def setup_method(self):
"""Set up test fixtures."""
self.analyzer = CommandAnalyzer()
def test_analyze_empty_commands(self):
"""Test analyzing empty command list."""
from shell_alias_gen.parsers import ParsedCommand
commands = []
suggestions = self.analyzer.analyze_commands(commands)
assert suggestions == []
def test_analyze_short_commands_filtered(self):
"""Test that short commands are filtered out."""
from shell_alias_gen.parsers import ParsedCommand
commands = [
ParsedCommand(raw_command="ls", line_number=1),
ParsedCommand(raw_command="cd /tmp", line_number=2),
]
suggestions = self.analyzer.analyze_commands(commands)
assert suggestions == []
def test_analyze_long_commands(self):
"""Test analyzing long commands."""
from shell_alias_gen.parsers import ParsedCommand
commands = [
ParsedCommand(
raw_command="git log --oneline --graph --all --decorate",
line_number=1
),
ParsedCommand(
raw_command="git log --oneline --graph --all --decorate",
line_number=2
),
]
suggestions = self.analyzer.analyze_commands(commands)
assert len(suggestions) == 1
assert suggestions[0].frequency == 2
def test_calculate_complexity(self):
"""Test complexity calculation."""
simple = "ls -la"
complex_cmd = "git log --oneline --graph --all --decorate --simplify-by-decoration"
simple_score = self.analyzer._calculate_complexity(simple)
complex_score = self.analyzer._calculate_complexity(complex_cmd)
assert complex_score > simple_score
def test_filter_suggestions_by_score(self):
"""Test filtering suggestions by minimum score."""
from shell_alias_gen.parsers import ParsedCommand
commands = [
ParsedCommand(
raw_command="docker-compose up -d --build",
line_number=1
),
] * 10
suggestions = self.analyzer.analyze_commands(commands)
filtered = self.analyzer.filter_suggestions(suggestions, min_score=0.5)
assert all(s.score >= 0.5 for s in filtered)
class TestExportManager:
"""Tests for ExportManager class."""
def test_get_bash_formatter(self):
"""Test getting bash formatter."""
manager = ExportManager()
formatter = manager.get_formatter("bash")
assert isinstance(formatter, BashFormatter)
def test_get_zsh_formatter(self):
"""Test getting zsh formatter."""
manager = ExportManager()
formatter = manager.get_formatter("zsh")
assert isinstance(formatter, ZshFormatter)
def test_get_fish_formatter(self):
"""Test getting fish formatter."""
manager = ExportManager()
formatter = manager.get_formatter("fish")
assert isinstance(formatter, FishFormatter)
def test_export_aliases(self):
"""Test exporting aliases."""
manager = ExportManager()
suggestion = AliasSuggestion(
alias_name="gco",
original_command="git checkout",
frequency=5,
score=0.8
)
result = manager.export([suggestion], "bash")
assert "alias gco='git checkout'" in result
assert "# Shell Aliases" in result
def test_get_supported_formats(self):
"""Test getting supported formats."""
manager = ExportManager()
formats = manager.get_supported_formats()
assert "bash" in formats
assert "zsh" in formats
assert "fish" in formats
class TestInteractiveUI:
"""Tests for InteractiveUI class."""
def test_ui_initialization(self):
"""Test UI initializes correctly."""
ui = InteractiveUI()
assert ui.selected_aliases == set()
def test_display_suggestions_empty(self, capsys):
"""Test displaying empty suggestions."""
ui = InteractiveUI()
ui.display_suggestions([])
captured = capsys.readouterr()
assert "No alias suggestions found" in captured.out