Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/kernel/src/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,46 @@ test('a shut-down tie marks every candidate shut down and an unknown state omits
},
);
});

const unbootedIphones: DeviceInfo[] = ['iPhone 16', 'iPhone 17', 'iPhone 17 Pro'].map(
(name, i) => ({
...shutDownIphone,
id: `sim-iphone-${i}`,
name,
}),
);

const bootedIpad: DeviceInfo = {
...shutDownIphone,
id: 'sim-ipad-booted',
name: 'iPad Pro 13-inch (M5)',
appleOs: 'ipados',
booted: true,
};

test('the only booted simulator wins over shut-down iPhones, even when it is an iPad', async () => {
const selected = await resolveDevice([...unbootedIphones, bootedIpad], { platform: 'ios' });
assert.equal(selected.id, bootedIpad.id);
});

test('two booted iPads with shut-down iPhones still refuse to choose', async () => {
const secondBootedIpad: DeviceInfo = { ...bootedIpad, id: 'sim-ipad-booted-2', name: 'iPad Air' };
await assert.rejects(
resolveDevice([...unbootedIphones, bootedIpad, secondBootedIpad], { platform: 'ios' }),
(error: unknown) => error instanceof AppError && error.code === 'AMBIGUOUS_MATCH',
);
});

test('with nothing booted, shut-down iPhones still tie ahead of a shut-down iPad', async () => {
const shutDownIpad: DeviceInfo = { ...bootedIpad, booted: false };
await assert.rejects(
resolveDevice([...unbootedIphones, shutDownIpad], { platform: 'ios' }),
(error: unknown) =>
error instanceof AppError &&
error.code === 'AMBIGUOUS_MATCH' &&
JSON.stringify(error.details?.devices) ===
JSON.stringify(
unbootedIphones.map((device) => ({ id: device.id, name: device.name, booted: false })),
),
);
});
21 changes: 16 additions & 5 deletions packages/kernel/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,18 @@ function preferVirtualCandidates(candidates: DeviceInfo[]): DeviceInfo[] {
return virtual.length > 0 ? virtual : candidates;
}

/**
* Each kind's iPhones and iPads share a tier. Its only booted member wins, because booting one says
* which handheld is meant; with none or several booted, an iPhone still ranks above an iPad.
*/
function candidatesWithBestAppleRank(candidates: DeviceInfo[]): DeviceInfo[] {
const bestRank = Math.min(...candidates.map((device) => appleDeviceSelectionRank(device)));
return candidates.filter((device) => appleDeviceSelectionRank(device) === bestRank);
const tierRank = (device: DeviceInfo) => appleDeviceSelectionRank(device, true);
const bestTierRank = Math.min(...candidates.map(tierRank));
const tier = candidates.filter((device) => tierRank(device) === bestTierRank);
const booted = tier.filter((device) => device.booted);
if (booted.length === 1) return booted;
const bestRank = Math.min(...tier.map((device) => appleDeviceSelectionRank(device)));
return tier.filter((device) => appleDeviceSelectionRank(device) === bestRank);
}

/** Tie-break policies `boot --pick` accepts. */
Expand Down Expand Up @@ -943,12 +952,14 @@ function compareAppleDevicesForSelection<TDevice extends DeviceInfo>(
);
}

function appleDeviceSelectionRank(device: DeviceInfo): number {
/** With `ipadAsIphone`, each kind's iPads share its iPhones' rank (the handheld tier). */
function appleDeviceSelectionRank(device: DeviceInfo, ipadAsIphone = false): number {
// A watch answers `--platform ios` only when no other Apple device can.
if (device.appleOs === 'watchos') return 15;
if (device.kind === 'simulator') return appleTargetSelectionRank(device, 0, 1, 2, 3);
const ipadStep = ipadAsIphone ? 0 : 1;
if (device.kind === 'simulator') return appleTargetSelectionRank(device, 0, ipadStep, 2, 3);
if (device.kind === 'device' && isApplePlatform(device.platform) && !isMacOs(device))
return appleTargetSelectionRank(device, 10, 11, 12, 13);
return appleTargetSelectionRank(device, 10, 10 + ipadStep, 12, 13);
return 14;
}

Expand Down
Loading