|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import type { AccessibilityNode } from '../../../../types/domain-results.ts'; |
| 3 | +import { createRuntimeSnapshotNextSteps } from '../shared/runtime-next-steps.ts'; |
| 4 | +import { |
| 5 | + __resetRuntimeSnapshotStoreForTests, |
| 6 | + getRuntimeSnapshot, |
| 7 | +} from '../shared/snapshot-ui-state.ts'; |
| 8 | +import { createNode, recordSnapshot, simulatorId } from './ui-action-test-helpers.ts'; |
| 9 | + |
| 10 | +function currentRuntimeSnapshot() { |
| 11 | + const snapshot = getRuntimeSnapshot(simulatorId); |
| 12 | + expect(snapshot).not.toBeNull(); |
| 13 | + return snapshot!.payload; |
| 14 | +} |
| 15 | + |
| 16 | +function createScrollView(overrides: Partial<AccessibilityNode> = {}): AccessibilityNode { |
| 17 | + return createNode({ |
| 18 | + type: 'ScrollView', |
| 19 | + role: 'AXScrollArea', |
| 20 | + frame: { x: 0, y: 0, width: 390, height: 844 }, |
| 21 | + AXIdentifier: 'scroll-view', |
| 22 | + ...overrides, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function nestNode(node: AccessibilityNode, depth: number): AccessibilityNode { |
| 27 | + let current = node; |
| 28 | + for (let index = 0; index < depth; index += 1) { |
| 29 | + current = createNode({ |
| 30 | + type: 'Group', |
| 31 | + role: 'AXGroup', |
| 32 | + AXIdentifier: `container.${index}`, |
| 33 | + frame: current.frame, |
| 34 | + children: [current], |
| 35 | + }); |
| 36 | + } |
| 37 | + return current; |
| 38 | +} |
| 39 | + |
| 40 | +describe('runtime snapshot next steps', () => { |
| 41 | + beforeEach(() => { |
| 42 | + __resetRuntimeSnapshotStoreForTests(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('prefers tap and scroll examples from the active foreground container', () => { |
| 46 | + recordSnapshot([ |
| 47 | + createScrollView({ |
| 48 | + AXIdentifier: 'weather.backgroundList', |
| 49 | + children: [ |
| 50 | + createNode({ |
| 51 | + AXLabel: 'Background, Details', |
| 52 | + AXIdentifier: 'weather.backgroundCard', |
| 53 | + frame: { x: 20, y: 120, width: 350, height: 80 }, |
| 54 | + }), |
| 55 | + ], |
| 56 | + }), |
| 57 | + createScrollView({ |
| 58 | + AXIdentifier: 'weather.settingsSheet', |
| 59 | + frame: { x: 0, y: 420, width: 390, height: 424 }, |
| 60 | + children: [ |
| 61 | + createNode({ AXLabel: 'Close', frame: { x: 310, y: 430, width: 60, height: 40 } }), |
| 62 | + createNode({ |
| 63 | + type: 'TextField', |
| 64 | + role: 'AXTextField', |
| 65 | + AXLabel: 'Search', |
| 66 | + frame: { x: 20, y: 480, width: 350, height: 40 }, |
| 67 | + }), |
| 68 | + createNode({ |
| 69 | + AXLabel: 'London, England', |
| 70 | + AXIdentifier: 'weather.locationCard', |
| 71 | + frame: { x: 20, y: 540, width: 350, height: 80 }, |
| 72 | + }), |
| 73 | + ], |
| 74 | + }), |
| 75 | + ]); |
| 76 | + |
| 77 | + const snapshot = currentRuntimeSnapshot(); |
| 78 | + const foregroundScrollRef = snapshot.elements.find( |
| 79 | + (element) => element.identifier === 'weather.settingsSheet', |
| 80 | + )?.ref; |
| 81 | + const foregroundCardRef = snapshot.elements.find( |
| 82 | + (element) => element.identifier === 'weather.locationCard', |
| 83 | + )?.ref; |
| 84 | + |
| 85 | + const steps = createRuntimeSnapshotNextSteps({ |
| 86 | + simulatorId, |
| 87 | + runtimeSnapshot: snapshot, |
| 88 | + includeRefreshAndWait: false, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(steps).toContainEqual({ |
| 92 | + label: 'Tap an elementRef', |
| 93 | + tool: 'tap', |
| 94 | + params: { simulatorId, elementRef: foregroundCardRef }, |
| 95 | + }); |
| 96 | + expect(steps).toContainEqual({ |
| 97 | + label: 'Scroll visible content', |
| 98 | + tool: 'swipe', |
| 99 | + params: { |
| 100 | + simulatorId, |
| 101 | + withinElementRef: foregroundScrollRef, |
| 102 | + direction: 'up', |
| 103 | + distance: 0.5, |
| 104 | + }, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('uses hierarchy depth only as a foreground-root tie breaker', () => { |
| 109 | + recordSnapshot([ |
| 110 | + nestNode( |
| 111 | + createScrollView({ |
| 112 | + AXIdentifier: 'deep.stateControls', |
| 113 | + frame: { x: 0, y: 0, width: 390, height: 80 }, |
| 114 | + children: [ |
| 115 | + createNode({ |
| 116 | + type: 'Switch', |
| 117 | + role: 'AXSwitch', |
| 118 | + AXLabel: 'Nested switch', |
| 119 | + AXValue: '0', |
| 120 | + }), |
| 121 | + ], |
| 122 | + }), |
| 123 | + 40, |
| 124 | + ), |
| 125 | + createScrollView({ |
| 126 | + AXIdentifier: 'shallow.searchPanel', |
| 127 | + frame: { x: 0, y: 100, width: 390, height: 500 }, |
| 128 | + children: [ |
| 129 | + createNode({ |
| 130 | + type: 'TextField', |
| 131 | + role: 'AXTextField', |
| 132 | + AXLabel: 'Search', |
| 133 | + frame: { x: 20, y: 130, width: 350, height: 40 }, |
| 134 | + }), |
| 135 | + ], |
| 136 | + }), |
| 137 | + ]); |
| 138 | + |
| 139 | + const snapshot = currentRuntimeSnapshot(); |
| 140 | + const shallowSearchRef = snapshot.elements.find( |
| 141 | + (element) => element.identifier === 'shallow.searchPanel', |
| 142 | + )?.ref; |
| 143 | + |
| 144 | + const steps = createRuntimeSnapshotNextSteps({ |
| 145 | + simulatorId, |
| 146 | + runtimeSnapshot: snapshot, |
| 147 | + includeRefreshAndWait: false, |
| 148 | + }); |
| 149 | + |
| 150 | + expect(steps).toContainEqual({ |
| 151 | + label: 'Scroll visible content', |
| 152 | + tool: 'swipe', |
| 153 | + params: { |
| 154 | + simulatorId, |
| 155 | + withinElementRef: shallowSearchRef, |
| 156 | + direction: 'up', |
| 157 | + distance: 0.5, |
| 158 | + }, |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments