"""Tests for ``mgf-common version`` / ``--version`` (OP-01 / WF-07)."""

from __future__ import annotations

import argparse
import json

from mgf.common import __version__

from mgf.fed.cli._mgfcli import main
from mgf.fed.cli._version import render_version, run_version, version_facts


class TestVersionFacts:
    def test_facts_shape(self) -> None:
        f = version_facts()
        assert f["name"] == "mgf-common"
        assert f["version"] == __version__
        assert f["python"]  # non-empty
        assert "pydantic" in f["deps"]  # type: ignore[operator]

    def test_render_contains_version_python_deps(self) -> None:
        out = render_version()
        assert f"mgf-common {__version__}" in out
        assert "python:" in out
        assert "deps:" in out


class TestVersionSubcommand:
    def test_run_version_human(self, capsys) -> None:
        rc = run_version(argparse.Namespace(json=False))
        assert rc == 0
        assert f"mgf-common {__version__}" in capsys.readouterr().out

    def test_run_version_json(self, capsys) -> None:
        rc = run_version(argparse.Namespace(json=True))
        assert rc == 0
        payload = json.loads(capsys.readouterr().out)
        assert payload["version"] == __version__
        assert payload["name"] == "mgf-common"


class TestBareVersionFlag:
    """WF-07: the release smoke calls `mgf-common --version` and aborts on
    a non-zero exit. This pins exit 0 + the version on stdout."""

    def test_dash_dash_version_exits_zero(self, capsys) -> None:
        rc = main(["--version"])
        assert rc == 0
        assert __version__ in capsys.readouterr().out

    def test_dash_v_exits_zero(self, capsys) -> None:
        rc = main(["-V"])
        assert rc == 0
        assert __version__ in capsys.readouterr().out
