import subprocess


def test_run_streaming_passes_expected_kwargs(monkeypatch, tmp_path):
    from armorer import docker_runner

    captured = {}

    def fake_popen(cmd, **kwargs):
        captured["cmd"] = cmd
        captured["kwargs"] = kwargs

        class Proc:
            stdout = None
            returncode = 0

            def wait(self):
                return 0

        return Proc()

    monkeypatch.setattr(docker_runner.subprocess, "Popen", fake_popen)

    docker_runner.run_streaming(
        ["echo", "hi"],
        cwd=tmp_path,
        env={"A": "B"},
        shell=False,
    )

    assert captured["cmd"] == ["echo", "hi"]
    assert captured["kwargs"]["cwd"] == str(tmp_path)
    assert captured["kwargs"]["shell"] is False
    assert captured["kwargs"]["text"] is True
    assert captured["kwargs"]["stdin"] == subprocess.DEVNULL
    assert captured["kwargs"]["stdout"] == subprocess.PIPE
    assert captured["kwargs"]["stderr"] == subprocess.STDOUT


def test_compose_up_streaming_builds_command(monkeypatch, tmp_path):
    from armorer import docker_runner

    captured = {}

    def fake_popen(cmd, **kwargs):
        captured["cmd"] = cmd

        class Proc:
            stdout = None
            returncode = 0

            def wait(self):
                return 0

        return Proc()

    monkeypatch.setattr(docker_runner.subprocess, "Popen", fake_popen)

    docker_runner.compose_up_streaming(
        cwd=tmp_path,
        profiles=["p1"],
        services=["svc1", "svc2"],
        remove_orphans=True,
        detach=True,
    )

    assert captured["cmd"][:6] == ["docker", "compose", "--profile", "p1", "up", "-d"]
    assert "--remove-orphans" in captured["cmd"]
    assert captured["cmd"][-2:] == ["svc1", "svc2"]

