Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

64.1 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Loads cloudflare_tunnel.py without the venv; run.py defaults are read by AST, not imported."""

import ast
import errno
import importlib.util
import io
import os
import ssl
import sys
import tarfile
import tempfile
import threading as _real_threading
import time as _real_time
import types
from pathlib import Path
from typing import Optional

import pytest

_BACKEND = Path(__file__).resolve().parent.parent
_CT_PY = _BACKEND / "cloudflare_tunnel.py"
_RUN_PY = _BACKEND / "run.py"


def _load_ct():
    spec = importlib.util.spec_from_file_location("cloudflare_tunnel", _CT_PY)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


ct = _load_ct()
_real_wait_before_retry = ct._wait_before_retry


def test_url_regex_extracts_and_ignores_noise():
    blob = (
        "2026-06-11T10:00:00Z INF Thank you for trying Cloudflare Tunnel.\n"
        "2026-06-11T10:00:01Z INF Requesting new quick Tunnel on trycloudflare.com...\n"
        "2026-06-11T10:00:01Z INF |  https://setting-democracy-gathering.trycloudflare.com  |\n"
        "2026-06-11T10:00:02Z INF Registered tunnel connection https://not-the-url.example.com\n"
    )
    m = ct._URL_RE.search(blob)
    assert m is not None
    assert m.group(0) == "https://setting-democracy-gathering.trycloudflare.com"


def test_url_regex_no_match_on_unrelated():
    assert ct._URL_RE.search("INF connecting to https://api.cloudflare.com/v4") is None


def test_url_regex_ignores_api_endpoint():
    # cloudflared's API host appears in failure lines; it is not a tunnel URL.
    line = (
        'failed to request quick Tunnel: Post "https://api.trycloudflare.com/tunnel": '
        "context deadline exceeded"
    )
    assert ct._URL_RE.search(line) is None


def test_url_regex_skips_api_host_but_matches_real_url():
    blob = (
        'ERR failed to request quick Tunnel: Post "https://api.trycloudflare.com/tunnel"\n'
        "INF |  https://brave-mountain-river-clouds.trycloudflare.com  |\n"
    )
    m = ct._URL_RE.search(blob)
    assert m is not None
    assert m.group(0) == "https://brave-mountain-river-clouds.trycloudflare.com"


@pytest.mark.parametrize(
    "host,expected",
    [
        ("localhost", "http://localhost:8080"),
        ("127.0.0.1", "http://127.0.0.1:8080"),
        ("::1", "http://[::1]:8080"),
        ("fe80::1234%eth0", "http://[fe80::1234%25eth0]:8080"),
        ("fe80::1234%12", "http://[fe80::1234%2512]:8080"),
    ],
)
def test_origin_url_brackets_ipv6_hosts(host, expected):
    assert ct._origin_url(host, 8080) == expected


@pytest.mark.parametrize(
    "system,machine,expected",
    [
        ("Linux", "x86_64", ("cloudflared-linux-amd64", False)),
        ("Linux", "aarch64", ("cloudflared-linux-arm64", False)),
        ("Darwin", "arm64", ("cloudflared-darwin-arm64.tgz", True)),
        ("Darwin", "x86_64", ("cloudflared-darwin-amd64.tgz", True)),
        ("Windows", "AMD64", ("cloudflared-windows-amd64.exe", False)),
        ("Windows", "x86", ("cloudflared-windows-386.exe", False)),
        ("Linux", "mips", None),
        ("Plan9", "x86_64", None),
    ],
)
def test_asset_name(monkeypatch, system, machine, expected):
    monkeypatch.setattr(ct.platform, "system", lambda: system)
    monkeypatch.setattr(ct.platform, "machine", lambda: machine)
    assert ct._asset_name() == expected


def test_find_cloudflared_prefers_path(monkeypatch):
    monkeypatch.setattr(ct.shutil, "which", lambda name: "/usr/local/bin/cloudflared")
    assert ct.find_cloudflared() == "/usr/local/bin/cloudflared"


def test_find_cloudflared_falls_back_to_cache(monkeypatch, tmp_path):
    cached = tmp_path / "cloudflared"
    cached.write_text("#!/bin/sh\n")
    cached.chmod(0o755)
    monkeypatch.setattr(ct.shutil, "which", lambda name: None)
    monkeypatch.setattr(ct, "_cache_path", lambda: cached)
    assert ct.find_cloudflared() == str(cached)


def test_find_cloudflared_none_when_missing(monkeypatch, tmp_path):
    monkeypatch.setattr(ct.shutil, "which", lambda name: None)
    monkeypatch.setattr(ct, "_cache_path", lambda: tmp_path / "absent")
    assert ct.find_cloudflared() is None


def test_ensure_downloads_and_chmods_when_missing(monkeypatch, tmp_path):
    cached = tmp_path / "cloudflared"
    monkeypatch.setattr(ct, "find_cloudflared", lambda: None)
    monkeypatch.setattr(ct, "_asset_name", lambda: ("cloudflared-linux-amd64", False))
    monkeypatch.setattr(ct, "_cache_path", lambda: cached)

    def fake_download(url, dest):
        assert url.endswith("/cloudflared-linux-amd64")
        dest.write_bytes(b"ELF-ish")
        return True

    monkeypatch.setattr(ct, "_download", fake_download)
    monkeypatch.setattr(ct.sys, "platform", "linux")
    path = ct.ensure_cloudflared()
    assert path == str(cached)
    assert cached.exists()
    if os.name != "nt":
        assert cached.stat().st_mode & 0o111


def test_ensure_returns_none_on_download_failure(monkeypatch, tmp_path):
    monkeypatch.setattr(ct, "find_cloudflared", lambda: None)
    monkeypatch.setattr(ct, "_asset_name", lambda: ("cloudflared-linux-amd64", False))
    monkeypatch.setattr(ct, "_cache_path", lambda: tmp_path / "cloudflared")
    monkeypatch.setattr(ct, "_download", lambda url, dest: False)
    assert ct.ensure_cloudflared() is None


def test_ensure_returns_none_for_unsupported_arch(monkeypatch, tmp_path):
    monkeypatch.setattr(ct, "find_cloudflared", lambda: None)
    monkeypatch.setattr(ct, "_asset_name", lambda: None)
    monkeypatch.setattr(ct, "_cache_path", lambda: tmp_path / "cloudflared")
    assert ct.ensure_cloudflared() is None


def test_download_sets_user_agent(monkeypatch, tmp_path):
    import urllib.request

    captured = {}

    class _Resp:
        _sent = False

        def __enter__(self):
            return self

        def __exit__(self, *a):
            return False

        def read(self, n = -1):
            if self._sent:
                return b""
            self._sent = True
            return b"payload"

    def fake_urlopen(req, timeout = None):
        captured["ua"] = req.get_header("User-agent")
        return _Resp()

    monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
    dest = tmp_path / "cloudflared"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert captured["ua"] == "unsloth-studio"  # GitHub CDN 403s the default UA
    assert dest.read_bytes() == b"payload"


def test_cache_path_uses_exe_on_windows(monkeypatch, tmp_path):
    import types

    fake_sr = types.ModuleType("utils.paths.storage_roots")
    fake_sr.studio_bin_root = lambda: tmp_path
    monkeypatch.setitem(sys.modules, "utils.paths.storage_roots", fake_sr)
    monkeypatch.setattr(ct.sys, "platform", "win32")
    assert ct._cache_path() == tmp_path / "cloudflared.exe"


def test_ensure_windows_downloads_exe(monkeypatch, tmp_path):
    cached = tmp_path / "cloudflared.exe"
    monkeypatch.setattr(ct, "find_cloudflared", lambda: None)
    monkeypatch.setattr(ct, "_asset_name", lambda: ("cloudflared-windows-amd64.exe", False))
    monkeypatch.setattr(ct, "_cache_path", lambda: cached)
    monkeypatch.setattr(ct.sys, "platform", "win32")

    def fake_download(url, dest):
        assert url.endswith("/cloudflared-windows-amd64.exe")
        dest.write_bytes(b"MZ")
        return True

    monkeypatch.setattr(ct, "_download", fake_download)
    monkeypatch.setattr(ct.os, "chmod", lambda *a, **k: pytest.fail("chmod called on win32"))
    assert ct.ensure_cloudflared() == str(cached)
    assert cached.read_bytes() == b"MZ"


def test_ensure_macos_extracts_tgz_and_chmods(monkeypatch, tmp_path):
    cached = tmp_path / "cloudflared"
    monkeypatch.setattr(ct, "find_cloudflared", lambda: None)
    monkeypatch.setattr(ct, "_asset_name", lambda: ("cloudflared-darwin-arm64.tgz", True))
    monkeypatch.setattr(ct, "_cache_path", lambda: cached)
    monkeypatch.setattr(ct.sys, "platform", "darwin")

    def fake_download(url, dest):
        assert url.endswith("/cloudflared-darwin-arm64.tgz")
        with tarfile.open(dest, "w:gz") as tar:
            data = b"mach-o"
            info = tarfile.TarInfo(name = "cloudflared")
            info.size = len(data)
            tar.addfile(info, io.BytesIO(data))
        return True

    monkeypatch.setattr(ct, "_download", fake_download)
    path = ct.ensure_cloudflared()
    assert path == str(cached)
    assert cached.read_bytes() == b"mach-o"
    if os.name != "nt":
        assert cached.stat().st_mode & 0o111
    assert not cached.with_suffix(".tgz").exists()


def _make_tgz(
    tmp_path,
    member_name,
    data = b"bin",
):
    tgz = tmp_path / "cf.tgz"
    with tarfile.open(tgz, "w:gz") as tar:
        info = tarfile.TarInfo(name = member_name)
        info.size = len(data)
        tar.addfile(info, io.BytesIO(data))
    return tgz


def test_tgz_extraction_extracts_clean_member(tmp_path):
    tgz = _make_tgz(tmp_path, "cloudflared")
    dest = tmp_path / "out"
    assert ct._extract_tgz_member(tgz, dest) is True
    assert dest.read_bytes() == b"bin"


def test_tgz_extraction_rejects_traversal(tmp_path):
    tgz = _make_tgz(tmp_path, "../cloudflared")
    dest = tmp_path / "out"
    assert ct._extract_tgz_member(tgz, dest) is False
    assert not dest.exists()


def test_tgz_extraction_missing_member(tmp_path):
    tgz = _make_tgz(tmp_path, "README")
    dest = tmp_path / "out"
    assert ct._extract_tgz_member(tgz, dest) is False


class _FakePopen:
    def __init__(self):
        self.terminated = False
        self.killed = False
        self._alive = True
        self.pid = 424243

    def poll(self):
        return None if self._alive else 0

    def terminate(self):
        self.terminated = True
        self._alive = False

    def wait(self, timeout = None):
        if self._alive:
            raise ct.subprocess.TimeoutExpired(cmd = "cloudflared", timeout = timeout)
        return 0

    def kill(self):
        self.killed = True
        self._alive = False


def test_stop_terminates_process():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    fake = _FakePopen()
    t._proc = fake
    t.stop()
    assert fake.terminated is True
    assert t._proc is None
    t.stop()


def test_runtime_callback_covers_process_start_through_stop(monkeypatch):
    events = []
    starts_admitted = []
    ct.set_studio_tunnel_runtime_callback(events.append)

    class _ObservedPopen(_FakePopen):
        stdout = None
        block_termination = True

        def terminate(self):
            assert events[-1] is True
            if self.block_termination:
                ct.start_studio_tunnel(8081, managed_by = "settings")
                raise OSError("termination unavailable")
            super().terminate()

    fake = _ObservedPopen()

    def fake_popen(*_args, **_kwargs):
        assert events[-1] is True
        return fake

    class _NoReaderThread:
        def __init__(self, **_kwargs):
            pass

        def start(self):
            pass

    # Patch Thread only on ct: a process-wide fake stalls process_lifetime's 5s spawner backstop.
    class _ThreadingShim:
        Thread = _NoReaderThread

        def __getattr__(self, name):
            return getattr(_real_threading, name)

    monkeypatch.setattr(ct.subprocess, "Popen", fake_popen)
    monkeypatch.setattr(ct, "threading", _ThreadingShim())
    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: starts_admitted.append(True))
    try:
        tunnel = ct.CloudflareTunnel(8080, "/bin/cloudflared")
        tunnel.start()
        assert events[-1] is True
        ct._active_tunnel = tunnel
        ct._tunnel_state = "online"
        ct._tunnel_owner = "settings"
        ct._tunnel_port = 8080
        ct._tunnel_state = "stopping"
        ct._active_tunnel_exited(tunnel)
        assert ct.get_studio_tunnel_status()["state"] == "stopping"
        ct._tunnel_state = "online"
        ct._active_tunnel_exited(tunnel)
        assert events[-1] is True
        assert starts_admitted == []
        assert ct._active_tunnel is tunnel
        assert ct.get_studio_tunnel_status()["state"] == "error"
        assert ct.get_studio_tunnel_status()["stop_pending"] is True
        fake.block_termination = False
        ct.stop_studio_tunnel()
        assert events[-1] is False
        assert ct.get_studio_tunnel_status()["state"] == "off"
        ct._retain_studio_tunnel_for_stop(tunnel)
        assert ct._tunnels_pending_stop_snapshot() == ()
    finally:
        ct.set_studio_tunnel_runtime_callback(None)


def test_start_after_stop_does_not_spawn(monkeypatch):
    # stop() before start() must not spawn an orphaned cloudflared.
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    spawned = []

    class _FakeProc:
        stdout = None

        def poll(self):
            return 0

    monkeypatch.setattr(ct.subprocess, "Popen", lambda *a, **k: (spawned.append(a), _FakeProc())[1])
    t.stop()
    t.start()
    assert spawned == []
    assert t._proc is None


def test_wait_for_ready_times_out_without_blocking():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    assert t.wait_for_ready(timeout = 0.05) is None


def _fake_proc(text):
    return types.SimpleNamespace(stdout = io.StringIO(text))


def test_reader_captures_url_and_registration():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    exited = []
    t.set_on_exit(exited.append)
    t._reader(
        _fake_proc(
            "INF Requesting new quick Tunnel on trycloudflare.com...\n"
            "INF |  https://words-here-abc.trycloudflare.com  |\n"
            "INF Registered tunnel connection connIndex=0 protocol=http2\n"
        )
    )
    assert t.url == "https://words-here-abc.trycloudflare.com"
    assert t.ready is True
    assert t.wait_for_ready(0) == t.url
    assert t.error == "cloudflared exited"
    assert exited == [t]


def test_reader_keeps_the_output_tail():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    lines = [f"ERR line {i}" for i in range(ct._OUTPUT_TAIL_LINES + 3)]
    t._reader(_fake_proc("".join(f"{line}\n" for line in lines)))
    assert t.output_tail() == "\n".join(lines[-ct._OUTPUT_TAIL_LINES :])


def test_reader_url_without_registration_is_not_ready():
    # A URL without a registered connection serves Cloudflare error 1033.
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    t._reader(
        _fake_proc(
            "INF |  https://words-here-abc.trycloudflare.com  |\n"
            'ERR failed to serve tunnel connection error="control stream failure"\n'
        )
    )
    assert t.url == "https://words-here-abc.trycloudflare.com"
    assert t.ready is False
    assert t.wait_for_ready(0) is None
    assert t.error == "cloudflared exited before the tunnel connection registered"


def test_reader_handles_none_stdout():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    t._reader(types.SimpleNamespace(stdout = None))
    assert t.url is None
    assert t.ready is False
    assert t.wait_for_ready(0) is None
    assert t.error == "cloudflared exited before emitting a tunnel URL"


def test_reader_ignores_api_endpoint_failure_line():
    t = ct.CloudflareTunnel(8080, "/bin/cloudflared")
    t._reader(
        _fake_proc(
            "ERR failed to request quick Tunnel: Post "
            '"https://api.trycloudflare.com/tunnel": context deadline exceeded\n'
        )
    )
    assert t.url is None
    assert t.wait_for_ready(0) is None
    assert t.error == "cloudflared exited before emitting a tunnel URL"


class _FakeResponse:
    def __init__(self, body):
        self._body = body

    def read(self, size = -1):
        return self._body

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        return False


def _patch_urlopen(monkeypatch, handler):
    import urllib.request
    monkeypatch.setattr(urllib.request, "urlopen", lambda req, timeout = None: handler(req))


@pytest.fixture(autouse = True)
def _stub_remote_lookups(monkeypatch, request):
    if request.node.name.startswith("test_verify_public_url"):
        monkeypatch.setattr(ct, "_wait_for_dns", lambda *a, **kw: None)
        monkeypatch.setattr(ct, "_edge_addresses", list)


def test_wait_for_dns_polls_until_answer(monkeypatch):
    calls = []

    def handler(req):
        calls.append(req.full_url)
        if len(calls) < 3:
            return _FakeResponse(b'{"Status":3}')
        return _FakeResponse(b'{"Status":0,"Answer":[{"data":"104.16.0.1"}]}')

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    ct._wait_for_dns("words.trycloudflare.com", ct.time.monotonic() + 5)
    assert len(calls) == 3
    assert "name=words.trycloudflare.com" in calls[0]
    assert all("cloudflare-dns.com" in call for call in calls)


def test_wait_for_dns_gives_up_at_deadline(monkeypatch):
    _patch_urlopen(monkeypatch, lambda req: _FakeResponse(b'{"Status":3}'))
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    ct._wait_for_dns("words.trycloudflare.com", ct.time.monotonic() + 0.05)


def test_wait_for_dns_retries_transient_doh_error(monkeypatch):
    calls = []

    def handler(req):
        calls.append(req.full_url)
        if len(calls) < 3:
            raise OSError("transient")
        return _FakeResponse(b'{"Status":0,"Answer":[{"data":"104.16.0.1"}]}')

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    ct._wait_for_dns("words.trycloudflare.com", ct.time.monotonic() + 5)
    assert len(calls) == 3


def test_wait_for_dns_bails_on_persistent_doh_errors(monkeypatch):
    calls = []

    def handler(req):
        calls.append(req.full_url)
        raise OSError("blocked")

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    ct._wait_for_dns("words.trycloudflare.com", ct.time.monotonic() + 5)
    assert len(calls) == ct._DNS_MAX_DOH_ERRORS


def test_wait_for_dns_delays_first_query(monkeypatch):
    order = []

    def handler(req):
        order.append("query")
        return _FakeResponse(b'{"Status":0,"Answer":[{"data":"104.16.0.1"}]}')

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda s: order.append(("sleep", s)))
    ct._wait_for_dns("words.trycloudflare.com", ct.time.monotonic() + 30)
    assert ct._DNS_INITIAL_GRACE >= 1.0
    assert order[0] == ("sleep", ct._DNS_INITIAL_GRACE)
    assert order[1] == "query"


def test_wait_for_dns_is_capped_below_the_probe_deadline(monkeypatch):
    clock = [0.0]
    calls = []

    def handler(req):
        calls.append(req.full_url)
        return _FakeResponse(b'{"Status":3}')

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(ct.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    ct._wait_for_dns("words.trycloudflare.com", 300.0)
    assert calls
    assert clock[0] <= ct._DNS_WAIT_MAX + ct._DNS_POLL_DELAY
    assert clock[0] < ct._PUBLIC_PROBE_TIMEOUT / 2


def test_verify_public_url_accepts_studio_marker(monkeypatch):
    seen = {}

    def handler(req):
        seen["url"] = req.full_url
        return _FakeResponse(b'{"status":"healthy","service":"Unsloth UI Backend"}')

    _patch_urlopen(monkeypatch, handler)
    assert ct.verify_public_url("https://words.trycloudflare.com") is True
    assert seen["url"] == "https://words.trycloudflare.com/api/health"


def test_verify_public_url_waits_for_dns_before_probing_the_hostname(monkeypatch):
    order = []
    monkeypatch.setattr(ct, "_wait_for_dns", lambda host, deadline: order.append(("dns", host)))

    def handler(req):
        order.append(("probe", req.full_url))
        return _FakeResponse(b'{"service":"Unsloth UI Backend"}')

    _patch_urlopen(monkeypatch, handler)
    assert ct.verify_public_url("https://words.trycloudflare.com") is True
    assert order[0] == ("dns", "words.trycloudflare.com")
    assert order[1][0] == "probe"


def test_verify_public_url_dns_wait_and_probe_share_deadline(monkeypatch):
    calls = []
    monkeypatch.setattr(ct, "_wait_for_dns", lambda host, deadline: None)

    def handler(req):
        calls.append(req.full_url)
        raise OSError("unreachable")

    _patch_urlopen(monkeypatch, handler)
    assert ct.verify_public_url("https://words.trycloudflare.com", timeout = 0) is False
    assert len(calls) == 1


def test_verify_public_url_retries_then_succeeds(monkeypatch):
    calls = []

    def handler(req):
        calls.append(req.full_url)
        if len(calls) < 3:
            raise OSError("Name or service not known")
        return _FakeResponse(b'{"service":"Unsloth UI Backend"}')

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    assert ct.verify_public_url("https://words.trycloudflare.com") is True
    assert len(calls) == 3


def test_verify_public_url_rejects_unreachable_host(monkeypatch):
    def handler(req):
        raise OSError("Name or service not known")

    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    assert ct.verify_public_url("https://words.trycloudflare.com", timeout = 0.05) is False


def test_verify_public_url_rejects_foreign_responder(monkeypatch):
    _patch_urlopen(monkeypatch, lambda req: _FakeResponse(b"<html>error 1033</html>"))
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    assert ct.verify_public_url("https://words.trycloudflare.com", timeout = 0.05) is False


def _fake_edge(
    monkeypatch,
    payload: bytes,
    connect_error: Optional[str] = None,
) -> dict:
    """Stand in for the TLS hop so the probe's own parsing is under test."""
    import socket as socket_module
    import ssl as ssl_module

    seen: dict = {}

    class _Closeable:
        def __enter__(self):
            return self

        def __exit__(self, *_a):
            return False

    class _Tls(_Closeable):
        def sendall(self, data):
            seen["request"] = data

        def makefile(self, *_a, **_kw):
            return io.BytesIO(payload)

    class _Context:
        # The probe dials a bare address, so SNI hostname matching binds the answer to the tunnel.
        check_hostname = True
        verify_mode = ssl_module.CERT_REQUIRED

        def wrap_socket(
            self,
            _raw,
            server_hostname = None,
        ):
            seen["sni"] = server_hostname
            seen["verified"] = self.check_hostname and self.verify_mode == ssl_module.CERT_REQUIRED
            return _Tls()

    def connect(address, timeout = None):
        seen["timeout"] = timeout
        if connect_error is not None:
            raise OSError(connect_error)
        seen["address"] = address
        return _Closeable()

    monkeypatch.setattr(socket_module, "create_connection", connect)
    monkeypatch.setattr(ssl_module, "create_default_context", _Context)
    return seen


def _edge_response(status: bytes, body: bytes) -> bytes:
    return b"HTTP/1.1 %s\r\nContent-Length: %d\r\n\r\n%s" % (status, len(body), body)


def test_edge_probe_selects_the_tunnel_by_sni(monkeypatch):
    seen = _fake_edge(monkeypatch, _edge_response(b"200 OK", b'{"service":"Unsloth UI Backend"}'))
    assert ct._probe_edge("104.16.0.1", "words.trycloudflare.com") is True
    assert seen["address"] == ("104.16.0.1", 443)
    assert seen["sni"] == "words.trycloudflare.com"
    assert seen["verified"] is True
    assert seen["timeout"] == ct._PUBLIC_PROBE_ATTEMPT_TIMEOUT
    assert seen["request"].startswith(f"GET {ct._PUBLIC_PROBE_PATH} HTTP/1.1\r\n".encode())
    assert b"Host: words.trycloudflare.com\r\n" in seen["request"]


def test_edge_probe_rejects_the_cloudflare_error_page(monkeypatch):
    _fake_edge(monkeypatch, _edge_response(b"530 ", b"<html>error 1033</html>"))
    assert ct._probe_edge("104.16.0.1", "words.trycloudflare.com") is False


def test_edge_probe_rejects_a_foreign_responder(monkeypatch):
    _fake_edge(monkeypatch, _edge_response(b"200 OK", b'{"service":"something else"}'))
    assert ct._probe_edge("104.16.0.1", "words.trycloudflare.com") is False


def test_edge_addresses_keep_one_entry_per_frontend(monkeypatch):
    import socket as socket_module

    resolved = [
        (socket_module.AF_INET, 1, 6, "", ("104.16.230.132", 443)),
        (socket_module.AF_INET6, 1, 6, "", ("::ffff:104.16.230.132", 443, 0, 0)),
        (socket_module.AF_INET6, 1, 6, "", ("2606:4700::6810:e684", 443, 0, 0)),
    ]
    monkeypatch.setattr(socket_module, "getaddrinfo", lambda *_a, **_kw: resolved)
    assert ct._edge_addresses() == ["104.16.230.132", "2606:4700::6810:e684"]


def test_edge_probe_reports_an_unreachable_edge_apart_from_a_wrong_answer(monkeypatch):
    _fake_edge(monkeypatch, b"", connect_error = "blocked")
    assert ct._probe_edge("104.16.0.1", "words.trycloudflare.com") is None


def test_edge_verification_skips_the_hostname_entirely(monkeypatch):
    seen = {}

    def probe(
        address,
        host,
        _timeout = None,
    ):
        seen["probe"] = (address, host)
        return True

    def resolve(*_a, **_kw):
        seen["dns"] = True

    monkeypatch.setattr(ct, "_edge_addresses", lambda: ["104.16.0.1"])
    monkeypatch.setattr(ct, "_probe_edge", probe)
    monkeypatch.setattr(ct, "_wait_for_dns", resolve)
    _patch_urlopen(monkeypatch, lambda req: pytest.fail("probed the hostname"))
    assert ct.verify_public_url("https://words.trycloudflare.com") is True
    assert seen["probe"] == ("104.16.0.1", "words.trycloudflare.com")
    assert "dns" not in seen


def test_edge_verification_polls_until_the_tunnel_answers(monkeypatch):
    # Error 1033 / proxy pages answer, so a markerless reply does not count towards giving up.
    answers = [False, None, False, None, True]
    monkeypatch.setattr(ct, "_edge_addresses", lambda: ["104.16.0.1"])
    monkeypatch.setattr(ct, "_probe_edge", lambda *_a: answers.pop(0))
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    assert ct._verify_through_edge("words.trycloudflare.com", ct.time.monotonic() + 5) is True
    assert not answers


def test_edge_verification_stops_at_the_deadline_mid_pass(monkeypatch):
    clock = [0.0]
    timeouts = []

    def probe(_address, _host, timeout):
        timeouts.append(timeout)
        clock[0] += 6.0
        return False

    monkeypatch.setattr(ct, "_edge_addresses", lambda: ["104.16.0.1", "104.16.0.2"])
    monkeypatch.setattr(ct, "_probe_edge", probe)
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(ct.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    assert ct._verify_through_edge("words.trycloudflare.com", 3.0) is False
    assert timeouts == [3.0]


def test_edge_verification_leaves_the_fallback_room_in_the_deadline(monkeypatch):
    clock = [0.0]
    monkeypatch.setattr(ct, "_edge_addresses", lambda: ["104.16.0.1"])
    monkeypatch.setattr(ct, "_probe_edge", lambda *_a: False)
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(ct.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    assert ct._verify_through_edge("words.trycloudflare.com", 300.0) is False
    assert clock[0] <= ct._EDGE_WAIT_MAX + ct._EDGE_PROBE_RETRY_DELAY
    left = ct._PUBLIC_PROBE_TIMEOUT - ct._EDGE_WAIT_MAX - ct._DNS_WAIT_MAX
    assert left >= 5 * ct._PUBLIC_PROBE_RETRY_DELAY


def test_blocked_edge_falls_back_to_the_hostname(monkeypatch):
    order = []

    def probe(*_a):
        order.append("edge")
        return None

    def handler(_req):
        order.append("hostname")
        return _FakeResponse(b'{"service":"Unsloth UI Backend"}')

    monkeypatch.setattr(ct, "_edge_addresses", lambda: ["104.16.0.1"])
    monkeypatch.setattr(ct, "_probe_edge", probe)
    monkeypatch.setattr(ct, "_wait_for_dns", lambda *_a: order.append("dns"))
    _patch_urlopen(monkeypatch, handler)
    monkeypatch.setattr(ct.time, "sleep", lambda _s: None)
    assert ct.verify_public_url("https://words.trycloudflare.com") is True
    assert order == ["edge"] * ct._EDGE_MAX_UNREACHABLE + ["dns", "hostname"]


@pytest.fixture(autouse = True)
def _stub_public_probe(monkeypatch, request):
    if not request.node.name.startswith("test_start_studio_tunnel"):
        return
    monkeypatch.setattr(ct, "verify_public_url", lambda url, **kw: True)
    monkeypatch.setattr(ct, "_wait_before_retry", lambda _d: False)


def test_start_studio_tunnel_no_binary(monkeypatch):
    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: None)
    assert ct.start_studio_tunnel(8080) is None


def test_start_studio_tunnel_drops_url_that_is_not_publicly_reachable(monkeypatch):
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return self.url

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    monkeypatch.setattr(ct, "verify_public_url", lambda url, **kw: False)
    assert ct.start_studio_tunnel(8080) is None
    assert attempts == [None]
    assert ct._active_tunnel is None


def test_start_studio_tunnel_returns_url_once_probe_passes(monkeypatch):
    probed = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            self.protocol = protocol
            self.origin_host = origin_host

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return self.url

        def stop(self):
            pass

    def _probe(url, **kw):
        probed.append(url)
        return True

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    monkeypatch.setattr(ct, "verify_public_url", _probe)
    try:
        assert ct.start_studio_tunnel(8080, origin_host = "::1") == "https://words.trycloudflare.com"
        assert probed == ["https://words.trycloudflare.com"]
        assert ct._active_tunnel.origin_host == "::1"
    finally:
        ct.stop_studio_tunnel()


def test_tunnel_status_tracks_owner_and_post_ready_exit(monkeypatch):
    instances = []

    class _Stub:
        late_exit = False

        def __init__(self, *_args, **_kwargs):
            self.url = "https://words.trycloudflare.com"
            self.error = None
            instances.append(self)

        start = stop = lambda self: None
        wait_for_ready = lambda self, timeout: self.url

        def set_on_exit(self, callback):
            self.on_exit = callback
            if self.late_exit:
                self.error = "cloudflared exited"

        def _publish_if_running(self, callback):
            if self.late_exit:
                return False
            callback()
            return True

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    monkeypatch.setattr(ct, "verify_public_url", lambda url, **kw: True)
    published = []
    ct.set_studio_tunnel_url_callback(published.append)
    assert ct.start_studio_tunnel(8087, managed_by = "settings") == instances[0].url
    status = ct.get_studio_tunnel_status()
    assert status["state"] == "online"
    assert status["managed_by"] == "settings"
    assert status["url"] == instances[0].url
    assert status["port"] == 8087
    instances[0].error = "cloudflared exited"
    instances[0].on_exit(instances[0])
    status = ct.get_studio_tunnel_status()
    assert status["state"] == "error"
    assert status["url"] is None
    assert status["error"] == "cloudflared exited"
    assert published[-1] is None
    ct.stop_studio_tunnel()
    _Stub.late_exit = True
    published.clear()
    assert ct.start_studio_tunnel(8087, managed_by = "settings") is None
    assert instances[-1].url not in published
    assert ct.get_studio_tunnel_status()["state"] == "error"
    ct.set_studio_tunnel_url_callback(None)


def test_start_studio_tunnel_registers_before_wait(monkeypatch):
    # Tunnel must be visible to stop_studio_tunnel() during the wait, else cloudflared is orphaned.
    seen = {}

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None

        def start(self):
            pass

        def wait_for_ready(self, timeout):
            seen["active_during_wait"] = ct._active_tunnel is self
            self.url = "https://x.trycloudflare.com"
            return self.url

        def stop(self):
            seen["stopped"] = True

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    try:
        assert ct.start_studio_tunnel(8080) == "https://x.trycloudflare.com"
        assert seen["active_during_wait"] is True
    finally:
        ct.stop_studio_tunnel()


def test_start_studio_tunnel_clears_and_stops_on_no_url(monkeypatch):
    seen = {}

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None

        def start(self):
            pass

        def wait_for_ready(self, timeout):
            return None

        def stop(self):
            seen["stopped"] = True

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    assert ct.start_studio_tunnel(8080) is None
    assert seen.get("stopped") is True
    assert ct._active_tunnel is None


def test_start_studio_tunnel_returns_url(monkeypatch):
    class _StubTunnel:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None

        def start(self):
            self.url = "https://stub-xyz.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return self.url

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _StubTunnel)
    try:
        assert ct.start_studio_tunnel(8080) == "https://stub-xyz.trycloudflare.com"
    finally:
        ct.stop_studio_tunnel()


def test_start_studio_tunnel_falls_back_to_http2(monkeypatch):
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.protocol = protocol
            self.url = None
            attempts.append(protocol)

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return self.url if self.protocol == "http2" else None

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    try:
        assert ct.start_studio_tunnel(8080) == "https://words.trycloudflare.com"
        assert attempts == [None, "http2"]
    finally:
        ct.stop_studio_tunnel()


def test_start_studio_tunnel_no_retry_when_shutdown_between_attempts(monkeypatch):
    # A stop() between the failed first attempt and the http2 retry must abort the retry.
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return None

        def stop(self):
            ct.stop_studio_tunnel()

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    assert ct.start_studio_tunnel(8080) is None
    assert attempts == [None]
    assert ct._active_tunnel is None


def test_start_studio_tunnel_no_http2_retry_when_no_url(monkeypatch):
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            pass

        def wait_for_ready(self, timeout):
            return None

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    assert ct.start_studio_tunnel(8080) is None
    assert attempts == [None] * (1 + len(ct._NO_URL_RETRY_DELAYS))


def _no_url_stub(
    attempts,
    succeed_on = None,
    fail_after = 0.0,
    clock = None,
):
    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            if succeed_on is not None and len(attempts) >= succeed_on:
                self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            if clock is not None:
                clock[0] += min(fail_after, timeout)
            return self.url

        def stop(self):
            pass

    return _Stub


def test_start_studio_tunnel_retries_when_no_url(monkeypatch):
    attempts, slept = [], []
    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _no_url_stub(attempts, succeed_on = 2))
    monkeypatch.setattr(ct, "_wait_before_retry", lambda d: slept.append(d) or False)
    try:
        assert ct.start_studio_tunnel(8080) == "https://words.trycloudflare.com"
        assert attempts == [None, None]
        assert slept == [2.0]
        assert ct.get_studio_tunnel_status()["state"] == "online"
    finally:
        ct.stop_studio_tunnel()


@pytest.mark.parametrize(
    # A retry is attempted only while its delay plus timeout still fits the budget.
    "fail_after, timeout, expected_attempts, expected_clock",
    [
        (0.0, 15.0, 3, 7.0),
        (5.0, 15.0, 2, 12.0),
        (15.0, 15.0, 1, 15.0),
        (0.0, 60.0, 1, 0.0),
    ],
)
def test_start_studio_tunnel_no_url_retries_stay_inside_the_budget(
    monkeypatch, fail_after, timeout, expected_attempts, expected_clock
):
    attempts, clock = [], [0.0]
    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(
        ct, "CloudflareTunnel", _no_url_stub(attempts, fail_after = fail_after, clock = clock)
    )
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(
        ct, "_wait_before_retry", lambda d: clock.__setitem__(0, clock[0] + d) or False
    )
    assert ct.start_studio_tunnel(8080, timeout) is None
    assert attempts == [None] * expected_attempts
    assert clock[0] == expected_clock
    status = ct.get_studio_tunnel_status()
    assert status["state"] == "error"
    assert status["error"] == "cloudflared did not produce a URL"


def test_start_studio_tunnel_no_url_retry_delay_ends_on_stop(monkeypatch):
    attempts = []

    def wait(delay):
        _real_threading.Timer(0.05, ct.stop_studio_tunnel).start()
        return _real_wait_before_retry(delay)

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _no_url_stub(attempts))
    monkeypatch.setattr(ct, "_NO_URL_RETRY_DELAYS", (5.0, 5.0))
    monkeypatch.setattr(ct, "_wait_before_retry", wait)
    t0 = _real_time.monotonic()
    assert ct.start_studio_tunnel(8080) is None
    assert _real_time.monotonic() - t0 < 2.0
    assert attempts == [None]
    assert ct.get_studio_tunnel_status()["state"] == "off"


def test_start_studio_tunnel_both_protocols_fail_registration(monkeypatch):
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            return None

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    assert ct.start_studio_tunnel(8080) is None
    assert attempts == [None, "http2"]
    assert ct._active_tunnel is None


def test_start_studio_tunnel_aborts_retry_on_concurrent_shutdown(monkeypatch):
    attempts = []

    class _Stub:
        def __init__(
            self,
            port,
            binary,
            protocol = None,
            origin_host = "localhost",
        ):
            self.url = None
            attempts.append(protocol)

        def start(self):
            self.url = "https://words.trycloudflare.com"

        def wait_for_ready(self, timeout):
            with ct._active_lock:
                ct._active_tunnel = None
            return None

        def stop(self):
            pass

    monkeypatch.setattr(ct, "ensure_cloudflared", lambda: "/bin/cloudflared")
    monkeypatch.setattr(ct, "CloudflareTunnel", _Stub)
    assert ct.start_studio_tunnel(8080) is None
    assert attempts == [None]
    assert ct._active_tunnel is None


def _func_param_defaults(source, func_name):
    tree = ast.parse(source)
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == func_name:
            args = node.args.args
            defaults = node.args.defaults
            offset = len(args) - len(defaults)
            out = {}
            for i, d in enumerate(defaults):
                if isinstance(d, ast.Constant):
                    out[args[offset + i].arg] = d.value
            return out
    return {}


def _argparse_default(source, option):
    tree = ast.parse(source)
    for node in ast.walk(tree):
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            if node.func.attr == "add_argument" and node.args:
                a0 = node.args[0]
                if isinstance(a0, ast.Constant) and a0.value == option:
                    for kw in node.keywords:
                        if kw.arg == "default" and isinstance(kw.value, ast.Constant):
                            return kw.value.value
    return None


def test_run_server_cloudflare_default_off():
    defaults = _func_param_defaults(_RUN_PY.read_text(encoding = "utf-8"), "run_server")
    assert "cloudflare" in defaults
    assert defaults["cloudflare"] is None


def test_argparse_cloudflare_default_off():
    assert _argparse_default(_RUN_PY.read_text(encoding = "utf-8"), "--cloudflare") is None


def test_verify_global_reachability_marks_private_address_unreachable():
    src = _RUN_PY.read_text(encoding = "utf-8")
    tree = ast.parse(src)
    func_src = next(
        ast.get_source_segment(src, n)
        for n in ast.walk(tree)
        if isinstance(n, ast.FunctionDef) and n.name == "_verify_global_reachability"
    )
    captured = []
    ns = {
        "_public_reachable": None,
        "_url_scheme": "http",
        "_stdout_color_ok": lambda: False,
        "is_wildcard_host": lambda _host: False,
        "_url_host": lambda host: host,
        "print": lambda *a, **k: captured.append(" ".join(str(x) for x in a)),
    }
    exec(compile(func_src, "<verify_global_reachability>", "exec"), ns)
    ns["_verify_global_reachability"]("192.168.1.10", 8888)

    assert ns["_public_reachable"] is False
    assert "private/LAN address" in "\n".join(captured)


def test_run_server_registers_tunnel_atexit_backstop():
    src = _RUN_PY.read_text(encoding = "utf-8")
    assert "atexit.register(close_studio_tunnel_lifecycle)" in src


def _run_print_cloudflare_line(
    monkeypatch,
    *,
    cloudflare_url,
    public_reachable,
    cloudflare_requested = False,
    cloudflare_flag = True,
    secure = False,
    loopback_host = "127.0.0.1",
    color = False,
):
    """Exec _print_cloudflare_line without importing run.py's heavy deps."""
    src = _RUN_PY.read_text(encoding = "utf-8")
    tree = ast.parse(src)
    func_src = next(
        ast.get_source_segment(src, n)
        for n in ast.walk(tree)
        if isinstance(n, ast.FunctionDef) and n.name == "_print_cloudflare_line"
    )
    stub = types.ModuleType("startup_banner")
    stub.stdout_supports_color = lambda: color
    monkeypatch.setitem(sys.modules, "startup_banner", stub)
    captured: list[str] = []
    ns = {
        "_cloudflare_url": cloudflare_url,
        "_public_reachable": public_reachable,
        "_cloudflare_requested": cloudflare_requested,
        "_cloudflare_flag": cloudflare_flag,
        "print": lambda *a, **k: captured.append(" ".join(str(x) for x in a)),
    }
    exec(compile(func_src, "<print_cloudflare_line>", "exec"), ns)
    ns["_print_cloudflare_line"](secure = secure, loopback_host = loopback_host)
    return "\n".join(captured)


def test_cloudflare_line_reworded_when_public_unreachable(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch, cloudflare_url = "https://x.trycloudflare.com", public_reachable = False
    )
    assert "Use the secure link access via Cloudflare instead: https://x.trycloudflare.com" in out


def test_cloudflare_line_default_wording_when_reachable(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch, cloudflare_url = "https://x.trycloudflare.com", public_reachable = True
    )
    assert "Secure link access via Cloudflare: https://x.trycloudflare.com" in out
    assert "Use the secure link" not in out


def test_cloudflare_line_default_wording_when_unknown(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch, cloudflare_url = "https://x.trycloudflare.com", public_reachable = None
    )
    assert "Secure link access via Cloudflare: https://x.trycloudflare.com" in out
    assert "Use the secure link" not in out


def test_cloudflare_line_states_inactive_when_enabled_but_not_requested(monkeypatch):
    out = _run_print_cloudflare_line(monkeypatch, cloudflare_url = None, public_reachable = False)
    assert "Cloudflare tunnel: OFF for this mode" in out
    assert "local network only" in out


def test_cloudflare_line_warns_when_public_url_up(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = "https://x.trycloudflare.com",
        public_reachable = True,
        cloudflare_requested = True,
    )
    assert "Secure link access via Cloudflare: https://x.trycloudflare.com" in out
    assert "Cloudflare tunnel: ON" in out
    assert "PUBLIC" in out
    assert "--no-cloudflare" in out
    assert "raw port is also publicly reachable" in out
    assert "local network only" not in out


def test_cloudflare_line_secure_mode_suppresses_public_warning(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = "https://x.trycloudflare.com",
        public_reachable = True,
        cloudflare_requested = True,
        secure = True,
    )
    assert "Secure link access via Cloudflare: https://x.trycloudflare.com" in out
    assert "Cloudflare tunnel: ON" not in out


def test_cloudflare_line_states_disabled_when_off(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = False,
        cloudflare_requested = False,
        cloudflare_flag = False,
    )
    assert "Cloudflare tunnel: OFF" in out
    assert "local network only" in out


def test_cloudflare_line_labels_unset_as_default(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = False,
        cloudflare_requested = False,
        cloudflare_flag = None,
    )
    assert "Cloudflare tunnel: OFF (default)" in out
    assert "--no-cloudflare" not in out


def test_cloudflare_line_labels_explicit_no_cloudflare(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = False,
        cloudflare_requested = False,
        cloudflare_flag = False,
    )
    assert "Cloudflare tunnel: OFF (--no-cloudflare)" in out


def test_cloudflare_line_states_failed_when_requested_but_no_url(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = False,
        cloudflare_requested = True,
        cloudflare_flag = True,
    )
    assert "requested but failed to start" in out
    assert "local network only" in out


def test_cloudflare_line_off_does_not_claim_local_only_when_unknown(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = None,
        cloudflare_requested = False,
        cloudflare_flag = False,
    )
    assert "Cloudflare tunnel: OFF" in out
    assert "Raw port reachability was not verified" in out
    assert "local network only" not in out


def test_cloudflare_line_failed_does_not_claim_local_only_when_unknown(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = None,
        cloudflare_requested = True,
        cloudflare_flag = True,
    )
    assert "requested but failed to start" in out
    assert "Raw port reachability was not verified" in out
    assert "local network only" not in out


@pytest.mark.parametrize(
    "cloudflare_requested,cloudflare_flag,expected",
    [
        (True, True, "requested but failed to start"),
        (False, True, "Cloudflare tunnel: OFF for this mode"),
        (False, False, "Cloudflare tunnel: OFF"),
    ],
)
def test_cloudflare_line_unknown_warns_with_loopback_host(
    monkeypatch, cloudflare_requested, cloudflare_flag, expected
):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = None,
        cloudflare_requested = cloudflare_requested,
        cloudflare_flag = cloudflare_flag,
        loopback_host = "::1",
        color = True,
    )
    assert expected in out
    assert "bind ::1" in out
    assert "bind 127.0.0.1" not in out
    assert "\033[38;5;215;1m" in out


def test_cloudflare_line_off_does_not_claim_local_only_when_publicly_reachable(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = True,
        cloudflare_requested = False,
        cloudflare_flag = False,
    )
    assert "Cloudflare tunnel: OFF" in out
    assert "reachable from the public internet" in out
    assert "local network only" not in out


def test_cloudflare_line_failed_does_not_claim_local_only_when_publicly_reachable(monkeypatch):
    out = _run_print_cloudflare_line(
        monkeypatch,
        cloudflare_url = None,
        public_reachable = True,
        cloudflare_requested = True,
        cloudflare_flag = True,
    )
    assert "requested but failed to start" in out
    assert "reachable from the public internet" in out
    assert "local network only" not in out


def test_download_retries_a_transient_failure(monkeypatch, tmp_path):
    import urllib.error
    import urllib.request

    attempts = []

    class _Resp:
        _sent = False

        def __enter__(self):
            return self

        def __exit__(self, *a):
            return False

        def read(self, n = -1):
            if self._sent:
                return b""
            self._sent = True
            return b"payload"

    def flaky_urlopen(req, timeout = None):
        attempts.append(req.full_url)
        if len(attempts) < 3:
            raise urllib.error.URLError("connection reset")
        return _Resp()

    monkeypatch.setattr(urllib.request, "urlopen", flaky_urlopen)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cloudflared"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert len(attempts) == 3
    assert dest.read_bytes() == b"payload"


def test_download_gives_up_and_says_so(monkeypatch, tmp_path, caplog):
    import logging
    import urllib.error
    import urllib.request

    def refused(req, timeout = None):
        raise urllib.error.HTTPError(req.full_url, 503, "unavailable", None, None)

    monkeypatch.setattr(urllib.request, "urlopen", refused)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cloudflared"
    with caplog.at_level(logging.WARNING, logger = ct.__name__):
        assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is False
    assert not dest.exists() and not list(tmp_path.glob("cloudflared.tmp-*"))
    assert "could not download cloudflared" in caplog.text


def test_download_does_not_retry_a_timeout(monkeypatch, tmp_path):
    import urllib.error
    import urllib.request

    attempts = []

    def slow(req, timeout = None):
        attempts.append(req.full_url)
        raise urllib.error.URLError(TimeoutError("timed out"))

    monkeypatch.setattr(urllib.request, "urlopen", slow)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(attempts) == 1


def test_download_does_not_retry_a_permanent_4xx(monkeypatch, tmp_path):
    """A 404 for this platform's asset repeats on retry, so retrying only delays the launch."""
    import urllib.error
    import urllib.request

    calls = []
    slept = []

    def missing(req, timeout = None):
        calls.append(timeout)
        raise urllib.error.HTTPError(req.full_url, 404, "Not Found", None, None)

    monkeypatch.setattr(urllib.request, "urlopen", missing)
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(calls) == 1
    assert slept == []


def test_download_still_retries_a_5xx(monkeypatch, tmp_path):
    import io
    import urllib.error
    import urllib.request

    calls = []

    def flaky(req, timeout = None):
        calls.append(timeout)
        if len(calls) == 1:
            raise urllib.error.HTTPError(req.full_url, 503, "Unavailable", None, None)
        return io.BytesIO(b"cloudflared-bytes")

    monkeypatch.setattr(urllib.request, "urlopen", flaky)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cf"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert dest.read_bytes() == b"cloudflared-bytes"
    assert len(calls) == 2


def test_download_retries_a_429(monkeypatch, tmp_path):
    import io
    import urllib.error
    import urllib.request

    calls = []

    def throttled(req, timeout = None):
        calls.append(timeout)
        if len(calls) == 1:
            raise urllib.error.HTTPError(req.full_url, 429, "Too Many Requests", None, None)
        return io.BytesIO(b"cloudflared-bytes")

    monkeypatch.setattr(urllib.request, "urlopen", throttled)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cf"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert dest.read_bytes() == b"cloudflared-bytes"
    assert len(calls) == 2


def test_download_does_not_retry_an_unresolvable_host(monkeypatch, tmp_path):
    import socket
    import urllib.error
    import urllib.request

    calls = []
    slept = []

    def offline(req, timeout = None):
        calls.append(timeout)
        raise urllib.error.URLError(
            socket.gaierror(socket.EAI_NONAME, "nodename nor servname provided")
        )

    monkeypatch.setattr(urllib.request, "urlopen", offline)
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(calls) == 1
    assert slept == []


def test_download_retries_a_temporary_resolver_failure(monkeypatch, tmp_path):
    import io
    import socket
    import urllib.error
    import urllib.request

    calls = []

    def resolving(req, timeout = None):
        calls.append(timeout)
        if len(calls) == 1:
            raise urllib.error.URLError(
                socket.gaierror(socket.EAI_AGAIN, "Temporary failure in name resolution")
            )
        return io.BytesIO(b"cloudflared-bytes")

    monkeypatch.setattr(urllib.request, "urlopen", resolving)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cf"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert dest.read_bytes() == b"cloudflared-bytes"
    assert len(calls) == 2


def test_download_does_not_retry_a_chain_it_cannot_verify(monkeypatch, tmp_path):
    """A stale CA bundle, wrong clock or TLS proxy is configuration that retrying cannot fix."""
    import urllib.error
    import urllib.request

    calls = []
    slept = []

    def unverifiable(req, timeout = None):
        calls.append(timeout)
        raise urllib.error.URLError(
            ssl.SSLCertVerificationError(
                1, "[SSL: CERTIFICATE_VERIFY_FAILED] self-signed certificate"
            )
        )

    monkeypatch.setattr(urllib.request, "urlopen", unverifiable)
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(calls) == 1
    assert slept == []


def test_download_retries_a_tls_stream_that_broke_mid_body(monkeypatch, tmp_path):
    """A TLS stream broken mid-body is a dead transfer; its partial file must not be published."""
    import io
    import urllib.request

    calls = []

    class Truncated(io.BytesIO):
        def read(self, size = -1):
            if not self.tell():
                return super().read(size)
            raise ssl.SSLError(1, "[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] bad record mac")

    def flaky(req, timeout = None):
        calls.append(timeout)
        return Truncated(b"half-a-") if len(calls) == 1 else io.BytesIO(b"cloudflared-bytes")

    monkeypatch.setattr(urllib.request, "urlopen", flaky)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cf"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert dest.read_bytes() == b"cloudflared-bytes"
    assert len(calls) == 2
    assert list(tmp_path.glob("cf.tmp-*")) == []


def test_download_does_not_retry_an_unwritable_cache(monkeypatch, tmp_path):
    import tempfile
    import urllib.request

    slept = []
    opened = []
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    monkeypatch.setattr(urllib.request, "urlopen", lambda *a, **k: opened.append(1))

    # Raised rather than chmod'd: a read-only directory still accepts writes on Windows.
    def denied(*a, **k):
        raise PermissionError(13, "Permission denied")

    monkeypatch.setattr(tempfile, "NamedTemporaryFile", denied)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert slept == []
    assert opened == []


@pytest.mark.parametrize("fails_on", ["write", "close", "publish"])
def test_download_does_not_retry_a_disk_that_fills_mid_transfer(monkeypatch, tmp_path, fails_on):
    """The temporary file is buffered, so a full volume surfaces at the write, at the close
    that flushes it, or at the rename. None of the three gains space on a retry."""
    import io
    import pathlib
    import urllib.request

    calls = []
    slept = []

    def responding(req, timeout = None):
        calls.append(timeout)
        return io.BytesIO(b"cloudflared-bytes")

    class _FullDisk:
        name = str(tmp_path / "cf.tmp-full")

        def __enter__(self):
            pathlib.Path(self.name).write_bytes(b"cloudflared-bytes")
            return self

        def __exit__(self, *a):
            self.close()
            return False

        def _no_space(self):
            raise OSError(errno.ENOSPC, "No space left on device")

        def write(self, data):
            if fails_on == "write":
                self._no_space()

        def close(self):
            if fails_on == "close":
                self._no_space()

    monkeypatch.setattr(urllib.request, "urlopen", responding)
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    monkeypatch.setattr(tempfile, "NamedTemporaryFile", lambda **k: _FullDisk())
    if fails_on == "publish":

        def full_rename(src, dst):
            raise OSError(errno.ENOSPC, "No space left on device")

        monkeypatch.setattr(ct.os, "replace", full_rename)
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(calls) == 1
    assert slept == []


def test_download_retries_an_empty_body(monkeypatch, tmp_path):
    import io
    import urllib.request

    calls = []

    def truncated(req, timeout = None):
        calls.append(timeout)
        return io.BytesIO(b"" if len(calls) == 1 else b"cloudflared-bytes")

    monkeypatch.setattr(urllib.request, "urlopen", truncated)
    monkeypatch.setattr(ct.time, "sleep", lambda s: None)
    dest = tmp_path / "cf"
    assert ct._download("https://github.com/cloudflare/cloudflared/x", dest) is True
    assert dest.read_bytes() == b"cloudflared-bytes"
    assert len(calls) == 2


def test_download_does_not_retry_when_a_reset_unwinds_into_a_full_disk(monkeypatch, tmp_path):
    """The disk is still full on retry, so the full-disk error must not be retried."""
    import pathlib
    import tempfile
    import urllib.request

    calls = []
    slept = []

    class _Reset:
        def __enter__(self):
            return self

        def __exit__(self, *a):
            return False

        def read(self, n = -1):
            raise ConnectionResetError(errno.ECONNRESET, "Connection reset by peer")

    class _FullOnClose:
        name = str(tmp_path / "cf.tmp-reset")

        def __enter__(self):
            pathlib.Path(self.name).write_bytes(b"partial")
            return self

        def __exit__(self, *a):
            raise OSError(errno.ENOSPC, "No space left on device")

        def write(self, data):
            pass

    def responding(req, timeout = None):
        calls.append(timeout)
        return _Reset()

    monkeypatch.setattr(urllib.request, "urlopen", responding)
    monkeypatch.setattr(ct.time, "sleep", slept.append)
    monkeypatch.setattr(tempfile, "NamedTemporaryFile", lambda **k: _FullOnClose())
    assert ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf") is False
    assert len(calls) == 1
    assert slept == []


def test_download_retries_share_one_deadline(monkeypatch, tmp_path):
    """A transfer that stalls for most of the budget and then resets gets what is left,
    not a fresh budget. Three fresh budgets would hold the launch banner for minutes."""
    import urllib.request

    clock = [1000.0]
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(ct.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    timeouts = []

    def stalls_then_resets(req, timeout = None):
        timeouts.append(timeout)
        clock[0] += 59.0
        raise ConnectionResetError("peer reset")

    monkeypatch.setattr(urllib.request, "urlopen", stalls_then_resets)
    assert (
        ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf", timeout = 60)
        is False
    )
    assert timeouts == [60.0]


def test_download_later_attempts_get_only_the_remaining_time(monkeypatch, tmp_path):
    import urllib.request

    clock = [1000.0]
    monkeypatch.setattr(ct.time, "monotonic", lambda: clock[0])
    monkeypatch.setattr(ct.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    timeouts = []

    def quick_resets(req, timeout = None):
        timeouts.append(round(timeout, 1))
        clock[0] += 10.0
        raise ConnectionResetError("peer reset")

    monkeypatch.setattr(urllib.request, "urlopen", quick_resets)
    assert (
        ct._download("https://github.com/cloudflare/cloudflared/x", tmp_path / "cf", timeout = 60)
        is False
    )
    assert timeouts == [60.0, 48.5, 35.5]