from unittest.mock import patch, MagicMock
from hyprsunset_qt import daemon


def test_is_running_true():
    with patch("hyprsunset_qt.daemon.subprocess.run") as run:
        run.return_value = MagicMock(returncode=0)
        assert daemon.is_running() is True
        run.assert_called_once()


def test_is_running_false():
    with patch("hyprsunset_qt.daemon.subprocess.run") as run:
        run.return_value = MagicMock(returncode=1)
        assert daemon.is_running() is False


def test_live_preview_identity():
    with patch("hyprsunset_qt.daemon.subprocess.run") as run:
        daemon.live_preview(identity=True)
        run.assert_called_with(
            ["hyprctl", "hyprsunset", "identity"], check=False
        )


def test_live_preview_temp_and_gamma():
    with patch("hyprsunset_qt.daemon.subprocess.run") as run:
        daemon.live_preview(temperature=5500, gamma=0.8)
        calls = [c.args[0] for c in run.call_args_list]
        assert ["hyprctl", "hyprsunset", "temperature", "5500"] in calls
        assert ["hyprctl", "hyprsunset", "gamma", "80"] in calls


def test_restart_kills_then_launches():
    with patch("hyprsunset_qt.daemon.subprocess.run") as run, \
         patch("hyprsunset_qt.daemon.subprocess.Popen") as popen:
        daemon.restart("hyprsunset")
        run.assert_called_with(["pkill", "hyprsunset"], check=False)
        popen.assert_called_once()
        assert popen.call_args[0][0] == ["hyprsunset"]
