diff --git a/src/driver.ts b/src/driver.ts index dda0a38..9ae9dd1 100644 --- a/src/driver.ts +++ b/src/driver.ts @@ -291,6 +291,17 @@ export class TestingBotDriver implements MobilewrightDriver { }; this.allocated.set(sessionId, session); this.everAllocated.set(sessionId, session); + if (this.options.sessionPerTest) { + // Workers rotating sessions on this slot need its device filter, which + // mobilewright's connect() does not forward. + this.runLog.append({ + type: 'slot', + deviceId: sessionId, + deviceType: criteria.deviceType, + osVersion: criteria.osVersion, + deviceNamePattern: criteria.deviceNamePattern, + }); + } this.keepalive.start(sessionId); debug('allocated session %s (%s %s) — https://testingbot.com/members/tests/%s', sessionId, session.deviceName, session.osVersion, sessionId); @@ -369,7 +380,7 @@ export class TestingBotDriver implements MobilewrightDriver { } this.rotatedDeviceIds.add(poolDeviceId); } - sessionId ??= await this.startFreshSession(config); + sessionId ??= await this.startFreshSession(config, poolDeviceId); this.active = { sessionId, platform: config.platform, deviceType: config.deviceType, connectedAt: Date.now() }; setActiveDriver(this); debug('connected to session %s (slot %s)', sessionId, poolDeviceId); @@ -415,12 +426,17 @@ export class TestingBotDriver implements MobilewrightDriver { } /** Create a new hub session for this slot from the connect() config. */ - private async startFreshSession(config: ConnectionConfig): Promise { + private async startFreshSession(config: ConnectionConfig, poolDeviceId: string): Promise { + // mobilewright's connect() passes only platform/deviceId/deviceType, so + // the slot's deviceName/osVersion filter comes from the coordinator's + // allocate() record. Values on the config win when a caller provides them. + const slot = this.runLog.slotFor(poolDeviceId); + const configName = config.deviceName instanceof RegExp ? config.deviceName.source : config.deviceName; const criteria: AllocationCriteria = { platform: config.platform, - deviceType: config.deviceType, - osVersion: config.osVersion, - deviceNamePattern: config.deviceName instanceof RegExp ? config.deviceName.source : config.deviceName, + deviceType: config.deviceType ?? (slot?.deviceType as AllocationCriteria['deviceType']), + osVersion: config.osVersion ?? slot?.osVersion, + deviceNamePattern: configName ?? slot?.deviceNamePattern, }; let pinned: PinnedDevice | undefined; if (criteria.deviceType === 'real') { diff --git a/src/run-log.ts b/src/run-log.ts index 65c3d7d..36036dc 100644 --- a/src/run-log.ts +++ b/src/run-log.ts @@ -41,7 +41,22 @@ export interface RecordingRecord { output: string; } -type Record_ = SessionRecord | AppRecord | RecordingRecord; +/** + * The criteria a pool slot was allocated with. mobilewright's connect() only + * forwards platform/deviceId/deviceType, so a worker rotating sessions + * (sessionPerTest) looks the slot's deviceName/osVersion filter up here — + * without it every rotated session would land on an arbitrary device. + */ +export interface SlotRecord { + type: 'slot'; + /** The pool deviceId — the WebDriver session id allocate() returned. */ + deviceId: string; + deviceType?: string; + osVersion?: string; + deviceNamePattern?: string; +} + +type Record_ = SessionRecord | AppRecord | RecordingRecord | SlotRecord; export interface MergedSession { sessionId: string; @@ -130,6 +145,10 @@ export class RunLog { return this.read().filter((r): r is RecordingRecord => r.type === 'recording'); } + slotFor(deviceId: string): SlotRecord | undefined { + return this.read().find((r): r is SlotRecord => r.type === 'slot' && r.deviceId === deviceId); + } + appUrlsFor(platform: string, deviceType: string | undefined): string[] | undefined { const apps = this.read().filter((r): r is AppRecord => r.type === 'app'); return ( diff --git a/test/fake-hub/fake-hub.ts b/test/fake-hub/fake-hub.ts index 7f8bdac..b3a6b7b 100644 --- a/test/fake-hub/fake-hub.ts +++ b/test/fake-hub/fake-hub.ts @@ -219,6 +219,8 @@ export class FakeHub { if (path === '/v1/devices/available' || path === '/v1/devices') { return rest({ devices: [ + // Listed first so an unfiltered real-device pick lands on it. + { id: 2, name: 'Pixel', platform_name: 'Android', version: '7.1' }, { id: 1, name: 'Pixel 8', platform_name: 'Android', version: '14' }, { id: 3, name: 'iPhone 15', platform_name: 'iOS', version: '17.4' }, ], diff --git a/test/integration/pool-lifecycle.test.ts b/test/integration/pool-lifecycle.test.ts index baab2e0..6117a90 100644 --- a/test/integration/pool-lifecycle.test.ts +++ b/test/integration/pool-lifecycle.test.ts @@ -315,6 +315,31 @@ describe('sessionPerTest against FakeHub', () => { expect(hub.testUpdates.get(fresh.id)).toMatchObject({ 'test[success]': '1' }); }); + it('keeps the slot\'s deviceName/osVersion filter on rotated sessions', async () => { + // mobilewright's connect() forwards only platform/deviceId/deviceType, so + // the rotated session must recover the filter from the allocate() record — + // otherwise it lands on any real device (e.g. an old "Pixel" on 7.1). + const opts = { ...options(), apps: { android: 'tb://prebuilt-android' } as const }; + const coordinator = new TestingBotDriver(opts); + await coordinator.prepare(); + const allocated = await coordinator.allocate( + { platform: 'android', deviceType: 'real', deviceNamePattern: '^(Pixel 8|Galaxy S23)$', osVersion: '>=12' }, + new Set(), + ); + expect(hub.liveSessions()[0]!.capabilities['appium:deviceName']).toBe('Pixel 8'); + + const worker = new TestingBotDriver(opts); + await worker.connect({ platform: 'android', deviceId: allocated.deviceId, deviceType: 'real' }); + await worker.disconnect(); + await worker.connect({ platform: 'android', deviceId: allocated.deviceId, deviceType: 'real' }); + const rotated = hub.liveSessions()[0]!; + expect(rotated.id).not.toBe(allocated.deviceId); + expect(rotated.capabilities['appium:deviceName']).toBe('Pixel 8'); + expect(rotated.capabilities['appium:platformVersion']).toBe('14'); + await worker.disconnect(); + await coordinator.dispose(); + }); + it('never reports one test\'s failure on the session another test ran in', async () => { // The reported defect, end to end through the real run file: two per-test // sessions, the first test passes and the second fails. FakeHub sessions