"""Tests for the _check_live_gui_health autouse fixture (Phase 2, Task 2.2)."""
import time
import pytest


def test_handle_has_ensure_alive_method(live_gui) -> None:
 """The _LiveGuiHandle has an ensure_alive() method that is callable."""
 handle = live_gui
 assert hasattr(handle, 'ensure_alive')
 assert callable(handle.ensure_alive)


def test_handle_respawn_count_is_zero_on_fresh_start(live_gui) -> None:
 """A freshly-spawned handle has respawn_count == 0."""
 handle = live_gui
 assert handle.respawn_count == 0


def test_ensure_alive_is_fast_on_clean_subprocess(live_gui) -> None:
 """ensure_alive() on a running subprocess is < 100ms (per Phase 2 budget)."""
 handle = live_gui
 t0 = time.perf_counter()
 handle.ensure_alive()
 elapsed = time.perf_counter() - t0
 assert elapsed < 0.1, f"ensure_alive took {elapsed:.3f}s on a clean subprocess"


def test_is_alive_returns_true_for_running_subprocess(live_gui) -> None:
 """is_alive() returns True for a running subprocess."""
 handle = live_gui
 assert handle.is_alive()


def test_autouse_health_fixture_does_not_break_tests(live_gui) -> None:
 """The _check_live_gui_health autouse fixture runs before this test and calls ensure_alive(). The test should complete without error."""
 handle = live_gui
 assert handle.is_alive()  # If the autouse fixture didn't run, the process might be dead
 assert handle.respawn_count == 0  # No respawns yet
