|
| 1 | +import { AgentRuntime } from '../src/agent-runtime'; |
| 2 | +import { TraceSink } from '../src/tracing/sink'; |
| 3 | +import { Tracer } from '../src/tracing/tracer'; |
| 4 | +import { CaptchaDiagnostics, Snapshot } from '../src/types'; |
| 5 | +import { MockPage } from './mocks/browser-mock'; |
| 6 | + |
| 7 | +class MockSink extends TraceSink { |
| 8 | + public events: any[] = []; |
| 9 | + emit(event: Record<string, any>): void { |
| 10 | + this.events.push(event); |
| 11 | + } |
| 12 | + async close(): Promise<void> { |
| 13 | + // no-op |
| 14 | + } |
| 15 | + getSinkType(): string { |
| 16 | + return 'MockSink'; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function makeRuntime(): AgentRuntime { |
| 21 | + const sink = new MockSink(); |
| 22 | + const tracer = new Tracer('test-run', sink); |
| 23 | + const page = new MockPage('https://example.com') as any; |
| 24 | + const browserLike = { |
| 25 | + snapshot: async () => ({ |
| 26 | + status: 'success', |
| 27 | + url: 'https://example.com', |
| 28 | + elements: [], |
| 29 | + timestamp: 't1', |
| 30 | + }), |
| 31 | + }; |
| 32 | + const runtime = new AgentRuntime(browserLike as any, page as any, tracer); |
| 33 | + runtime.setCaptchaOptions({ minConfidence: 0.1, policy: 'abort' }); |
| 34 | + return runtime; |
| 35 | +} |
| 36 | + |
| 37 | +function makeSnapshot(captcha: CaptchaDiagnostics): Snapshot { |
| 38 | + return { |
| 39 | + status: 'success', |
| 40 | + url: 'https://example.com', |
| 41 | + elements: [], |
| 42 | + diagnostics: { captcha }, |
| 43 | + timestamp: 't1', |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +describe('AgentRuntime captcha detection', () => { |
| 48 | + it('ignores passive recaptcha badges', () => { |
| 49 | + const runtime = makeRuntime(); |
| 50 | + const captcha: CaptchaDiagnostics = { |
| 51 | + detected: true, |
| 52 | + confidence: 0.95, |
| 53 | + provider_hint: 'recaptcha', |
| 54 | + evidence: { |
| 55 | + iframe_src_hits: ['https://www.google.com/recaptcha/api2/anchor?ar=1'], |
| 56 | + selector_hits: [], |
| 57 | + text_hits: [], |
| 58 | + url_hits: [], |
| 59 | + }, |
| 60 | + }; |
| 61 | + |
| 62 | + const detected = (runtime as any).isCaptchaDetected(makeSnapshot(captcha)); |
| 63 | + expect(detected).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('detects interactive captcha challenges', () => { |
| 67 | + const runtime = makeRuntime(); |
| 68 | + const captcha: CaptchaDiagnostics = { |
| 69 | + detected: true, |
| 70 | + confidence: 0.95, |
| 71 | + provider_hint: 'recaptcha', |
| 72 | + evidence: { |
| 73 | + iframe_src_hits: [], |
| 74 | + selector_hits: [], |
| 75 | + text_hits: ["I'm not a robot"], |
| 76 | + url_hits: [], |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + const detected = (runtime as any).isCaptchaDetected(makeSnapshot(captcha)); |
| 81 | + expect(detected).toBe(true); |
| 82 | + }); |
| 83 | +}); |
0 commit comments