Live reads go through an operator proxy pool (round-robin over the newest 100 entries, cooling on connect failures, no direct fallback); stored clearances are replayed only on the exit that earned them. Identity, clearance, solver and Turnstile rungs escalate automatically. fetch_pages reads up to 12 URLs four at a time with per-item isolation. report/reports/reports_update keep a durable queue of unreadable pages for the operator. Ships a pinned multi-stage image (non-root 10001, data in /data), an ordered CI with static gates, tests on 3.12 and 3.13 at 85% coverage, an offline image smoke, and sha/latest publishing gated on main.
145 lines
5.2 KiB
Python
145 lines
5.2 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
import solve
|
|
|
|
REAL_WORK_DIR, REAL_CLEANUP = solve._work_dir, solve._cleanup
|
|
|
|
|
|
class Done:
|
|
def __init__(self, stdout: str = "", stderr: str = "", code: int = 0) -> None:
|
|
self.stdout = stdout
|
|
self.stderr = stderr
|
|
self.returncode = code
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fast(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(solve, "_work_dir", lambda runner: str(tmp_path))
|
|
monkeypatch.setattr(solve, "_cleanup", lambda runner, work: None)
|
|
|
|
|
|
def test_command_defaults_when_unset(monkeypatch):
|
|
monkeypatch.delenv("MCP_SOLVER", raising=False)
|
|
assert solve.command()[:2] == ["wsl", "-e"]
|
|
|
|
|
|
@pytest.mark.parametrize("value", ["off", "0", "false"])
|
|
def test_command_can_be_disabled(monkeypatch, value):
|
|
monkeypatch.setenv("MCP_SOLVER", value)
|
|
assert solve.command() == []
|
|
|
|
|
|
def test_command_json_array_and_plain_string(monkeypatch):
|
|
monkeypatch.setenv("MCP_SOLVER", json.dumps(["/opt/challenge"]))
|
|
assert solve.command() == ["/opt/challenge"]
|
|
monkeypatch.setenv("MCP_SOLVER", "/opt/challenge --fast")
|
|
assert solve.command() == ["/opt/challenge", "--fast"]
|
|
|
|
|
|
def test_available_checks_the_binary(monkeypatch):
|
|
assert solve.available([]) is False
|
|
monkeypatch.setattr(solve.shutil, "which", lambda name: "/usr/bin/" + name)
|
|
assert solve.available(["wsl"]) is True
|
|
|
|
|
|
def test_cookies_parses_and_requires_clearance():
|
|
assert solve._cookies("cf_clearance=v; __cf_bm=b")["cf_clearance"] == "v"
|
|
with pytest.raises(solve.SolverError) as caught:
|
|
solve._cookies("0.TOKEN")
|
|
assert caught.value.code == "solver_warm"
|
|
|
|
|
|
def test_clearance_success_passes_proxy(monkeypatch):
|
|
seen: dict[str, object] = {}
|
|
|
|
def fake_run(runner, page, extra):
|
|
seen.update(runner=runner, page=page, extra=extra)
|
|
return "cf_clearance=v"
|
|
|
|
monkeypatch.setattr(solve, "_run", fake_run)
|
|
cleared = solve.clearance(["s"], "https://e.com/", "UA", "http://p:1")
|
|
assert cleared.cookies == {"cf_clearance": "v"}
|
|
assert cleared.user_agent == "UA"
|
|
assert seen["extra"] == ["--proxy", "http://p:1"]
|
|
solve.clearance(["s"], "https://e.com/", "UA")
|
|
assert seen["extra"] == []
|
|
|
|
|
|
def test_turnstile_returns_token(monkeypatch):
|
|
monkeypatch.setattr(solve, "_run", lambda *a: "1.TOKEN")
|
|
assert solve.turnstile(["s"], "https://e.com/", "0xKEY") == "1.TOKEN"
|
|
|
|
|
|
def test_turnstile_rejects_error_text(monkeypatch):
|
|
monkeypatch.setattr(solve, "_run", lambda *a: "Error: nope")
|
|
with pytest.raises(solve.SolverError):
|
|
solve.turnstile(["s"], "https://e.com/", "0xKEY")
|
|
|
|
|
|
def test_run_parses_stdout(monkeypatch):
|
|
monkeypatch.setattr(solve.subprocess, "run", lambda *a, **k: Done(stdout="\n TOKEN \n"))
|
|
assert solve._run(["s"], "https://e.com/", []) == "TOKEN"
|
|
|
|
|
|
def test_run_maps_failure_and_warm(monkeypatch):
|
|
monkeypatch.setattr(solve.subprocess, "run", lambda *a, **k: Done(stderr="boom\nlast line", code=1))
|
|
with pytest.raises(solve.SolverError) as caught:
|
|
solve._run(["s"], "https://e.com/", [])
|
|
assert caught.value.code == "solver_failed"
|
|
assert "last line" in str(caught.value)
|
|
|
|
monkeypatch.setattr(
|
|
solve.subprocess, "run", lambda *a, **k: Done(stderr="Error: page has no .cf-turnstile container", code=1)
|
|
)
|
|
with pytest.raises(solve.SolverError) as warm:
|
|
solve._run(["s"], "https://e.com/", [])
|
|
assert warm.value.code == "solver_warm"
|
|
|
|
|
|
def test_run_maps_timeout_and_missing_binary(monkeypatch):
|
|
def boom(*a, **k):
|
|
raise solve.subprocess.TimeoutExpired("s", 1)
|
|
|
|
monkeypatch.setattr(solve.subprocess, "run", boom)
|
|
with pytest.raises(solve.SolverError) as caught:
|
|
solve._run(["s"], "https://e.com/", [])
|
|
assert caught.value.code == "solver_timeout"
|
|
|
|
def missing(*a, **k):
|
|
raise OSError("nope")
|
|
|
|
monkeypatch.setattr(solve.subprocess, "run", missing)
|
|
with pytest.raises(solve.SolverError) as gone:
|
|
solve._run(["s"], "https://e.com/", [])
|
|
assert gone.value.code == "solver_unavailable"
|
|
|
|
|
|
def test_sitekey_extraction():
|
|
assert solve.sitekey(b'<div class="cf-turnstile" data-sitekey="0x4AAAAAAAAGzw6rXeQWJ_y2P"></div>')
|
|
assert solve.sitekey(b"turnstile.render(el, {sitekey: '0x4AAAAAAAAGzw6rXeQWJ_y2P'})")
|
|
assert solve.sitekey(b"<p>none</p>") is None
|
|
|
|
|
|
def test_work_dir_without_wsl(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(solve, "_work_dir", REAL_WORK_DIR)
|
|
monkeypatch.setattr(solve, "_cleanup", REAL_CLEANUP)
|
|
monkeypatch.setattr(solve.tempfile, "mkdtemp", lambda prefix="": str(tmp_path / "w"))
|
|
(tmp_path / "w").mkdir()
|
|
assert solve._work_dir(["challenge"]) == str(tmp_path / "w")
|
|
solve._cleanup(["challenge"], str(tmp_path / "w"))
|
|
assert not (tmp_path / "w").exists()
|
|
|
|
|
|
def test_wsl_helpers(monkeypatch):
|
|
monkeypatch.setattr(solve, "_work_dir", REAL_WORK_DIR)
|
|
monkeypatch.setattr(solve, "_cleanup", REAL_CLEANUP)
|
|
assert solve._wsl(["wsl"]) and not solve._wsl(["challenge"])
|
|
calls: list[list[str]] = []
|
|
monkeypatch.setattr(solve.tempfile, "mkdtemp", lambda prefix="": "C:/tmp/x")
|
|
monkeypatch.setattr(solve.subprocess, "run", lambda args, **k: calls.append(args) or Done(stdout="/mnt/c/tmp/x\n"))
|
|
assert solve._work_dir(["wsl"]) == "/mnt/c/tmp/x"
|
|
solve._cleanup(["wsl"], "/mnt/c/tmp/x")
|
|
assert calls[-1][:3] == ["wsl", "-e", "rm"]
|