|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from sentience.agent_runtime import AgentRuntime |
| 6 | +from sentience.captcha import PageControlHook |
| 7 | +from sentience.models import CaptchaDiagnostics, CaptchaEvidence, Snapshot, SnapshotDiagnostics |
| 8 | + |
| 9 | + |
| 10 | +class EvalBackend: |
| 11 | + async def eval(self, code: str): |
| 12 | + _ = code |
| 13 | + return "ok" |
| 14 | + |
| 15 | + |
| 16 | +class MockTracer: |
| 17 | + def __init__(self) -> None: |
| 18 | + self.events: list[dict] = [] |
| 19 | + self.run_id = "test-run" |
| 20 | + |
| 21 | + def emit(self, event_type: str, data: dict, step_id: str | None = None) -> None: |
| 22 | + self.events.append({"type": event_type, "data": data, "step_id": step_id}) |
| 23 | + |
| 24 | + |
| 25 | +def make_captcha_snapshot() -> Snapshot: |
| 26 | + evidence = CaptchaEvidence( |
| 27 | + iframe_src_hits=["https://www.google.com/recaptcha/api2/anchor"], |
| 28 | + text_hits=["captcha"], |
| 29 | + selector_hits=[], |
| 30 | + url_hits=[], |
| 31 | + ) |
| 32 | + captcha = CaptchaDiagnostics( |
| 33 | + detected=True, |
| 34 | + provider_hint="recaptcha", |
| 35 | + confidence=0.9, |
| 36 | + evidence=evidence, |
| 37 | + ) |
| 38 | + diagnostics = SnapshotDiagnostics(captcha=captcha) |
| 39 | + return Snapshot( |
| 40 | + status="success", |
| 41 | + url="https://example.com", |
| 42 | + elements=[], |
| 43 | + diagnostics=diagnostics, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_captcha_context_page_control_evaluate_js() -> None: |
| 49 | + runtime = AgentRuntime(backend=EvalBackend(), tracer=MockTracer()) |
| 50 | + runtime.begin_step("captcha_test") |
| 51 | + |
| 52 | + ctx = runtime._build_captcha_context(make_captcha_snapshot(), source="gateway") |
| 53 | + assert isinstance(ctx.page_control, PageControlHook) |
| 54 | + |
| 55 | + result = await ctx.page_control.evaluate_js("1+1") |
| 56 | + assert result == "ok" |
0 commit comments