|
| 1 | +// Proves the exact androidAdbExecutor reference reaches the session handler |
| 2 | +// through the neutral providerScope, and that an empty scope forwards none. |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { test, vi } from 'vitest'; |
| 5 | + |
| 6 | +const handleSessionCommandsMock = vi.fn(async (_params: unknown) => ({ ok: true, data: {} })); |
| 7 | +vi.mock('../handlers/session.ts', () => ({ |
| 8 | + handleSessionCommands: handleSessionCommandsMock, |
| 9 | +})); |
| 10 | + |
| 11 | +import { INTERNAL_COMMANDS } from '../../command-catalog.ts'; |
| 12 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 13 | +import { runRequestHandlerChain } from '../request-handler-chain.ts'; |
| 14 | +import { makeIosSession } from '../../__tests__/test-utils/index.ts'; |
| 15 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 16 | +import { |
| 17 | + unavailableBindDevice, |
| 18 | + unavailableBindExactDevice, |
| 19 | +} from './test-device-runtime-gateway.ts'; |
| 20 | +import { createScreenRecordingAdmissionLedger } from '../screen-recording-admission-ledger.ts'; |
| 21 | +import type { DaemonRequest } from '../types.ts'; |
| 22 | +import type { AndroidAdbExecutor } from '../../platforms/android/adb-executor.ts'; |
| 23 | + |
| 24 | +function makeRequest(command: string, sessionName: string): DaemonRequest { |
| 25 | + return { |
| 26 | + command, |
| 27 | + token: 'test-token', |
| 28 | + session: sessionName, |
| 29 | + positionals: [], |
| 30 | + flags: {}, |
| 31 | + meta: { requestId: `req-${command}` }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function baseChainParams(sessionName: string) { |
| 36 | + const sessionStore = makeSessionStore('agent-device-provider-scope-'); |
| 37 | + sessionStore.set(sessionName, makeIosSession(sessionName)); |
| 38 | + return { |
| 39 | + req: makeRequest(INTERNAL_COMMANDS.runtime, sessionName), |
| 40 | + sessionName, |
| 41 | + logPath: '/tmp/agent-device-provider-scope.log', |
| 42 | + sessionStore, |
| 43 | + leaseRegistry: new LeaseRegistry(), |
| 44 | + invoke: async () => ({ ok: true, data: {} }) as const, |
| 45 | + bindDevice: unavailableBindDevice, |
| 46 | + bindExactDevice: unavailableBindExactDevice, |
| 47 | + reconcileOrphanedDeviceClaim: async () => ({ |
| 48 | + status: 'retained' as const, |
| 49 | + reason: 'test-no-recovery', |
| 50 | + }), |
| 51 | + screenRecordingAdmissionLedger: createScreenRecordingAdmissionLedger(), |
| 52 | + requestScope: { |
| 53 | + signal: new AbortController().signal, |
| 54 | + diagnostics: { emit: () => {} }, |
| 55 | + progress: { report: () => {} }, |
| 56 | + }, |
| 57 | + retainDeviceExecutionLock: async () => {}, |
| 58 | + throwIfCanceled: () => {}, |
| 59 | + contextFromFlags: () => ({ logPath: '/tmp/agent-device-provider-scope.log' }), |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +test('the android adb executor from the generic provider scope reaches the session handler by the exact same reference', async () => { |
| 64 | + handleSessionCommandsMock.mockClear(); |
| 65 | + const androidAdbExecutor: AndroidAdbExecutor = async () => ({ |
| 66 | + stdout: '', |
| 67 | + stderr: '', |
| 68 | + exitCode: 0, |
| 69 | + }); |
| 70 | + |
| 71 | + await runRequestHandlerChain({ |
| 72 | + ...baseChainParams('provider-scope-test'), |
| 73 | + providerScope: { androidAdbExecutor }, |
| 74 | + }); |
| 75 | + |
| 76 | + assert.equal(handleSessionCommandsMock.mock.calls.length, 1); |
| 77 | + const forwardedParams = handleSessionCommandsMock.mock.calls[0]?.[0] as { |
| 78 | + androidAdbExecutor?: AndroidAdbExecutor; |
| 79 | + }; |
| 80 | + assert.equal(forwardedParams.androidAdbExecutor, androidAdbExecutor); |
| 81 | +}); |
| 82 | + |
| 83 | +test('an empty provider scope forwards no android adb executor to the session handler', async () => { |
| 84 | + handleSessionCommandsMock.mockClear(); |
| 85 | + |
| 86 | + await runRequestHandlerChain({ |
| 87 | + ...baseChainParams('provider-scope-empty'), |
| 88 | + providerScope: {}, |
| 89 | + }); |
| 90 | + |
| 91 | + assert.equal(handleSessionCommandsMock.mock.calls.length, 1); |
| 92 | + const forwardedParams = handleSessionCommandsMock.mock.calls[0]?.[0] as { |
| 93 | + androidAdbExecutor?: AndroidAdbExecutor; |
| 94 | + }; |
| 95 | + assert.equal(forwardedParams.androidAdbExecutor, undefined); |
| 96 | +}); |
0 commit comments