|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { CommandResult } from '../types/index.js'; |
| 3 | +import { runCommand } from '../utils/process.js'; |
| 4 | +import * as simulators from './simulators.js'; |
| 5 | +import { |
| 6 | + cleanupSimulatorsAfterTest, |
| 7 | + formatSimulatorTestCleanup, |
| 8 | + isTestSimulatorFlagEnabled, |
| 9 | + withTestSimulatorHooks, |
| 10 | +} from './test-simulator.js'; |
| 11 | + |
| 12 | +vi.mock('../utils/process.js', () => ({ |
| 13 | + runCommand: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('./simulators.js', () => ({ |
| 17 | + listSimulators: vi.fn(), |
| 18 | + shutdownSimulator: vi.fn(), |
| 19 | + deleteSimulator: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +const mockRunCommand = vi.mocked(runCommand); |
| 23 | +const mockListSimulators = vi.mocked(simulators.listSimulators); |
| 24 | +const mockShutdownSimulator = vi.mocked(simulators.shutdownSimulator); |
| 25 | +const mockDeleteSimulator = vi.mocked(simulators.deleteSimulator); |
| 26 | + |
| 27 | +const mockSuccess: CommandResult = { |
| 28 | + command: 'xcrun', |
| 29 | + args: ['simctl'], |
| 30 | + exitCode: 0, |
| 31 | + output: '', |
| 32 | + durationMs: 10, |
| 33 | + truncated: false, |
| 34 | +}; |
| 35 | + |
| 36 | +describe('isTestSimulatorFlagEnabled', () => { |
| 37 | + it('accepts boolean and string truthy values', () => { |
| 38 | + expect(isTestSimulatorFlagEnabled(true)).toBe(true); |
| 39 | + expect(isTestSimulatorFlagEnabled('true')).toBe(true); |
| 40 | + expect(isTestSimulatorFlagEnabled(1)).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('rejects falsy values', () => { |
| 44 | + expect(isTestSimulatorFlagEnabled(false)).toBe(false); |
| 45 | + expect(isTestSimulatorFlagEnabled(undefined)).toBe(false); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('cleanupSimulatorsAfterTest', () => { |
| 50 | + beforeEach(() => { |
| 51 | + vi.clearAllMocks(); |
| 52 | + mockShutdownSimulator.mockResolvedValue(mockSuccess); |
| 53 | + mockDeleteSimulator.mockResolvedValue(mockSuccess); |
| 54 | + mockRunCommand.mockResolvedValue(mockSuccess); |
| 55 | + }); |
| 56 | + |
| 57 | + it('shuts down newly booted simulators and deletes BAZEL_TEST simulators', async () => { |
| 58 | + mockListSimulators |
| 59 | + .mockResolvedValueOnce({ |
| 60 | + command: mockSuccess, |
| 61 | + devices: [ |
| 62 | + { |
| 63 | + udid: 'NEW-1', |
| 64 | + name: 'BAZEL_TEST_iPhone 11_26.3_abc', |
| 65 | + state: 'Booted', |
| 66 | + runtime: 'iOS 26.3', |
| 67 | + isAvailable: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + udid: 'OLD-1', |
| 71 | + name: 'iPhone 15', |
| 72 | + state: 'Booted', |
| 73 | + runtime: 'iOS 18.0', |
| 74 | + isAvailable: true, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }) |
| 78 | + .mockResolvedValueOnce({ command: mockSuccess, devices: [] }); |
| 79 | + |
| 80 | + const result = await cleanupSimulatorsAfterTest(new Set(['OLD-1'])); |
| 81 | + |
| 82 | + expect(mockShutdownSimulator).toHaveBeenCalledWith('NEW-1'); |
| 83 | + expect(mockDeleteSimulator).toHaveBeenCalledWith('NEW-1'); |
| 84 | + expect(mockShutdownSimulator).not.toHaveBeenCalledWith('OLD-1'); |
| 85 | + expect(result.shutDown).toHaveLength(1); |
| 86 | + expect(result.deleted).toHaveLength(1); |
| 87 | + expect(result.quitSimulatorApp).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('leaves pre-booted non-BAZEL simulators alone', async () => { |
| 91 | + mockListSimulators |
| 92 | + .mockResolvedValueOnce({ |
| 93 | + command: mockSuccess, |
| 94 | + devices: [ |
| 95 | + { |
| 96 | + udid: 'OLD-1', |
| 97 | + name: 'iPhone 15', |
| 98 | + state: 'Booted', |
| 99 | + runtime: 'iOS 18.0', |
| 100 | + isAvailable: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }) |
| 104 | + .mockResolvedValueOnce({ |
| 105 | + command: mockSuccess, |
| 106 | + devices: [ |
| 107 | + { |
| 108 | + udid: 'OLD-1', |
| 109 | + name: 'iPhone 15', |
| 110 | + state: 'Booted', |
| 111 | + runtime: 'iOS 18.0', |
| 112 | + isAvailable: true, |
| 113 | + }, |
| 114 | + ], |
| 115 | + }); |
| 116 | + |
| 117 | + const result = await cleanupSimulatorsAfterTest(new Set(['OLD-1'])); |
| 118 | + |
| 119 | + expect(mockShutdownSimulator).not.toHaveBeenCalled(); |
| 120 | + expect(result.shutDown).toHaveLength(0); |
| 121 | + expect(result.quitSimulatorApp).toBe(false); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('formatSimulatorTestCleanup', () => { |
| 126 | + it('describes shutdown and delete actions', () => { |
| 127 | + const text = formatSimulatorTestCleanup({ |
| 128 | + shutDown: ['BAZEL_TEST_iPhone 11 (NEW-1)'], |
| 129 | + deleted: ['BAZEL_TEST_iPhone 11 (NEW-1)'], |
| 130 | + quitSimulatorApp: true, |
| 131 | + }); |
| 132 | + expect(text).toContain('Simulator shutdown'); |
| 133 | + expect(text).toContain('Simulator deleted'); |
| 134 | + expect(text).toContain('Simulator.app quit'); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('withTestSimulatorHooks', () => { |
| 139 | + beforeEach(() => { |
| 140 | + vi.useFakeTimers(); |
| 141 | + mockListSimulators.mockResolvedValue({ command: mockSuccess, devices: [] }); |
| 142 | + mockRunCommand.mockResolvedValue(mockSuccess); |
| 143 | + mockShutdownSimulator.mockResolvedValue(mockSuccess); |
| 144 | + mockDeleteSimulator.mockResolvedValue(mockSuccess); |
| 145 | + }); |
| 146 | + |
| 147 | + afterEach(() => { |
| 148 | + vi.useRealTimers(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('runs cleanup after the wrapped command when shutdown is enabled', async () => { |
| 152 | + mockListSimulators |
| 153 | + .mockResolvedValueOnce({ command: mockSuccess, devices: [] }) |
| 154 | + .mockResolvedValueOnce({ |
| 155 | + command: mockSuccess, |
| 156 | + devices: [ |
| 157 | + { |
| 158 | + udid: 'NEW-1', |
| 159 | + name: 'BAZEL_TEST_iPhone 11_26.3_abc', |
| 160 | + state: 'Booted', |
| 161 | + runtime: 'iOS 26.3', |
| 162 | + isAvailable: true, |
| 163 | + }, |
| 164 | + ], |
| 165 | + }) |
| 166 | + .mockResolvedValueOnce({ command: mockSuccess, devices: [] }); |
| 167 | + |
| 168 | + const wrapped = withTestSimulatorHooks( |
| 169 | + { shutdownSimulatorAfterTest: true }, |
| 170 | + async () => 'done', |
| 171 | + ); |
| 172 | + |
| 173 | + await expect(wrapped).resolves.toEqual({ |
| 174 | + result: 'done', |
| 175 | + cleanupSummary: expect.stringContaining('Simulator shutdown'), |
| 176 | + }); |
| 177 | + expect(mockShutdownSimulator).toHaveBeenCalledWith('NEW-1'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('polls minimize while the wrapped command runs', async () => { |
| 181 | + let resolveRun: (value: string) => void = () => undefined; |
| 182 | + const runPromise = new Promise<string>((resolve) => { |
| 183 | + resolveRun = resolve; |
| 184 | + }); |
| 185 | + |
| 186 | + const wrapped = withTestSimulatorHooks({ minimizeSimulator: true }, () => runPromise); |
| 187 | + const resultPromise = wrapped; |
| 188 | + |
| 189 | + await vi.advanceTimersByTimeAsync(2_000); |
| 190 | + expect(mockRunCommand).toHaveBeenCalled(); |
| 191 | + |
| 192 | + resolveRun('done'); |
| 193 | + await expect(resultPromise).resolves.toEqual({ result: 'done', cleanupSummary: undefined }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments