Repository navigation
Expand file tree
/
Copy pathsession-device-resolution.ts
More file actions
93 lines (86 loc) · 3.33 KB
/
Copy pathsession-device-resolution.ts
File metadata and controls
93 lines (86 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { isIosFamily, type DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import { isActiveProviderDevice } from './provider-device-admission.ts';
import { appleSessionObservation } from '../platform-runtime-apple-resources.ts';
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
import type { DaemonRequest, DaemonResponse } from './daemon-request.ts';
import type { SessionState } from './session-state.ts';
import {
hasDeviceSelectionInput,
hasExplicitDeviceSelector,
} from './device/device-selector-intent.ts';
import { listSessionSelectorConflicts } from './session-selector.ts';
import { errorResponse } from '@agent-device/kernel/contracts';
export function requireSessionOrExplicitSelector(
command: string,
session: SessionState | undefined,
flags: DaemonRequest['flags'] | undefined,
): DaemonResponse | null {
if (session || hasDeviceSelectionInput(flags)) {
return null;
}
return errorResponse(
'INVALID_ARGS',
`${command} requires an active session or an explicit device selector (e.g. --platform ios).`,
);
}
export function hasExplicitSessionFlag(flags: DaemonRequest['flags'] | undefined): boolean {
return typeof flags?.session === 'string' && flags.session.trim().length > 0;
}
export async function resolveCommandDevice(params: {
session: SessionState | undefined;
flags: DaemonRequest['flags'] | undefined;
androidAvdSelection?: 'running-only' | 'include-stopped';
}): Promise<DeviceInfo> {
const shouldUseExplicitIdentity = hasExplicitDeviceSelector(params.flags);
const device =
shouldUseExplicitIdentity || !params.session
? await resolveTargetDevice(params.flags ?? {}, {
androidAvdSelection: params.androidAvdSelection,
})
: await refreshSessionDeviceIfNeeded(params.session.device);
return device;
}
export async function refreshSessionDeviceIfNeeded(device: DeviceInfo): Promise<DeviceInfo> {
if (isActiveProviderDevice(device)) {
return device;
}
if (!isIosFamily(device) || device.kind !== 'simulator') {
return device;
}
if (process.platform !== 'darwin') {
return device;
}
// A live XCUITest runner session is attached to this exact UDID, which
// proves the simulator still exists and is booted — the two facts the
// ~0.7s re-resolve inventory listing exists to establish.
if ((await appleSessionObservation.observeRunnerSession(device.id))?.alive) {
return { ...device, booted: true };
}
const exactSelector: NonNullable<DaemonRequest['flags']> = {
platform: 'ios',
target: device.target,
udid: device.id,
...(device.simulatorSetPath ? { iosSimulatorDeviceSet: device.simulatorSetPath } : {}),
};
try {
return await resolveTargetDevice(exactSelector);
} catch (error) {
if (!(error instanceof AppError) || error.code !== 'DEVICE_NOT_FOUND') {
throw error;
}
}
return await resolveTargetDevice({
platform: 'ios',
target: device.target,
device: device.name,
...(device.simulatorSetPath ? { iosSimulatorDeviceSet: device.simulatorSetPath } : {}),
});
}
export function selectorTargetsSessionDevice(
flags: DaemonRequest['flags'] | undefined,
session: SessionState | undefined,
): boolean {
if (!session) return false;
return listSessionSelectorConflicts(session, flags).length === 0;
}