pryscraper/tests/test_referrals.py
cryptorugmunch a7c30b12cd
Some checks failed
CI / lint (push) Failing after 2s
CI / typecheck (push) Failing after 2s
CI / test (push) Failing after 2s
CI / Secret scan (gitleaks) (push) Failing after 1s
CI / Security audit (bandit) (push) Failing after 2s
chore(lint): auto-fix 253 of 283 ruff issues (F401, I001, E402, RUF100, UP037, SIM105)
Mass ruff auto-fix:
  - ruff check --fix: 109 issues fixed (F401 unused imports,
    I001 unsorted imports, UP037 quoted annotations, SIM105
    suppressible exception, RUF100 unused-noqa)
  - ruff check --fix --unsafe-fixes: 22 additional issues
  - ruff format: 70 files reformatted
  - Manual pass: fix 16 misplaced import httpx lines
  - Manual pass: fix remaining E402 (import-after-docstring)

Result: 283 errors -> 30 errors.

The remaining 30 are real issues that need manual review:
  5 F401 unused-import (likely auto-generated stubs)
  5 F821 undefined-name (real bugs in code that references
    redis/pydantic/LLMRegistry without imports)
  3 BLE001 (the compliance LLM fallback is intentional; the
    other two are real)
  3 RUF012 mutable-class-default
  3 SIM105, 3 SIM117, 2 E722, 2 E741
  1 B007, 1 B025, 1 E402, 1 RUF200 (pyproject.toml issue)

Tests: 436/437 pass (1 pre-existing SSE sandbox failure).
format check + import sort: now clean.
make ci: still gated on the 30 remaining real issues.
Follow-up: triage the 30 issues file-by-file.
2026-07-02 21:51:25 +02:00

101 lines
3.4 KiB
Python

# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Rug Munch Media LLC
#
# Part of Pry — https://git.rugmunch.io/RugMunchMedia/pryscraper
# Licensed under MIT. See LICENSE.
"""Tests for referral/affiliate system."""
from referrals import PROVIDER_CATALOG, ReferralTracker
from x402 import X402_PRICING, X402Handler
def test_catalog_completeness() -> None:
"""We have a good catalog of providers."""
total = sum(len(v) for v in PROVIDER_CATALOG.values())
assert total >= 50, f"Expected 50+ providers, got {total}"
def test_catalog_categories() -> None:
assert "llm" in PROVIDER_CATALOG
assert "hosting" in PROVIDER_CATALOG
assert "email" in PROVIDER_CATALOG
assert "monitoring" in PROVIDER_CATALOG
def test_provider_format() -> None:
for cat, providers in PROVIDER_CATALOG.items():
for p in providers:
assert "name" in p, f"Provider in {cat} missing name"
assert "url" in p, f"Provider in {cat} missing url"
is_open_source = (
"open source" in str(p.get("commission", "")).lower()
or "no affiliate" in str(p.get("note", "")).lower()
)
if is_open_source:
continue
assert (
"pry" in p["url"].lower()
or "ref" in p["url"].lower()
or "affiliate" in p["url"].lower()
), f"Provider {p['name']} URL missing referral: {p['url']}"
def test_referral_click() -> None:
rt = ReferralTracker()
click_id = rt.record_click(
"openai", "https://platform.openai.com/signup?via=pry", source="test"
)
assert click_id is not None
assert any(c["id"] == click_id for c in rt.clicks)
def test_referral_conversion() -> None:
rt = ReferralTracker()
click_id = rt.record_click("anthropic", "https://console.anthropic.com/?ref=pry")
success = rt.record_conversion(click_id, revenue_usd=50.0, notes="Annual plan")
assert success is True
def test_referral_stats() -> None:
rt = ReferralTracker()
click_id = rt.record_click("openai", "https://platform.openai.com/signup?via=pry")
rt.record_conversion(click_id, revenue_usd=100.0)
stats = rt.get_stats(days_back=1)
assert stats["total_clicks"] >= 1
assert stats["total_revenue_usd"] >= 100.0
def test_x402_payment_request() -> None:
h = X402Handler()
body, headers = h.create_payment_required("scrape", "https://example.com/scrape")
assert body["x402Version"] == 1
assert "accepts" in body
base_accept = next(a for a in body["accepts"] if a["network"] == "base")
assert base_accept["maxAmountRequired"] == "1000" # $0.001 = 1000 USDC units
assert base_accept["asset"] == "USDC"
assert "PAYMENT-REQUIRED" in headers
def test_x402_require_payment() -> None:
h = X402Handler()
resp = h.require_payment("crawl")
assert resp["status_code"] == 402
assert "PAYMENT-REQUIRED" in resp["headers"]
assert "accepts" in resp["body"]
def test_x402_pricing() -> None:
assert "scrape" in X402_PRICING
assert "crawl" in X402_PRICING
assert "llm_call" in X402_PRICING
for _op, info in X402_PRICING.items():
assert info["price_usd"] > 0
assert "description" in info
def test_x402_stats() -> None:
h = X402Handler()
stats = h.get_stats()
assert "total_payments" in stats
assert "wallet" in stats
assert "pricing" in stats