|
| 1 | +import { test, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { attachRefs } from '@agent-device/kernel/snapshot'; |
| 3 | +import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts'; |
| 4 | +import { handleInteractionCommands } from '../interaction.ts'; |
| 5 | +import { |
| 6 | + contextFromFlags, |
| 7 | + createEmulateCaptureSnapshotForSession, |
| 8 | + makeAndroidSession, |
| 9 | +} from './interaction-touch-fixtures.ts'; |
| 10 | + |
| 11 | +// The Android ref-refresh capture a @ref mutation takes before dispatch: when |
| 12 | +// it runs, what it may not retarget, and the comparison-safe baseline it hands |
| 13 | +// finalization when the refresh fails. |
| 14 | + |
| 15 | +const { mockRunAppleRunnerCommand } = vi.hoisted(() => ({ |
| 16 | + mockRunAppleRunnerCommand: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('../../../core/dispatch.ts', async (importOriginal) => { |
| 20 | + const actual = await importOriginal<typeof import('../../../core/dispatch.ts')>(); |
| 21 | + return { ...actual, dispatchCommand: vi.fn(async () => ({})) }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock('../../../platforms/android/input-actions.ts', async (importOriginal) => { |
| 25 | + const actual = |
| 26 | + await importOriginal<typeof import('../../../platforms/android/input-actions.ts')>(); |
| 27 | + return { ...actual, getAndroidScreenSize: vi.fn(async () => ({ width: 1344, height: 2992 })) }; |
| 28 | +}); |
| 29 | + |
| 30 | +vi.mock('../../../platforms/android/app-lifecycle.ts', async (importOriginal) => { |
| 31 | + const actual = |
| 32 | + await importOriginal<typeof import('../../../platforms/android/app-lifecycle.ts')>(); |
| 33 | + return { |
| 34 | + ...actual, |
| 35 | + getAndroidAppState: vi.fn(async () => ({})), |
| 36 | + getAndroidBlockingDialogFocus: vi.fn(async () => null), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock('../interaction-snapshot.ts', async (importOriginal) => { |
| 41 | + const actual = await importOriginal<typeof import('../interaction-snapshot.ts')>(); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + captureSnapshotForSession: vi.fn(async () => ({ |
| 45 | + nodes: [], |
| 46 | + createdAt: 0, |
| 47 | + backend: 'xctest' as const, |
| 48 | + })), |
| 49 | + }; |
| 50 | +}); |
| 51 | + |
| 52 | +vi.mock('../../../platforms/apple/core/runner/runner-client.ts', async (importOriginal) => { |
| 53 | + const actual = |
| 54 | + await importOriginal<typeof import('../../../platforms/apple/core/runner/runner-client.ts')>(); |
| 55 | + return { ...actual, runAppleRunnerCommand: mockRunAppleRunnerCommand }; |
| 56 | +}); |
| 57 | + |
| 58 | +import { dispatchCommand } from '../../../core/dispatch.ts'; |
| 59 | +import { |
| 60 | + getAndroidAppState, |
| 61 | + getAndroidBlockingDialogFocus, |
| 62 | +} from '../../../platforms/android/app-lifecycle.ts'; |
| 63 | +import { getAndroidScreenSize } from '../../../platforms/android/input-actions.ts'; |
| 64 | +import { captureSnapshotForSession } from '../interaction-snapshot.ts'; |
| 65 | + |
| 66 | +const mockDispatch = vi.mocked(dispatchCommand); |
| 67 | +const mockGetAndroidAppState = vi.mocked(getAndroidAppState); |
| 68 | +const mockGetAndroidBlockingDialogFocus = vi.mocked(getAndroidBlockingDialogFocus); |
| 69 | +const mockGetAndroidScreenSize = vi.mocked(getAndroidScreenSize); |
| 70 | +const mockCaptureSnapshotForSession = vi.mocked(captureSnapshotForSession); |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + mockDispatch.mockReset(); |
| 74 | + mockDispatch.mockResolvedValue({}); |
| 75 | + mockGetAndroidAppState.mockReset(); |
| 76 | + mockGetAndroidAppState.mockResolvedValue({}); |
| 77 | + mockGetAndroidBlockingDialogFocus.mockReset(); |
| 78 | + mockGetAndroidBlockingDialogFocus.mockResolvedValue(null); |
| 79 | + mockGetAndroidScreenSize.mockReset(); |
| 80 | + mockGetAndroidScreenSize.mockResolvedValue({ width: 1344, height: 2992 }); |
| 81 | + mockCaptureSnapshotForSession.mockReset(); |
| 82 | + mockCaptureSnapshotForSession.mockImplementation( |
| 83 | + createEmulateCaptureSnapshotForSession(mockDispatch), |
| 84 | + ); |
| 85 | + mockRunAppleRunnerCommand.mockReset(); |
| 86 | + mockRunAppleRunnerCommand.mockResolvedValue({}); |
| 87 | +}); |
| 88 | + |
| 89 | +test('press @ref refreshes Android snapshot when freshness tracking is active', async () => { |
| 90 | + const sessionStore = makeSessionStore(); |
| 91 | + const sessionName = 'android-fresh-ref-refresh'; |
| 92 | + const session = makeAndroidSession(sessionName); |
| 93 | + session.snapshot = { |
| 94 | + nodes: attachRefs([ |
| 95 | + { |
| 96 | + index: 0, |
| 97 | + type: 'android.widget.Button', |
| 98 | + label: 'Continue', |
| 99 | + rect: { x: 0, y: 0, width: 40, height: 40 }, |
| 100 | + enabled: true, |
| 101 | + hittable: true, |
| 102 | + }, |
| 103 | + ]), |
| 104 | + createdAt: Date.now(), |
| 105 | + backend: 'android', |
| 106 | + comparisonSafe: true, |
| 107 | + }; |
| 108 | + session.androidSnapshotFreshness = { |
| 109 | + action: 'press', |
| 110 | + markedAt: Date.now(), |
| 111 | + baselineCount: 1, |
| 112 | + routeComparable: false, |
| 113 | + }; |
| 114 | + sessionStore.set(sessionName, session); |
| 115 | + |
| 116 | + mockDispatch.mockImplementation(async (_device, command, args) => { |
| 117 | + if (command === 'snapshot') { |
| 118 | + return { |
| 119 | + nodes: [ |
| 120 | + { |
| 121 | + index: 0, |
| 122 | + type: 'android.widget.Button', |
| 123 | + label: 'Continue', |
| 124 | + rect: { x: 100, y: 200, width: 80, height: 40 }, |
| 125 | + enabled: true, |
| 126 | + hittable: true, |
| 127 | + }, |
| 128 | + ], |
| 129 | + backend: 'android', |
| 130 | + }; |
| 131 | + } |
| 132 | + return { pressed: true, args }; |
| 133 | + }); |
| 134 | + |
| 135 | + const response = await handleInteractionCommands({ |
| 136 | + req: { |
| 137 | + token: 't', |
| 138 | + session: sessionName, |
| 139 | + command: 'press', |
| 140 | + positionals: ['@e1', 'Continue'], |
| 141 | + flags: {}, |
| 142 | + }, |
| 143 | + sessionName, |
| 144 | + sessionStore, |
| 145 | + contextFromFlags, |
| 146 | + }); |
| 147 | + |
| 148 | + expect(response?.ok).toBe(true); |
| 149 | + expect(mockCaptureSnapshotForSession.mock.calls[0]?.[4]).toEqual({ |
| 150 | + interactiveOnly: true, |
| 151 | + androidFreshnessMode: 'ref-refresh', |
| 152 | + }); |
| 153 | + expect(mockDispatch.mock.calls.map((call) => call[1])).toEqual(['snapshot', 'press']); |
| 154 | + expect(mockDispatch.mock.calls[1]?.[2]).toEqual(['140', '220']); |
| 155 | + expect(sessionStore.get(sessionName)?.androidSnapshotFreshness).toMatchObject({ |
| 156 | + action: 'press', |
| 157 | + baselineCount: 1, |
| 158 | + routeComparable: true, |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +test('ADR 0014: Android freshness cannot retarget an admitted ref by positional coincidence', async () => { |
| 163 | + const sessionStore = makeSessionStore(); |
| 164 | + const sessionName = 'android-fresh-ref-no-retarget'; |
| 165 | + const session = makeAndroidSession(sessionName); |
| 166 | + const frameTree = { |
| 167 | + nodes: attachRefs([ |
| 168 | + { |
| 169 | + index: 0, |
| 170 | + type: 'android.widget.Button', |
| 171 | + label: 'Continue', |
| 172 | + rect: { x: 0, y: 0, width: 40, height: 40 }, |
| 173 | + enabled: true, |
| 174 | + hittable: true, |
| 175 | + }, |
| 176 | + ]), |
| 177 | + createdAt: Date.now(), |
| 178 | + backend: 'android' as const, |
| 179 | + comparisonSafe: true, |
| 180 | + }; |
| 181 | + session.snapshot = frameTree; |
| 182 | + // ADR 0014: the authorized frame tree names WHICH node @e1 authorizes. |
| 183 | + session.refFrameTree = frameTree; |
| 184 | + session.androidSnapshotFreshness = { |
| 185 | + action: 'press', |
| 186 | + markedAt: Date.now(), |
| 187 | + baselineCount: 1, |
| 188 | + routeComparable: false, |
| 189 | + }; |
| 190 | + sessionStore.set(sessionName, session); |
| 191 | + |
| 192 | + // The freshness refresh returns a DIFFERENT element at @e1's index — after |
| 193 | + // navigation the button at that position is now "Cancel", not "Continue". |
| 194 | + mockDispatch.mockImplementation(async (_device, command, args) => { |
| 195 | + if (command === 'snapshot') { |
| 196 | + return { |
| 197 | + nodes: [ |
| 198 | + { |
| 199 | + index: 0, |
| 200 | + type: 'android.widget.Button', |
| 201 | + label: 'Cancel', |
| 202 | + rect: { x: 100, y: 200, width: 80, height: 40 }, |
| 203 | + enabled: true, |
| 204 | + hittable: true, |
| 205 | + }, |
| 206 | + ], |
| 207 | + backend: 'android', |
| 208 | + }; |
| 209 | + } |
| 210 | + return { pressed: true, args }; |
| 211 | + }); |
| 212 | + |
| 213 | + const response = await handleInteractionCommands({ |
| 214 | + req: { |
| 215 | + token: 't', |
| 216 | + session: sessionName, |
| 217 | + command: 'press', |
| 218 | + positionals: ['@e1'], |
| 219 | + flags: {}, |
| 220 | + }, |
| 221 | + sessionName, |
| 222 | + sessionStore, |
| 223 | + contextFromFlags, |
| 224 | + }); |
| 225 | + |
| 226 | + expect(response?.ok).toBe(true); |
| 227 | + // The identity at @e1 changed (Continue -> Cancel), so the refreshed |
| 228 | + // observation must NOT redefine the target: the press stays on the frame |
| 229 | + // node's coordinates (center of {0,0,40,40}), never the fresh (140, 220). |
| 230 | + expect(mockDispatch.mock.calls.map((call) => call[1])).toEqual(['snapshot', 'press']); |
| 231 | + expect(mockDispatch.mock.calls[1]?.[2]).toEqual(['20', '20']); |
| 232 | +}); |
| 233 | + |
| 234 | +test('press @ref falls back to cached Android ref when freshness refresh fails', async () => { |
| 235 | + const sessionStore = makeSessionStore(); |
| 236 | + const sessionName = 'android-fresh-ref-refresh-failure'; |
| 237 | + const session = makeAndroidSession(sessionName); |
| 238 | + session.snapshot = { |
| 239 | + nodes: attachRefs([ |
| 240 | + { |
| 241 | + index: 0, |
| 242 | + type: 'android.widget.Button', |
| 243 | + label: 'Continue', |
| 244 | + rect: { x: 10, y: 20, width: 100, height: 40 }, |
| 245 | + enabled: true, |
| 246 | + hittable: true, |
| 247 | + }, |
| 248 | + ]), |
| 249 | + createdAt: Date.now(), |
| 250 | + backend: 'android', |
| 251 | + comparisonSafe: true, |
| 252 | + }; |
| 253 | + session.androidSnapshotFreshness = { |
| 254 | + action: 'press', |
| 255 | + markedAt: Date.now(), |
| 256 | + baselineCount: 1, |
| 257 | + routeComparable: true, |
| 258 | + }; |
| 259 | + sessionStore.set(sessionName, session); |
| 260 | + |
| 261 | + mockCaptureSnapshotForSession.mockRejectedValueOnce(new Error('uiautomator timeout')); |
| 262 | + mockDispatch.mockResolvedValue({ pressed: true }); |
| 263 | + |
| 264 | + const response = await handleInteractionCommands({ |
| 265 | + req: { |
| 266 | + token: 't', |
| 267 | + session: sessionName, |
| 268 | + command: 'press', |
| 269 | + positionals: ['@e1', 'Continue'], |
| 270 | + flags: {}, |
| 271 | + }, |
| 272 | + sessionName, |
| 273 | + sessionStore, |
| 274 | + contextFromFlags, |
| 275 | + }); |
| 276 | + |
| 277 | + expect(response?.ok).toBe(true); |
| 278 | + expect(mockCaptureSnapshotForSession).toHaveBeenCalledTimes(1); |
| 279 | + expect(mockDispatch.mock.calls.map((call) => call[1])).toEqual(['press']); |
| 280 | + expect(mockDispatch.mock.calls[0]?.[2]).toEqual(['60', '40']); |
| 281 | + expect(sessionStore.get(sessionName)?.androidSnapshotFreshness).toMatchObject({ |
| 282 | + action: 'press', |
| 283 | + baselineCount: 1, |
| 284 | + routeComparable: true, |
| 285 | + }); |
| 286 | +}); |
| 287 | + |
| 288 | +test('coordinate press preserves Android route freshness from last comparable snapshot', async () => { |
| 289 | + const sessionStore = makeSessionStore(); |
| 290 | + const sessionName = 'android-coordinate-freshness-baseline'; |
| 291 | + const session = makeAndroidSession(sessionName); |
| 292 | + const comparableSnapshot = { |
| 293 | + nodes: attachRefs([ |
| 294 | + { |
| 295 | + index: 0, |
| 296 | + type: 'android.widget.ScrollView', |
| 297 | + label: 'Albums', |
| 298 | + rect: { x: 0, y: 0, width: 400, height: 700 }, |
| 299 | + }, |
| 300 | + { |
| 301 | + index: 1, |
| 302 | + type: 'android.widget.Button', |
| 303 | + label: 'Go to Contacts', |
| 304 | + rect: { x: 16, y: 120, width: 160, height: 48 }, |
| 305 | + enabled: true, |
| 306 | + hittable: true, |
| 307 | + }, |
| 308 | + ]), |
| 309 | + createdAt: Date.now(), |
| 310 | + backend: 'android' as const, |
| 311 | + comparisonSafe: true, |
| 312 | + }; |
| 313 | + session.lastComparisonSafeSnapshot = comparableSnapshot; |
| 314 | + session.snapshot = { |
| 315 | + nodes: attachRefs([ |
| 316 | + { |
| 317 | + index: 0, |
| 318 | + type: 'android.widget.Button', |
| 319 | + label: 'Go to Contacts', |
| 320 | + rect: { x: 16, y: 120, width: 160, height: 48 }, |
| 321 | + enabled: true, |
| 322 | + hittable: true, |
| 323 | + }, |
| 324 | + ]), |
| 325 | + createdAt: Date.now(), |
| 326 | + backend: 'android', |
| 327 | + comparisonSafe: false, |
| 328 | + }; |
| 329 | + sessionStore.set(sessionName, session); |
| 330 | + mockDispatch.mockResolvedValue({ pressed: true }); |
| 331 | + |
| 332 | + const response = await handleInteractionCommands({ |
| 333 | + req: { |
| 334 | + token: 't', |
| 335 | + session: sessionName, |
| 336 | + command: 'press', |
| 337 | + positionals: ['96', '144'], |
| 338 | + flags: {}, |
| 339 | + }, |
| 340 | + sessionName, |
| 341 | + sessionStore, |
| 342 | + contextFromFlags, |
| 343 | + }); |
| 344 | + |
| 345 | + expect(response?.ok).toBe(true); |
| 346 | + expect(sessionStore.get(sessionName)?.androidSnapshotFreshness).toMatchObject({ |
| 347 | + action: 'press', |
| 348 | + baselineCount: 2, |
| 349 | + baselineSignatures: expect.any(Array), |
| 350 | + routeComparable: true, |
| 351 | + }); |
| 352 | +}); |
0 commit comments