diff --git a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts index 5f2378ff6c..0939d25d3d 100644 --- a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts +++ b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts @@ -278,6 +278,40 @@ test('resolveTargetDevice reuses request-scoped device resolution cache for iden assert.equal(mockListAppleDevices.mock.calls.length, 1); }); +test('resolveTargetDevice reuses the request cache after the inventory configures the host env', async () => { + const env: Record = { PATH: '/usr/bin' }; + mockListAppleDevices.mockImplementation(async () => { + env.ANDROID_HOME = '/sdk'; + env.PATH = `/sdk/platform-tools:${env.PATH}`; + return [bootedSimulator]; + }); + + await withResolveTargetDeviceCacheScope(async () => { + const first = await resolveTargetDevice({ udid: 'sim-2' }, { env }); + const inventoryReads = mockListAppleDevices.mock.calls.length; + const second = await resolveTargetDevice({ udid: 'sim-2' }, { env }); + + assert.equal(first.id, 'sim-2'); + assert.equal(second.id, 'sim-2'); + assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads); + }); +}); + +test('resolveTargetDevice request cache key separates env-derived device allowlists', async () => { + mockListAppleDevices.mockResolvedValue([bootedSimulator]); + + await withResolveTargetDeviceCacheScope(async () => { + await resolveTargetDevice({ udid: 'sim-2' }, { env: {} }); + const inventoryReads = mockListAppleDevices.mock.calls.length; + await resolveTargetDevice( + { udid: 'sim-2' }, + { env: { AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST: 'emulator-5554' } }, + ); + + assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads * 2); + }); +}); + test('resolveTargetDevice request cache key separates device selectors', async () => { mockListAppleDevices.mockResolvedValue([simulator, bootedSimulator]); diff --git a/packages/device-selection/src/dispatch-resolve.ts b/packages/device-selection/src/dispatch-resolve.ts index 321536b560..e686abf428 100644 --- a/packages/device-selection/src/dispatch-resolve.ts +++ b/packages/device-selection/src/dispatch-resolve.ts @@ -309,8 +309,10 @@ function buildResolveTargetDeviceCacheKey( ): string { // The app target only informs the first device choice. Once a request has // chosen a device, every later resolution must reuse that same device even - // when dispatch has no app target to pass back through this seam. + // when dispatch has no app target to pass back through this seam. The request + // holds every value selection derives from env, so env itself stays out. const { + env: _env, appleSimulatorAppTarget: _appleSimulatorAppTarget, appDeviceTarget: _appDeviceTarget, webSession: _webSession,