Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

18.2 KB
Raw Blame
"""Unit tests for the cloakbrowser CLI diagnostics (`info` / `doctor`)."""

import json
import os
import sys
from argparse import Namespace
from unittest.mock import patch

import pytest

import cloakbrowser.__main__
from cloakbrowser.__main__ import _binary_version, cmd_info
from cloakbrowser.license import LicenseInfo, ProReleaseInfo, SessionSeats


def _run(args, *, key=None, license_info=None, sessions=None):
    """Run cmd_info with license resolution mocked and the real downloaders patched.

    key=None  -> no license key.
    key set   -> validate_license returns license_info (entitled if valid).
    sessions  -> the SessionSeats the seat lookup reports (it is mocked out, so a
                 non-quick Pro run never reaches the network). None defaults to a
                 server-cannot-count result, which keeps unrelated tests offline.

    Returns (claim_mock, download_mock, session_count_mock) so callers can assert
    the command never starts the free-key claim, a binary download or an
    unwanted lookup.
    """
    with (
        patch("cloakbrowser.license.resolve_license_key", return_value=key),
        patch("cloakbrowser.license.validate_license", return_value=license_info),
        patch(
            "cloakbrowser.license.get_session_seats",
            return_value=sessions if sessions is not None else SessionSeats(state="unknown"),
        ) as mock_sessions,
        patch("cloakbrowser.download._claim_free_key") as mock_dl_free,
        patch("cloakbrowser.download._download_pro_binary") as mock_dl_pro,
    ):
        cmd_info(args)
    return mock_dl_free, mock_dl_pro, mock_sessions


def test_info_text_never_downloads(capsys):
    free_dl, pro_dl, _ = _run(Namespace(quick=True, json=False))
    free_dl.assert_not_called()
    pro_dl.assert_not_called()
    out = capsys.readouterr().out
    assert "CloakBrowser diagnostics" in out
    assert "Python:" in out
    assert "Platform:" in out
    assert "License:   none" in out
    assert "Modules:" in out


def test_info_quick_skips_launch(capsys):
    _run(Namespace(quick=True, json=False))
    out = capsys.readouterr().out
    assert "skipped (--quick)" in out


def test_info_without_proxy_shows_hint_and_never_resolves(capsys):
    with patch("cloakbrowser.geoip.resolve_proxy_geo_with_ip") as resolver:
        _run(Namespace(quick=True, json=False))
    resolver.assert_not_called()
    out = capsys.readouterr().out
    assert "pass --proxy" in out
    assert "Exit IP:" not in out


def test_info_proxy_resolves_and_prints_exit_ip(capsys):
    with patch(
        "cloakbrowser.geoip.resolve_proxy_geo_with_ip",
        return_value=("Europe/Berlin", "de-DE", "203.0.113.9"),
    ) as resolver:
        _run(Namespace(quick=True, json=False, proxy="http://p:8080"))
    resolver.assert_called_once_with("http://p:8080")
    out = capsys.readouterr().out
    assert "Exit IP:   203.0.113.9" in out
    assert "Timezone:  Europe/Berlin" in out
    assert "Locale:    de-DE" in out
    assert "pass --proxy" not in out  # hint suppressed once resolved


def test_info_proxy_json_includes_resolved(capsys):
    with patch(
        "cloakbrowser.geoip.resolve_proxy_geo_with_ip",
        return_value=("Europe/Berlin", "de-DE", "203.0.113.9"),
    ):
        _run(Namespace(quick=True, json=True, proxy="http://p:8080"))
    data = json.loads(capsys.readouterr().out)
    assert data["geoip"]["resolved"] == {
        "exit_ip": "203.0.113.9",
        "timezone": "Europe/Berlin",
        "locale": "de-DE",
    }


def test_info_proxy_resolution_failure_is_reported_not_fatal(capsys):
    with patch(
        "cloakbrowser.geoip.resolve_proxy_geo_with_ip",
        side_effect=RuntimeError("proxy refused"),
    ):
        _run(Namespace(quick=True, json=False, proxy="http://p:8080"))
    out = capsys.readouterr().out
    assert "could not resolve" in out
    assert "proxy refused" in out


def test_no_key_reports_none(capsys):
    """No license key -> nothing launches without one; info says so."""
    claim, _, _ = _run(Namespace(quick=True, json=True))
    claim.assert_not_called()  # info never starts the claim flow
    data = json.loads(capsys.readouterr().out)
    assert data["binary"]["tier"] == "none"
    assert data["license"]["tier"] == "none"


def test_valid_key_reports_pro_binary(capsys):
    """A server-validated key -> the binary section reflects the PRO binary.

    ``latest_version`` reports the server's latest (mocked); ``version`` is the
    build that will actually launch (a cached Pro build if present, otherwise the
    latest it will fetch). The two are surfaced separately so they can't diverge.
    """
    valid = LicenseInfo(valid=True, plan="business", expires=None)
    # quick=False: the server latest lookup is skipped under --quick (network-free),
    # so exercise the full path to see latest_version populated.
    release = ProReleaseInfo("148.0.0.0", "stable", "stable", False)
    with patch("cloakbrowser.license.get_pro_latest_release", return_value=release):
        _run(Namespace(quick=False, json=True), key="cb_test", license_info=valid)
    data = json.loads(capsys.readouterr().out)
    assert data["binary"]["tier"] == "pro"
    assert data["binary"]["latest_version"] == "148.0.0.0"
    # A Pro user always resolves to a Pro version (cached-or-latest), never the free base.
    assert data["binary"]["version"]
    assert data["license"]["tier"] == "business"


def test_quick_skips_pro_latest_lookup(capsys):
    """--quick keeps `info` network-free: no server latest-version lookup for Pro."""
    valid = LicenseInfo(valid=True, plan="business", expires=None)
    with patch(
        "cloakbrowser.license.get_pro_latest_release",
        return_value=ProReleaseInfo("148.0.0.0", "stable", "stable", False),
    ) as mock_latest:
        _run(Namespace(quick=True, json=True), key="cb_test", license_info=valid)
    data = json.loads(capsys.readouterr().out)
    mock_latest.assert_not_called()
    assert data["binary"]["latest_version"] is None


def test_info_reports_preview_stable_fallback(capsys):
    valid = LicenseInfo(valid=True, plan="business", expires=None)
    release = ProReleaseInfo("150.0.7871.114.3", "preview", "stable", True)
    with (
        patch.dict(os.environ, {"CLOAKBROWSER_RELEASE_CHANNEL": "preview"}),
        patch("cloakbrowser.license.get_pro_latest_release", return_value=release),
    ):
        _run(Namespace(quick=False, json=False), key="cb_test", license_info=valid)
    assert "Channel:   Preview → Stable fallback" in capsys.readouterr().out


def test_invalid_key_is_not_entitled(capsys):
    """A key the server rejects -> not entitled."""
    invalid = LicenseInfo(valid=False, plan="solo", expires=None)
    _run(Namespace(quick=True, json=True), key="cb_bad", license_info=invalid)
    data = json.loads(capsys.readouterr().out)
    assert data["binary"]["tier"] == "none"
    assert data["license"]["tier"] == "invalid"


# ── Seat count ────────────────────────────────────────

_PRO = LicenseInfo(valid=True, plan="business", expires=None)


def _seats(args_json=False, **seat_kwargs):
    """Render the Sessions line for one SessionSeats result."""
    return _run(
        Namespace(quick=False, json=args_json),
        key="cb_test",
        license_info=_PRO,
        sessions=SessionSeats(**seat_kwargs),
    )


def test_pro_reports_seats_and_limit_in_json(capsys):
    _seats(args_json=True, active=8, limit=2000, state="ok")
    data = json.loads(capsys.readouterr().out)
    assert data["license"]["sessions"] == {
        "active": 8, "limit": 2000, "state": "ok", "reason": None,
    }


def test_seat_line_shows_used_over_limit(capsys):
    """The point of the change: a scale-plan customer can see they are nowhere near
    the ceiling (or right on it) instead of reading a bare number."""
    _seats(active=8, limit=2000, state="ok")
    assert "Sessions:  8/2000 in use" in capsys.readouterr().out


def test_seat_line_falls_back_when_the_server_sends_no_limit(capsys):
    """Older server, unlimited licence, or an unrecognised plan. Print the count we do
    have rather than "8/unknown"."""
    _seats(active=8, limit=None, state="ok")
    out = capsys.readouterr().out
    assert "Sessions:  8 seats in use" in out
    assert "unavailable" not in out


def test_seat_fallback_is_singular_for_one(capsys):
    _seats(active=1, limit=None, state="ok")
    assert "Sessions:  1 seat in use" in capsys.readouterr().out


def test_one_of_one_seat_shows_the_limit(capsys):
    """A free key holds exactly one seat — the cohort most likely to hit its cap."""
    _seats(active=1, limit=1, state="ok")
    assert "Sessions:  1/1 in use" in capsys.readouterr().out


def test_zero_seats_reads_as_a_real_answer_not_unavailable(capsys):
    """0 is a real answer ("nothing running"); only an unknown prints unavailable."""
    _seats(active=0, limit=5, state="ok")
    out = capsys.readouterr().out
    assert "Sessions:  0/5 in use" in out
    assert "unavailable" not in out


def test_unreachable_server_says_so(capsys):
    _seats(state="unreachable")
    assert "Sessions:  unavailable (cannot reach cloakbrowser.dev)" in capsys.readouterr().out


@pytest.mark.parametrize(
    "code,shown",
    [
        ("license_inactive", "license inactive"),
        ("invalid_key", "invalid key"),
        ("rate_limited", "rate limited"),
    ],
)
def test_denial_reasons_are_spelled_out(capsys, code, shown):
    """These four used to be one string. A dead key and a healthy key behind a
    degraded backend must not read identically."""
    _seats(state="denied", reason=code)
    assert f"Sessions:  unavailable ({shown})" in capsys.readouterr().out


def test_unrecognised_denial_reason_is_passed_through(capsys):
    """A server code we have no wording for still says something actionable."""
    _seats(state="denied", reason="some_new_code")
    assert "Sessions:  unavailable (some_new_code)" in capsys.readouterr().out


def test_server_cannot_count_is_not_an_error(capsys):
    """Leaseless mode / seat store down: the customer's key is fine and there is
    nothing for them to do. Must not read like a licence problem."""
    _seats(state="unknown")
    out = capsys.readouterr().out
    assert "Sessions:  unavailable (server cannot report seats right now)" in out
    assert "invalid" not in out


def test_quick_skips_the_seat_lookup(capsys):
    """--quick keeps `info` network-free — same rule as the Pro latest lookup."""
    _, _, mock_sessions = _run(
        Namespace(quick=True, json=True), key="cb_test", license_info=_PRO, sessions=3
    )
    data = json.loads(capsys.readouterr().out)
    mock_sessions.assert_not_called()
    assert "sessions" not in data["license"]


def test_no_key_never_looks_up_seats(capsys):
    """No key holds no seats — don't ask the server about it."""
    _, _, mock_sessions = _run(Namespace(quick=False, json=True))
    data = json.loads(capsys.readouterr().out)
    mock_sessions.assert_not_called()
    assert "sessions" not in data["license"]


def test_invalid_key_never_looks_up_seats(capsys):
    invalid = LicenseInfo(valid=False, plan="solo", expires=None)
    _, _, mock_sessions = _run(
        Namespace(quick=False, json=True), key="cb_bad", license_info=invalid
    )
    mock_sessions.assert_not_called()


def test_info_json_is_valid(capsys):
    _run(Namespace(quick=True, json=True))
    out = capsys.readouterr().out
    data = json.loads(out)
    assert data["environment"]["python"]
    assert "modules" in data
    # Upgrade hint is text-only; JSON exposes tier as a field instead.
    assert "cloakbrowser.dev" not in out


def test_no_key_shows_login_hint(capsys):
    _run(Namespace(quick=True, json=False))
    out = capsys.readouterr().out
    assert "License:   none" in out
    assert "cloakbrowser login" in out


def test_free_key_shows_upgrade_hint(capsys):
    free = LicenseInfo(valid=True, plan="free", expires=None)
    _run(Namespace(quick=True, json=False), key="cb_free", license_info=free)
    out = capsys.readouterr().out
    assert "License:   Free (latest binary, 1 concurrent session)" in out
    assert "For more than one concurrent session" in out


# ---------------------------------------------------------------------------
# Font probe — the Linux-only Windows/macOS-persona font check. Forces the Linux
# branch and mocks fc-list so the count is deterministic on any host.
# ---------------------------------------------------------------------------


def _fonts_linux(mac_present):
    """Patch context: force the Linux font branch and stub fc-list so the Windows
    and Office sets read fully present while the mac set reports `mac_present`.
    """
    from cloakbrowser.browser import _MAC_FONT_TELLS

    def fake_count(tells):
        return mac_present if tells is _MAC_FONT_TELLS else len(tells)

    return (
        patch("cloakbrowser.__main__.platform.system", return_value="Linux"),
        patch("cloakbrowser.browser._count_fonts_present", side_effect=fake_count),
    )


def test_info_reports_partial_mac_fonts_with_conditional_nudge(capsys):
    # 5/20 = the Windows-box overlap (Arial, Georgia, Times New Roman, ...).
    p_platform, p_count = _fonts_linux(5)
    with p_platform, p_count:
        _run(Namespace(quick=True, json=False))
    out = capsys.readouterr().out
    assert "Mac fonts: partial (5/20)" in out
    assert "if spoofing macOS on this host" in out


def test_info_full_mac_fonts_has_no_nudge(capsys):
    from cloakbrowser.browser import _MAC_FONT_TELLS

    p_platform, p_count = _fonts_linux(len(_MAC_FONT_TELLS))
    with p_platform, p_count:
        _run(Namespace(quick=True, json=False))
    out = capsys.readouterr().out
    assert "Mac fonts: ok (20/20)" in out
    assert "if spoofing macOS on this host" not in out


def test_info_json_includes_mac_font_count_on_linux(capsys):
    p_platform, p_count = _fonts_linux(5)
    with p_platform, p_count:
        _run(Namespace(quick=True, json=True))
    data = json.loads(capsys.readouterr().out)
    assert data["fonts"]["macos"] == [5, 20]


# ---------------------------------------------------------------------------
# Launch test — exercises the real subprocess path (not --quick) against a stub
# executable, so the launch-test code is actually covered by CI.
# ---------------------------------------------------------------------------

pytestmark_posix = pytest.mark.skipif(
    sys.platform == "win32", reason="uses a POSIX shell stub binary"
)


@pytestmark_posix
def test_binary_version_runs_stub(tmp_path):
    stub = tmp_path / "fakechrome"
    stub.write_text("#!/bin/sh\necho 'Chromium 1.2.3.4'\n")
    stub.chmod(0o755)
    ok, version, err = _binary_version(str(stub))
    assert ok
    assert "Chromium 1.2.3.4" in version
    assert err == ""


def test_console_glyph_falls_back_on_legacy_windows_encoding(monkeypatch):
    """cmd.exe defaults to cp850/cp1252, which carry no check mark — printing one
    there aborted the whole report with UnicodeEncodeError."""
    import io

    monkeypatch.setattr(
        cloakbrowser.__main__.sys, "stdout", io.TextIOWrapper(io.BytesIO(), encoding="cp1252")
    )
    assert cloakbrowser.__main__._console_glyph("✓", "OK") == "OK"
    assert cloakbrowser.__main__._console_glyph("→", "->") == "->"
    # cp1252 does carry the em dash, so it is kept — the check is per glyph.
    assert cloakbrowser.__main__._console_glyph("—", "-") == "—"


def test_console_glyph_kept_on_utf8(monkeypatch):
    import io

    monkeypatch.setattr(
        cloakbrowser.__main__.sys, "stdout", io.TextIOWrapper(io.BytesIO(), encoding="utf-8")
    )
    assert cloakbrowser.__main__._console_glyph("✓", "OK") == "✓"
    assert cloakbrowser.__main__._console_glyph("✗", "x") == "✗"


@pytestmark_posix
def test_info_never_launches_binary_on_windows(tmp_path, monkeypatch, capsys):
    """Windows Chrome ignores --version and starts a real browser, which takes a
    license seat it never releases. info must read the version manifest instead."""
    marker = tmp_path / "ran"
    stub = tmp_path / "chrome.exe"
    stub.write_text(f"#!/bin/sh\ntouch '{marker}'\n")
    stub.chmod(0o755)
    (tmp_path / "1.2.3.4.manifest").write_text("")
    fake_binary = {
        "version": "1.2.3.4.1",
        "tier": "pro",
        "bundled_version": "x",
        "path": str(stub),
        "installed": True,
        "cache_dir": str(tmp_path),
        "override": None,
    }
    monkeypatch.setattr(cloakbrowser.__main__.platform, "system", lambda: "Windows")
    with (
        patch("cloakbrowser.license.resolve_license_key", return_value=None),
        patch("cloakbrowser.__main__._effective_binary", return_value=fake_binary),
    ):
        cmd_info(Namespace(quick=False, json=True))
    data = json.loads(capsys.readouterr().out)
    assert data["launch"]["tested"] is False
    assert "1.2.3.4" in data["launch"]["reason"]
    assert not marker.exists(), "info launched the binary on Windows"


@pytestmark_posix
def test_binary_version_reports_failure(tmp_path):
    stub = tmp_path / "failchrome"
    stub.write_text("#!/bin/sh\necho 'libfoo missing' >&2\nexit 1\n")
    stub.chmod(0o755)
    ok, version, err = _binary_version(str(stub))
    assert not ok
    assert "libfoo missing" in err


@pytestmark_posix
def test_launch_section_runs_binary_without_downloading(tmp_path, capsys):
    """Full non-quick run: the launch test executes the resolved binary, and no
    download function is ever invoked."""
    stub = tmp_path / "fakechrome"
    stub.write_text("#!/bin/sh\necho 'Chromium 9.9.9.9'\n")
    stub.chmod(0o755)
    fake_binary = {
        "version": "9.9.9.9",
        "tier": "free",
        "bundled_version": "x",
        "path": str(stub),
        "installed": True,
        "cache_dir": str(tmp_path),
        "override": None,
    }
    with (
        patch("cloakbrowser.license.resolve_license_key", return_value=None),
        patch("cloakbrowser.__main__._effective_binary", return_value=fake_binary),
        patch("cloakbrowser.download._claim_free_key") as mock_dl_free,
        patch("cloakbrowser.download._download_pro_binary") as mock_dl_pro,
    ):
        cmd_info(Namespace(quick=False, json=True))
    data = json.loads(capsys.readouterr().out)
    assert data["launch"]["tested"] is True
    assert data["launch"]["ok"] is True
    assert "Chromium 9.9.9.9" in data["launch"]["version"]
    mock_dl_free.assert_not_called()
    mock_dl_pro.assert_not_called()