"""testctl CLI — fail-closed when the warden is down; honest broker:down on status."""

from __future__ import annotations

import json
from pathlib import Path

import pytest

from mgf.test_web.testctl import main


def test_run_fails_closed_when_warden_down(
    tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
    rc = main(["-C", str(tmp_path), "run", "tests/test_x.py::test_y"])
    assert rc == 1
    assert "REFUSED" in capsys.readouterr().out


def test_status_reports_broker_down(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
    rc = main(["-C", str(tmp_path), "status"])
    assert rc == 0
    assert "broker=down" in capsys.readouterr().out


def test_status_json_broker_down(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
    rc = main(["-C", str(tmp_path), "--format", "json", "status"])
    assert rc == 0
    assert json.loads(capsys.readouterr().out)["broker"] == "down"
