import subprocess

import pytest

from l4d2host.status import get_instance_status, map_active_state


def test_status_mapping() -> None:
    assert map_active_state("active") == "running"
    assert map_active_state("inactive") == "stopped"
    assert map_active_state("failed") == "stopped"
    assert map_active_state("weird") == "unknown"


def test_get_instance_status_uses_systemctl_helper(monkeypatch: pytest.MonkeyPatch) -> None:
    calls: list[list[str]] = []

    def fake_run_command(cmd, **kwargs):
        del kwargs
        calls.append(list(cmd))
        return subprocess.CompletedProcess(cmd, 0, stdout="ActiveState=active\nSubState=running\n", stderr="")

    monkeypatch.setattr("l4d2host.service_control.run_command", fake_run_command)

    status = get_instance_status("alpha")

    assert calls == [["sudo", "-n", "/usr/local/libexec/left4me/left4me-systemctl", "show", "alpha"]]
    assert status.state == "running"
