Skip to content

Commit 855bd0f

Browse files
committed
refactor(daemon): fake the application-lifecycle runtime-binding port in daemon suites
The daemon lane now binds a canned lifecycle fixture built on the contracts direct-lifecycle binding plus port spies (readiness pause, android discovery, dispatch, hint apply/clear, runner prepare). The real composed fixture moves to the root-owned daemon-composition lane, built on the same seam set so both lanes observe one spy family. Hint-observation suites retarget to the fixture's port spies (assertion bodies unchanged); suites whose assertions pin what the real apple/android runtime modules do under composition relocate: session-relaunch-close and session-open-url-prewarm whole-file, and the close-finalization, shutdown-gating, recording-finalization, post-open observation, runtime-hint lazy-evaluation, and android IME cancellation cases as extracted composition siblings. The gateway and session-command harness take an injectable lifecycle-operations loader so composition suites build the real gateway behind the same handler harness. Scan for daemon -> non-package src/ edges: 0.
1 parent 51ae6cc commit 855bd0f

30 files changed

Lines changed: 1469 additions & 962 deletions
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setHarnessLifecycleOperationsLoader } from '../../../daemon/handlers/__tests__/session-command-harness.ts';
2+
import { applicationLifecycleRuntimeFixture } from '../application-lifecycle-runtime-fixture.ts';
3+
4+
// Composition suites bind the REAL composed lifecycle (root gateway + apple/android application
5+
// tools) behind the daemon session-command harness. Import this module before any module that
6+
// captures the harness's `handleSessionCommands`: vitest evaluates this file's module graph first,
7+
// and every test file gets its own module instance, so the install cannot leak into daemon files.
8+
setHarnessLifecycleOperationsLoader(applicationLifecycleRuntimeFixture);

‎src/__tests__/daemon-composition/handlers/session-close-runner-retention.test.ts‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ vi.mock('@agent-device/platform-android/mechanics', async (importOriginal) => {
5454
return { ...actual, resolveAndroidPackageForOpen: vi.fn(async () => undefined) };
5555
});
5656

57-
import {
58-
createRequestHandler,
59-
lifecycleDeviceRuntimeGateway,
60-
} from '../../../daemon/__tests__/test-device-runtime-gateway.ts';
57+
import { createRequestHandler } from '../../../daemon/__tests__/test-device-runtime-gateway.ts';
58+
import { realLifecycleDeviceRuntimeGateway } from '../lifecycle-runtime-gateway.ts';
6159
import {
6260
dispatchApplicationLifecycleEffect,
6361
awaitFixtureReadiness,
6462
discoverReadyAndroidEmulators,
65-
} from '../../../daemon/__tests__/application-lifecycle-runtime-fixture.ts';
63+
} from '../application-lifecycle-runtime-fixture.ts';
6664
import {
6765
stopIosRunnerSession,
6866
releaseIosRunnerOnClose,
@@ -102,7 +100,7 @@ function createHandler(sessionStore: ReturnType<typeof makeSessionStore>) {
102100
token: 'test-token',
103101
sessionStore,
104102
leaseRegistry: new LeaseRegistry(),
105-
deviceRuntimeGateway: lifecycleDeviceRuntimeGateway,
103+
deviceRuntimeGateway: realLifecycleDeviceRuntimeGateway,
106104
deviceInventoryGateways: createTestDeviceInventoryGateways(),
107105
platformResourceCleanup,
108106
trackDownloadableArtifact: () => 'artifact-id',
@@ -137,11 +135,11 @@ test('close on iOS simulator session retains runner and deletes the session', as
137135
sessionName,
138136
makeSession(sessionName, {
139137
device: {
140-
platform: 'apple',
141-
id: 'sim-1',
142-
name: 'iPhone 17 Pro',
143-
kind: 'simulator',
144-
booted: true,
138+
platform: 'apple',
139+
id: 'sim-1',
140+
name: 'iPhone 17 Pro',
141+
kind: 'simulator',
142+
booted: true,
145143
},
146144
appName: 'com.example.app',
147145
}),
@@ -162,12 +160,12 @@ test('close on iOS simulator with scoped simulator set retains runner and delete
162160
sessionName,
163161
makeSession(sessionName, {
164162
device: {
165-
platform: 'apple',
166-
id: 'sim-1',
167-
name: 'iPhone 17 Pro',
168-
kind: 'simulator',
169-
booted: true,
170-
simulatorSetPath: '/tmp/tenant-a/simulator-set',
163+
platform: 'apple',
164+
id: 'sim-1',
165+
name: 'iPhone 17 Pro',
166+
kind: 'simulator',
167+
booted: true,
168+
simulatorSetPath: '/tmp/tenant-a/simulator-set',
171169
},
172170
appName: 'com.example.app',
173171
}),
@@ -187,11 +185,11 @@ test('close <app> on iOS simulator retains runner while terminating app', async
187185
sessionName,
188186
makeSession(sessionName, {
189187
device: {
190-
platform: 'apple',
191-
id: 'sim-1',
192-
name: 'iPhone 17 Pro',
193-
kind: 'simulator',
194-
booted: true,
188+
platform: 'apple',
189+
id: 'sim-1',
190+
name: 'iPhone 17 Pro',
191+
kind: 'simulator',
192+
booted: true,
195193
},
196194
appName: 'com.example.app',
197195
}),

0 commit comments

Comments
 (0)