|
1 | | -import { test } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, expect, test, vi } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | | -import { parseIosReadyPayload, resolveIosReadyHint } from '../device-ready.ts'; |
| 3 | +import type { DeviceInfo } from '../../utils/device.ts'; |
| 4 | + |
| 5 | +vi.mock('../../platforms/ios/index.ts', () => ({ |
| 6 | + ensureBootedSimulator: vi.fn(async () => {}), |
| 7 | +})); |
| 8 | +vi.mock('../../platforms/android/devices.ts', () => ({ |
| 9 | + waitForAndroidBoot: vi.fn(async () => {}), |
| 10 | +})); |
| 11 | + |
| 12 | +import { waitForAndroidBoot } from '../../platforms/android/devices.ts'; |
| 13 | +import { ensureBootedSimulator } from '../../platforms/ios/index.ts'; |
| 14 | +import { ANDROID_EMULATOR, IOS_SIMULATOR } from '../../__tests__/test-utils/index.ts'; |
| 15 | +import { |
| 16 | + clearDeviceReadyCacheForTests, |
| 17 | + DEVICE_READY_CACHE_TTL_MS, |
| 18 | + ensureDeviceReady, |
| 19 | + parseIosReadyPayload, |
| 20 | + resolveIosReadyHint, |
| 21 | +} from '../device-ready.ts'; |
| 22 | + |
| 23 | +const mockEnsureBootedSimulator = vi.mocked(ensureBootedSimulator); |
| 24 | +const mockWaitForAndroidBoot = vi.mocked(waitForAndroidBoot); |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + vi.useFakeTimers(); |
| 28 | + vi.setSystemTime(new Date('2026-04-01T10:00:00.000Z')); |
| 29 | + clearDeviceReadyCacheForTests(); |
| 30 | + mockEnsureBootedSimulator.mockReset(); |
| 31 | + mockEnsureBootedSimulator.mockResolvedValue(undefined); |
| 32 | + mockWaitForAndroidBoot.mockReset(); |
| 33 | + mockWaitForAndroidBoot.mockResolvedValue(undefined); |
| 34 | +}); |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + clearDeviceReadyCacheForTests(); |
| 38 | + vi.useRealTimers(); |
| 39 | +}); |
| 40 | + |
| 41 | +test('ensureDeviceReady caches successful simulator readiness checks', async () => { |
| 42 | + const device: DeviceInfo = { ...IOS_SIMULATOR, simulatorSetPath: '/tmp/simset-a' }; |
| 43 | + |
| 44 | + await ensureDeviceReady(device); |
| 45 | + await ensureDeviceReady({ ...device }); |
| 46 | + |
| 47 | + expect(mockEnsureBootedSimulator).toHaveBeenCalledTimes(1); |
| 48 | +}); |
| 49 | + |
| 50 | +test('ensureDeviceReady includes simulator set path in the cache key', async () => { |
| 51 | + await ensureDeviceReady({ ...IOS_SIMULATOR, simulatorSetPath: '/tmp/simset-a' }); |
| 52 | + await ensureDeviceReady({ ...IOS_SIMULATOR, simulatorSetPath: '/tmp/simset-b' }); |
| 53 | + |
| 54 | + expect(mockEnsureBootedSimulator).toHaveBeenCalledTimes(2); |
| 55 | +}); |
| 56 | + |
| 57 | +test('ensureDeviceReady expires cached readiness checks after the ttl', async () => { |
| 58 | + await ensureDeviceReady(ANDROID_EMULATOR); |
| 59 | + vi.setSystemTime(new Date(Date.now() + DEVICE_READY_CACHE_TTL_MS - 1)); |
| 60 | + await ensureDeviceReady({ ...ANDROID_EMULATOR }); |
| 61 | + vi.setSystemTime(new Date(Date.now() + 1)); |
| 62 | + await ensureDeviceReady({ ...ANDROID_EMULATOR }); |
| 63 | + |
| 64 | + expect(mockWaitForAndroidBoot).toHaveBeenCalledTimes(2); |
| 65 | +}); |
| 66 | + |
| 67 | +test('ensureDeviceReady does not cache failed readiness checks', async () => { |
| 68 | + mockEnsureBootedSimulator.mockRejectedValueOnce(new Error('boot failed')); |
| 69 | + |
| 70 | + await expect(ensureDeviceReady(IOS_SIMULATOR)).rejects.toThrow('boot failed'); |
| 71 | + await ensureDeviceReady(IOS_SIMULATOR); |
| 72 | + |
| 73 | + expect(mockEnsureBootedSimulator).toHaveBeenCalledTimes(2); |
| 74 | +}); |
4 | 75 |
|
5 | 76 | test('parseIosReadyPayload reads tunnelState from direct connectionProperties', () => { |
6 | 77 | const parsed = parseIosReadyPayload({ |
|
0 commit comments