|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { promises as fs } from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'vitest'; |
| 6 | +import type { AndroidAdbExecutor } from '../adb-executor.ts'; |
| 7 | +import { createDeviceAdbExecutor } from '../adb-executor.ts'; |
| 8 | +import { getAndroidAppStateWithAdb, listAndroidAppsWithAdb } from '../app-helpers.ts'; |
| 9 | + |
| 10 | +async function withMockedAdbScript(script: string, run: () => Promise<void>): Promise<void> { |
| 11 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-android-app-helpers-')); |
| 12 | + const adbPath = path.join(tmpDir, 'adb'); |
| 13 | + await fs.writeFile(adbPath, script, 'utf8'); |
| 14 | + await fs.chmod(adbPath, 0o755); |
| 15 | + |
| 16 | + const previousPath = process.env.PATH; |
| 17 | + process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`; |
| 18 | + try { |
| 19 | + await run(); |
| 20 | + } finally { |
| 21 | + process.env.PATH = previousPath; |
| 22 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +test('listAndroidAppsWithAdb uses an injected executor', async () => { |
| 27 | + const calls: string[][] = []; |
| 28 | + const adb: AndroidAdbExecutor = async (args) => { |
| 29 | + calls.push(args); |
| 30 | + if (args.includes('query-activities')) { |
| 31 | + return { |
| 32 | + exitCode: 0, |
| 33 | + stdout: 'com.example.alpha/.MainActivity\ncom.example.beta/.MainActivity\n', |
| 34 | + stderr: '', |
| 35 | + }; |
| 36 | + } |
| 37 | + return { |
| 38 | + exitCode: 0, |
| 39 | + stdout: 'package:com.example.beta\n', |
| 40 | + stderr: '', |
| 41 | + }; |
| 42 | + }; |
| 43 | + |
| 44 | + const apps = await listAndroidAppsWithAdb(adb, { filter: 'user-installed', target: 'mobile' }); |
| 45 | + |
| 46 | + assert.deepEqual(apps, [{ package: 'com.example.beta', name: 'Beta' }]); |
| 47 | + assert.deepEqual(calls, [ |
| 48 | + [ |
| 49 | + 'shell', |
| 50 | + 'cmd', |
| 51 | + 'package', |
| 52 | + 'query-activities', |
| 53 | + '--brief', |
| 54 | + '-a', |
| 55 | + 'android.intent.action.MAIN', |
| 56 | + '-c', |
| 57 | + 'android.intent.category.LAUNCHER', |
| 58 | + ], |
| 59 | + ['shell', 'pm', 'list', 'packages', '-3'], |
| 60 | + ]); |
| 61 | +}); |
| 62 | + |
| 63 | +test('Android app helpers work with a local ADB provider', async () => { |
| 64 | + await withMockedAdbScript( |
| 65 | + [ |
| 66 | + '#!/bin/sh', |
| 67 | + 'if [ "$1" = "-s" ]; then', |
| 68 | + ' shift', |
| 69 | + ' shift', |
| 70 | + 'fi', |
| 71 | + 'if [ "$1" = "shell" ] && [ "$3" = "window" ]; then', |
| 72 | + ' echo "mCurrentFocus=Window{42 u0 com.example.app/.MainActivity}"', |
| 73 | + ' exit 0', |
| 74 | + 'fi', |
| 75 | + 'if [ "$1" = "shell" ] && [ "$3" = "package" ]; then', |
| 76 | + ' echo "com.example.app/.MainActivity"', |
| 77 | + ' exit 0', |
| 78 | + 'fi', |
| 79 | + 'exit 0', |
| 80 | + '', |
| 81 | + ].join('\n'), |
| 82 | + async () => { |
| 83 | + const adb = createDeviceAdbExecutor({ |
| 84 | + platform: 'android', |
| 85 | + id: 'emulator-5554', |
| 86 | + name: 'Pixel', |
| 87 | + kind: 'emulator', |
| 88 | + booted: true, |
| 89 | + }); |
| 90 | + |
| 91 | + const [apps, state] = await Promise.all([ |
| 92 | + listAndroidAppsWithAdb(adb, { target: 'mobile' }), |
| 93 | + getAndroidAppStateWithAdb(adb), |
| 94 | + ]); |
| 95 | + |
| 96 | + assert.deepEqual(apps, [{ package: 'com.example.app', name: 'Example' }]); |
| 97 | + assert.deepEqual(state, { package: 'com.example.app', activity: '.MainActivity' }); |
| 98 | + }, |
| 99 | + ); |
| 100 | +}); |
0 commit comments