|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import type { Rect } from '@agent-device/kernel/snapshot'; |
| 4 | +import { buildScrollGesturePlan, type ScrollDirection } from './scroll-gesture.ts'; |
| 5 | + |
| 6 | +// The iPhone geometry #2714 was reported on: a 402x874 frame, the default 10% edge padding leaving |
| 7 | +// y 87..787 for a swipe. |
| 8 | +const FRAME = { referenceWidth: 402, referenceHeight: 874 }; |
| 9 | +const PADDED_TOP = 87; |
| 10 | +const PADDED_BOTTOM = 787; |
| 11 | +const INSET_CONTAINER: Rect = { x: 18, y: 178, width: 366, height: 662 }; |
| 12 | +const FITTING_CONTAINER: Rect = { x: 18, y: 62, width: 366, height: 729 }; |
| 13 | + |
| 14 | +function endpointsInside( |
| 15 | + plan: { x1: number; y1: number; x2: number; y2: number }, |
| 16 | + rect: Rect, |
| 17 | +): boolean { |
| 18 | + const inside = (x: number, y: number) => |
| 19 | + x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height; |
| 20 | + return inside(plan.x1, plan.y1) && inside(plan.x2, plan.y2); |
| 21 | +} |
| 22 | + |
| 23 | +test.each<ScrollDirection>(['up', 'down'])( |
| 24 | + 'a %s swipe longer than an inset container shrinks to the container and reports both spans', |
| 25 | + (direction) => { |
| 26 | + const unclamped = buildScrollGesturePlan({ direction, amount: 0.75, ...FRAME }); |
| 27 | + const plan = buildScrollGesturePlan({ |
| 28 | + direction, |
| 29 | + amount: 0.75, |
| 30 | + ...FRAME, |
| 31 | + container: INSET_CONTAINER, |
| 32 | + }); |
| 33 | + |
| 34 | + assert.ok(endpointsInside(plan, INSET_CONTAINER), JSON.stringify(plan)); |
| 35 | + assert.deepEqual( |
| 36 | + [Math.min(plan.y1, plan.y2), Math.max(plan.y1, plan.y2)], |
| 37 | + [INSET_CONTAINER.y, PADDED_BOTTOM], |
| 38 | + ); |
| 39 | + assert.equal(plan.pixels, PADDED_BOTTOM - INSET_CONTAINER.y); |
| 40 | + assert.equal(plan.requestedPixels, unclamped.pixels); |
| 41 | + assert.equal(Math.sign(plan.y2 - plan.y1), Math.sign(unclamped.y2 - unclamped.y1)); |
| 42 | + }, |
| 43 | +); |
| 44 | + |
| 45 | +test.each<ScrollDirection>(['up', 'down', 'left', 'right'])( |
| 46 | + 'a %s swipe already inside its container keeps today’s endpoints and distance', |
| 47 | + (direction) => { |
| 48 | + const unclamped = buildScrollGesturePlan({ direction, amount: 0.75, ...FRAME }); |
| 49 | + const plan = buildScrollGesturePlan({ |
| 50 | + direction, |
| 51 | + amount: 0.75, |
| 52 | + ...FRAME, |
| 53 | + container: FITTING_CONTAINER, |
| 54 | + }); |
| 55 | + |
| 56 | + assert.deepEqual(plan, unclamped); |
| 57 | + assert.equal('requestedPixels' in plan, false); |
| 58 | + }, |
| 59 | +); |
| 60 | + |
| 61 | +test('a swipe that fits an offset container moves into it without shortening', () => { |
| 62 | + const lowerContainer: Rect = { x: 0, y: 300, width: 402, height: 574 }; |
| 63 | + const unclamped = buildScrollGesturePlan({ direction: 'down', amount: 0.5, ...FRAME }); |
| 64 | + const plan = buildScrollGesturePlan({ |
| 65 | + direction: 'down', |
| 66 | + amount: 0.5, |
| 67 | + ...FRAME, |
| 68 | + container: lowerContainer, |
| 69 | + }); |
| 70 | + |
| 71 | + assert.ok(endpointsInside(plan, lowerContainer), JSON.stringify(plan)); |
| 72 | + assert.equal(Math.min(plan.y1, plan.y2), lowerContainer.y); |
| 73 | + assert.equal(Math.abs(plan.y1 - plan.y2), Math.abs(unclamped.y1 - unclamped.y2)); |
| 74 | + assert.equal(plan.pixels, unclamped.pixels); |
| 75 | + assert.equal('requestedPixels' in plan, false); |
| 76 | +}); |
| 77 | + |
| 78 | +test('a vertical swipe beside a narrow container runs down its centre line', () => { |
| 79 | + const rightColumn: Rect = { x: 250, y: 0, width: 152, height: 874 }; |
| 80 | + const unclamped = buildScrollGesturePlan({ direction: 'up', ...FRAME }); |
| 81 | + const plan = buildScrollGesturePlan({ direction: 'up', ...FRAME, container: rightColumn }); |
| 82 | + |
| 83 | + assert.equal(plan.x1, rightColumn.x + rightColumn.width / 2); |
| 84 | + assert.equal(plan.x2, plan.x1); |
| 85 | + assert.deepEqual([plan.y1, plan.y2, plan.pixels], [unclamped.y1, unclamped.y2, unclamped.pixels]); |
| 86 | +}); |
| 87 | + |
| 88 | +test('a container lying wholly in the edge padding leaves the padded swipe as it was', () => { |
| 89 | + const statusBand: Rect = { x: 0, y: 0, width: 402, height: PADDED_TOP - 1 }; |
| 90 | + const unclamped = buildScrollGesturePlan({ direction: 'up', ...FRAME }); |
| 91 | + const plan = buildScrollGesturePlan({ direction: 'up', ...FRAME, container: statusBand }); |
| 92 | + |
| 93 | + assert.deepEqual(plan, unclamped); |
| 94 | +}); |
0 commit comments