|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + createMockExecutor, |
| 4 | + createCommandMatchingMockExecutor, |
| 5 | + createMockCommandResponse, |
| 6 | +} from '../../test-utils/mock-executors.ts'; |
| 7 | +import type { CommandExecutor } from '../execution/index.ts'; |
| 8 | +import { ensureSimulatorAccessibility } from '../simulator-accessibility.ts'; |
| 9 | + |
| 10 | +const SIM_UUID = '12345678-1234-4234-8234-123456789012'; |
| 11 | + |
| 12 | +describe('ensureSimulatorAccessibility', () => { |
| 13 | + it('should enable accessibility when currently disabled', async () => { |
| 14 | + const executorCalls: string[][] = []; |
| 15 | + const mockExecutor = createCommandMatchingMockExecutor({ |
| 16 | + 'defaults read': { output: '0' }, |
| 17 | + 'defaults write': { success: true, output: '' }, |
| 18 | + }); |
| 19 | + |
| 20 | + const trackingExecutor: CommandExecutor = async (...args) => { |
| 21 | + executorCalls.push(args[0] as string[]); |
| 22 | + return mockExecutor(...args); |
| 23 | + }; |
| 24 | + |
| 25 | + await ensureSimulatorAccessibility(SIM_UUID, trackingExecutor); |
| 26 | + |
| 27 | + expect(executorCalls).toHaveLength(3); |
| 28 | + expect(executorCalls[0].join(' ')).toContain('defaults read'); |
| 29 | + expect(executorCalls[1].join(' ')).toContain('AccessibilityEnabled'); |
| 30 | + expect(executorCalls[1].join(' ')).toContain('defaults write'); |
| 31 | + expect(executorCalls[2].join(' ')).toContain('ApplicationAccessibilityEnabled'); |
| 32 | + expect(executorCalls[2].join(' ')).toContain('defaults write'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should skip writes when accessibility is already enabled', async () => { |
| 36 | + const executorCalls: string[][] = []; |
| 37 | + const mockExecutor = createMockExecutor({ |
| 38 | + success: true, |
| 39 | + output: '1', |
| 40 | + }); |
| 41 | + |
| 42 | + const trackingExecutor: CommandExecutor = async (...args) => { |
| 43 | + executorCalls.push(args[0] as string[]); |
| 44 | + return mockExecutor(...args); |
| 45 | + }; |
| 46 | + |
| 47 | + await ensureSimulatorAccessibility(SIM_UUID, trackingExecutor); |
| 48 | + |
| 49 | + // Only the read command should have been called |
| 50 | + expect(executorCalls).toHaveLength(1); |
| 51 | + expect(executorCalls[0].join(' ')).toContain('defaults read'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should not throw when read command fails', async () => { |
| 55 | + const mockExecutor = createCommandMatchingMockExecutor({ |
| 56 | + 'defaults read': { success: false, output: '', error: 'Domain does not exist' }, |
| 57 | + 'AccessibilityEnabled -bool': { success: false, error: 'write failed' }, |
| 58 | + }); |
| 59 | + |
| 60 | + // Should not throw |
| 61 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not throw when executor throws', async () => { |
| 65 | + const mockExecutor = createMockExecutor(new Error('spawn failed')); |
| 66 | + |
| 67 | + // Should not throw |
| 68 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should stop if first write fails', async () => { |
| 72 | + const executorCalls: string[][] = []; |
| 73 | + const callCount = { n: 0 }; |
| 74 | + const mockExecutor: CommandExecutor = async (command, ...rest) => { |
| 75 | + const cmd = (command as string[]).join(' '); |
| 76 | + executorCalls.push(command as string[]); |
| 77 | + callCount.n++; |
| 78 | + if (cmd.includes('defaults read')) { |
| 79 | + return createMockCommandResponse({ success: true, output: '0' }); |
| 80 | + } |
| 81 | + if (cmd.includes('defaults write') && callCount.n === 2) { |
| 82 | + return createMockCommandResponse({ success: false, error: 'write failed' }); |
| 83 | + } |
| 84 | + return createMockCommandResponse({ success: true, output: '' }); |
| 85 | + }; |
| 86 | + |
| 87 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 88 | + |
| 89 | + // read + first write only (second write should not happen) |
| 90 | + expect(executorCalls).toHaveLength(2); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should pass correct simctl spawn commands', async () => { |
| 94 | + const executorCalls: string[][] = []; |
| 95 | + const mockExecutor: CommandExecutor = async (command) => { |
| 96 | + const cmd = (command as string[]).join(' '); |
| 97 | + executorCalls.push(command as string[]); |
| 98 | + if (cmd.includes('defaults read')) { |
| 99 | + return createMockCommandResponse({ success: true, output: '0' }); |
| 100 | + } |
| 101 | + return createMockCommandResponse({ success: true, output: '' }); |
| 102 | + }; |
| 103 | + |
| 104 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 105 | + |
| 106 | + expect(executorCalls[0]).toEqual([ |
| 107 | + 'xcrun', |
| 108 | + 'simctl', |
| 109 | + 'spawn', |
| 110 | + SIM_UUID, |
| 111 | + 'defaults', |
| 112 | + 'read', |
| 113 | + 'com.apple.Accessibility', |
| 114 | + 'AccessibilityEnabled', |
| 115 | + ]); |
| 116 | + expect(executorCalls[1]).toEqual([ |
| 117 | + 'xcrun', |
| 118 | + 'simctl', |
| 119 | + 'spawn', |
| 120 | + SIM_UUID, |
| 121 | + 'defaults', |
| 122 | + 'write', |
| 123 | + 'com.apple.Accessibility', |
| 124 | + 'AccessibilityEnabled', |
| 125 | + '-bool', |
| 126 | + 'true', |
| 127 | + ]); |
| 128 | + expect(executorCalls[2]).toEqual([ |
| 129 | + 'xcrun', |
| 130 | + 'simctl', |
| 131 | + 'spawn', |
| 132 | + SIM_UUID, |
| 133 | + 'defaults', |
| 134 | + 'write', |
| 135 | + 'com.apple.Accessibility', |
| 136 | + 'ApplicationAccessibilityEnabled', |
| 137 | + '-bool', |
| 138 | + 'true', |
| 139 | + ]); |
| 140 | + }); |
| 141 | +}); |
0 commit comments