|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import type { OpenApplicationInput } from '@agent-device/contracts/application-lifecycle-runtime'; |
| 3 | +import type { Interactor } from '@agent-device/contracts/interactor-types'; |
| 4 | +import type { PlatformRuntimeHost } from '@agent-device/contracts/platform-runtime-operations'; |
| 5 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 6 | +import { bindAppleApplicationLifecycle } from './lifecycle.ts'; |
| 7 | +import { platformRuntimeHostFixture } from './runtime.fixtures.ts'; |
| 8 | + |
| 9 | +const device: DeviceInfo = { |
| 10 | + platform: 'apple', |
| 11 | + appleOs: 'ios', |
| 12 | + id: 'ios-device', |
| 13 | + name: 'iPhone', |
| 14 | + kind: 'device', |
| 15 | + target: 'mobile', |
| 16 | + booted: true, |
| 17 | + iosPhysicalDeviceBackend: 'coredevice', |
| 18 | +}; |
| 19 | + |
| 20 | +test.each(['coredevice', 'xctest'] as const)( |
| 21 | + 'retains a physical iOS runner through relaunch and resets its target with the %s backend', |
| 22 | + async (iosPhysicalDeviceBackend) => { |
| 23 | + const selectedDevice = { ...device, iosPhysicalDeviceBackend }; |
| 24 | + const signal = new AbortController().signal; |
| 25 | + const events: string[] = []; |
| 26 | + const interactor = { |
| 27 | + close: vi.fn(async () => { |
| 28 | + events.push('close'); |
| 29 | + }), |
| 30 | + open: vi.fn(async () => { |
| 31 | + events.push('open'); |
| 32 | + }), |
| 33 | + } as unknown as Interactor; |
| 34 | + const baseHost = platformRuntimeHostFixture(); |
| 35 | + const stopRunnerSession = vi.fn(async () => { |
| 36 | + events.push('stop'); |
| 37 | + }); |
| 38 | + const prewarmRunnerSession = vi.fn(async () => { |
| 39 | + events.push('prewarm'); |
| 40 | + }); |
| 41 | + const notifyRunnerAppRelaunched = vi.fn(async () => { |
| 42 | + events.push('reset'); |
| 43 | + }); |
| 44 | + const host = { |
| 45 | + ...baseHost, |
| 46 | + localInteractors: { resolve: async () => interactor }, |
| 47 | + appleApplications: { |
| 48 | + ...baseHost.appleApplications, |
| 49 | + stopRunnerSession, |
| 50 | + prewarmRunnerSession, |
| 51 | + notifyRunnerAppRelaunched, |
| 52 | + }, |
| 53 | + } as unknown as PlatformRuntimeHost; |
| 54 | + const lifecycle = bindAppleApplicationLifecycle({ host, device: selectedDevice, signal }); |
| 55 | + |
| 56 | + await lifecycle.openApplication(openInput()); |
| 57 | + |
| 58 | + expect(events).toEqual(['close', 'open', 'prewarm', 'reset']); |
| 59 | + expect(stopRunnerSession).not.toHaveBeenCalled(); |
| 60 | + expect(notifyRunnerAppRelaunched).toHaveBeenCalledWith(selectedDevice, {}, signal); |
| 61 | + }, |
| 62 | +); |
| 63 | + |
| 64 | +test.each(['ipados', 'tvos', 'visionos'] as const)( |
| 65 | + 'preserves runner restart semantics for a physical %s target', |
| 66 | + async (appleOs) => { |
| 67 | + const selectedDevice = { ...device, appleOs }; |
| 68 | + const signal = new AbortController().signal; |
| 69 | + const events: string[] = []; |
| 70 | + const interactor = { |
| 71 | + close: vi.fn(async () => { |
| 72 | + events.push('close'); |
| 73 | + }), |
| 74 | + open: vi.fn(async () => { |
| 75 | + events.push('open'); |
| 76 | + }), |
| 77 | + } as unknown as Interactor; |
| 78 | + const baseHost = platformRuntimeHostFixture(); |
| 79 | + const stopRunnerSession = vi.fn(async () => { |
| 80 | + events.push('stop'); |
| 81 | + }); |
| 82 | + const prewarmRunnerSession = vi.fn(async () => { |
| 83 | + events.push('prewarm'); |
| 84 | + }); |
| 85 | + const notifyRunnerAppRelaunched = vi.fn(async () => { |
| 86 | + events.push('reset'); |
| 87 | + }); |
| 88 | + const host = { |
| 89 | + ...baseHost, |
| 90 | + localInteractors: { resolve: async () => interactor }, |
| 91 | + appleApplications: { |
| 92 | + ...baseHost.appleApplications, |
| 93 | + stopRunnerSession, |
| 94 | + prewarmRunnerSession, |
| 95 | + notifyRunnerAppRelaunched, |
| 96 | + }, |
| 97 | + } as unknown as PlatformRuntimeHost; |
| 98 | + const lifecycle = bindAppleApplicationLifecycle({ host, device: selectedDevice, signal }); |
| 99 | + |
| 100 | + await lifecycle.openApplication(openInput()); |
| 101 | + |
| 102 | + expect(events).toEqual(['stop', 'close', 'open', 'prewarm']); |
| 103 | + expect(notifyRunnerAppRelaunched).not.toHaveBeenCalled(); |
| 104 | + }, |
| 105 | +); |
| 106 | + |
| 107 | +test('discards a retained physical iOS runner when relaunch fails and preserves the failure', async () => { |
| 108 | + const signal = new AbortController().signal; |
| 109 | + const events: string[] = []; |
| 110 | + const relaunchFailure = new Error('app failed to reopen'); |
| 111 | + const interactor = { |
| 112 | + close: vi.fn(async () => { |
| 113 | + events.push('close'); |
| 114 | + }), |
| 115 | + open: vi.fn(async () => { |
| 116 | + events.push('open'); |
| 117 | + throw relaunchFailure; |
| 118 | + }), |
| 119 | + } as unknown as Interactor; |
| 120 | + const baseHost = platformRuntimeHostFixture(); |
| 121 | + const stopRunnerSession = vi.fn(async () => { |
| 122 | + events.push('stop'); |
| 123 | + throw new Error('runner cleanup failed'); |
| 124 | + }); |
| 125 | + const notifyRunnerAppRelaunched = vi.fn(async () => { |
| 126 | + events.push('reset'); |
| 127 | + }); |
| 128 | + const host = { |
| 129 | + ...baseHost, |
| 130 | + localInteractors: { resolve: async () => interactor }, |
| 131 | + appleApplications: { |
| 132 | + ...baseHost.appleApplications, |
| 133 | + stopRunnerSession, |
| 134 | + notifyRunnerAppRelaunched, |
| 135 | + }, |
| 136 | + } as unknown as PlatformRuntimeHost; |
| 137 | + const lifecycle = bindAppleApplicationLifecycle({ host, device, signal }); |
| 138 | + |
| 139 | + await expect(lifecycle.openApplication(openInput())).rejects.toBe(relaunchFailure); |
| 140 | + |
| 141 | + expect(events).toEqual(['close', 'open', 'stop']); |
| 142 | + expect(stopRunnerSession).toHaveBeenCalledWith(device.id); |
| 143 | + expect(notifyRunnerAppRelaunched).not.toHaveBeenCalled(); |
| 144 | +}); |
| 145 | + |
| 146 | +function openInput(): OpenApplicationInput { |
| 147 | + return { |
| 148 | + target: 'com.example.app', |
| 149 | + positionals: ['com.example.app'], |
| 150 | + appBundleId: 'com.example.app', |
| 151 | + surface: 'app', |
| 152 | + hasExistingSession: true, |
| 153 | + relaunch: true, |
| 154 | + prewarmRunnerBeforeOpen: false, |
| 155 | + enableTestIme: false, |
| 156 | + stateDir: '/tmp/agent-device-lifecycle-test', |
| 157 | + runtimeHints: {}, |
| 158 | + execution: {}, |
| 159 | + }; |
| 160 | +} |
0 commit comments