"""Step definitions for testing scripts functionality."""

import subprocess
from pathlib import Path

from behave import given, then, when


@given("I have the do-something.sh script")
def step_have_shell_script(context):
    """Ensure the shell script exists."""
    script_path = Path("scripts/do-something.sh")
    assert script_path.exists(), f"Script {script_path} does not exist"
    context.script_path = script_path


@when("I execute the do-something.sh script")
def step_execute_shell_script(context):
    """Execute the shell script."""
    result = subprocess.run(
        ["sh", str(context.script_path)], capture_output=True, text=True, check=False
    )
    context.script_output = result.stdout
    context.script_error = result.stderr
    context.script_returncode = result.returncode


@then('the script output should contain "{expected_text}"')
def step_verify_script_output(context, expected_text):
    """Verify the script output contains expected text."""
    assert expected_text in context.script_output, (
        f"Expected '{expected_text}' in output, but got: {context.script_output}"
    )
