diff --git a/packages/kernel/src/device-selector-flags.test.ts b/packages/kernel/src/device-selector-flags.test.ts index 877c1adfab..a46e49b03b 100644 --- a/packages/kernel/src/device-selector-flags.test.ts +++ b/packages/kernel/src/device-selector-flags.test.ts @@ -51,9 +51,69 @@ test('matching selector/platform pairs still resolve', async () => { assert.equal((await resolveDevice([APPLE], { platform: 'ios', udid: 'SIM-001' })).id, 'SIM-001'); }); -test('an unspecified platform keeps the existing device-not-found behavior', async () => { +// With no explicit --platform there is no pairing to reject: the value reaches resolution and +// fails as "No Apple device with UDID ..." — literally true, and silent about the flag that does +// take it. When the value identifies an observed device through the OTHER flag, the failure names +// that flag, read through deviceIdentityFlag rather than a new inline pairing. + +test('--udid with no --platform names --serial when the value is an attached serial', async () => { const error = await resolveError([ANDROID], { udid: 'emulator-5580' }); assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.match(error.message, /No Apple device with UDID emulator-5580/); + assert.match( + String(error.details?.hint ?? ''), + /emulator-5580 identifies "Pixel 7" here\. Did you mean --serial emulator-5580\?/, + ); +}); + +test('--serial with no --platform names --udid when the value is an attached UDID', async () => { + const error = await resolveError([APPLE], { serial: 'SIM-001' }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.match( + String(error.details?.hint ?? ''), + /SIM-001 identifies "iPhone 16" here\. Did you mean --udid SIM-001\?/, + ); +}); + +test('a serial-side hint names --serial for harmonyos and vega devices too', async () => { + const VEGA: DeviceInfo = { + platform: 'vega', + target: 'mobile', + id: 'vvd-42', + name: 'Vega VVD', + kind: 'device', + booted: true, + }; + const error = await resolveError([VEGA], { udid: 'vvd-42' }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.match(String(error.details?.hint ?? ''), /Did you mean --serial vvd-42\?/); +}); + +test('an identity value that matches no candidate stays hint-free', async () => { + // The hint is grounded in observed identity: a value nothing carries earns no flag answer, + // exactly like the --device name hint below. + const udid = await resolveError([ANDROID], { udid: 'not-attached' }); + assert.equal(udid.code, 'DEVICE_NOT_FOUND'); + assert.equal(udid.details?.hint, undefined); + const serial = await resolveError([APPLE], { serial: 'not-attached' }); + assert.equal(serial.code, 'DEVICE_NOT_FOUND'); + assert.equal(serial.details?.hint, undefined); +}); + +test('a web-device id passed to --udid stays hint-free', async () => { + // --udid resolves only Apple devices and --serial only serial-addressable ones; a platform with + // no identity flag has no flag that could take the value, so a hint naming one is unrunnable. + const WEB: DeviceInfo = { + platform: 'web', + target: 'desktop', + id: 'agent-browser-chrome', + name: 'Agent Browser Chrome', + kind: 'device', + booted: true, + }; + const error = await resolveError([WEB], { udid: 'agent-browser-chrome' }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.equal(error.details?.hint, undefined); }); // Resolution fails before any device is touched, so the refusal proves nothing was dispatched. @@ -106,6 +166,52 @@ test('an id passed to --device on a platform with no identity flag stays hint-fr assert.equal(error.details?.hint, undefined); }); +// `--device` already folds case, underscores, and whitespace when comparing names; quote STYLE is +// the fold it lacks, and renamed devices carry curly quotes a typed request spells straight. When +// that one fold matches exactly one candidate, the failure names it. + +test('a name differing only in curly quotes is answered with the candidate name', async () => { + const CURLED: DeviceInfo = { ...APPLE, id: 'SIM-002', name: 'iPhone “16”' }; + const error = await resolveError([CURLED, ANDROID], { deviceName: 'iPhone "16"' }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.match(error.message, /No device named iPhone "16"/); + assert.match( + String(error.details?.hint ?? ''), + /A device is named "iPhone “16”", which differs only in punctuation\. Did you mean --device "iPhone “16”"\?/, + ); +}); + +test('a curly single-quote name folds onto a straight-quote request', async () => { + const CURLED: DeviceInfo = { ...ANDROID, id: 'emulator-5581', name: 'Pixel ‘7’' }; + const error = await resolveError([CURLED], { deviceName: "Pixel '7'" }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.match(String(error.details?.hint ?? ''), /Did you mean --device "Pixel ‘7’"\?/); +}); + +test('a name two candidates share under the quote fold earns no hint', async () => { + // Both candidates fold onto the straight-quote request — one spelled with U+2018 quotes, one + // with U+2019 — so the request cannot tell them apart. The request keeps its quotes: a + // quote-less request would fold onto neither candidate and make this case vacuous. + const CURLED_A: DeviceInfo = { ...ANDROID, id: 'emulator-5581', name: 'Pixel ‘7‘' }; + const CURLED_B: DeviceInfo = { ...ANDROID, id: 'emulator-5582', name: 'Pixel ’7’' }; + const error = await resolveError([CURLED_A, CURLED_B], { deviceName: "Pixel '7'" }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.equal(error.details?.hint, undefined); +}); + +test('a name the quote fold cannot reach stays hint-free', async () => { + const error = await resolveError([APPLE], { deviceName: 'iPhone 17' }); + assert.equal(error.code, 'DEVICE_NOT_FOUND'); + assert.equal(error.details?.hint, undefined); +}); + +test('an id passed to --device still wins over the closest-name fold', async () => { + // The identity answer is the sharper one: the value belongs to a different flag entirely, so it + // outranks a punctuation-level name guess. + const error = await resolveError([APPLE], { deviceName: 'SIM-001' }); + assert.match(String(error.details?.hint ?? ''), /is the id of "iPhone 16", not its name/); +}); + test('an unknown --device value gets the generic hint, even when it looks like a UDID', async () => { // The hint is grounded in observed identity: only a candidate's actual id earns the // wrong-flag answer. A UDID-shaped value that no listed device carries stays a plain diff --git a/packages/kernel/src/device.ts b/packages/kernel/src/device.ts index 9ba9d4ddee..7c7df3cb8e 100644 --- a/packages/kernel/src/device.ts +++ b/packages/kernel/src/device.ts @@ -655,7 +655,9 @@ function assertSelectorFlagMatchesPlatform(selector: DeviceSelector): void { function resolveAppleDeviceByUdid(candidates: DeviceInfo[], udid: string): DeviceInfo { const match = candidates.find((device) => device.id === udid && isApplePlatform(device.platform)); if (!match) { + const hint = deviceNotFoundHint(candidates, udid, 'udid'); throw new AppError('DEVICE_NOT_FOUND', `No Apple device with UDID ${udid}`, { + ...(hint === undefined ? {} : { hint }), dispatched: 'no', }); } @@ -667,7 +669,9 @@ function resolveDeviceBySerial(candidates: DeviceInfo[], selector: DeviceSelecto (device) => device.id === selector.serial && isSerialAddressablePlatform(device.platform), ); if (!match) { + const hint = deviceNotFoundHint(candidates, selector.serial ?? '', 'serial'); throw new AppError('DEVICE_NOT_FOUND', serialDeviceNotFoundMessage(selector), { + ...(hint === undefined ? {} : { hint }), dispatched: 'no', }); } @@ -681,7 +685,7 @@ function resolveDeviceByName( if (!deviceName) return undefined; const match = candidates.find((device) => matchesDeviceNameSelector(device, deviceName)); if (!match) { - const hint = deviceIdentityMistakenForNameHint(candidates, deviceName); + const hint = deviceNotFoundHint(candidates, deviceName); throw new AppError( 'DEVICE_NOT_FOUND', `No device named ${deviceName}`, @@ -691,30 +695,24 @@ function resolveDeviceByName( return match; } -/** - * `--device` takes a device NAME, and `--udid`/`--serial` take a device IDENTITY. Passing an - * identity to `--device` reached name resolution and answered "No device named - * 204BFFD9-9644-4830-B2C1-1B946597A07C" (#2064) — literally true, and unactionable: it names - * neither the flag that does take that value nor the fact that one exists. It is the same class of - * mistake `assertSelectorFlagMatchesPlatform` already answers for a mismatched identity flag, so - * answer it the same way: name the flag the value belongs to. Only an observed identity earns the - * hint — the candidates' own ids, with the flag derived from that device's platform; guessing from - * the value's shape would have to reimplement every platform's identity syntax here. A platform - * with no identity flag at all (web, linux) earns no hint either: `--udid` resolves only Apple - * devices, so naming it would send the user to a flag that provably cannot work. - */ -function deviceIdentityMistakenForNameHint( - candidates: DeviceInfo[], - deviceName: string, -): string | undefined { - const identityMatch = candidates.find((device) => device.id === deviceName); - if (!identityMatch) return undefined; - const flag = deviceIdentityFlag(identityMatch.platform); - if (!flag) return undefined; - return ( - `${deviceName} is the id of ${JSON.stringify(identityMatch.name)}, not its name. ` + - `Did you mean --${flag} ${deviceName}?` - ); +/** The one not-found answer for all three selector sites: the flag an observed device carries the value under, or for `--device` the one candidate name a quote fold away (#2064). */ +function deviceNotFoundHint(candidates: DeviceInfo[], value: string, flag?: DeviceIdentityFlag) { + const identity = candidates.find((device) => device.id === value); + const identityFlag = identity && deviceIdentityFlag(identity.platform); + if (identity && identityFlag && identityFlag !== flag) { + return flag === undefined + ? `${value} is the id of ${JSON.stringify(identity.name)}, not its name. Did you mean --${identityFlag} ${value}?` + : `${value} identifies ${JSON.stringify(identity.name)} here. Did you mean --${identityFlag} ${value}?`; + } + const requested = quoteFoldDeviceName(value); + const close = candidates.filter((device) => quoteFoldDeviceName(device.name) === requested); + const only = flag === undefined && close.length === 1 ? close[0] : undefined; + if (!only) return undefined; + return `A device is named ${JSON.stringify(only.name)}, which differs only in punctuation. Did you mean --device ${JSON.stringify(only.name)}?`; +} + +function quoteFoldDeviceName(value: string): string { + return normalizeDeviceName(value.replaceAll(/[‘’‚‛]/g, "'").replaceAll(/[“”„‟]/g, '"')); } export type DeviceIdentityFlag = 'udid' | 'serial';