|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import type { DeviceInfo } from '../../utils/device.ts'; |
| 4 | +import { resolveIosAppStateFromSnapshots } from '../app-state.ts'; |
| 5 | +import type { CommandFlags, dispatchCommand } from '../../core/dispatch.ts'; |
| 6 | + |
| 7 | +const iosSimulator: DeviceInfo = { |
| 8 | + platform: 'ios', |
| 9 | + id: 'sim-1', |
| 10 | + name: 'iPhone Simulator', |
| 11 | + kind: 'simulator', |
| 12 | + booted: true, |
| 13 | +}; |
| 14 | + |
| 15 | +const iosDevice: DeviceInfo = { |
| 16 | + platform: 'ios', |
| 17 | + id: '00008110-000E12341234002E', |
| 18 | + name: 'iPhone', |
| 19 | + kind: 'device', |
| 20 | + booted: true, |
| 21 | +}; |
| 22 | + |
| 23 | +test('appstate uses xctest first on iOS simulator', async () => { |
| 24 | + const backends: string[] = []; |
| 25 | + const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => { |
| 26 | + backends.push(context?.snapshotBackend ?? 'unknown'); |
| 27 | + return { |
| 28 | + nodes: [ |
| 29 | + { |
| 30 | + type: 'XCUIElementTypeApplication', |
| 31 | + label: 'Settings', |
| 32 | + identifier: 'com.apple.Preferences', |
| 33 | + }, |
| 34 | + ], |
| 35 | + }; |
| 36 | + }; |
| 37 | + |
| 38 | + const result = await resolveIosAppStateFromSnapshots( |
| 39 | + iosSimulator, |
| 40 | + '/tmp/daemon.log', |
| 41 | + undefined, |
| 42 | + {} as CommandFlags, |
| 43 | + fakeDispatch, |
| 44 | + ); |
| 45 | + |
| 46 | + assert.deepEqual(backends, ['xctest']); |
| 47 | + assert.equal(result.source, 'snapshot-xctest'); |
| 48 | + assert.equal(result.appBundleId, 'com.apple.Preferences'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('appstate falls back to ax on simulator when xctest is empty', async () => { |
| 52 | + const backends: string[] = []; |
| 53 | + const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => { |
| 54 | + const backend = context?.snapshotBackend ?? 'unknown'; |
| 55 | + backends.push(backend); |
| 56 | + if (backend === 'xctest') { |
| 57 | + return { nodes: [] }; |
| 58 | + } |
| 59 | + return { |
| 60 | + nodes: [ |
| 61 | + { |
| 62 | + type: 'AXApplication', |
| 63 | + label: 'Calculator', |
| 64 | + identifier: 'com.apple.calculator', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + const result = await resolveIosAppStateFromSnapshots( |
| 71 | + iosSimulator, |
| 72 | + '/tmp/daemon.log', |
| 73 | + undefined, |
| 74 | + {} as CommandFlags, |
| 75 | + fakeDispatch, |
| 76 | + ); |
| 77 | + |
| 78 | + assert.deepEqual(backends, ['xctest', 'ax']); |
| 79 | + assert.equal(result.source, 'snapshot-ax'); |
| 80 | + assert.equal(result.appBundleId, 'com.apple.calculator'); |
| 81 | +}); |
| 82 | + |
| 83 | +test('appstate does not try ax on iOS device', async () => { |
| 84 | + const backends: string[] = []; |
| 85 | + const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => { |
| 86 | + backends.push(context?.snapshotBackend ?? 'unknown'); |
| 87 | + return { nodes: [] }; |
| 88 | + }; |
| 89 | + |
| 90 | + const result = await resolveIosAppStateFromSnapshots( |
| 91 | + iosDevice, |
| 92 | + '/tmp/daemon.log', |
| 93 | + undefined, |
| 94 | + {} as CommandFlags, |
| 95 | + fakeDispatch, |
| 96 | + ); |
| 97 | + |
| 98 | + assert.deepEqual(backends, ['xctest']); |
| 99 | + assert.equal(result.source, 'snapshot-xctest'); |
| 100 | + assert.equal(result.appName, 'unknown'); |
| 101 | +}); |
0 commit comments