from unittest.mock import patch
from pathlib import Path

import pytest
from click.testing import CliRunner
from onako.cli import main


@pytest.fixture(autouse=True)
def isolate_pid_file(tmp_path):
    """Prevent tests from touching the real PID file."""
    fake_pid = tmp_path / "onako.pid"
    fake_dev = tmp_path / "onako.dev"
    with patch("onako.cli.PID_FILE", fake_pid), \
         patch("onako.cli.DEV_MARKER", fake_dev):
        yield fake_pid


def test_status_reports_something():
    runner = CliRunner()
    result = runner.invoke(main, ["status"])
    assert result.exit_code == 0
    assert "Onako server:" in result.output


def test_stop_completes():
    runner = CliRunner()
    result = runner.invoke(main, ["stop"])
    assert result.exit_code == 0
    assert "not running" in result.output or "stopped" in result.output


def test_stop_handles_no_service():
    """Stop should handle the case where neither pid file nor service exists."""
    runner = CliRunner()
    result = runner.invoke(main, ["stop"])
    assert result.exit_code == 0
