|
| 1 | +import type { ApplicationLifecycleRuntimeOperations } from '@agent-device/contracts/application-lifecycle-runtime'; |
| 2 | +import type { DeviceBinding } from '@agent-device/contracts/platform-runtime'; |
| 3 | +import type { |
| 4 | + PlatformRuntimeHost, |
| 5 | + PlatformRuntimeOperations, |
| 6 | +} from '@agent-device/contracts/platform-runtime-operations'; |
| 7 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 8 | +import type { TargetShutdownResult } from '@agent-device/contracts/device'; |
| 9 | +// The lifecycle seams (dispatch spy, readiness pauses, fixture interactor) have ONE owner: the |
| 10 | +// daemon's canned lifecycle fixture. This real-composition fixture builds the REAL root gateway |
| 11 | +// and platform application tools on top of the same seams, so every lane observes one spy set. |
| 12 | +export { |
| 13 | + awaitFixtureReadiness, |
| 14 | + discoverReadyAndroidEmulators, |
| 15 | + dispatchApplicationLifecycleEffect, |
| 16 | +} from '../../daemon/__tests__/application-lifecycle-runtime-fixture.ts'; |
| 17 | +import { |
| 18 | + applicationLifecycleFixtureInteractor, |
| 19 | + awaitFixtureReadiness, |
| 20 | + discoverReadyAndroidEmulators, |
| 21 | +} from '../../daemon/__tests__/application-lifecycle-runtime-fixture.ts'; |
| 22 | +import type { Interactor, RunnerContext } from '@agent-device/contracts/interactor-types'; |
| 23 | +import { createAndroidApplicationTools } from '../../platform-runtime-android-application-tools.ts'; |
| 24 | +import { createAppleApplicationTools } from '../../platform-runtime-apple-application-tools.ts'; |
| 25 | +import { createComposedPlatformRuntimeGateway } from '../../platform-runtime-gateway.ts'; |
| 26 | +import { platformRuntimeModules } from '../../platform-runtime.ts'; |
| 27 | + |
| 28 | +export async function applicationLifecycleRuntimeFixture( |
| 29 | + device: DeviceInfo, |
| 30 | + signal: AbortSignal = new AbortController().signal, |
| 31 | + shutdownTarget: (device: DeviceInfo) => Promise<TargetShutdownResult> = async () => ({ |
| 32 | + success: true, |
| 33 | + exitCode: 0, |
| 34 | + stdout: '', |
| 35 | + stderr: '', |
| 36 | + }), |
| 37 | +): Promise<ApplicationLifecycleRuntimeOperations> { |
| 38 | + const gateway = createComposedPlatformRuntimeGateway({ |
| 39 | + modules: platformRuntimeModules, |
| 40 | + loadHost: async () => fixtureHost(device, shutdownTarget), |
| 41 | + }); |
| 42 | + const binding = await gateway.bind({ |
| 43 | + device, |
| 44 | + intent: { kind: 'ordinary' }, |
| 45 | + scope: { signal, diagnostics: { emit: () => {} }, progress: { report: () => {} } }, |
| 46 | + }); |
| 47 | + return lifecycleOperations(binding); |
| 48 | +} |
| 49 | + |
| 50 | +function lifecycleOperations( |
| 51 | + binding: DeviceBinding<PlatformRuntimeOperations>, |
| 52 | +): ApplicationLifecycleRuntimeOperations { |
| 53 | + const operations = binding.operations; |
| 54 | + const keys = [ |
| 55 | + 'resolveOpenTarget', |
| 56 | + 'prepareApplicationOpen', |
| 57 | + 'openApplication', |
| 58 | + 'applyRuntimeHints', |
| 59 | + 'clearRuntimeHints', |
| 60 | + 'closeApplication', |
| 61 | + 'finalizeApplicationClose', |
| 62 | + 'prepareAppleRunner', |
| 63 | + 'configureProviderPortReverse', |
| 64 | + ] as const satisfies readonly (keyof ApplicationLifecycleRuntimeOperations)[]; |
| 65 | + const selected = Object.fromEntries( |
| 66 | + keys.flatMap((key) => { |
| 67 | + const operation = operations[key]; |
| 68 | + return operation ? [[key, operation]] : []; |
| 69 | + }), |
| 70 | + ); |
| 71 | + return Object.freeze(selected) as ApplicationLifecycleRuntimeOperations; |
| 72 | +} |
| 73 | + |
| 74 | +function fixtureHost( |
| 75 | + readyDevice: DeviceInfo, |
| 76 | + shutdownTarget: (device: DeviceInfo) => Promise<TargetShutdownResult>, |
| 77 | +): PlatformRuntimeHost { |
| 78 | + let simulatorState = readyDevice.booted === false ? 'Shutdown' : 'Booted'; |
| 79 | + const localInteractors = Object.freeze({ |
| 80 | + resolve: async (device: DeviceInfo, runner: RunnerContext): Promise<Interactor> => |
| 81 | + applicationLifecycleFixtureInteractor(device, runner), |
| 82 | + }); |
| 83 | + return { |
| 84 | + localInteractors, |
| 85 | + appleApplications: createAppleApplicationTools(), |
| 86 | + androidApplications: createAndroidApplicationTools(), |
| 87 | + deviceShutdown: Object.freeze({ |
| 88 | + apple: Object.freeze({ |
| 89 | + shutdownTarget: async (device: DeviceInfo) => await shutdownTarget(device), |
| 90 | + }), |
| 91 | + android: Object.freeze({ |
| 92 | + shutdownTarget: async (device: DeviceInfo) => await shutdownTarget(device), |
| 93 | + }), |
| 94 | + close: Object.freeze({ |
| 95 | + canShutdownTarget: async (device: DeviceInfo) => |
| 96 | + (device.platform === 'apple' && device.kind === 'simulator') || |
| 97 | + (device.platform === 'android' && device.kind === 'emulator'), |
| 98 | + shutdownTarget: async (device: DeviceInfo) => await shutdownTarget(device), |
| 99 | + }), |
| 100 | + }), |
| 101 | + // Readiness is package-owned; these handler fixtures exercise lifecycle dispatch, so the |
| 102 | + // narrow ports report an already-ready device instead of booting anything. |
| 103 | + deviceReadiness: Object.freeze({ |
| 104 | + applePhysical: Object.freeze({ |
| 105 | + ensureConnected: async (_device: DeviceInfo, signal: AbortSignal) => { |
| 106 | + signal.throwIfAborted(); |
| 107 | + }, |
| 108 | + }), |
| 109 | + appleAutomation: Object.freeze({ |
| 110 | + keepHot: () => undefined, |
| 111 | + markBooted: () => {}, |
| 112 | + wasRecentlyObservedBooted: async () => false, |
| 113 | + }), |
| 114 | + androidEmulator: Object.freeze({ |
| 115 | + discover: async (_request: unknown, signal: AbortSignal) => { |
| 116 | + signal.throwIfAborted(); |
| 117 | + await awaitFixtureReadiness(readyDevice); |
| 118 | + return await discoverReadyAndroidEmulators(readyDevice); |
| 119 | + }, |
| 120 | + launch: () => 1, |
| 121 | + terminate: async () => undefined, |
| 122 | + }), |
| 123 | + }), |
| 124 | + // Readiness reads the device's own boot state, so a `booted: false` fixture still exercises |
| 125 | + // the cold-boot path (and its runner-cache prewarm hook) before settling. |
| 126 | + appleTools: Object.freeze({ |
| 127 | + isXcrunAvailable: async () => true, |
| 128 | + run: async (request: Readonly<{ args: readonly string[] }>) => { |
| 129 | + await awaitFixtureReadiness(readyDevice); |
| 130 | + if (request.args.includes('boot')) { |
| 131 | + simulatorState = 'Booted'; |
| 132 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 133 | + } |
| 134 | + return { |
| 135 | + stdout: JSON.stringify({ |
| 136 | + devices: { fixture: [{ udid: readyDevice.id, state: simulatorState }] }, |
| 137 | + }), |
| 138 | + stderr: '', |
| 139 | + exitCode: 0, |
| 140 | + }; |
| 141 | + }, |
| 142 | + }), |
| 143 | + commands: Object.freeze({ |
| 144 | + which: async (executable: string) => executable, |
| 145 | + run: async () => ({ stdout: '1', stderr: '', exitCode: 0 }), |
| 146 | + }), |
| 147 | + toolchains: Object.freeze({ prepare: async () => undefined }), |
| 148 | + clock: Object.freeze({ |
| 149 | + now: () => Date.now(), |
| 150 | + sleep: async () => undefined, |
| 151 | + }), |
| 152 | + processTransports: Object.freeze({ |
| 153 | + resolve: async () => ({ mode: 'local' as const }), |
| 154 | + }), |
| 155 | + networkTransports: Object.freeze({ |
| 156 | + resolve: async () => ({ mode: 'local' as const }), |
| 157 | + }), |
| 158 | + screenRecording: Object.freeze({ |
| 159 | + apple: Object.freeze({ |
| 160 | + availability: async () => ({ |
| 161 | + available: false as const, |
| 162 | + hint: 'Screen recording is outside this lifecycle fixture.', |
| 163 | + }), |
| 164 | + }), |
| 165 | + android: Object.freeze({ resolve: async () => ({}) }), |
| 166 | + web: Object.freeze({ resolve: async () => undefined }), |
| 167 | + }), |
| 168 | + audioProbe: Object.freeze({ |
| 169 | + hostCapture: Object.freeze({ |
| 170 | + info: Object.freeze({ |
| 171 | + source: 'system-audio' as const, |
| 172 | + backend: 'fixture', |
| 173 | + sourceCount: 0, |
| 174 | + notes: () => [], |
| 175 | + }), |
| 176 | + start: async () => { |
| 177 | + throw new Error('Audio probe is outside this lifecycle fixture.'); |
| 178 | + }, |
| 179 | + inspectProcess: async () => 'missing' as const, |
| 180 | + terminateProcess: async () => 'already-missing' as const, |
| 181 | + }), |
| 182 | + web: Object.freeze({ resolve: async () => undefined }), |
| 183 | + ownedProcesses: Object.freeze({ replace: () => {}, clear: () => {} }), |
| 184 | + }), |
| 185 | + } as unknown as PlatformRuntimeHost; |
| 186 | +} |
0 commit comments