"""Integration tests for ``live_server``'s real subprocess lifecycle (tier-2).

These spawn a trivial Python child rather than a real console — one that exits
immediately, and one that runs but never binds its port — so both failure paths
are exercised with no browser and no console repo present. They also pin EH-08:
the raised error carries machine-readable fields, not just a message.
"""

from __future__ import annotations

import sys

import pytest

from mgf.uitest_web import ServerStartError, free_port, live_server

pytestmark = pytest.mark.integration


def test_live_server_raises_with_returncode_when_process_exits_early() -> None:
    port = free_port()
    with (
        pytest.raises(ServerStartError) as ei,
        live_server([sys.executable, "-c", "import sys; sys.exit(7)"], port=port, timeout=5),
    ):
        pass
    assert ei.value.returncode == 7
    assert "exited with code 7" in str(ei.value)


def test_live_server_times_out_when_it_never_serves() -> None:
    port = free_port()
    with (
        pytest.raises(ServerStartError) as ei,
        live_server([sys.executable, "-c", "import time; time.sleep(30)"], port=port, timeout=1),
    ):
        pass
    assert ei.value.returncode is None  # still running when we gave up → terminated
    assert "did not answer" in str(ei.value)
