import sys
from io import StringIO

from l4d2host.logging import emit_step


def test_emit_step_uses_callback() -> None:
    calls: list[str] = []
    emit_step("test step", on_stdout=calls.append, passthrough=False)
    assert calls == ["Step: test step"]


def test_emit_step_uses_passthrough_stdout(monkeypatch) -> None:
    fake_out = StringIO()
    monkeypatch.setattr(sys, "stdout", fake_out)
    emit_step("passthrough step", on_stdout=None, passthrough=True)
    assert fake_out.getvalue() == "Step: passthrough step\n"


def test_emit_step_does_nothing_if_no_target() -> None:
    emit_step("silent step", on_stdout=None, passthrough=False)


def test_emit_step_does_both(monkeypatch) -> None:
    calls: list[str] = []
    fake_out = StringIO()
    monkeypatch.setattr(sys, "stdout", fake_out)
    emit_step("both step", on_stdout=calls.append, passthrough=True)
    assert calls == ["Step: both step"]
    assert fake_out.getvalue() == "Step: both step\n"
