diff --git a/packages/platform-android/src/inventory.test.ts b/packages/platform-android/src/inventory.test.ts index 67282f9b3e..46f5e2e528 100644 --- a/packages/platform-android/src/inventory.test.ts +++ b/packages/platform-android/src/inventory.test.ts @@ -199,6 +199,88 @@ test('Android inventory classifies a device by has-feature amazon.hardware.fire_ assert.deepEqual(featureListProbeCalls, []); }); +test('Android inventory keeps a device whose feature probe times out from failing discovery of another', async () => { + const diagnostics: unknown[] = []; + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') { + return result( + 'List of devices attached\nemulator-5554 device model:Busy\nemulator-5556 device model:Idle\n', + ); + } + if (key.includes('emulator-5554') && key.includes('pm\0list\0features')) { + throw new AppError('COMMAND_FAILED', 'adb timed out after 10000ms', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + const devices = await createAndroidInventory(host).discover( + { androidAvdSelection: 'running-only' }, + { ...scope, diagnostics: { emit: (event) => diagnostics.push(event) } }, + ); + + assert.deepEqual( + devices.map((device) => [device.id, device.target]), + [ + ['emulator-5554', undefined], + ['emulator-5556', 'mobile'], + ], + ); + assert.deepEqual(diagnostics, [ + { + level: 'warn', + phase: 'android_inventory_target_probe_failed', + data: { serial: 'emulator-5554', adbFailure: 'timeout' }, + }, + ]); +}); + +test('Android inventory still aborts when the request is cancelled during the feature probe', async () => { + const controller = new AbortController(); + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') return result('List of devices attached\nemulator-5554 device\n'); + if (key.includes('pm\0list\0features')) { + controller.abort(); + throw new AppError('COMMAND_FAILED', 'aborted', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + await assert.rejects( + createAndroidInventory(host).discover( + { androidAvdSelection: 'running-only' }, + { ...scope, signal: controller.signal }, + ), + (error: unknown) => error instanceof AppError && error.code === 'COMMAND_FAILED', + ); +}); + +test('Android inventory fails when the feature probe of the requested serial times out', async () => { + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') return result('List of devices attached\nemulator-5554 device\n'); + if (key.includes('pm\0list\0features')) { + throw new AppError('COMMAND_FAILED', 'adb timed out after 10000ms', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + await assert.rejects( + createAndroidInventory(host).discover( + { serial: 'emulator-5554', androidAvdSelection: 'running-only' }, + scope, + ), + (error: unknown) => error instanceof AppError && error.details?.adbFailure === 'timeout', + ); +}); + test('Android inventory fails closed when adb is unavailable', async () => { const host = createHost(async () => result(''), { adb: undefined }); await assert.rejects( diff --git a/packages/platform-android/src/inventory.ts b/packages/platform-android/src/inventory.ts index c43285fa25..ddde88e63c 100644 --- a/packages/platform-android/src/inventory.ts +++ b/packages/platform-android/src/inventory.ts @@ -43,6 +43,8 @@ type AndroidInventoryContext = Readonly<{ host: DeviceInventoryHostFor<'android'>; adb: string; emulator?: string; + /** The serial the request names; its probe failures fail discovery. */ + selectedSerial?: string; scope: PlatformRequestScope; }>; @@ -75,6 +77,7 @@ async function discoverAndroidDevices( host, adb, emulator, + selectedSerial: request.serial?.trim() || undefined, scope, }; const allowlist = resolveSerialAllowlist(request); @@ -136,7 +139,7 @@ async function probeRunningDevice( const [name, booted, target, description] = await Promise.all([ resolveDeviceName(context, entry), isBooted(context, entry.serial), - resolveTarget(context, entry.serial), + resolveTargetOrUnknown(context, entry.serial), readDeviceDescription(context, entry.serial), ]); return { @@ -219,6 +222,35 @@ async function isBooted(context: AndroidInventoryContext, serial: string): Promi } } +/** + * A busy device that times out a target probe stays listed with an unknown target, so it cannot + * fail a request that selects another device. The selected serial and every other failure still + * fail discovery, so a selected device never runs with a demoted target. + */ +async function resolveTargetOrUnknown( + context: AndroidInventoryContext, + serial: string, +): Promise<'mobile' | 'tv' | undefined> { + try { + return await resolveTarget(context, serial); + } catch (error) { + const appError = asAppError(error); + if ( + context.scope.signal.aborted || + serial === context.selectedSerial || + appError.details?.adbFailure !== 'timeout' + ) { + throw error; + } + context.scope.diagnostics.emit({ + level: 'warn', + phase: 'android_inventory_target_probe_failed', + data: { serial, adbFailure: 'timeout' }, + }); + return undefined; + } +} + async function resolveTarget( context: AndroidInventoryContext, serial: string,