"""The console entry runs the app on localhost:8788 via uvicorn (no real serve)."""

from __future__ import annotations

from typing import Any

import pytest
from fastapi import FastAPI

import mgf.fed_web.__main__ as entry


def test_main_runs_uvicorn_on_localhost_8788(monkeypatch: pytest.MonkeyPatch) -> None:
    captured: dict[str, Any] = {}

    def fake_run(app: Any, *, host: str, port: int) -> None:
        captured["app"] = app
        captured["host"] = host
        captured["port"] = port

    monkeypatch.setattr("uvicorn.run", fake_run)
    entry.main([])

    assert captured["host"] == "127.0.0.1"
    assert captured["port"] == 8788
    assert isinstance(captured["app"], FastAPI)


def test_version_flag_reports_and_exits(capsys: pytest.CaptureFixture[str]) -> None:
    # The console entry must answer --version (OP-01) — the release-smoke CI step asserts it.
    with pytest.raises(SystemExit) as exc:
        entry.main(["--version"])
    assert exc.value.code == 0
    assert "0.1.0" in capsys.readouterr().out


def test_host_and_port_overrides(monkeypatch: pytest.MonkeyPatch) -> None:
    captured: dict[str, Any] = {}
    monkeypatch.setattr(
        "uvicorn.run",
        lambda app, *, host, port: captured.update(host=host, port=port),
    )
    bind = "0.0.0.0"  # noqa: S104 — exercising the override, not a real bind
    entry.main(["--host", bind, "--port", "9001"])
    assert captured == {"host": bind, "port": 9001}
