"""Tests for the markdown parser."""

from regimen.parser import parse, Command


def test_parse_basic():
    text = """# My regimen

## Step one

Some description.

```bash
echo hello
```

## Step two

Another step with two commands.

```bash
ls -la
```

```bash
pwd
```
"""
    reg = parse(text)
    assert reg.title == "My regimen"
    assert len(reg.steps) == 2
    assert reg.steps[0].title == "Step one"
    assert reg.steps[0].prose == "Some description."
    assert reg.steps[0].commands == [Command("echo hello")]
    assert reg.steps[1].title == "Step two"
    assert reg.steps[1].prose == "Another step with two commands."
    assert reg.steps[1].commands == [Command("ls -la"), Command("pwd")]


def test_parse_no_title():
    text = """## Only steps

```bash
echo hi
```
"""
    reg = parse(text)
    assert reg.title == ""
    assert len(reg.steps) == 1


def test_parse_empty():
    reg = parse("")
    assert reg.title == ""
    assert len(reg.steps) == 0


def test_parse_no_code_blocks():
    text = """# Title

## Step with no commands

Just prose, no commands here.
"""
    reg = parse(text)
    assert len(reg.steps) == 1
    assert reg.steps[0].commands == []
    assert "Just prose" in reg.steps[0].prose


def test_parse_multiline_command():
    text = """# Test

## Multi-line

```bash
for i in 1 2 3; do
    echo $i
done
```
"""
    reg = parse(text)
    assert len(reg.steps[0].commands) == 1
    assert "for i in" in reg.steps[0].commands[0].code
    assert "done" in reg.steps[0].commands[0].code


def test_non_bash_blocks_ignored():
    text = """# Test

## Step

```python
print("ignored")
```

```bash
echo "included"
```
"""
    reg = parse(text)
    assert len(reg.steps[0].commands) == 1
    assert reg.steps[0].commands[0].code == 'echo "included"'


def test_parse_timeout():
    text = """# Test

## Slow step

```bash timeout=120
./long-running-script.sh
```

```bash
echo fast
```
"""
    reg = parse(text)
    assert len(reg.steps[0].commands) == 2
    assert reg.steps[0].commands[0].timeout == 120
    assert reg.steps[0].commands[0].code == "./long-running-script.sh"
    assert reg.steps[0].commands[1].timeout is None
