captchaforge 0.2.38

[DO NOT USE — UNDER ACTIVE DEVELOPMENT, NOT PRODUCTION-READY] Captcha solver scaffolding for Firefox + BiDi-driven browsers. The architecture is in place (vendor solvers, retry-loop iframe walking, VLM provider abstraction, real-WAF bench harness) but the live-vendor success rate is still 0% — Cloudflare Turnstile / hCaptcha / reCAPTCHA detect us at a TLS / BiDi fingerprint layer that no flag-based stealth has cleared. Watch the repo; do not depend on this for any real workload.
Documentation
#!/usr/bin/env python3
"""Tests for tools/scorecard.py. Verifies badge generation,
marker injection, color thresholds, and idempotent re-runs."""

import json
import sys
import tempfile
from pathlib import Path

ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
import scorecard as sc  # noqa: E402


def _scorecard(rows):
    return {"aggregates": rows}


def test_color_thresholds():
    assert sc.color_for_rate(0.98) == "brightgreen"
    assert sc.color_for_rate(0.85) == "green"
    assert sc.color_for_rate(0.70) == "yellow"
    assert sc.color_for_rate(0.50) == "orange"
    assert sc.color_for_rate(0.10) == "red"
    assert sc.color_for_rate(0.0) == "red"
    print("PASS: color thresholds")


def test_build_badges_filters_availability_rows():
    s = _scorecard([
        {"suite": "_availability", "fixture": "captchaforge", "success_rate": 1.0},
        {"suite": "real_waf", "fixture": "turnstile", "success_rate": 0.97},
        {"suite": "solve", "fixture": "hcaptcha", "success_rate": 0.50},
        {"suite": "detection", "fixture": "x", "success_rate": 1.0},  # not in allowlist
    ])
    badges = sc.build_badges(s)
    assert len(badges) == 2, f"expected 2 badges, got {len(badges)}: {badges}"
    assert any("turnstile" in b for b in badges)
    assert any("hcaptcha" in b for b in badges)
    assert not any("_availability" in b for b in badges)
    assert not any("detection" in b for b in badges)
    print("PASS: build_badges filters")


def test_inject_badges_inserts_between_markers():
    readme = (
        "# captchaforge\n\n"
        f"{sc.START_MARKER}\n"
        "_(no scorecard rows yet)_\n"
        f"{sc.END_MARKER}\n\n"
        "more content\n"
    )
    new = sc.inject_badges(readme, ["![turnstile](https://img.shields.io/badge/turnstile-97.0%25-brightgreen)"])
    assert "_(no scorecard rows yet)_" not in new
    assert "turnstile" in new
    assert "more content" in new, "content after markers preserved"
    assert sc.START_MARKER in new and sc.END_MARKER in new
    print("PASS: inject between markers")


def test_inject_badges_appends_when_no_markers():
    readme = "# captchaforge\n\nNothing else.\n"
    new = sc.inject_badges(readme, ["![cf](https://img.shields.io/badge/cf-1-green)"])
    assert sc.START_MARKER in new and sc.END_MARKER in new
    assert "## Scorecard badges" in new
    print("PASS: append when no markers")


def test_inject_idempotent_on_unchanged_input():
    readme = (
        f"{sc.START_MARKER}\n"
        "![x](https://img.shields.io/badge/x-1-red)\n"
        f"{sc.END_MARKER}\n"
    )
    badges = ["![x](https://img.shields.io/badge/x-1-red)"]
    once = sc.inject_badges(readme, badges)
    twice = sc.inject_badges(once, badges)
    assert once == twice
    print("PASS: idempotent")


def test_end_to_end_writes_readme():
    with tempfile.TemporaryDirectory() as td:
        td = Path(td)
        readme = td / "README.md"
        readme.write_text("# x\n\nstub\n")
        scorecard = td / "scorecard.json"
        scorecard.write_text(json.dumps(_scorecard([
            {"suite": "real_waf", "fixture": "turnstile_real", "success_rate": 0.85},
        ])))
        rc = sc.main_with_args(scorecard, readme) if hasattr(sc, "main_with_args") else None
        # Run via direct call into main() — patch argv.
        if rc is None:
            saved = sys.argv
            sys.argv = [
                "scorecard.py",
                "--scorecard",
                str(scorecard),
                "--readme",
                str(readme),
            ]
            try:
                rc = sc.main()
            finally:
                sys.argv = saved
        assert rc == 0
        body = readme.read_text()
        assert "turnstile_real" in body
        assert "green" in body  # 0.85 → green
    print("PASS: end-to-end writes README")


if __name__ == "__main__":
    test_color_thresholds()
    test_build_badges_filters_availability_rows()
    test_inject_badges_inserts_between_markers()
    test_inject_badges_appends_when_no_markers()
    test_inject_idempotent_on_unchanged_input()
    test_end_to_end_writes_readme()
    print("ALL PASS")