|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { runScrollEdgePasses, runScrollPasses } from '../scroll-edge-state.ts'; |
| 5 | + |
| 6 | +type Frame = { readable: boolean; done: boolean; fingerprint?: string }; |
| 7 | + |
| 8 | +const REFUSED = new AppError('COMMAND_FAILED', 'refused', { reason: 'refused' }); |
| 9 | +const PASS_LIMIT = new AppError('COMMAND_FAILED', 'limit', { reason: 'limit' }); |
| 10 | +const NO_PROGRESS = new AppError('COMMAND_FAILED', 'stuck', { reason: 'stuck' }); |
| 11 | + |
| 12 | +/** Drives the shared loop over scripted frames, counting what it asked for. */ |
| 13 | +async function run(frames: Frame[], passLimit = 10) { |
| 14 | + const calls = { observe: 0, shouldStop: 0, scroll: 0, settle: 0 }; |
| 15 | + const outcome = runScrollPasses({ |
| 16 | + observe: async () => frames[Math.min(calls.observe++, frames.length - 1)]!, |
| 17 | + refuse: (frame) => (frame.readable ? undefined : REFUSED), |
| 18 | + shouldStop: async (frame) => { |
| 19 | + calls.shouldStop += 1; |
| 20 | + return frame.done; |
| 21 | + }, |
| 22 | + passLimit, |
| 23 | + passLimitError: () => PASS_LIMIT, |
| 24 | + fingerprint: async (frame) => frame.fingerprint, |
| 25 | + noProgressError: () => NO_PROGRESS, |
| 26 | + scroll: async () => ({ pass: ++calls.scroll }), |
| 27 | + settleAfterPass: async () => { |
| 28 | + calls.settle += 1; |
| 29 | + }, |
| 30 | + }); |
| 31 | + return { outcome, calls }; |
| 32 | +} |
| 33 | + |
| 34 | +test('runScrollPasses: a stop on the first observation costs no gesture', async () => { |
| 35 | + const { outcome, calls } = await run([{ readable: true, done: true }]); |
| 36 | + assert.deepEqual(await outcome, { passes: 0, result: undefined }); |
| 37 | + assert.equal(calls.scroll, 0); |
| 38 | +}); |
| 39 | + |
| 40 | +test('runScrollPasses: scrolls and settles once per pass and reports the last gesture', async () => { |
| 41 | + const { outcome, calls } = await run([ |
| 42 | + { readable: true, done: false, fingerprint: 'a' }, |
| 43 | + { readable: true, done: false, fingerprint: 'b' }, |
| 44 | + { readable: true, done: true }, |
| 45 | + ]); |
| 46 | + assert.deepEqual(await outcome, { passes: 2, result: { pass: 2 } }); |
| 47 | + assert.equal(calls.settle, 2); |
| 48 | +}); |
| 49 | + |
| 50 | +test('runScrollPasses: refuses an unreadable observation before asking the stop condition', async () => { |
| 51 | + const { outcome, calls } = await run([{ readable: false, done: true }]); |
| 52 | + await assert.rejects(outcome, (error) => error === REFUSED); |
| 53 | + assert.equal(calls.shouldStop, 0); |
| 54 | + assert.equal(calls.scroll, 0); |
| 55 | +}); |
| 56 | + |
| 57 | +test('runScrollPasses: refuses an observation that goes unreadable after a pass', async () => { |
| 58 | + const { outcome, calls } = await run([ |
| 59 | + { readable: true, done: false, fingerprint: 'a' }, |
| 60 | + { readable: false, done: true }, |
| 61 | + ]); |
| 62 | + await assert.rejects(outcome, (error) => error === REFUSED); |
| 63 | + assert.equal(calls.scroll, 1); |
| 64 | +}); |
| 65 | + |
| 66 | +test('runScrollPasses: spends exactly its pass budget, then raises the caller error', async () => { |
| 67 | + const budget = 3; |
| 68 | + const frames = Array.from({ length: budget + 1 }, (_, index) => ({ |
| 69 | + readable: true, |
| 70 | + done: false, |
| 71 | + fingerprint: `f${index}`, |
| 72 | + })); |
| 73 | + const { outcome, calls } = await run(frames, budget); |
| 74 | + await assert.rejects(outcome, (error) => error === PASS_LIMIT); |
| 75 | + assert.equal(calls.scroll, budget); |
| 76 | +}); |
| 77 | + |
| 78 | +/** |
| 79 | + * The budget is checked before the stuck window, so an explicit small budget reports the budget |
| 80 | + * rather than no-progress (the order `scroll --until` documented before the loops were shared). |
| 81 | + */ |
| 82 | +test('runScrollPasses: a surface that never shifts stops as no-progress, the budget winning ties', async () => { |
| 83 | + const stuck = { readable: true, done: false, fingerprint: 'same' }; |
| 84 | + const unbounded = await run([stuck], 10); |
| 85 | + await assert.rejects(unbounded.outcome, (error) => error === NO_PROGRESS); |
| 86 | + assert.equal(unbounded.calls.scroll, 3); |
| 87 | + |
| 88 | + const bounded = await run([stuck], 3); |
| 89 | + await assert.rejects(bounded.outcome, (error) => error === PASS_LIMIT); |
| 90 | +}); |
| 91 | + |
| 92 | +test('runScrollPasses: an observation without a fingerprint never trips no-progress', async () => { |
| 93 | + const { outcome, calls } = await run([{ readable: true, done: false }], 5); |
| 94 | + await assert.rejects(outcome, (error) => error === PASS_LIMIT); |
| 95 | + assert.equal(calls.scroll, 5); |
| 96 | +}); |
| 97 | + |
| 98 | +test('runScrollEdgePasses: a refused capture raises the typed edge refusal before any gesture', async () => { |
| 99 | + let scrolls = 0; |
| 100 | + await assert.rejects( |
| 101 | + runScrollEdgePasses({ |
| 102 | + edge: 'bottom', |
| 103 | + captureState: async () => ({ |
| 104 | + refused: { reason: 'sparse-tree', detail: 'AX bridge unavailable' }, |
| 105 | + }), |
| 106 | + scroll: async () => { |
| 107 | + scrolls += 1; |
| 108 | + }, |
| 109 | + }), |
| 110 | + (error: unknown) => { |
| 111 | + assert.ok(error instanceof AppError); |
| 112 | + assert.equal(error.details?.reason, 'scroll_edge_capture_unreadable'); |
| 113 | + assert.equal(error.details?.edge, 'bottom'); |
| 114 | + assert.equal(error.details?.captureRefusal, 'sparse-tree'); |
| 115 | + assert.match(error.message, /AX bridge unavailable/); |
| 116 | + return true; |
| 117 | + }, |
| 118 | + ); |
| 119 | + assert.equal(scrolls, 0); |
| 120 | +}); |
0 commit comments