|
| 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 always write both accessibility flags', async () => { |
| 14 | + const executorCalls: string[][] = []; |
| 15 | + const mockExecutor = createCommandMatchingMockExecutor({ |
| 16 | + 'defaults write': { success: true, output: '' }, |
| 17 | + }); |
| 18 | + |
| 19 | + const trackingExecutor: CommandExecutor = async (...args) => { |
| 20 | + executorCalls.push(args[0] as string[]); |
| 21 | + return mockExecutor(...args); |
| 22 | + }; |
| 23 | + |
| 24 | + await ensureSimulatorAccessibility(SIM_UUID, trackingExecutor); |
| 25 | + |
| 26 | + expect(executorCalls).toHaveLength(2); |
| 27 | + expect(executorCalls[0].join(' ')).toContain('AccessibilityEnabled'); |
| 28 | + expect(executorCalls[0].join(' ')).toContain('defaults write'); |
| 29 | + expect(executorCalls[1].join(' ')).toContain('ApplicationAccessibilityEnabled'); |
| 30 | + expect(executorCalls[1].join(' ')).toContain('defaults write'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should not throw when executor throws', async () => { |
| 34 | + const mockExecutor = createMockExecutor(new Error('spawn failed')); |
| 35 | + |
| 36 | + // Should not throw |
| 37 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should stop and not write second flag if first write fails', async () => { |
| 41 | + const executorCalls: string[][] = []; |
| 42 | + const callCount = { n: 0 }; |
| 43 | + const mockExecutor: CommandExecutor = async (command) => { |
| 44 | + executorCalls.push(command as string[]); |
| 45 | + callCount.n++; |
| 46 | + if (callCount.n === 1) { |
| 47 | + return createMockCommandResponse({ success: false, error: 'write failed' }); |
| 48 | + } |
| 49 | + return createMockCommandResponse({ success: true, output: '' }); |
| 50 | + }; |
| 51 | + |
| 52 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 53 | + |
| 54 | + // Both writes should be attempted even when first fails |
| 55 | + expect(executorCalls).toHaveLength(2); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not throw when first write fails', async () => { |
| 59 | + const mockExecutor = createCommandMatchingMockExecutor({ |
| 60 | + 'AccessibilityEnabled -bool': { success: false, error: 'write failed' }, |
| 61 | + }); |
| 62 | + |
| 63 | + // Should not throw |
| 64 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should pass correct simctl spawn commands', async () => { |
| 68 | + const executorCalls: string[][] = []; |
| 69 | + const mockExecutor: CommandExecutor = async (command) => { |
| 70 | + executorCalls.push(command as string[]); |
| 71 | + return createMockCommandResponse({ success: true, output: '' }); |
| 72 | + }; |
| 73 | + |
| 74 | + await ensureSimulatorAccessibility(SIM_UUID, mockExecutor); |
| 75 | + |
| 76 | + expect(executorCalls[0]).toEqual([ |
| 77 | + 'xcrun', |
| 78 | + 'simctl', |
| 79 | + 'spawn', |
| 80 | + SIM_UUID, |
| 81 | + 'defaults', |
| 82 | + 'write', |
| 83 | + 'com.apple.Accessibility', |
| 84 | + 'AccessibilityEnabled', |
| 85 | + '-bool', |
| 86 | + 'true', |
| 87 | + ]); |
| 88 | + expect(executorCalls[1]).toEqual([ |
| 89 | + 'xcrun', |
| 90 | + 'simctl', |
| 91 | + 'spawn', |
| 92 | + SIM_UUID, |
| 93 | + 'defaults', |
| 94 | + 'write', |
| 95 | + 'com.apple.Accessibility', |
| 96 | + 'ApplicationAccessibilityEnabled', |
| 97 | + '-bool', |
| 98 | + 'true', |
| 99 | + ]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments