from clan_lib.async_run import AsyncRuntime
from clan_lib.cmd import ClanCmdTimeoutError, Log, RunOpts
from clan_lib.ssh.remote import Remote

host = Remote(address="some_host", user="root", command_prefix="local_test")


def test_run_environment(runtime: AsyncRuntime) -> None:
    p2 = runtime.async_run(
        None,
        host.run_local,
        ["echo $env_var"],
        RunOpts(shell=True, log=Log.STDERR),  # noqa: S604 - Testing shell env var expansion
        extra_env={"env_var": "true"},
    )

    assert p2.wait().result.stdout == "true\n"

    p3 = runtime.async_run(
        None,
        host.run_local,
        ["env"],
        RunOpts(shell=True, log=Log.STDERR),  # noqa: S604 - Testing shell env passing
        extra_env={"env_var": "true"},
    )
    assert "env_var=true" in p3.wait().result.stdout


def test_run_local(runtime: AsyncRuntime) -> None:
    p1 = runtime.async_run(
        None,
        host.run_local,
        ["echo", "hello"],
        RunOpts(log=Log.STDERR),
    )
    assert p1.wait().result.stdout == "hello\n"


def test_timeout(runtime: AsyncRuntime) -> None:
    p1 = runtime.async_run(None, host.run_local, ["sleep", "10"], RunOpts(timeout=0.01))
    error = p1.wait().error
    assert isinstance(error, ClanCmdTimeoutError)


def test_run_exception(runtime: AsyncRuntime) -> None:
    p1 = runtime.async_run(None, host.run_local, ["exit 1"], RunOpts(shell=True))  # noqa: S604 - Testing shell error handling
    assert p1.wait().error is not None


def test_run_local_non_shell(runtime: AsyncRuntime) -> None:
    p2 = runtime.async_run(None, host.run_local, ["echo", "1"], RunOpts(log=Log.STDERR))
    assert p2.wait().result.stdout == "1\n"
