Files

187 lines
5.4 KiB
Python

"""Tests for CLI interface."""
import pytest
import sys
from unittest.mock import Mock, patch, MagicMock
from io import StringIO
class TestArgParser:
"""Tests for argument parser."""
def test_create_parser(self):
"""Test parser creation."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
assert parser is not None
def test_parse_generate_command(self):
"""Test parsing generate command."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["generate", "list files"])
assert args.command == "generate"
assert args.description == "list files"
def test_parse_shell_argument(self):
"""Test parsing --shell argument."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["--shell", "zsh", "generate", "list"])
assert args.shell == "zsh"
def test_parse_backend_argument(self):
"""Test parsing --backend argument."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["--backend", "ollama", "generate", "list"])
assert args.backend == "ollama"
def test_parse_execute_flag(self):
"""Test parsing --execute flag."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["generate", "list", "--execute"])
assert args.execute is True
def test_parse_auto_execute_flag(self):
"""Test parsing --auto-execute flag."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["generate", "list", "--auto-execute"])
assert args.auto_execute is True
def test_parse_force_flag(self):
"""Test parsing --force flag."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["generate", "list", "--force"])
assert args.force is True
def test_parse_history_command(self):
"""Test parsing history command."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["history"])
assert args.command == "history"
def test_parse_history_limit(self):
"""Test parsing history --limit."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["history", "--limit", "50"])
assert args.limit == 50
def test_parse_feedback_command(self):
"""Test parsing feedback command."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args(["feedback", "5"])
assert args.command == "feedback"
assert args.id == 5
def test_parse_feedback_options(self):
"""Test parsing feedback options."""
from shellgen.ui.argparse import create_parser
parser = create_parser()
args = parser.parse_args([
"feedback", "5",
"--corrected", "ls -la",
"--feedback", "wrong flags"
])
assert args.id == 5
assert args.corrected == "ls -la"
assert args.feedback == "wrong flags"
class TestConsoleUI:
"""Tests for console UI."""
def test_console_ui_initialization(self):
"""Test ConsoleUI initialization."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
assert hasattr(ui, 'console')
def test_print_header(self):
"""Test printing header."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.print_header()
def test_display_generated_command(self):
"""Test displaying generated command."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.display_generated_command("ls -la", "List all files")
def test_display_history(self):
"""Test displaying history."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
entries = [
{"id": 1, "prompt": "list files", "command": "ls", "shell": "bash", "executed": True},
{"id": 2, "prompt": "show git status", "command": "git status", "shell": "bash", "executed": False},
]
ui.display_history(entries)
def test_display_empty_history(self):
"""Test displaying empty history."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.display_history([])
def test_print_safety_warning(self):
"""Test printing safety warning."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.print_safety_warning("This command is dangerous")
def test_print_error(self):
"""Test printing error message."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.print_error("Something went wrong")
def test_print_cancelled(self):
"""Test printing cancelled message."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.print_cancelled()
def test_print_feedback_submitted(self):
"""Test printing feedback confirmation."""
from shellgen.ui.console import ConsoleUI
ui = ConsoleUI()
ui.print_feedback_submitted()