"""Unit tests for ngsite.botcheck.is_bot — the declared-bot filter that
decides whether /jump forwards to the raw URL (no affiliate wrap, no
click tally) vs. wraps + logs a human click. Getting this wrong either
pollutes click stats with bots or fails to credit real affiliate clicks.
See memory: jump-bot-filter.
"""
import pytest

from ngsite import botcheck


REAL_BROWSERS = [
    # Chrome on Windows
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/124.0 Safari/537.36",
    # Safari on iPhone
    "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) "
    "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E Safari/604.1",
    # Firefox on Linux
    "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0",
]

BOTS = [
    "Googlebot/2.1 (+http://www.google.com/bot.html)",
    "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
    "meta-externalagent/1.1",
    "facebookexternalhit/1.1",
    "GPTBot/1.0",
    "ClaudeBot/1.0",
    "Bytespider",
    "python-requests/2.31.0",
    "curl/8.4.0",
    "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)",
]


@pytest.mark.parametrize("ua", REAL_BROWSERS)
def test_real_browsers_are_not_bots(ua):
    assert botcheck.is_bot(ua) is False


@pytest.mark.parametrize("ua", BOTS)
def test_known_bots_are_detected(ua):
    assert botcheck.is_bot(ua) is True


def test_empty_or_missing_ua_treated_as_bot():
    # No real browser omits the UA; cost of a false positive is only an
    # unwrapped redirect, never a broken one.
    assert botcheck.is_bot("") is True
    assert botcheck.is_bot(None) is True
    assert botcheck.is_bot("   ") is True


def test_case_insensitive():
    assert botcheck.is_bot("GOOGLEBOT") is True
    assert botcheck.is_bot("PyThOn-UrlLib") is True
