|
| 1 | +import { beforeEach, expect, test } from 'vitest'; |
| 2 | +import { createRequestHandler } from '../request-router.ts'; |
| 3 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 4 | +import { SessionStore } from '../session-store.ts'; |
| 5 | +import { AppError } from '../../kernel/errors.ts'; |
| 6 | +import { resetAndroidSnapshotHelperInstallCache } from '../../platforms/android/snapshot-helper-install.ts'; |
| 7 | +import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from '../../__tests__/test-utils/index.ts'; |
| 8 | +import type { AndroidAdbProvider } from '../../platforms/android/adb-executor.ts'; |
| 9 | + |
| 10 | +function makeAndroidSessionStore(name: string): SessionStore { |
| 11 | + const sessionStore = new SessionStore(`/tmp/${name}`); |
| 12 | + sessionStore.set('default', { |
| 13 | + name: 'default', |
| 14 | + createdAt: Date.now(), |
| 15 | + device: { |
| 16 | + platform: 'android', |
| 17 | + id: 'remote-android-1', |
| 18 | + name: 'Remote Android', |
| 19 | + kind: 'device', |
| 20 | + booted: true, |
| 21 | + }, |
| 22 | + appBundleId: 'com.example.app', |
| 23 | + actions: [], |
| 24 | + }); |
| 25 | + return sessionStore; |
| 26 | +} |
| 27 | + |
| 28 | +function makeHandler(sessionStore: SessionStore, androidAdbProvider: () => AndroidAdbProvider) { |
| 29 | + return createRequestHandler({ |
| 30 | + logPath: '/tmp/daemon.log', |
| 31 | + token: 'token', |
| 32 | + sessionStore, |
| 33 | + leaseRegistry: new LeaseRegistry(), |
| 34 | + androidAdbProvider, |
| 35 | + trackDownloadableArtifact: () => 'artifact-id', |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + resetAndroidSnapshotHelperInstallCache(); |
| 41 | +}); |
| 42 | + |
| 43 | +// Regression for #1284/#1285 P1: a provider whose `install` REJECTS with an |
| 44 | +// enriched INSTALL_FAILED_* error (instead of resolving with a nonzero result) |
| 45 | +// must still surface the device-side install classification on the public |
| 46 | +// daemon snapshot error, not the generic retry/doctor hint. |
| 47 | +test('snapshot reports a device-side install failure when the provider install rejects', async () => { |
| 48 | + const sessionStore = makeAndroidSessionStore( |
| 49 | + 'agent-device-request-router-snapshot-helper-install-reject-test', |
| 50 | + ); |
| 51 | + const provider: AndroidAdbProvider = { |
| 52 | + // Version probe reports no installed helper; every other device query is inert. |
| 53 | + exec: async () => ({ exitCode: 0, stdout: '', stderr: '' }), |
| 54 | + install: async () => { |
| 55 | + throw new AppError('COMMAND_FAILED', 'Failed to install Android snapshot helper', { |
| 56 | + stderr: 'adb: failed to install helper.apk: Failure [INSTALL_FAILED_TEST_ONLY]', |
| 57 | + stdout: '', |
| 58 | + exitCode: 1, |
| 59 | + processExitError: true, |
| 60 | + }); |
| 61 | + }, |
| 62 | + snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT, |
| 63 | + }; |
| 64 | + const handler = makeHandler(sessionStore, () => provider); |
| 65 | + |
| 66 | + const response = await handler({ |
| 67 | + token: 'token', |
| 68 | + session: 'default', |
| 69 | + command: 'snapshot', |
| 70 | + positionals: [], |
| 71 | + flags: {}, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(response.ok).toBe(false); |
| 75 | + if (response.ok) throw new Error('Expected snapshot to fail'); |
| 76 | + expect(response.error.message).toMatch(/Android snapshot helper failed/); |
| 77 | + expect(response.error.message).toMatch(/INSTALL_FAILED_TEST_ONLY/); |
| 78 | + // The provider funnel's classified adb hint is preserved, with the |
| 79 | + // device-side framing appended rather than replacing it. |
| 80 | + expect(response.error.hint).toMatch(/package installer rejected the APK/); |
| 81 | + expect(response.error.hint).toMatch(/device-side install failure/); |
| 82 | + expect(response.error.hint).not.toMatch(/pnpm build:android/); |
| 83 | + expect(response.error.details?.androidSnapshotHelperInstallFailure).toBe(true); |
| 84 | +}); |
0 commit comments