|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import crypto from 'node:crypto'; |
| 3 | +import { promises as fs } from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { beforeEach, test } from 'vitest'; |
| 6 | +import './test-utils/android-host-test-setup.ts'; |
| 7 | +import { AppError } from '@agent-device/kernel/errors'; |
| 8 | +import { |
| 9 | + ensureAndroidSnapshotHelper, |
| 10 | + resetAndroidSnapshotHelperInstallCache, |
| 11 | +} from '../snapshot-helper-install.ts'; |
| 12 | +import type { AndroidAdbExecutor, AndroidAdbProvider } from '../adb-executor.ts'; |
| 13 | +import type { AndroidAdbInstaller } from '../adb-transport.ts'; |
| 14 | +import type { AndroidSnapshotHelperManifest } from '../snapshot-helper-types.ts'; |
| 15 | +import { mkdtempForTest } from './test-utils/tmp-dir.ts'; |
| 16 | + |
| 17 | +const manifest: AndroidSnapshotHelperManifest = { |
| 18 | + name: 'android-snapshot-helper', |
| 19 | + version: '0.13.3', |
| 20 | + apkUrl: null, |
| 21 | + sha256: 'a'.repeat(64), |
| 22 | + packageName: 'com.callstack.agentdevice.snapshothelper', |
| 23 | + versionCode: 13003, |
| 24 | + instrumentationRunner: 'com.callstack.agentdevice.snapshothelper/.SnapshotInstrumentation', |
| 25 | + minSdk: 23, |
| 26 | + targetSdk: 36, |
| 27 | + outputFormat: 'uiautomator-xml', |
| 28 | + statusProtocol: 'android-snapshot-helper-v1', |
| 29 | + installArgs: ['install', '-r'], |
| 30 | +}; |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + resetAndroidSnapshotHelperInstallCache(); |
| 34 | +}); |
| 35 | + |
| 36 | +test('a snapshot helper install timeout points at the OEM install dialog', async () => { |
| 37 | + const apkPath = await writeHelperApk('snapshot-helper-install-timeout-'); |
| 38 | + const adbProvider = missingPackageProvider(async () => { |
| 39 | + throw adbInstallTimeout(); |
| 40 | + }); |
| 41 | + |
| 42 | + await assert.rejects( |
| 43 | + () => |
| 44 | + ensureAndroidSnapshotHelper({ |
| 45 | + adb: adbProvider.exec, |
| 46 | + adbProvider, |
| 47 | + artifact: { apkPath, manifest: { ...manifest, sha256: sha256Text('helper-apk') } }, |
| 48 | + deviceKey: 'android:R52N30PZXBT', |
| 49 | + }), |
| 50 | + (error) => { |
| 51 | + const details = (error as AppError).details; |
| 52 | + assert.equal(details?.adbFailure, 'timeout'); |
| 53 | + assert.equal(details?.androidSnapshotHelperInstallFailure, true); |
| 54 | + assert.match(String(details?.hint), /install-confirmation dialog/); |
| 55 | + assert.doesNotMatch(String(details?.hint), /wedged/); |
| 56 | + return true; |
| 57 | + }, |
| 58 | + ); |
| 59 | +}); |
| 60 | + |
| 61 | +test('an exec-shaped helper install timeout names the dialog, not a wedged adb server', async () => { |
| 62 | + // The reporter's shape: a local USB device whose provider has no semantic installer, so the |
| 63 | + // install crosses the legacy exec fallback. |
| 64 | + const apkPath = await writeHelperApk('snapshot-helper-install-timeout-exec-'); |
| 65 | + const exec: AndroidAdbExecutor = async (args) => { |
| 66 | + if (args.includes('--show-versioncode')) { |
| 67 | + return { exitCode: 1, stdout: '', stderr: 'not found' }; |
| 68 | + } |
| 69 | + if (args[0] === 'install') throw adbInstallTimeout(); |
| 70 | + throw new Error(`unexpected adb call: ${args.join(' ')}`); |
| 71 | + }; |
| 72 | + |
| 73 | + await assert.rejects( |
| 74 | + () => |
| 75 | + ensureAndroidSnapshotHelper({ |
| 76 | + adb: exec, |
| 77 | + adbProvider: exec, |
| 78 | + artifact: { apkPath, manifest: { ...manifest, sha256: sha256Text('helper-apk') } }, |
| 79 | + deviceKey: 'android:R52N30PZXBT', |
| 80 | + }), |
| 81 | + (error) => { |
| 82 | + const details = (error as AppError).details; |
| 83 | + assert.equal(details?.adbFailure, 'timeout'); |
| 84 | + assert.match(String(details?.hint), /install-confirmation dialog/); |
| 85 | + return true; |
| 86 | + }, |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | +function adbInstallTimeout(): AppError { |
| 91 | + // An unattended first install on ColorOS: adb blocks on the system install-confirmation dialog |
| 92 | + // and the exec layer kills the command, so stdout/stderr stay empty. |
| 93 | + return new AppError('COMMAND_FAILED', 'adb timed out after 30000ms', { |
| 94 | + stdout: '', |
| 95 | + stderr: '', |
| 96 | + exitCode: 1, |
| 97 | + timeoutMs: 30_000, |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +async function writeHelperApk(prefix: string): Promise<string> { |
| 102 | + const tmpDir = await mkdtempForTest(prefix); |
| 103 | + const apkPath = path.join(tmpDir, 'helper.apk'); |
| 104 | + await fs.writeFile(apkPath, 'helper-apk'); |
| 105 | + return apkPath; |
| 106 | +} |
| 107 | + |
| 108 | +function missingPackageProvider(install: AndroidAdbInstaller): AndroidAdbProvider { |
| 109 | + return { |
| 110 | + exec: async (args) => { |
| 111 | + if (args.includes('--show-versioncode')) { |
| 112 | + return { exitCode: 1, stdout: '', stderr: 'not found' }; |
| 113 | + } |
| 114 | + throw new Error(`unexpected adb call: ${args.join(' ')}`); |
| 115 | + }, |
| 116 | + install, |
| 117 | + }; |
| 118 | +} |
| 119 | + |
| 120 | +function sha256Text(text: string): string { |
| 121 | + return crypto.createHash('sha256').update(text).digest('hex'); |
| 122 | +} |
0 commit comments