"""CLI dispatch tests — every subcommand routes to its handler."""

from __future__ import annotations

import pytest

from mgf.fed import __main__
from mgf.fed.__main__ import main


def test_version_flag(capsys: pytest.CaptureFixture[str]) -> None:
    with pytest.raises(SystemExit) as exc:
        main(["--version"])
    assert exc.value.code == 0
    assert "mgf-fed" in capsys.readouterr().out


@pytest.mark.parametrize(
    ("argv", "handler"),
    [
        (["status"], "run_status"),
        (["drift-check"], "run_drift_check"),
        (["local-index"], "run_local_index"),
        (["gates"], "run_gates"),
        (["local-ci", "mgf-http"], "run_local_ci"),
        (["release-local", "mgf-x"], "release_local"),
        (["release-remote", "mgf-x"], "release_remote"),
        (["scaffold", "mgf-x"], "run_scaffold"),
        (["registry"], "run_registry"),
    ],
)
def test_dispatch(monkeypatch: pytest.MonkeyPatch, argv: list[str], handler: str) -> None:
    seen: dict[str, bool] = {}

    def _fake(*_a: object, **_k: object) -> int:
        seen["called"] = True
        return 0

    monkeypatch.setattr(__main__, handler, _fake)
    assert main(argv) == 0
    assert seen.get("called") is True


def test_no_command_errors() -> None:
    with pytest.raises(SystemExit):
        main([])  # subparsers required=True → argparse exits non-zero
