"""Tests for loom.orchestration.verify — run_verify_command."""

from __future__ import annotations

import pytest

from loom.orchestration.verify import run_verify_command


@pytest.mark.asyncio
async def test_successful_command_returns_none():
    result = await run_verify_command("echo ok", task_id="t1")
    assert result is None


@pytest.mark.asyncio
async def test_failed_command_returns_error_dict():
    result = await run_verify_command("echo fail >&2 && exit 1", task_id="t2")
    assert result is not None
    assert result["error"] == "verify_command failed"
    assert result["task_id"] == "t2"
    assert result["returncode"] == 1
    assert "fail" in result["stderr"]
    assert isinstance(result["stdout"], str)


@pytest.mark.asyncio
async def test_timeout_returns_timeout_error():
    result = await run_verify_command("sleep 10", task_id="t3", timeout=1)
    assert result is not None
    assert result["error"] == "verify_command timed out (1s)"
    assert result["task_id"] == "t3"


@pytest.mark.asyncio
async def test_stdout_stderr_truncated_to_500_chars():
    # Generate >500 chars of output on both stdout and stderr
    cmd = "python3 -c \"import sys; sys.stdout.write('A' * 800); sys.stderr.write('B' * 800); sys.exit(1)\""
    result = await run_verify_command(cmd, task_id="t4")
    assert result is not None
    assert len(result["stdout"]) == 500
    assert len(result["stderr"]) == 500
    assert result["stdout"] == "A" * 500
    assert result["stderr"] == "B" * 500
