|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const { |
| 4 | + persistSessionDefaultsPatchMock, |
| 5 | + resolveSimulatorNameToIdMock, |
| 6 | + resolveSimulatorIdToNameMock, |
| 7 | + inferPlatformMock, |
| 8 | + logMock, |
| 9 | +} = vi.hoisted(() => ({ |
| 10 | + persistSessionDefaultsPatchMock: vi.fn(), |
| 11 | + resolveSimulatorNameToIdMock: vi.fn(), |
| 12 | + resolveSimulatorIdToNameMock: vi.fn(), |
| 13 | + inferPlatformMock: vi.fn(), |
| 14 | + logMock: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../config-store.ts', () => ({ |
| 18 | + persistSessionDefaultsPatch: persistSessionDefaultsPatchMock, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../simulator-resolver.ts', () => ({ |
| 22 | + resolveSimulatorNameToId: resolveSimulatorNameToIdMock, |
| 23 | + resolveSimulatorIdToName: resolveSimulatorIdToNameMock, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('../infer-platform.ts', () => ({ |
| 27 | + inferPlatform: inferPlatformMock, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('../logger.ts', () => ({ |
| 31 | + log: logMock, |
| 32 | +})); |
| 33 | + |
| 34 | +import { sessionStore } from '../session-store.ts'; |
| 35 | +import { scheduleSimulatorDefaultsRefresh } from '../simulator-defaults-refresh.ts'; |
| 36 | + |
| 37 | +describe('scheduleSimulatorDefaultsRefresh', () => { |
| 38 | + const originalNodeEnv = process.env.NODE_ENV; |
| 39 | + const originalVitestEnv = process.env.VITEST; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + sessionStore.clearAll(); |
| 43 | + persistSessionDefaultsPatchMock.mockReset(); |
| 44 | + resolveSimulatorNameToIdMock.mockReset(); |
| 45 | + resolveSimulatorIdToNameMock.mockReset(); |
| 46 | + inferPlatformMock.mockReset(); |
| 47 | + logMock.mockReset(); |
| 48 | + |
| 49 | + process.env.NODE_ENV = 'development'; |
| 50 | + delete process.env.VITEST; |
| 51 | + |
| 52 | + inferPlatformMock.mockResolvedValue({ |
| 53 | + platform: 'iOS Simulator', |
| 54 | + source: 'simulator-runtime', |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + process.env.NODE_ENV = originalNodeEnv; |
| 60 | + if (originalVitestEnv == null) { |
| 61 | + delete process.env.VITEST; |
| 62 | + } else { |
| 63 | + process.env.VITEST = originalVitestEnv; |
| 64 | + } |
| 65 | + |
| 66 | + vi.useRealTimers(); |
| 67 | + }); |
| 68 | + |
| 69 | + async function runRefresh(options: { simulatorId: string; simulatorName: string }) { |
| 70 | + vi.useFakeTimers(); |
| 71 | + |
| 72 | + sessionStore.setDefaults({ |
| 73 | + simulatorId: options.simulatorId, |
| 74 | + simulatorName: options.simulatorName, |
| 75 | + }); |
| 76 | + const expectedRevision = sessionStore.getRevision(); |
| 77 | + |
| 78 | + const scheduled = scheduleSimulatorDefaultsRefresh({ |
| 79 | + expectedRevision, |
| 80 | + reason: 'startup-hydration', |
| 81 | + profile: null, |
| 82 | + persist: false, |
| 83 | + simulatorId: options.simulatorId, |
| 84 | + simulatorName: options.simulatorName, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(scheduled).toBe(true); |
| 88 | + await vi.runAllTimersAsync(); |
| 89 | + } |
| 90 | + |
| 91 | + it('does not patch defaults when both values are set and name resolves to same id', async () => { |
| 92 | + resolveSimulatorNameToIdMock.mockResolvedValue({ |
| 93 | + success: true, |
| 94 | + simulatorId: 'SIM-1', |
| 95 | + simulatorName: 'iPhone 17 Pro', |
| 96 | + }); |
| 97 | + inferPlatformMock.mockResolvedValue({ |
| 98 | + platform: 'iOS Simulator', |
| 99 | + source: 'default', |
| 100 | + }); |
| 101 | + |
| 102 | + await runRefresh({ simulatorId: 'SIM-1', simulatorName: 'iPhone 17 Pro' }); |
| 103 | + |
| 104 | + expect(resolveSimulatorNameToIdMock).toHaveBeenCalledTimes(1); |
| 105 | + expect(sessionStore.getAll()).toEqual({ |
| 106 | + simulatorId: 'SIM-1', |
| 107 | + simulatorName: 'iPhone 17 Pro', |
| 108 | + }); |
| 109 | + expect(persistSessionDefaultsPatchMock).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('patches simulatorId in memory when both are set and name resolves to a different id', async () => { |
| 113 | + resolveSimulatorNameToIdMock.mockResolvedValue({ |
| 114 | + success: true, |
| 115 | + simulatorId: 'SIM-2', |
| 116 | + simulatorName: 'iPhone 17 Pro', |
| 117 | + }); |
| 118 | + |
| 119 | + await runRefresh({ simulatorId: 'SIM-1', simulatorName: 'iPhone 17 Pro' }); |
| 120 | + |
| 121 | + expect(resolveSimulatorNameToIdMock).toHaveBeenCalledTimes(1); |
| 122 | + expect(sessionStore.getAll()).toEqual({ |
| 123 | + simulatorId: 'SIM-2', |
| 124 | + simulatorName: 'iPhone 17 Pro', |
| 125 | + simulatorPlatform: 'iOS Simulator', |
| 126 | + }); |
| 127 | + expect(persistSessionDefaultsPatchMock).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('keeps the existing simulatorId when name lookup fails and logs a warning', async () => { |
| 131 | + resolveSimulatorNameToIdMock.mockRejectedValue(new Error('simctl failed')); |
| 132 | + |
| 133 | + await runRefresh({ simulatorId: 'SIM-1', simulatorName: 'iPhone 17 Pro' }); |
| 134 | + |
| 135 | + expect(resolveSimulatorNameToIdMock).toHaveBeenCalledTimes(1); |
| 136 | + expect(sessionStore.getAll()).toEqual({ |
| 137 | + simulatorId: 'SIM-1', |
| 138 | + simulatorName: 'iPhone 17 Pro', |
| 139 | + }); |
| 140 | + expect(logMock).toHaveBeenCalledWith( |
| 141 | + 'warn', |
| 142 | + expect.stringContaining( |
| 143 | + 'Background simulator defaults refresh failed (startup-hydration): Error: simctl failed', |
| 144 | + ), |
| 145 | + ); |
| 146 | + expect(persistSessionDefaultsPatchMock).not.toHaveBeenCalled(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments