"""Construct the optional Gradio UI without opening a listener."""

from __future__ import annotations

import subprocess
import sys

import pytest


pytestmark = pytest.mark.operator_ui


def test_gradio_application_constructs_without_public_listener():
    pytest.importorskip("gradio", reason="install the locked spaces extra")
    probe = subprocess.run(
        [
            sys.executable,
            "-c",
            "import app, gradio; "
            "assert type(app.demo).__name__ == 'Blocks'; "
            "assert app.demo.local_url is None; "
            "assert int(gradio.__version__.split('.')[0]) >= 6; "
            "print('operator UI constructed without a listener')",
        ],
        check=False,
        capture_output=True,
        text=True,
        timeout=120,
    )
    assert probe.returncode == 0, probe.stderr
    assert probe.stdout.strip() == "operator UI constructed without a listener"
    assert probe.stderr == ""
