import pytest

from llmcli import ShellAI


@pytest.fixture
def shell_ai():
    return ShellAI()

def test_filter_command_basic(shell_ai):
    """测试基本的命令过滤"""
    command = "ls -la"
    assert shell_ai._filter_command(command) == "ls -la"


@pytest.mark.parametrize("input_cmd,expected", [
    ("```\nls -la\n```", "ls -la"),
    ("```ls -la```", "ls -la"),
])
def test_filter_command_with_code_block(shell_ai, input_cmd, expected):
    """test code block"""
    assert shell_ai._filter_command(input_cmd) == expected

@pytest.mark.parametrize("input_cmd,expected", [
    ("```bash\nls -la\n```", "ls -la"),
    ("```zsh\nls -la\n```", "ls -la"),
    ("```shell\nls -la\n```", "ls -la"),
    ("```sh\nls -la\n```", "ls -la"),
])
def test_filter_command_with_shell_type(shell_ai, input_cmd, expected):
    """测试带有shell类型声明的代码块"""
    assert shell_ai._filter_command(input_cmd) == expected

def test_filter_command_multiline(shell_ai):
    """测试多行命令"""
    command = "```\ncd /tmp\nls -la\n```"
    assert shell_ai._filter_command(command) == "cd /tmp\nls -la"

def test_filter_command_with_spaces(shell_ai):
    """测试带有额外空格的命令"""
    command = "```bash  \n  ls -la  \n  ```"
    assert shell_ai._filter_command(command) == "ls -la"

def test_filter_command_empty_block(shell_ai):
    """测试空代码块"""
    command = "```\n\n```"
    assert shell_ai._filter_command(command) == ""

def test_filter_command_nested_blocks(shell_ai):
    """测试嵌套代码块"""
    command = "```bash\n```echo hello```\n```"
    assert shell_ai._filter_command(command) == "```echo hello```"
