|
| 1 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 2 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 3 | +import { IOS_SYSTEM_SURFACE_HOSTS } from '@agent-device/contracts/ios-system-surface'; |
| 4 | + |
| 5 | +const runner = vi.hoisted(() => ({ |
| 6 | + readRunnerSessionLiveness: vi.fn(), |
| 7 | + runAppleRunnerCommand: vi.fn(), |
| 8 | +})); |
| 9 | +vi.mock('./core/runner-client.ts', () => runner); |
| 10 | + |
| 11 | +import { createSystemSurfaceForegroundProbe } from './system-surface-foreground.ts'; |
| 12 | + |
| 13 | +const ios = { |
| 14 | + platform: 'apple', |
| 15 | + appleOs: 'ios', |
| 16 | + id: 'ios-1', |
| 17 | + name: 'iPhone', |
| 18 | + kind: 'simulator', |
| 19 | + target: 'mobile', |
| 20 | + booted: true, |
| 21 | +} as const satisfies DeviceInfo; |
| 22 | + |
| 23 | +const [safari, passbook] = IOS_SYSTEM_SURFACE_HOSTS.map((host) => host.bundleId); |
| 24 | + |
| 25 | +beforeEach(() => { |
| 26 | + runner.readRunnerSessionLiveness.mockReset(); |
| 27 | + runner.runAppleRunnerCommand.mockReset(); |
| 28 | + runner.readRunnerSessionLiveness.mockReturnValue({ sessionId: 'runner-1', liveness: 'ready' }); |
| 29 | +}); |
| 30 | + |
| 31 | +function statesByHost(states: Record<string, unknown>) { |
| 32 | + runner.runAppleRunnerCommand.mockImplementation(async (_device, command) => ({ |
| 33 | + applicationState: states[command.appBundleId], |
| 34 | + })); |
| 35 | +} |
| 36 | + |
| 37 | +function probe() { |
| 38 | + return createSystemSurfaceForegroundProbe()(ios, { |
| 39 | + signal: new AbortController().signal, |
| 40 | + requestId: 'req-1', |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +test('every registered host off screen is background, read only from the ready runner session', async () => { |
| 45 | + statesByHost({ [safari!]: 'runningBackgroundSuspended', [passbook!]: 'notRunning' }); |
| 46 | + |
| 47 | + await expect(probe()).resolves.toBe('background'); |
| 48 | + expect( |
| 49 | + runner.runAppleRunnerCommand.mock.calls.map(([, command, options]) => [command, options]), |
| 50 | + ).toEqual( |
| 51 | + [safari, passbook].map((bundleId) => [ |
| 52 | + { command: 'appState', appBundleId: bundleId }, |
| 53 | + expect.objectContaining({ expectedRunnerSessionId: 'runner-1', requestId: 'req-1' }), |
| 54 | + ]), |
| 55 | + ); |
| 56 | +}); |
| 57 | + |
| 58 | +test('one background host does not hide another one presenting', async () => { |
| 59 | + statesByHost({ [safari!]: 'runningBackground', [passbook!]: 'runningForeground' }); |
| 60 | + |
| 61 | + await expect(probe()).resolves.toBe('foreground'); |
| 62 | +}); |
| 63 | + |
| 64 | +test.for([ |
| 65 | + ['no runner session', null], |
| 66 | + ['a runner still starting', { sessionId: 'runner-1', liveness: 'starting' }], |
| 67 | +] as const)('%s is unknown and sends no command', async ([, liveness]) => { |
| 68 | + runner.readRunnerSessionLiveness.mockReturnValue(liveness); |
| 69 | + |
| 70 | + await expect(probe()).resolves.toBe('unknown'); |
| 71 | + expect(runner.runAppleRunnerCommand).not.toHaveBeenCalled(); |
| 72 | +}); |
| 73 | + |
| 74 | +test.for([ |
| 75 | + ['an unknown state', 'unknown'], |
| 76 | + ['an undeclared state', 'suspendedSomehow'], |
| 77 | + ['no state', undefined], |
| 78 | +] as const)('%s is unknown', async ([, state]) => { |
| 79 | + statesByHost({ [safari!]: state, [passbook!]: 'notRunning' }); |
| 80 | + |
| 81 | + await expect(probe()).resolves.toBe('unknown'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('a failed read is unknown, and a cancelled request still cancels', async () => { |
| 85 | + runner.runAppleRunnerCommand.mockRejectedValue(new Error('runner unreachable')); |
| 86 | + await expect(probe()).resolves.toBe('unknown'); |
| 87 | + |
| 88 | + const controller = new AbortController(); |
| 89 | + controller.abort(new Error('request cancelled')); |
| 90 | + await expect( |
| 91 | + createSystemSurfaceForegroundProbe()(ios, { signal: controller.signal }), |
| 92 | + ).rejects.toThrow('request cancelled'); |
| 93 | +}); |
0 commit comments